-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappUI.py
More file actions
410 lines (354 loc) · 14.6 KB
/
Copy pathappUI.py
File metadata and controls
410 lines (354 loc) · 14.6 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import dash
from dash import dcc, html
import pandas as pd
import plotly.graph_objs as go
from dash.dependencies import Input, Output
from keras.models import load_model
from sklearn.preprocessing import MinMaxScaler
import xgboost as xgb
import numpy as np
from module.utils import (
handle_data,
handle_data_roc,
handle_data_xgboost,
handle_data_roc_xgboost,
)
app = dash.Dash(__name__)
server = app.server
# Load models
models = {
"btc": {
"lstm": load_model("models/lstm-BTC-USD.h5"),
"rnn": load_model("models/rnnBTC-USD.h5"),
"xgb": xgb.XGBRegressor(),
},
"btc_roc": {
"lstm": load_model("models/lstm-BTC-USD-roc.h5"),
"rnn": load_model("models/rnnBTC-USD-roc.h5"),
"xgb": xgb.XGBRegressor(),
},
"eth": {
"lstm": load_model("models/lstm-ETH-USD.h5"),
"rnn": load_model("models/rnnETH-USD.h5"),
"xgb": xgb.XGBRegressor(),
},
"eth_roc": {
"lstm": load_model("models/lstm-ETH-USD-roc.h5"),
"rnn": load_model("models/rnnETH-USD-roc.h5"),
"xgb": xgb.XGBRegressor(),
},
"ada": {
"lstm": load_model("models/lstm-ADA-USD.h5"),
"rnn": load_model("models/rnnADA-USD.h5"),
"xgb": xgb.XGBRegressor(),
},
"ada_roc": {
"lstm": load_model("models/lstm-ADA-USD-roc.h5"),
"rnn": load_model("models/rnnADA-USD-roc.h5"),
"xgb": xgb.XGBRegressor(),
},
}
# Load XGB models
models["btc"]["xgb"].load_model("models/xgbBTC-USD.json")
models["btc_roc"]["xgb"].load_model("models/xgbBTC-USD-roc.json")
models["eth"]["xgb"].load_model("models/xgbETH-USD.json")
models["eth_roc"]["xgb"].load_model("models/xgbETH-USD-roc.json")
models["ada"]["xgb"].load_model("models/xgbADA-USD.json")
models["ada_roc"]["xgb"].load_model("models/xgbADA-USD-roc.json")
# Process data
def process_data(currency, name):
"""
Process the data for the given currency and model name.
Args:
currency (str): The currency to process the data for.
name (str): The name of the model.
Returns:
tuple: A tuple containing the processed data and the original data.
"""
[_, _, X_test, valid_data, scaler, df] = handle_data(currency)
[_, _, X_xgb_test, _, _, _] = handle_data_xgboost(currency)
lstm_pred = models[name]["lstm"].predict(X_test)
lstm_pred = scaler.inverse_transform(lstm_pred)
rnn_pred = models[name]["rnn"].predict(X_test)
rnn_pred = scaler.inverse_transform(rnn_pred)
xgb_pred = models[name]["xgb"].predict(X_xgb_test)
xgb_pred = np.reshape(xgb_pred, (xgb_pred.shape[0], 1))
xgb_pred = scaler.inverse_transform(xgb_pred)
valid_data["Predictions-lstm"] = lstm_pred
valid_data["Predictions-rnn"] = rnn_pred
valid_data["Predictions-xgb"] = xgb_pred
return valid_data, df
def process_data_roc(currency, name):
"""
Process the data for rate of change (ROC) prediction.
Args:
currency (str): The currency to process the data for.
name (str): The name of the model to use for prediction.
Returns:
tuple: A tuple containing the processed data for ROC prediction and the original data.
"""
[_, _, X_test, valid_data, scaler, df] = handle_data_roc(currency)
[_, _, X_xgb_test, _, _, _] = handle_data_roc_xgboost(currency)
lstm_pred = models[name]["lstm"].predict(X_test)
lstm_pred = scaler.inverse_transform(lstm_pred)
A = valid_data.shift(1)["Close"]
lstm_pred = lstm_pred.reshape(-1)
B = lstm_pred * A
valid_data["Predictions-lstm"] = valid_data.shift(1)["Close"] + B
rnn_pred = models[name]["rnn"].predict(X_test)
rnn_pred = scaler.inverse_transform(rnn_pred)
A = valid_data.shift(1)["Close"]
rnn_pred = rnn_pred.reshape(-1)
B = rnn_pred * A
valid_data["Predictions-rnn"] = valid_data.shift(1)["Close"] + B
xgb_pred = models[name]["xgb"].predict(X_xgb_test)
xgb_pred = np.reshape(xgb_pred, (xgb_pred.shape[0], 1))
xgb_pred = scaler.inverse_transform(xgb_pred)
A = valid_data.shift(1)["Close"]
xgb_pred = xgb_pred.reshape(-1)
B = xgb_pred * A
valid_data["Predictions-xgb"] = valid_data.shift(1)["Close"] + B
return valid_data, df
# Prepare data for plotting
data = {
"btc": process_data("BTC-USD", "btc"),
"btc_roc": process_data_roc("BTC-USD", "btc_roc"),
"eth": process_data("ETH-USD", "eth"),
"eth_roc": process_data_roc("ETH-USD", "eth_roc"),
"ada": process_data("ADA-USD", "ada"),
"ada_roc": process_data_roc("ADA-USD", "ada_roc"),
}
# Layout
app.layout = html.Div(
[
html.H1("Stock Price Analysis Dashboard", style={"textAlign": "center"}),
dcc.Tabs(
id="tabs",
children=[
dcc.Tab(
label="BTC-USD Stock Data",
children=[
html.Div(
[
html.H1(
"LSTM Predicted closing price",
style={"textAlign": "center"},
),
dcc.Dropdown(
id="btc-dropdown",
options=[
{"label": "Closed", "value": "Closed"},
{"label": "RoC", "value": "Roc"},
],
value="Closed",
style={
"display": "block",
"margin-left": "auto",
"margin-right": "auto",
"width": "60%",
},
),
dcc.Graph(id="btc-lstm"),
html.H1(
"RNN Predicted closing price",
style={"textAlign": "center"},
),
dcc.Graph(id="btc-rnn"),
html.H1(
"XGB Predicted closing price",
style={"textAlign": "center"},
),
dcc.Graph(id="btc-xgb"),
],
className="container",
),
],
),
dcc.Tab(
label="ETH-USD Stock Data",
children=[
html.Div(
[
html.H1(
"LSTM Predicted closing price",
style={"textAlign": "center"},
),
dcc.Dropdown(
id="eth-dropdown",
options=[
{"label": "Closed", "value": "Closed"},
{"label": "RoC", "value": "Roc"},
],
value="Closed",
style={
"display": "block",
"margin-left": "auto",
"margin-right": "auto",
"width": "60%",
},
),
dcc.Graph(id="eth-lstm"),
html.H1(
"RNN Predicted closing price",
style={"textAlign": "center"},
),
dcc.Graph(id="eth-rnn"),
html.H1(
"XGB Predicted closing price",
style={"textAlign": "center"},
),
dcc.Graph(id="eth-xgb"),
],
className="container",
),
],
),
dcc.Tab(
label="ADA-USD Stock Data",
children=[
html.Div(
[
html.H1(
"LSTM Predicted closing price",
style={"textAlign": "center"},
),
dcc.Dropdown(
id="ada-dropdown",
options=[
{"label": "Closed", "value": "Closed"},
{"label": "RoC", "value": "Roc"},
],
value="Closed",
style={
"display": "block",
"margin-left": "auto",
"margin-right": "auto",
"width": "60%",
},
),
dcc.Graph(id="ada-lstm"),
html.H1(
"RNN Predicted closing price",
style={"textAlign": "center"},
),
dcc.Graph(id="ada-rnn"),
html.H1(
"XGB Predicted closing price",
style={"textAlign": "center"},
),
dcc.Graph(id="ada-xgb"),
],
className="container",
),
],
),
],
),
]
)
def get_figure(dropdown_value, currency, model_type, title):
"""
Generate a figure object for plotting candlestick and predicted close data.
Parameters:
- dropdown_value (str): The selected dropdown value ("Closed" or "ROC").
- currency (str): The currency for which the data is fetched.
- model_type (str): The type of model used for predictions.
- title (str): The title of the figure.
Returns:
- figure (dict): A dictionary containing the data and layout for the figure.
"""
valid_data, df = data[currency]
valid_data_roc, _ = data[currency + "_roc"]
selected_data = valid_data if dropdown_value == "Closed" else valid_data_roc
actual_close = go.Candlestick(
x=df.index,
open=df["Open"],
high=df["High"],
low=df["Low"],
close=df["Close"],
name="Actual",
)
predicted_close = go.Scatter(
x=selected_data.index,
y=selected_data[f"Predictions-{model_type}"],
name="Predicted",
mode="text",
text="-",
textfont=dict(size=25, color="blue"),
legendgroup="predicted",
showlegend=False
)
predicted_close_marker = go.Scatter(
x=[None], # No data points to display
y=[None],
mode="markers", # Only markers
marker=dict(color="blue", symbol="diamond"),
name="Predicted",
legendgroup="predicted", # Same group as the text trace
showlegend=True # Show this trace in the legend
)
figure = {
"data": [actual_close, predicted_close, predicted_close_marker],
"layout": go.Layout(
colorway=["#5E0DAC", "#FF4F00", "#375CB1", "#FF7400", "#FFF400", "#FF0056"],
height=600,
title=title,
xaxis={
"title": "Date",
"rangeselector": {
"buttons": list(
[
{
"count": 1,
"label": "1M",
"step": "month",
"stepmode": "backward",
},
{
"count": 6,
"label": "6M",
"step": "month",
"stepmode": "backward",
},
{"step": "all"},
]
)
},
"rangeslider": {"visible": True},
"type": "date",
},
yaxis={"title": "Price"},
),
}
return figure
@app.callback(Output("btc-lstm", "figure"), Input("btc-dropdown", "value"))
def update_btc_lstm(dropdown_value):
return get_figure(dropdown_value, "btc", "lstm", "BTC LSTM Predicted Price")
@app.callback(Output("btc-rnn", "figure"), Input("btc-dropdown", "value"))
def update_btc_rnn(dropdown_value):
return get_figure(dropdown_value, "btc", "rnn", "BTC RNN Predicted Price")
@app.callback(Output("btc-xgb", "figure"), Input("btc-dropdown", "value"))
def update_btc_xgb(dropdown_value):
return get_figure(dropdown_value, "btc", "xgb", "BTC XGB Predicted Price")
@app.callback(Output("eth-lstm", "figure"), Input("eth-dropdown", "value"))
def update_eth_lstm(dropdown_value):
return get_figure(dropdown_value, "eth", "lstm", "ETH LSTM Predicted Price")
@app.callback(Output("eth-rnn", "figure"), Input("eth-dropdown", "value"))
def update_eth_rnn(dropdown_value):
return get_figure(dropdown_value, "eth", "rnn", "ETH RNN Predicted Price")
@app.callback(Output("eth-xgb", "figure"), Input("eth-dropdown", "value"))
def update_eth_xgb(dropdown_value):
return get_figure(dropdown_value, "eth", "xgb", "ETH XGB Predicted Price")
@app.callback(Output("ada-lstm", "figure"), Input("ada-dropdown", "value"))
def update_ada_lstm(dropdown_value):
return get_figure(dropdown_value, "ada", "lstm", "ADA LSTM Predicted Price")
@app.callback(Output("ada-rnn", "figure"), Input("ada-dropdown", "value"))
def update_ada_rnn(dropdown_value):
return get_figure(dropdown_value, "ada", "rnn", "ADA RNN Predicted Price")
@app.callback(Output("ada-xgb", "figure"), Input("ada-dropdown", "value"))
def update_ada_xgb(dropdown_value):
return get_figure(dropdown_value, "ada", "xgb", "ADA XGB Predicted Price")
def run_app():
app.run_server(debug=True)
if __name__ == "__main__":
run_app()