-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
279 lines (215 loc) · 6.6 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import dash
import dash_core_components as dcc
import dash_html_components as html
from datetime import datetime as dt
from datetime import date
from dash.dependencies import Input, Output, State
import yfinance as yf
import pandas as pd
import plotly.express as px
from dash.exceptions import PreventUpdate
from model import prediction
app = dash.Dash(__name__)
server = app.server
# df = yf.download("PFI", "2018-01-22", "2019-01-22" )
# df.reset_index(inplace=True)
# def get_stock_price_fig(df):
# fig = px.line(df,
# x="Date", y="Open",
# title="Closing and Opening Price vs Date")
# return fig
item1 = html.Div(
[
html.H1("Welcome to the Stock Dash App!", className="start"),
html.Div([
# stock code input
dcc.Input(value='', type='text',className="inputs",id = "inpt"),
html.Button('Submit', id='submitbutton',className="btn",n_clicks=0)
]
),
html.Div([
# Date range picker input
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()
),
]),
html.Div([
# Stock price button
html.Button('stockprice', id='stockprice',className="btn"),
# Indicators button
html.Button('Indicators', id='Indicator',className="btn"),
# Number of days of forecast input
dcc.Input(value='', type='text',placeholder="number of days",className="inputs",id = "n_days"),
# Forecast button
html.Button('Forecast', id='forecast',className="btn")
],id = "inside"),
],
className="box1")
item2 = html.Div(
[
html.Div(
[ # Logo
html.Img(id = "lgc"),
# Company Name
html.Div(id = "header")
#html.P("need to be chnaged")
],
),
html.Div( #Description
id="description", className="decription_ticker"),
html.Div([], id="graphs-content"),
html.Div([], id="main-content"),
html.Div([
# Forecast plot
], id="forecast-content")
],
className="content",)
app.layout = html.Div([item1, item2],className="container")
#dcc._css_dist[0][‘relative_package_path’].append(‘test.css’)
# app.css.config.serve_locally = True
# app.scripts.config.serve_locally = True
#setting logo,companies name and description dynamically
@app.callback(
[
Output(component_id = "header", component_property = "children"),
Output("lgc","src"),
Output("description","children")
],
[Input("submitbutton","n_clicks")],
[State("inpt", "value")])
def update_data(n_clicks,inputdata):
if(n_clicks == None ):
return "Hey there! Please enter a legitimate stock code to get details.",None,None
else:
if(inputdata == None):
raise PreventUpdate
else:
ticker = yf.Ticker(inputdata)
inf = ticker.info
df = pd.DataFrame().from_dict(inf, orient="index").T
# for i in inf.keys():
# print(i,":",inf[i])
return [inf["shortName"],inf["logo_url"] ,inf["longBusinessSummary"]]# input parameter(s)'
# callback for stocks graphs
def get_stock_price_fig(df):
fig = px.line(df,
x="Date",
y=["Close", "Open"],
title="Closing and Openning Price vs Date")
return fig
@app.callback([
Output("graphs-content", "children"),
], [
Input("stockprice", "n_clicks"),
Input('my-date-picker-range', 'start_date'),
Input('my-date-picker-range', 'end_date')
], [State("inpt", "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
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.callback([Output("main-content", "children")], [
Input("Indicator", "n_clicks"),
Input('my-date-picker-range', 'start_date'),
Input('my-date-picker-range', 'end_date')
], [State("inpt", "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("inpt", "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)
#plotting price vs date graph
# def get_stock_price_fig(df):
# fig = px.line(df,
# x="Date", y="Open",
# title="Closing and Opening Price vs Date")
# return fig
# @app.callback(
# [
# Output(component_id = "graphs-content", component_property = "figure")
# ],
# [
# Input("stockprice","n_clicks"),
# Input('my-date-picker-range', 'start_date'),
# Input('my-date-picker-range', 'end_date')
# ],
# [State("inpt", "value")])
# def update(n_clicks,startdate,enddate,inputdata):
# df = yf.download(inputdata, startdate, enddate )
# df.reset_index(inplace=True)
# fig = get_stock_price_fig(df)
# return [fig]
# # inputdata = "PFI"
# # ticker = yf.Ticker(inputdata)
# # inf = ticker.info
# # df = pd.DataFrame().from_dict(inf, orient="index").T
# # for i in inf.keys():
# # print(i,":",inf[i])
# 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")# appropriate mode)
# return fig
# @app.callback(
# [
# Output(component_id = "main-content", component_property = "figure")
# ],
# [
# Input("Indicator","n_clicks"),
# Input('my-date-picker-range', 'start_date'),
# Input('my-date-picker-range', 'end_date')
# ],
# [State("inpt", "value")])
# def upd(n_clicks,startdate,enddate,inputdata):
# df = yf.download(inputdata, startdate, enddate )
# df.reset_index(inplace=True)
# fig = get_more(df)
# return [fig]
# # your function here