forked from plotly/dash-stock-tickers-demo-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
115 lines (100 loc) · 3.66 KB
/
app.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
import dash
import dash_core_components as dcc
import dash_html_components as html
import colorlover as cl
import datetime as dt
import flask
import os
import pandas as pd
import time
app = dash.Dash(
__name__,
assets_external_scripts='https://cdn.plot.ly/plotly-finance-1.28.0.min.js'
)
server = app.server
app.scripts.config.serve_locally = False
colorscale = cl.scales['9']['qual']['Paired']
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/dash-stock-ticker-demo.csv')
app.layout = html.Div([
html.Div([
html.H2('Finance Explorer',
style={'display': 'inline',
'float': 'left',
'font-size': '2.65em',
'margin-left': '7px',
'font-weight': 'bolder',
'font-family': 'Product Sans',
'color': "rgba(117, 117, 117, 0.95)",
'margin-top': '20px',
'margin-bottom': '0'
}),
html.Img(src="https://s3-us-west-1.amazonaws.com/plotly-tutorials/logo/new-branding/dash-logo-by-plotly-stripe.png",
style={
'height': '100px',
'float': 'right'
},
),
]),
dcc.Dropdown(
id='stock-ticker-input',
options=[{'label': s[0], 'value': str(s[1])}
for s in zip(df.Stock.unique(), df.Stock.unique())],
value=['YHOO', 'GOOGL'],
multi=True
),
html.Div(id='graphs')
], className="container")
def bbands(price, window_size=10, num_of_std=5):
rolling_mean = price.rolling(window=window_size).mean()
rolling_std = price.rolling(window=window_size).std()
upper_band = rolling_mean + (rolling_std*num_of_std)
lower_band = rolling_mean - (rolling_std*num_of_std)
return rolling_mean, upper_band, lower_band
@app.callback(
dash.dependencies.Output('graphs','children'),
[dash.dependencies.Input('stock-ticker-input', 'value')])
def update_graph(tickers):
graphs = []
if not tickers:
graphs.append(html.H3(
"Select a stock ticker.",
style={'marginTop': 20, 'marginBottom': 20}
))
else:
for i, ticker in enumerate(tickers):
dff = df[df['Stock'] == ticker]
candlestick = {
'x': dff['Date'],
'open': dff['Open'],
'high': dff['High'],
'low': dff['Low'],
'close': dff['Close'],
'type': 'candlestick',
'name': ticker,
'legendgroup': ticker,
'increasing': {'line': {'color': colorscale[0]}},
'decreasing': {'line': {'color': colorscale[1]}}
}
bb_bands = bbands(dff.Close)
bollinger_traces = [{
'x': dff['Date'], 'y': y,
'type': 'scatter', 'mode': 'lines',
'line': {'width': 1, 'color': colorscale[(i*2) % len(colorscale)]},
'hoverinfo': 'none',
'legendgroup': ticker,
'showlegend': True if i == 0 else False,
'name': '{} - bollinger bands'.format(ticker)
} for i, y in enumerate(bb_bands)]
graphs.append(dcc.Graph(
id=ticker,
figure={
'data': [candlestick] + bollinger_traces,
'layout': {
'margin': {'b': 0, 'r': 10, 'l': 60, 't': 0},
'legend': {'x': 0}
}
}
))
return graphs
if __name__ == '__main__':
app.run_server(debug=True)