-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
157 lines (140 loc) · 5.07 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
from dash import Dash, html, dcc, Output, Input, State , exceptions
from datetime import datetime as dt
from model import build_model, plot_predictions
import yfinance as yf
import pandas as pd
import plotly.express as px
def get_stock_price_fig(df):
fig = px.line(df, x='Date', y=['Open', 'Close'], title='Closing and Opening Price')
return fig
def get_ema_fig(df):
df['EMA_20'] = df['Close'].ewm(span=20, adjust=False).mean()
fig = px.scatter(df,x= 'Date', y= 'EMA_20', title="Exponential Moving Average vs Date")
fig.update_traces(mode='lines+markers')
return fig
app = Dash(__name__, external_stylesheets=['assests/styles.css'])
server = app.server
app.layout= html.Div([
html.Div([
html.H1("Welcome to Dash Stock Forecasting app", className="start"),
html.Div([
#stock code input
html.Label('Input stock code'),
html.Br(),
dcc.Input(placeholder='Stock Name', id='stock-name', type='text'),
html.Button('Submit', id='submit-button', n_clicks=0),
], className="stock-code"),
html.Div([
#date range picker input
html.Label('Select a date range'),
html.Br(),
dcc.DatePickerRange(
id='my-date-picker-range',
initial_visible_month=dt.now(),
start_date_placeholder_text='Start Date',
end_date_placeholder_text='End Date',
calendar_orientation='horizontal',
clearable=True,
),
], className="date-range"),
html.Div([
#stock price button
#indicators button
#Number of days of forecast input
#forecast button
html.Button('Stock Price', id='stock-price-button'),
html.Button('Indicators', id='indicators-button'),
dcc.Input(type='number', id='forecast-period'),
html.Button('Forecast', id='forecast-period-button'),
]),
], className="nav"
),
html.Div([
html.Div([
#logo
#company name
], className="header", id="header"),
html.Div([
#description
], className="description-ticker", id="description"),
html.Div([
#stock price plot
], id="graphs-content"),
html.Div([
#indicator plot
], id="main-content"),
html.Div([
#forecast plot
], id="forecast-content")
], className="content"
)
], className="container")
@app.callback(
Output('header', 'children'),
Output('description', 'children'),
Input('submit-button', 'n_clicks'),
State('stock-name', 'value')
)
def update_info(n_clicks, stock_name):
if n_clicks is None or n_clicks == 0:
raise exceptions.PreventUpdate
elif n_clicks > 0:
stock = yf.Ticker(stock_name)
info = stock.info
df = pd.DataFrame().from_dict(info, orient="index").T
domain = df['website'].values[0]
logo_url = f"https://logo.clearbit.com/{domain}"
return [
html.Img(src=logo_url, className="logo"),
html.H3(df['shortName'].values[0])
], html.P(df['longBusinessSummary'].values[0])
@app.callback(
Output('graphs-content', 'children'),
Input('stock-price-button', 'n_clicks'),
State('stock-name', 'value'),
State('my-date-picker-range', 'start_date'),
State('my-date-picker-range', 'end_date')
)
def update_stock_graph(n_clicks, stock_name, start_date, end_date):
if n_clicks is None:
raise exceptions.PreventUpdate
elif n_clicks > 0:
df = yf.download(stock_name, start=start_date, end=end_date)
df.reset_index(inplace=True)
fig = get_stock_price_fig(df)
return dcc.Graph(figure=fig)
else:
return None
@app.callback(
Output('main-content', 'children'),
Input('indicators-button', 'n_clicks'),
State('stock-name', 'value'),
State('my-date-picker-range', 'start_date'),
State('my-date-picker-range', 'end_date')
)
def update_indicators_graph(n_clicks, stock_name, start_date, end_date):
if n_clicks is None:
raise exceptions.PreventUpdate
elif n_clicks > 0:
df = yf.download(stock_name, start=start_date, end=end_date)
df.reset_index(inplace=True)
fig = get_ema_fig(df)
return dcc.Graph(figure=fig)
else:
return None
@app.callback(
Output('forecast-content', 'children'),
Input('forecast-period-button', 'n_clicks'),
State('stock-name', 'value'),
State('forecast-period', 'value')
)
def update_forecast_graph(n_clicks, stock_name, forecast_period):
if n_clicks is None:
raise exceptions.PreventUpdate
elif n_clicks > 0:
fig = plot_predictions(build_model(stock_name), stock_name, forecast_period)
return dcc.Graph(figure=fig)
else:
return None
if __name__ == "__main__":
app.run(debug=True)