|
| 1 | +import dash |
| 2 | +import dash_core_components as dcc |
| 3 | +import dash_html_components as html |
| 4 | + |
| 5 | +import colorlover as cl |
| 6 | +import datetime as dt |
| 7 | +import flask |
| 8 | +import os |
| 9 | +import pandas as pd |
| 10 | +import time |
| 11 | + |
| 12 | +app = dash.Dash( |
| 13 | + __name__, |
| 14 | + assets_external_scripts='https://cdn.plot.ly/plotly-finance-1.28.0.min.js' |
| 15 | +) |
| 16 | +server = app.server |
| 17 | + |
| 18 | +app.scripts.config.serve_locally = False |
| 19 | + |
| 20 | + |
| 21 | +colorscale = cl.scales['9']['qual']['Paired'] |
| 22 | + |
| 23 | +df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/dash-stock-ticker-demo.csv') |
| 24 | + |
| 25 | +app.layout = html.Div([ |
| 26 | + html.Div([ |
| 27 | + html.H2('Finance Explorer', |
| 28 | + style={'display': 'inline', |
| 29 | + 'float': 'left', |
| 30 | + 'font-size': '2.65em', |
| 31 | + 'margin-left': '7px', |
| 32 | + 'font-weight': 'bolder', |
| 33 | + 'font-family': 'Product Sans', |
| 34 | + 'color': "rgba(117, 117, 117, 0.95)", |
| 35 | + 'margin-top': '20px', |
| 36 | + 'margin-bottom': '0' |
| 37 | + }), |
| 38 | + html.Img(src="https://s3-us-west-1.amazonaws.com/plotly-tutorials/logo/new-branding/dash-logo-by-plotly-stripe.png", |
| 39 | + style={ |
| 40 | + 'height': '100px', |
| 41 | + 'float': 'right' |
| 42 | + }, |
| 43 | + ), |
| 44 | + ]), |
| 45 | + dcc.Dropdown( |
| 46 | + id='stock-ticker-input', |
| 47 | + options=[{'label': s[0], 'value': str(s[1])} |
| 48 | + for s in zip(df.Stock.unique(), df.Stock.unique())], |
| 49 | + value=['YHOO', 'GOOGL'], |
| 50 | + multi=True |
| 51 | + ), |
| 52 | + html.Div(id='graphs') |
| 53 | +], className="container") |
| 54 | + |
| 55 | +def bbands(price, window_size=10, num_of_std=5): |
| 56 | + rolling_mean = price.rolling(window=window_size).mean() |
| 57 | + rolling_std = price.rolling(window=window_size).std() |
| 58 | + upper_band = rolling_mean + (rolling_std*num_of_std) |
| 59 | + lower_band = rolling_mean - (rolling_std*num_of_std) |
| 60 | + return rolling_mean, upper_band, lower_band |
| 61 | + |
| 62 | +@app.callback( |
| 63 | + dash.dependencies.Output('graphs','children'), |
| 64 | + [dash.dependencies.Input('stock-ticker-input', 'value')]) |
| 65 | +def update_graph(tickers): |
| 66 | + graphs = [] |
| 67 | + |
| 68 | + if not tickers: |
| 69 | + graphs.append(html.H3( |
| 70 | + "Select a stock ticker.", |
| 71 | + style={'marginTop': 20, 'marginBottom': 20} |
| 72 | + )) |
| 73 | + else: |
| 74 | + for i, ticker in enumerate(tickers): |
| 75 | + |
| 76 | + dff = df[df['Stock'] == ticker] |
| 77 | + |
| 78 | + candlestick = { |
| 79 | + 'x': dff['Date'], |
| 80 | + 'open': dff['Open'], |
| 81 | + 'high': dff['High'], |
| 82 | + 'low': dff['Low'], |
| 83 | + 'close': dff['Close'], |
| 84 | + 'type': 'candlestick', |
| 85 | + 'name': ticker, |
| 86 | + 'legendgroup': ticker, |
| 87 | + 'increasing': {'line': {'color': colorscale[0]}}, |
| 88 | + 'decreasing': {'line': {'color': colorscale[1]}} |
| 89 | + } |
| 90 | + bb_bands = bbands(dff.Close) |
| 91 | + bollinger_traces = [{ |
| 92 | + 'x': dff['Date'], 'y': y, |
| 93 | + 'type': 'scatter', 'mode': 'lines', |
| 94 | + 'line': {'width': 1, 'color': colorscale[(i*2) % len(colorscale)]}, |
| 95 | + 'hoverinfo': 'none', |
| 96 | + 'legendgroup': ticker, |
| 97 | + 'showlegend': True if i == 0 else False, |
| 98 | + 'name': '{} - bollinger bands'.format(ticker) |
| 99 | + } for i, y in enumerate(bb_bands)] |
| 100 | + graphs.append(dcc.Graph( |
| 101 | + id=ticker, |
| 102 | + figure={ |
| 103 | + 'data': [candlestick] + bollinger_traces, |
| 104 | + 'layout': { |
| 105 | + 'margin': {'b': 0, 'r': 10, 'l': 60, 't': 0}, |
| 106 | + 'legend': {'x': 0} |
| 107 | + } |
| 108 | + } |
| 109 | + )) |
| 110 | + |
| 111 | + return graphs |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == '__main__': |
| 115 | + app.run_server(debug=True) |
0 commit comments