In [1]:
import pandas as pd

import plotly
plotly.offline.init_notebook_mode()
import plotly.graph_objs as go

holding = pd.read_csv("~/Downloads/holdings(3).csv")

#holding.set_index('Instrument',inplace=True)

print holding


    Instrument  Qty.  Avg. cost      LTP   Cur. val       P&L  Net chg.  \
0     CROMPTON    36     269.90   214.50    7722.00  -1994.40    -20.53   
1         GSFC   200     127.60   103.65   20730.00  -4790.70    -18.77   
2      GABRIEL   265     138.22   143.80   38107.00   1478.30      4.04   
3    ICICIBANK    60     236.50   359.95   21597.00   7407.00     52.20   
4       INDIGO     8    1144.68  1167.35    9338.80    181.40      1.98   
5   INDIANHUME     5     434.95   324.50    1622.50   -552.25    -25.39   
6   SUNDRMFAST   100     664.00   522.55   52255.00 -14145.00    -21.30   
7     JSWSTEEL    20     330.97   283.15    5663.00   -956.40    -14.45   
8   JBCHEPHARM    42     311.03   317.50   13335.00    271.80      2.08   
9   UTISENSETF   200     408.85   379.50   75900.00  -5869.05     -7.18   
10    EIDPARRY     7     358.90   202.55    1417.85  -1094.45    -43.56   
11         VTL     9    1381.10  1072.50    9652.50  -2777.40    -22.34   
12  INOXLEISUR    34     248.66   248.10    8435.40    -19.00     -0.22   
13    INFIBEAM    60     143.15    45.40    2724.00  -5864.75    -68.28   
14    CLNINDIA   150     589.74   396.95   59542.50 -28917.90    -32.69   
15       NIACL    72     312.60   179.45   12920.40  -9586.60    -42.59   
16        INFY    40     727.28   734.65   29386.00    295.00      1.01   
17  PIDILITIND    50     832.80  1114.20   55710.00  14070.20     33.79   
18    HDFCLIFE    56     398.34   363.25   20342.00  -1965.00     -8.81   
19     CPSEETF  2461      23.74    23.53   57907.33   -515.71     -0.88   
20       GICRE    58     368.29   251.55   14589.90  -6770.95    -31.70   
21  HINDUNILVR    20    1564.45  1771.90   35438.00   4149.00     13.26   
22   BRITANNIA    20    3171.05  3250.40   65008.00   1587.00      2.50   
23         KEC     7     341.53   255.70    1789.90   -600.80    -25.13   
24    GODREJCP   150     612.39   800.00  120000.00  28141.95     30.64   
25     RELNV20   100     621.74   554.00   55400.00  -6774.35    -10.90   
26    SUPRAJIT     8     294.00   206.80    1654.40   -697.60    -29.66   
27         TCS    20    2067.05  1931.05   38621.00  -2720.00     -6.58   
28     SBILIFE    30     693.75   627.95   18838.50  -1974.00     -9.48   
29        FLFL    28     337.56   420.40   11771.20   2319.60     24.54   
30     ICICIGI    27     797.53   866.85   23404.95   1871.55      8.69   
31     HAVELLS   100     725.90   720.35   72035.00   -555.00     -0.76   
32       VMART    10    3279.90  2495.70   24957.00  -7842.00    -23.91   

    Day chg.  
0      -1.94  
1      -1.61  
2      -1.57  
3      -1.33  
4      -0.95  
5      -0.38  
6      -0.38  
7      -0.18  
8      -0.16  
9      -0.12  
10     -0.10  
11     -0.05  
12      0.02  
13      0.11  
14      0.20  
15      0.25  
16      0.36  
17      0.44  
18      0.57  
19      0.60  
20      0.62  
21      0.70  
22      1.04  
23      1.07  
24      1.19  
25      1.19  
26      1.42  
27      1.55  
28      1.57  
29      1.68  
30      2.17  
31      2.53  
32      2.59  

In [1]:
#currently owned with avg cost line

import plotly
plotly.offline.init_notebook_mode()
import plotly.graph_objs as go
import MySQLdb as mdb
import pandas as pd
holding = pd.read_csv("~/Downloads/holdings(3).csv")

holding.set_index('Instrument',inplace=True)

def plotSingle(stock):
    con = mdb.connect('127.0.0.1', 'root', 'paytm@197', 'stocks');
    with con:
        cur = con.cursor()
        cur.execute("select * from stock_data where symbol = '"+stock+"'  and series = 'EQ' and date>'2010-01-01'  order by date asc")
        rows = cur.fetchall()
        df = pd.DataFrame( [[ij for ij in i] for i in rows] )
        #print(df.head(20))
        layout = go.Layout(
                        {'shapes':[{
                           'type': 'line',
                            'x0': df[3].min(),
                            'y0': holding.loc[stock]['Avg. cost'],
                            'x1': df[3].max(),
                            'y1': holding.loc[stock]['Avg. cost'],
                            'line': {
                                'color': 'rgb(255, 0, 0)',
                                'width': 1,
                                }
                            }]
                            
                       },title=stock)
        percent = (df[4].iloc[-1]- holding.loc[stock]['Avg. cost'])/holding.loc[stock]['Avg. cost']*100
        percent_str = str(float("%0.2f"%percent))
        absolute_pl = (df[4].iloc[-1]- holding.loc[stock]['Avg. cost'])*holding.loc[stock]['Qty.']
        absolute_pl_str = str(float("%0.2f"%absolute_pl))
        
        title = "Rs."+ absolute_pl_str+"/- & percent "+ percent_str+" % "+stock+" p/l for "+str(holding.loc[stock]['Qty.'])+" qty"
        pl_layout = go.Layout({"xaxis":{"range":["2017-01-01",df[3].max()]}},title=title)
        plotly.offline.iplot({
        "data": [go.Scatter(x=df[3], y=df[4])],
        "layout": layout
        },filename = stock)
        plotly.offline.iplot({
        "data": [go.Scatter(x=df[3], y=(df[4]- holding.loc[stock]['Avg. cost'])*holding.loc[stock]['Qty.'], fill="tozeroy")],
        "layout": pl_layout
        })
        

for stock in ["CROMPTON","GSFC","GABRIEL","ICICIBANK","INDIGO","INDIANHUME","SUNDRMFAST","JSWSTEEL","JBCHEPHARM","UTISENSETF","EIDPARRY","VTL","INOXLEISUR","INFIBEAM","CLNINDIA","NIACL","INFY","PIDILITIND","HDFCLIFE","CPSEETF","GICRE","HINDUNILVR","BRITANNIA","KEC","GODREJCP","RELNV20","SUPRAJIT","TCS","SBILIFE","FLFL","ICICIGI","HAVELLS","VMART"]:
    plotSingle(stock)



In [2]:
#used for testing graph features
import plotly
plotly.offline.init_notebook_mode()
import plotly.graph_objs as go
import MySQLdb as mdb
import pandas as pd
con = mdb.connect('127.0.0.1', 'root', 'paytm@197', 'stocks');
stock="GSFC"
with con:
    cur = con.cursor()
    cur.execute("select * from stock_data where symbol = '"+stock+"'  and series = 'EQ' order by date asc")
    rows = cur.fetchall()
    df = pd.DataFrame( [[ij for ij in i] for i in rows] )
    #print df[3].isnull().sum()
    #print(df.head(20))
    layout = go.Layout({"shapes":[{
                           'type': 'line',
                            'x0': df[3].min(),
                            'y0': 300,
                            'x1': df[3].max(),
                            'y1': 300,
                            'line': {
                                'color': 'rgb(255, 0, 0)',
                                'width': 1,
                                }
                            }]
                            
                       }, title=stock)
    plotly.offline.iplot({
    "data": [go.Scatter(x=df[3], y=df[4],mode="lines")],
    "layout" : layout
    },filename = stock)



In [19]:
#plot multiple graphs standalone
import plotly
plotly.offline.init_notebook_mode()
import plotly.graph_objs as go
import MySQLdb as mdb
import pandas as pd
con = mdb.connect('127.0.0.1', 'root', 'paytm@197', 'stocks');


def plotSingle(symbol):
    with con:
        cur = con.cursor()
        cur.execute("select * from stock_data where symbol = '"+symbol+"'  and series = 'EQ' order by date asc")
        rows = cur.fetchall()
        df = pd.DataFrame( [[ij for ij in i] for i in rows] )
        #print df[3].isnull().sum()
        #print(df.head(20))
        layout = go.Layout(title=symbol)
        plotly.offline.iplot({
        "data": [go.Scatter(x=df[3], y=df[4],mode="lines")],
        "layout": layout
        },filename = stock)
        

for stock in ['JINDCOT']:
    plotSingle(stock)



In [77]:
import plotly
plotly.offline.init_notebook_mode()
import plotly.graph_objs as go
import MySQLdb as mdb
import pandas as pd
con = mdb.connect('127.0.0.1', 'root', 'paytm@197', 'stocks');


def findData(symbol):
    df = pd.read_sql("select * from stock_data where symbol = '"+symbol+"'  and series = 'EQ' order by date asc",con)
    return df;
    
data = findData('GSFC')
for key in ['open_price','high_price','low_price','close_price']:
    data[key+'_30_max']=data.rolling(window=80)[key].mean()
    data[key+'_30_min']=data.rolling(window=50)[key].mean()

plotly.offline.iplot({
        "data": [go.Scatter(x=data['date'], y=data['close_price']),
                go.Scatter(x=data['date'], y=data['close_price_30_max']),
                 go.Scatter(x=data['date'], y=data['close_price_30_min'])
                ],
        "layout": layout
        },filename = stock)



In [32]:
#plot multiple graphs standalone
import plotly
plotly.offline.init_notebook_mode()
import plotly.graph_objs as go
import MySQLdb as mdb
import pandas as pd
con = mdb.connect('127.0.0.1', 'root', 'paytm@197', 'stocks');


def plotSingle(symbol):
    days=200
    
    df = pd.read_sql("select * from stock_data where symbol = '"+symbol+"'  and date>date_sub(curdate(),interval "+str(days)+" day) and series = 'EQ' order by date asc",con)
    #print df
    #print(df.head(20))
    layout = go.Layout(xaxis=dict(rangeslider=dict(visible=False)),title=symbol)
    plotly.offline.iplot({
    "data": [go.Candlestick(x=df['date'],open=df['open_price'],high=df['high_price'],low=df['low_price'],close=df['close_price'])],
    "layout": layout
    },filename = stock)
        

for stock in ['HINDUNILVR']:
    plotSingle(stock)



In [67]:
from ta import *
#plot multiple graphs standalone
import plotly
plotly.offline.init_notebook_mode()
import plotly.graph_objs as go
import MySQLdb as mdb
import pandas as pd
con = mdb.connect('127.0.0.1', 'root', 'paytm@197', 'stocks');


def plotSingle(symbol):
    days=150
    
    df = pd.read_sql("select * from stock_data where symbol = '"+symbol+"'  and date>date_sub(curdate(),interval "+str(days)+" day) and series = 'EQ' order by date asc",con)
    #print df
    df = add_all_ta_features(df, "open_price", "high_price", "low_price", "close_price", "total_traded_quantity", fillna=True)
    #print(df.head(20))
    df['bb_high'] = bollinger_hband(df["close_price"], n=20, ndev=2, fillna=True)
    

    df['bb_low'] = bollinger_lband(df["close_price"], n=20, ndev=2, fillna=True)
    
    #df.info()
    #print df['bb_low_indicator']
    layout = go.Layout(xaxis=dict(rangeslider=dict(visible=False)),title=symbol)
    plotly.offline.iplot({
    "data": [go.Candlestick(x=df['date'],open=df['open_price'],high=df['high_price'],low=df['low_price'],close=df['close_price']),go.Scatter(x=df['date'],y=df['volatility_bbh']),go.Scatter(x=df['date'],y=df['volatility_bbl'])],
    "layout": layout
    },filename = stock)
        

for stock in ['hindunilvr']:
    plotSingle(stock)



In [ ]: