-
Notifications
You must be signed in to change notification settings - Fork 1
/
dashboard.py
409 lines (352 loc) · 16.7 KB
/
dashboard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
from dash_html_components.Div import Div
import yfinance as yf
import pandas as pd
pd.options.mode.chained_assignment = None
from dash.exceptions import PreventUpdate
from datetime import date, datetime
from functions import *
app = dash.Dash(external_stylesheets=['https://codepen.io/chriddyp/pen/bWLwgP.css'])
app.layout = html.Div([
html.Div([
html.Div([
html.H1('Dashboard',style={'text-align':'center'}, className = "start"),
html.H6("Time Period",style={'color':'white'}),
dcc.Dropdown(id="time_period", options=[
{'label': '6 Months', 'value': '6m'},
{'label': '1 year', 'value': '1y'},
{'label': '3 years', 'value': '3y'},
{'label': '5 years', 'value': '5y'},
], placeholder='Time Period', value='1y'),
html.Br(),
html.H6("Technical Indicators",style={'color':'white'}),
dcc.Dropdown(id="indicators", options=[
{'label': 'RSI', 'value': 'RSI'},
{'label': 'SMA', 'value': 'SMA'},
{'label': 'EMA', 'value': 'EMA'},
{'label': 'MACD', 'value': 'MACD'},
{'label': 'Bollinger Bands', 'value': 'Bollinger Bands'}
],placeholder='Indicator', value='Bollinger Bands' ),
html.Br(),
html.H6("Returns",style={'color':'white'}),
dcc.Dropdown(id="returns", options=[
{'label': 'Daily Returns', 'value': 'Daily Returns'},
{'label': 'Cumulative Returns', 'value': 'Cumulative Returns'}
],placeholder='Returns', value='Daily Returns'),
]),
], className="Navigation"),
html.Br(),html.Br(),
html.Div([
html.Div([
html.Div([
dcc.Dropdown(id="dropdown_tickers", options=[
{"label":"HDFC Bank Limited", "value":"HDFCBANK.NS"},
{"label":"ICICI Bank Limited", "value":"ICICIBANK.NS"},
{"label":"RBL Bank Limited", "value":"RBLBANK.NS"},
{"label":"Equitas Small Finance Bank Limited", "value":"EQUITASBNK.NS"},
{"label":"DCB Bank Limited", "value":"DCBBANK.NS"},
{"label":"Maruti Suzuki India Limited", "value":"MARUTI.NS"},
{"label":"Tata Motors Limited ", "value":"TATAMOTORS.NS"},
{"label":"Escorts Limited", "value":"ESCORTS.NS"},
{"label":"Atul Auto Limited", "value":"ATULAUTO.NS"},
{"label":"Force Motors Limited", "value":"FORCEMOT.BO"},
{"label":"Tata Chemicals Limited", "value":"TATACHEM.NS"},
{"label":"Pidilite Industries Limited", "value":"PIDILITIND.NS"},
{"label":"Deepak Nitrite Limited", "value":"DEEPAKNTR.NS"},
{"label":"Navin Fluorine International Limited", "value":"NAVINFLUOR.NS"},
{"label":"Valiant Organics Limited", "value":"VALIANTORG.NS"},
{"label":"Avenue Supermarts Limited ", "value":"DMART.NS"},
{"label":"Trent Limited", "value":"TRENT.NS"},
{"label":"V-Mart Retail Limited", "value":"VMART.NS"},
{"label":"Future Retail Limited", "value":"FRETAIL.NS"},
{"label":"Shoppers Stop Limited", "value":"SHOPERSTOP.NS"},
{"label":"Zomato Limited", "value":"ZOMATO.NS"},
{"label":"G R Infraprojects Limited", "value":"GRINFRA.NS"},
{"label":"Dodla Dairy Limited", "value":"DODLA.NS"},
{"label":"India Pesticides Limited ", "value":"IPL.NS"},
{"label":"Times Green Energy (India) Lim", "value":"TIMESGREEN.BO"},
{"label":"DLF Limited", "value":"DLF.NS"},
{"label":"Godrej Properties Limited", "value":"GODREJPROP.NS"},
{"label":"Oberoi Realty Limited", "value":"OBEROIRLTY.NS"},
{"label":"Sunteck Realty Limited ", "value":"SUNTECK.NS"},
{"label":"Nirlon Limited", "value":"NIRLON.BO"},
], placeholder='Select Stock'),
html.Div([], id="c_graph"),
html.Div([], id="graphs"),
html.Br(),
html.H4('Past Trend vs. Future Projections',style={'text-align':'center'}),
html.H5('Closing Prices',style={'text-align':'center'}),
html.Div([], id="gbm_graph"),
html.Br(),
html.H5('Daily Volatility (%)',style={'text-align':'center'}),
html.Div([], id="garch_graph"),
html.Br(),
html.H4('Risk Ratios',style={'text-align':'center'}),
html.Div([
html.Div([
html.H6("Alpha (NIFTY 50)"),
html.Div(id="a_val"),
],style={'width': '49%', 'display': 'inline-block'}),
html.Div([
html.H6("Beta (NIFTY 50)"),
html.Div(id="b_val"),
],style={'width': '49%', 'display': 'inline-block'}),
]),
html.Div([
html.Div([
html.H6("Sharpe Ratio"),
html.Div(id="sr_val"),
],style={'width': '49%', 'display': 'inline-block'}),
html.Div([
html.H6("Sortino Ratio"),
html.Div(id="sor_val"),
],style={'width': '49%', 'display': 'inline-block'}),
]),
html.Div([
html.H6("Standard Deviation"),
html.Div(id="sd_val"),
]),
], id="main-content"),
],className="Panel1"),
html.Div([
html.Div([
dcc.Dropdown(id="dropdown_tickers2", options=[
{"label":"HDFC Bank Limited", "value":"HDFCBANK.NS"},
{"label":"ICICI Bank Limited", "value":"ICICIBANK.NS"},
{"label":"RBL Bank Limited", "value":"RBLBANK.NS"},
{"label":"Equitas Small Finance Bank Limited", "value":"EQUITASBNK.NS"},
{"label":"DCB Bank Limited", "value":"DCBBANK.NS"},
{"label":"Maruti Suzuki India Limited", "value":"MARUTI.NS"},
{"label":"Tata Motors Limited ", "value":"TATAMOTORS.NS"},
{"label":"Escorts Limited", "value":"ESCORTS.NS"},
{"label":"Atul Auto Limited", "value":"ATULAUTO.NS"},
{"label":"Tata Chemicals Limited", "value":"TATACHEM.NS"},
{"label":"Pidilite Industries Limited", "value":"PIDILITIND.NS"},
{"label":"Deepak Nitrite Limited", "value":"DEEPAKNTR.NS"},
{"label":"Navin Fluorine International Limited", "value":"NAVINFLUOR.NS"},
{"label":"Valiant Organics Limited", "value":"VALIANTORG.NS"},
{"label":"Avenue Supermarts Limited ", "value":"DMART.NS"},
{"label":"Trent Limited", "value":"TRENT.NS"},
{"label":"V-Mart Retail Limited", "value":"VMART.NS"},
{"label":"Future Retail Limited", "value":"FRETAIL.NS"},
{"label":"Shoppers Stop Limited", "value":"SHOPERSTOP.NS"},
{"label":"Zomato Limited", "value":"ZOMATO.NS"},
{"label":"G R Infraprojects Limited", "value":"GRINFRA.NS"},
{"label":"Dodla Dairy Limited", "value":"DODLA.NS"},
{"label":"India Pesticides Limited ", "value":"IPL.NS"},
{"label":"Times Green Energy (India) Lim", "value":"TIMESGREEN.BO"},
{"label":"DLF Limited", "value":"DLF.NS"},
{"label":"Godrej Properties Limited", "value":"GODREJPROP.NS"},
{"label":"Oberoi Realty Limited", "value":"OBEROIRLTY.NS"},
{"label":"Sunteck Realty Limited ", "value":"SUNTECK.NS"},
{"label":"Nirlon Limited", "value":"NIRLON.BO"},
], placeholder='Select Stock'),
html.Div([], id="c_graph2"),
html.Div([], id="graphs2"),
html.Br(),
html.H4('Past Trend vs. Future Projections',style={'text-align':'center'}),
html.H5('Closing Prices',style={'text-align':'center'}),
html.Div([], id="gbm_graph2"),
html.Br(),
html.H5('Daily Volatility (%)',style={'text-align':'center'}),
html.Div([], id="garch_graph2"),
html.Br(),
html.H4('Risk Ratios',style={'text-align':'center'}),
html.Div([
html.Div([
html.H6("Alpha (NIFTY 50)"),
html.Div(id="a_val2"),
],style={'width': '49%', 'display': 'inline-block'}),
html.Div([
html.H6("Beta (NIFTY 50)"),
html.Div(id="b_val2"),
],style={'width': '49%', 'display': 'inline-block'}),
]),
html.Div([
html.Div([
html.H6("Sharpe Ratio"),
html.Div(id="sr_val2"),
],style={'width': '49%', 'display': 'inline-block'}),
html.Div([
html.H6("Sortino Ratio"),
html.Div(id="sor_val2"),
],style={'width': '49%', 'display': 'inline-block'}),
]),
html.Div([
html.H6("Standard Deviation"),
html.Div(id="sd_val2"),
]),
], id="main-content2"),
], className="Panel2"),
html.Br(),
html.Div([
html.H3('Interpretation',style={'text-align':'center'}),
html.H5('Technical indicators'),
html.Li('Bollinger Bands is a measure of volatility. High volatility is signified by wide bands while low volatility is signified by narrow bands. Generally, high volatility is followed by low volatility'),
html.Li('RSI or Relative Strength Index, is a measure to evaluate overbought and oversold conditions.'),
html.Li('SMA or Simple Moving Average using 50 day (fast) and 200 day (slow) lines - short term going above long term is bullish trend. Short term going below long term is bearish'),
html.Li('EMA or Exponential Moving Average gives higher significance to recent price data'),
html.Li('MACD or Moving Average Convergence Divergence signifies no trend reversal unless there are crossovers. The market is bullish when signal line crosses above blue line, bearish when signal line crosses below blue line'),
html.H5('Risk ratios'),
html.Li('Alpha: Return performance as compared to benchmark of market'),
html.Li('Beta: Relative price movement of a stock to go up and down as compared to the market trend'),
html.Li('Sharpe Ratio: Returns generated per unit of risk - the higher the better'),
html.Li('Sortino Ratio: Returns as compared to only downside risk'),
])
],className="Panels"),
],className="container")
beta_r = N50()
@app.callback(
[Output("c_graph", "children")],
[Output("graphs", "children")],
[Output("a_val", "children")],
[Output("b_val", "children")],
[Output("sr_val", "children")],
[Output("sor_val", "children")],
[Output("sd_val", "children")],
[Output("gbm_graph", "children")],
[Output("garch_graph", "children")],
[Input("time_period", "value")],
[Input("dropdown_tickers", "value")],
[Input("indicators", "value")],
[Input("returns", "value")],
)
def stock_prices(v, v2, v3, v4):
if v2 == None:
raise PreventUpdate
if os.path.exists(v2+'.csv'):
df = pd.read_csv(v2+'.csv')
now = datetime.now()
today345pm = now.replace(hour=15, minute=45, second=0, microsecond=0)
if df['Date'].iloc[-1]!=date.today().isoformat() and date.today().isoweekday() in range(1,6) and now>today345pm:
df = yf.download(v2,start='2016-01-01')
df.reset_index(inplace=True)
df.to_csv(v2+'.csv')
else:
df = yf.download(v2,start='2016-01-01')
df.reset_index(inplace=True)
df.to_csv(v2+'.csv')
df = df.tail(1800)
df['Date']= pd.to_datetime(df['Date'])
if v=='6m':
time_period = 126
elif v=='1y':
time_period = 252
elif v=='3y':
time_period = 756
elif v=='5y':
time_period = 1800
# Alpha & Beta Ratio
beta_r = pd.read_csv('benchmark.csv')
beta_r = beta_r.tail(time_period)
df_data = df.tail(time_period)
Alpha_Ratio, Beta_Ratio = alpha_beta(beta_r, df_data)
# Standard Deviation
SD = round(df_data['Adj Close'].std(),2)
# Sharpe & Sortino Ratio
Sharpe_Ratio, Sortino_Ratio = sharpe_sortino(df_data)
# Plotting over the time period's data
MACD(df)
RSI(df)
BB(df)
df['SMA_50'] = SMA(df, 50)
df['SMA_200'] = SMA(df, 200)
df['EMA'] = EMA(df)
fig = get_stock_price_fig(df.tail(time_period),v3,v4)
current = df_data.iloc[-1][2]
yesterday = df_data.iloc[-2][2]
# Change graph
fig1 = change_graph(current,yesterday)
df = df[['Date','Adj Close']]
# GBM Model
fig2= gbm(df.tail(30))
# GARCH Model
fig3 = garch(df.tail(30))
return [dcc.Graph(figure=fig1,config={'displayModeBar': False}),
dcc.Graph(figure=fig,config={'displayModeBar': False}),
Alpha_Ratio,
Beta_Ratio,
Sharpe_Ratio,
Sortino_Ratio,
SD,
dcc.Graph(figure=fig2,config={'displayModeBar': False}),
dcc.Graph(figure=fig3,config={'displayModeBar': False}),]
@app.callback(
[Output("c_graph2", "children")],
[Output("graphs2", "children")],
[Output("a_val2", "children")],
[Output("b_val2", "children")],
[Output("sr_val2", "children")],
[Output("sor_val2", "children")],
[Output("sd_val2", "children")],
[Output("gbm_graph2", "children")],
[Output("garch_graph2", "children")],
[Input("time_period", "value")],
[Input("dropdown_tickers2", "value")],
[Input("indicators", "value")],
[Input("returns", "value")],
)
def stock_prices2(v, v2, v3, v4):
if v2 == None:
raise PreventUpdate
if os.path.exists(v2+'.csv'):
df2 = pd.read_csv(v2+'.csv')
now = datetime.now()
today345pm = now.replace(hour=15, minute=45, second=0, microsecond=0)
if df2['Date'].iloc[-1]!=date.today().isoformat() and date.today().isoweekday() in range(1,6) and now>today345pm:
df2 = yf.download(v2,start='2016-01-01')
df2.reset_index(inplace=True)
df2.to_csv(v2+'.csv')
else:
df2 = yf.download(v2,start='2016-01-01')
df2.reset_index(inplace=True)
df2.to_csv(v2+'.csv')
df2 = df2.tail(1800)
df2['Date']= pd.to_datetime(df2['Date'])
if v=='6m':
time_period = 126
elif v=='1y':
time_period = 252
elif v=='3y':
time_period = 756
elif v=='5y':
time_period = 1800
# Alpha & Beta Ratio
beta_r2 = pd.read_csv('benchmark.csv')
beta_r2 = beta_r2.tail(time_period)
df_data = df2.tail(time_period)
Alpha_Ratio, Beta_Ratio = alpha_beta(beta_r2, df_data)
# Standard Deviation
SD = round(df_data['Adj Close'].std(),2)
# Sharpe & Sortino Ratio
Sharpe_Ratio, Sortino_Ratio = sharpe_sortino(df_data)
# Plotting over the time period's data
MACD(df2)
RSI(df2)
BB(df2)
df2['SMA_50'] = SMA(df2, 50)
df2['SMA_200'] = SMA(df2, 200)
df2['EMA'] = EMA(df2)
fig = get_stock_price_fig(df2.tail(time_period),v3,v4)
current = df2.iloc[-1][2]
yesterday = df2.iloc[-2][2]
# Change graph
fig1 = change_graph(current,yesterday)
df2 = df2[['Date','Adj Close']]
# GBM Model
fig2= gbm(df2.tail(30))
# GARCH Model
fig3 = garch(df2.tail(30))
return [dcc.Graph(figure=fig1,config={'displayModeBar': False}),
dcc.Graph(figure=fig,config={'displayModeBar': False}),
Alpha_Ratio,
Beta_Ratio,
Sharpe_Ratio,
Sortino_Ratio,
SD,
dcc.Graph(figure=fig2,config={'displayModeBar': False}),
dcc.Graph(figure=fig3,config={'displayModeBar': False}),]
app.run_server(debug=True)