-
Notifications
You must be signed in to change notification settings - Fork 23
/
app.py
189 lines (167 loc) · 5.94 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
import dash
from dash import dcc
from dash import html
from datetime import datetime as dt
import yfinance as yf
from dash.dependencies import Input, Output, State
from dash.exceptions import PreventUpdate
import pandas as pd
import plotly.graph_objs as go
import plotly.express as px
# model
from model import prediction
from sklearn.svm import SVR
def get_stock_price_fig(df):
fig = px.line(df,
x="Date",
y=["Close", "Open"],
title="Closing and Openning Price vs Date")
return fig
def get_more(df):
df['EWA_20'] = df['Close'].ewm(span=20, adjust=False).mean()
fig = px.scatter(df,
x="Date",
y="EWA_20",
title="Exponential Moving Average vs Date")
fig.update_traces(mode='lines+markers')
return fig
app = dash.Dash(
__name__,
external_stylesheets=[
"https://fonts.googleapis.com/css2?family=Roboto&display=swap"
])
server = app.server
# html layout of site
app.layout = html.Div(
[
html.Div(
[
# Navigation
html.P("Welcome to the Stock Dash App!", className="start"),
html.Div([
html.P("Input stock code: "),
html.Div([
dcc.Input(id="dropdown_tickers", type="text"),
html.Button("Submit", id='submit'),
],
className="form")
],
className="input-place"),
html.Div([
dcc.DatePickerRange(id='my-date-picker-range',
min_date_allowed=dt(1995, 8, 5),
max_date_allowed=dt.now(),
initial_visible_month=dt.now(),
end_date=dt.now().date()),
],
className="date"),
html.Div([
html.Button(
"Stock Price", className="stock-btn", id="stock"),
html.Button("Indicators",
className="indicators-btn",
id="indicators"),
dcc.Input(id="n_days",
type="text",
placeholder="number of days"),
html.Button(
"Forecast", className="forecast-btn", id="forecast")
],
className="buttons"),
# here
],
className="nav"),
# content
html.Div(
[
html.Div(
[ # header
html.Img(id="logo"),
html.P(id="ticker")
],
className="header"),
html.Div(id="description", className="decription_ticker"),
html.Div([], id="graphs-content"),
html.Div([], id="main-content"),
html.Div([], id="forecast-content")
],
className="content"),
],
className="container")
# callback for company info
@app.callback([
Output("description", "children"),
Output("logo", "src"),
Output("ticker", "children"),
Output("stock", "n_clicks"),
Output("indicators", "n_clicks"),
Output("forecast", "n_clicks")
], [Input("submit", "n_clicks")], [State("dropdown_tickers", "value")])
def update_data(n, val): # inpur parameter(s)
if n == None:
return "Hey there! Please enter a legitimate stock code to get details.", "https://melmagazine.com/wp-content/uploads/2019/07/Screen-Shot-2019-07-31-at-5.47.12-PM.png", "Stonks", None, None, None
# raise PreventUpdate
else:
if val == None:
raise PreventUpdate
else:
ticker = yf.Ticker(val)
inf = ticker.info
df = pd.DataFrame().from_dict(inf, orient="index").T
df[['logo_url', 'shortName', 'longBusinessSummary']]
return df['longBusinessSummary'].values[0], df['logo_url'].values[
0], df['shortName'].values[0], None, None, None
# callback for stocks graphs
@app.callback([
Output("graphs-content", "children"),
], [
Input("stock", "n_clicks"),
Input('my-date-picker-range', 'start_date'),
Input('my-date-picker-range', 'end_date')
], [State("dropdown_tickers", "value")])
def stock_price(n, start_date, end_date, val):
if n == None:
return [""]
#raise PreventUpdate
if val == None:
raise PreventUpdate
else:
if start_date != None:
df = yf.download(val, str(start_date), str(end_date))
else:
df = yf.download(val)
df.reset_index(inplace=True)
fig = get_stock_price_fig(df)
return [dcc.Graph(figure=fig)]
# callback for indicators
@app.callback([Output("main-content", "children")], [
Input("indicators", "n_clicks"),
Input('my-date-picker-range', 'start_date'),
Input('my-date-picker-range', 'end_date')
], [State("dropdown_tickers", "value")])
def indicators(n, start_date, end_date, val):
if n == None:
return [""]
if val == None:
return [""]
if start_date == None:
df_more = yf.download(val)
else:
df_more = yf.download(val, str(start_date), str(end_date))
df_more.reset_index(inplace=True)
fig = get_more(df_more)
return [dcc.Graph(figure=fig)]
# callback for forecast
@app.callback([Output("forecast-content", "children")],
[Input("forecast", "n_clicks")],
[State("n_days", "value"),
State("dropdown_tickers", "value")])
def forecast(n, n_days, val):
if n == None:
return [""]
if val == None:
raise PreventUpdate
fig = prediction(val, int(n_days) + 1)
return [dcc.Graph(figure=fig)]
if __name__ == '__main__':
app.run_server(debug=True)