farquasar commited on
Commit
cdd758f
·
verified ·
1 Parent(s): 5192360

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -19
app.py CHANGED
@@ -199,31 +199,77 @@ def predict_and_plot(timeframe, limit, epsilon, n_steps, ma):
199
  return fig
200
 
201
 
202
- def predict_both_plots(limit, epsilon, ma):
203
- period = f'{limit}d'
204
- n_steps = limit
205
- btc_data_1d = fetch_yfinance_data('BTC/USDT', period, interval='1d')
206
- btc_data_4h = fetch_yfinance_data('BTC/USDT', period, interval='4h')
207
- bch_data_1d = fetch_yfinance_data('BCH/USDT', period, interval='1d')
208
- bch_data_4h = fetch_yfinance_data('BCH/USDT', period, interval='4h')
209
-
210
- btc_data_1d, _ = normalize(btc_data_1d)
211
- bch_data_1d, _ = normalize(bch_data_1d)
212
- btc_data_4h, _ = normalize(btc_data_4h)
213
- bch_data_4h, _ = normalize(bch_data_4h)
214
-
215
- preds_1d, label_1d = predictions(model_n1d_cat, btc_data_1d, bch_data_1d, name='1 day', n_steps=n_steps)
216
- preds_4h, label_4h = predictions(model_n4h_cat, btc_data_4h, bch_data_4h, name='4 hours', n_steps=n_steps)
217
- fig_1d = plot(preds_1d, label=label_1d, timeframe='1d', ma=ma, n_steps=n_steps)
218
- fig_4h = plot(preds_4h, label=label_4h, timeframe='4h', ma=ma, n_steps=n_steps)
219
- return fig_1d, fig_4h
220
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221
  with gr.Blocks() as demo:
222
  with gr.Row():
223
  with gr.Column(scale=1):
224
  limit = gr.Slider(50,500,step=50,value=100,label='Number of points')
225
  epsilon = gr.Slider(0.1,5.0,step=0.1,value=2.0, label='Epsilon')
226
- # n_steps = gr.Slider(50,500,step=50,value=200,label='N_steps')
227
  ma = gr.Slider(1,20,step=1,value=5, label='MA Window')
228
  run_btn = gr.Button("Run Prediction")
229
 
 
199
  return fig
200
 
201
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
202
 
203
+ def make_interactive_fig(y, label, timeframe='1h', ma=5, n_steps=None, line_width=2):
204
+ # align lengths
205
+ if n_steps is None or n_steps > len(label):
206
+ n_steps = len(label)
207
+ dates = label['timestamp'].iloc[-n_steps:]
208
+ real = label['close'].iloc[-n_steps:]
209
+ preds = 5 * (y[-n_steps:] - 0.5)
210
+
211
+ # rolling means
212
+ real_ma = pd.Series(real.values).rolling(window=ma).mean()
213
+ pred_ma = pd.Series(preds).rolling(window=ma).mean()
214
+ diff_ma = (real_ma - pred_ma)
215
+
216
+ fig = go.Figure()
217
+ fig.add_trace(go.Scatter(
218
+ x=dates, y=pred_ma,
219
+ mode='lines', name='Predicted Δ',
220
+ line=dict(width=line_width)
221
+ ))
222
+ fig.add_trace(go.Scatter(
223
+ x=dates, y=real_ma,
224
+ mode='lines', name='Real Close',
225
+ line=dict(width=line_width)
226
+ ))
227
+ fig.add_trace(go.Scatter(
228
+ x=dates, y=diff_ma,
229
+ mode='lines', name='Difference',
230
+ line=dict(width=line_width)
231
+ ))
232
+ # horizontal zero line
233
+ fig.add_shape(
234
+ type='line', x0=dates.min(), x1=dates.max(),
235
+ y0=0, y1=0, line=dict(dash='dash', width=line_width)
236
+ )
237
+ fig.update_layout(
238
+ title=f"BTC {timeframe} Forecast vs. Real",
239
+ xaxis_title='Timestamp',
240
+ yaxis_title='Value',
241
+ hovermode='x unified'
242
+ )
243
+ return fig
244
+
245
+ def predict_both_plots(limit, epsilon, n_steps, ma):
246
+ period = f'{limit}d'
247
+ # fetch & normalize both timeframes
248
+ btc_1d = fetch_yfinance_data('BTC/USDT', period, '1d')
249
+ bch_1d = fetch_yfinance_data('BCH/USDT', period, '1d')
250
+ btc_1d, _ = normalize(btc_1d)
251
+ bch_1d, _ = normalize(bch_1d)
252
+
253
+ btc_4h = fetch_yfinance_data('BTC/USDT', period, '4h')
254
+ bch_4h = fetch_yfinance_data('BCH/USDT', period, '4h')
255
+ btc_4h, _ = normalize(btc_4h)
256
+ bch_4h, _ = normalize(bch_4h)
257
+
258
+ # generate predictions
259
+ y1, lbl1 = predictions(model_n1d_cat, btc_1d, bch_1d, '1d', n_steps)
260
+ y2, lbl2 = predictions(model_n4h_cat, btc_4h, bch_4h, '4h', n_steps)
261
+
262
+ # build interactive figures
263
+ fig1 = make_interactive_fig(y1, lbl1, timeframe='1d', ma=ma, n_steps=n_steps)
264
+ fig2 = make_interactive_fig(y2, lbl2, timeframe='4h', ma=ma, n_steps=n_steps)
265
+
266
+ return fig1, fig2
267
+
268
  with gr.Blocks() as demo:
269
  with gr.Row():
270
  with gr.Column(scale=1):
271
  limit = gr.Slider(50,500,step=50,value=100,label='Number of points')
272
  epsilon = gr.Slider(0.1,5.0,step=0.1,value=2.0, label='Epsilon')
 
273
  ma = gr.Slider(1,20,step=1,value=5, label='MA Window')
274
  run_btn = gr.Button("Run Prediction")
275