-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.py
249 lines (227 loc) · 8.36 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
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
# key packages:
# standard packages
import numpy as np
import pandas as pd
import datetime
from dateutil import parser
# dash packages
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
# plotly packages for visualizations
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
# janitor for data cleaning
from janitor import clean_names, remove_empty
# yahoo finance for stock info
import yfinance as yf
plotly_figure = dict()
# instantiating the default asset to be displayed upon start-up
present_asset_name = 'VTI'
# sourcing some css to style the app
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
dcc.Markdown('''
## Asset Time Machine
Welcome to the Asset Time Machine App!
Check out the [documentation here](https://github.com/pmaji/asset-time-machine-app), then select your inputs and click "SUBMIT" to get started.
'''
),
# this Div will hold the label and asset-input component:
html.Div(
children=[
html.P("Pick the asset you'd like to explore:"),
dcc.Input(id='asset-input', type='text', value=present_asset_name)
],
style=dict(justifyContent='left')
),
# this Div will hold the label and start date picker component:
html.Div(
children=[
html.P("Pick how far back you'd like to explore:"),
html.P(" "),
dcc.DatePickerSingle(
id='ts-start-date-picker-range',
# this is the date from which we will start our calculations
date='2000-01-01'
)
],
style=dict(justifyContent='left')
),
html.Br(),
html.Button(id='submit-button', n_clicks=0, children='Submit'),
html.Div([dcc.Graph(id="asset-graph", figure=plotly_figure)],
# shouldn't be setting opacity to 0, div should be gone, but plotly messes up horizontal sizing with that
id="graph-div", style={"opacity": "0"}),
dcc.Markdown('''
#### Notes:
If you have any questions or comments, don't hesitate to [reach out via Twitter](https://twitter.com/ByPaulJ).
'''
)
])
@app.callback(Output('graph-div', 'style'), [Input('submit-button', 'n_clicks')])
def display_div(n_clicks):
if n_clicks > 0:
return {"opacity": "1"}
return {"opacity": "0"}
@app.callback(Output('asset-graph', 'figure'),
[Input('submit-button', 'n_clicks')],
[State('asset-input', 'value'),
State('ts-start-date-picker-range', 'date')])
def update_output(n_clicks, asset, date):
present_asset_name = asset
date_today = datetime.datetime.today()
# formatting today's date appropriately for yf.download() call
formatted_date_today = date_today.strftime('%Y-%m-%d')
# setting start date of period for which to pull data
# need to think about how far back we want to go; code is fast but plotly is a bit slow
start_date = date
try:
initial_data_pull_df = (
yf.download(
tickers=asset,
start=start_date,
end=formatted_date_today,
progress=False
)
# using janitor to clean the column names
.pipe(clean_names)
)
except:
asset = present_asset_name
initial_data_pull_df = (
yf.download(
tickers=present_asset_name,
start=start_date,
end=formatted_date_today,
progress=False
)
# using janitor to clean the column names
.pipe(clean_names)
)
# renaming the index as well to make it lowercase (sadly janitor doesn't yet catch the index name)
initial_data_pull_df.index.rename('date', inplace=True)
initial_data_pull_df.reset_index(level=0, inplace=True)
cleaned_close_df = initial_data_pull_df.loc[:, ('date', 'adj_close')]
cleaned_close_df['recent_ath_val'] = cleaned_close_df['adj_close'].cummax()
recent_ath_date_df = (cleaned_close_df
.groupby("recent_ath_val")['date']
.first()
.reset_index()
.rename(columns={"date": "recent_ath_date"})
)
close_and_ath_df = cleaned_close_df.merge(
recent_ath_date_df, on="recent_ath_val")
close_and_ath_df['perc_down_from_ath'] = (
100 * (1 - (close_and_ath_df['adj_close'] /
close_and_ath_df['recent_ath_val']))
)
close_and_ath_df["days_since_ath"] = close_and_ath_df['date'] - \
close_and_ath_df['recent_ath_date']
closing_price_trace = (
go.Scatter(
x=close_and_ath_df['date'],
y=close_and_ath_df['adj_close'],
line=dict(
color='rgb(100, 143, 255)',
width=1.5
),
hoverlabel=dict(namelength=-1),
opacity=0.75,
yaxis='y',
name="Adj. Close Price"
)
)
# building the trace for percent down from ATH values
pct_down_frm_ath_trace = (
go.Scatter(
x=close_and_ath_df['date'],
y=close_and_ath_df['perc_down_from_ath'],
line=dict(
color='rgb(255, 176, 0)',
width=3
),
hoverlabel=dict(namelength=-1),
opacity=1,
yaxis='y2',
name="% Down from ATH"
)
)
# building the figure itself
result = {
'data': [closing_price_trace, pct_down_frm_ath_trace],
'layout': go.Layout(
title=dict(
text=f"Asset Selected: {present_asset_name} <br> Time Series Start: {start_date} <br> Most Recent Date: {close_and_ath_df['date'].iloc[-1].date()}"
),
showlegend=True,
legend=dict(
orientation='h',
yanchor='top',
xanchor='right',
y=1.15,
x=1
),
xaxis=dict(
rangeselector=dict(
buttons=list([
dict(count=1,
label='1m',
step='month',
stepmode='backward'),
dict(count=6,
label='6m',
step='month',
stepmode='backward'),
dict(count=1,
label='YTD',
step='year',
stepmode='todate'),
dict(count=1,
label='1y',
step='year',
stepmode='backward'),
dict(count=2,
label='2y',
step='year',
stepmode='backward'),
dict(step='all')
])
),
type='date',
title="Date",
rangeslider=dict(
visible=True
),
),
yaxis=dict(
title="Closing Price ($)",
anchor='x',
mirror=True,
side='right',
showline=True
),
yaxis2=dict(
title="% Down from ATH",
anchor='x',
mirror=True,
side='left',
showline=True,
overlaying='y'
)
)
}
# in order to get the range slider to not show the full chart...
# we need to update the range value of the x-axis...
# here I'm setting it to default to a 1-year lookback from whatever the most recent in the data is
initial_range = [
close_and_ath_df['date'].iloc[-1] - pd.DateOffset(years=1),
close_and_ath_df['date'].iloc[-1]
]
# updating the xaxis to just cover the initial range we want (defined above)
result['layout']['xaxis'].update(range=initial_range)
return result
if __name__ == '__main__':
app.run_server(debug=True, host='0.0.0.0')