-
Notifications
You must be signed in to change notification settings - Fork 12
/
liveplot_dash.py
116 lines (99 loc) · 4.22 KB
/
liveplot_dash.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
import dash, plotly
from dash.dependencies import Output, Event, Input
import dash_core_components as dcc
import dash_html_components as html
from multipuller import puller
app = dash.Dash(__name__)
# app.css.append_css({ "external_url" : "https://codepen.io/chriddyp/pen/bWLwgP.css" })
app.layout = html.Div([
html.Div(['Stock to plot: ',
dcc.Input(id='input', value='ACC', type='text'),
],style = {'display':'inline-block'}),
html.Div(['Time Interval: ',
dcc.Dropdown(id='peri', options=[
{'label': '1', 'value': '61'},
{'label': '5', 'value': '301'},
{'label': '15', 'value': '901'}],value='61')
],style = {'display':'inline-block'}),
dcc.Graph(id = 'livegraph_with_sma'),
dcc.Graph(id = 'vol'),
dcc.Graph(id = 'livegraph_with_bands'),
dcc.Interval(id = 'interval_', interval = 60000)
])
@app.callback(
Output('livegraph_with_sma', 'figure'),
[Input(component_id = 'input', component_property = 'value'),
Input(component_id = 'peri', component_property = 'value')],
events=[Event('interval_', 'interval')]
)
def update_graph(in_data, period):
df = puller(STOCK=in_data, NO_OF_DAYS=1, EXCHANGE='NSE', INTERVAL=period, WRITE_TO_FILE = False)
df['20MMA'] = df.Close.rolling(window=20).mean()
df['30MMA'] = df.Close.rolling(window=30).mean()
traces = []
traces.append(plotly.graph_objs.Scatter(
x = df.Time,
y = df.Close,
name = 'Close',
mode = 'lines'))
traces.append(plotly.graph_objs.Scatter(
x = df.Time,
y = df.20MMA,
name = '20MMA',
mode = 'lines'))
traces.append(plotly.graph_objs.Scatter(
x = df.Time,
y = df.30MMA,
name = '30MMA',
mode = 'lines'))
if period == '61':
layout = plotly.graph_objs.Layout(title='1 Minute plot of '+in_data)
elif period == '301':
layout = plotly.graph_objs.Layout(title='5 Minute plot of '+in_data)
else:
layout = plotly.graph_objs.Layout(title='15 Minute plot of '+in_data)
return {'data': traces,'layout':layout}
@app.callback(
Output('vol', 'figure'),
[Input(component_id = 'stock_input', component_property = 'value'),
Input(component_id = 'peri', component_property = 'value')],
events=[Event('interval_', 'interval')]
)
def update_graph_vol(in_data,period):
df = puller(STOCK=in_data, NO_OF_DAYS=1, EXCHANGE='NSE', INTERVAL=period, WRITE_TO_FILE = False)
traces = []
traces.append(plotly.graph_objs.Bar(
x = df.Time, y = df.Volume, name='Volume'))
layout = plotly.graph_objs.Layout(title = 'Volume data for '+in_data)
return {'data': traces,'layout':layout}
@app.callback(Output('livegraph_with_bands', 'figure'),
[Input(component_id = 'input', component_property = 'value'),
Input(component_id = 'peri', component_property = 'value')],
events=[Event('interval_', 'interval')]
)
def update_graph_bands(in_data,period):
df = puller(STOCK=in_data, NO_OF_DAYS=1, EXCHANGE='NSE', INTERVAL=period, WRITE_TO_FILE = False)
df['MMA20']=df.Close.rolling(window=20).mean()
df['UpperBand']=df.MMA20 + (df.Close.rolling(window=20).std()*2)
df['LowerBand']=df.MMA20 - (df.Close.rolling(window=20).std()*2)
traces = []
traces.append(plotly.graph_objs.Scatter(
x=df.Time,y=df.Close,
name='Close',
mode= 'lines'))
traces.append(plotly.graph_objs.Scatter(
x = df.Time, y = df.UpperBand,
fill = None,
line = dict(color='rgb(143, 19, 13)'),
name = 'UB',
mode = 'lines'))
traces.append(plotly.graph_objs.Scatter(
x = df.Time, y = df.LowerBand,
fill = 'tonexty',
line = dict(color='rgb(143, 19, 13)'),
name = 'LB',
mode= 'lines'))
layout = plotly.graph_objs.Layout(title = 'Bollinger Band for '+in_data)
return {'data': traces,'layout':layout}
if __name__ == '__main__':
app.run_server(debug=True)