Kashaf1 commited on
Commit
9446bfc
·
1 Parent(s): 51e41b5

Deleted all

Browse files
CHANGES.md DELETED
@@ -1,383 +0,0 @@
1
- # Changelog: 10-feature covariate input
2
-
3
- What changed, in one place, so you don't have to diff every file. Nothing
4
- listed under "Unchanged" was touched.
5
-
6
- ## New files
7
- - `features/feature_pipeline.py` — the leak-free feature engine. Builds the
8
- 14-column covariate frame (Open/High/Low/Volume + RSI/MACD/Bollinger/
9
- SMA-EMA/Stochastic; Close and Bollinger's mid-band deliberately excluded,
10
- see its module docstring) from `indicators/indicators.py`'s existing,
11
- already-causal indicator functions. Also owns `carry_forward_future()`
12
- (the documented persistence approximation for multi-step forecasts) and
13
- `validate_features()` (fails loudly on length/NaN problems instead of
14
- silently feeding a model bad data).
15
- - `tests/` (new directory, 3 files) — see README's new "Running the tests"
16
- section. Proves the no-lookahead property and backtest window-alignment
17
- by execution against synthetic data, and integration-tests all 5 models'
18
- `features=` path (skips gracefully per-model if that model's own
19
- dependency isn't installed in a given environment).
20
- - `CHANGES.md` — this file.
21
-
22
- ## Changed files
23
- - `models/base_model.py` — `predict()`'s abstract signature gained
24
- `features: pd.DataFrame = None`. Default `None` = old behavior, unchanged.
25
- - `models/arima_model.py`, `models/auto_arima_model.py` — `features=` is
26
- used as `statsmodels` `exog` (a genuine ARIMAX), including in the order
27
- search itself. Forecasting beyond the last observed candle uses
28
- `carry_forward_future()` for the exog rows it structurally needs.
29
- - `models/arima_garch_model.py` — same idea, fed into the ARIMA **mean**
30
- step only (not the GARCH variance step — see its module docstring for
31
- why). Needed one extra bit of care: aligning `features` against
32
- `log_returns`, which is one row shorter than `history` (verified
33
- numerically in `tests/test_models_with_features.py`'s
34
- `TestArimaFamilyAlignment`, no `statsmodels` needed to check it).
35
- - `models/moirai_model.py` — `features=` becomes `past_feat_dynamic_real`
36
- (uni2ts/GluonTS's own past-only covariate input — Moirai never needs
37
- `carry_forward_future()`, it wasn't designed to need future covariate
38
- values at all).
39
- - `models/timesfm_model.py` — `features=` goes through TimesFM 2.5's own
40
- `forecast_with_covariates()` (needs the new `timesfm[xreg]` extra).
41
- - `models/registry.py` — `get_model()` gained `use_features=`; for the 3
42
- ARIMA-family models this extends the cache key so a features-on and a
43
- features-off call never share (and silently misuse) the same cached
44
- (p,d,q) order. `fresh_model()` (what Backtest already uses) needed no
45
- change — it never caches.
46
- - `backtest/backtester.py` — `backtest_model()` gained `features_df=`,
47
- sliced with the *identical* `[i-window:i]` window as `history` at every
48
- step. New `run_comparison_backtest()` runs baseline and featured back to
49
- back on identical data and returns both, labeled, never blended.
50
- - `app.py` — Prediction tab and Live tab each got a "use 10-feature input"
51
- checkbox; Backtest tab got a 3-way mode (Close-only / +10-feature /
52
- Compare both) plus a "Mode" column on its summary table and per-model
53
- detail logs. Live tab's accuracy number is computed per-mode (a log
54
- containing both modes' rows, from toggling mid-session, is never blended
55
- into one accuracy figure).
56
- - `live_runner.py` — new `--use-features` flag; logs to a separate
57
- `..._featured.csv` file so a baseline run and a featured run never share
58
- a log.
59
- - `utils/helpers.py` — new `feature_warmup_days()`, used by
60
- `data/historical.py`'s `get_historical_with_features()` (see the Update
61
- section below) to know how many extra days of padding a timeframe needs
62
- for indicators to warm up on.
63
- - `requirements.txt` — `timesfm[torch]` → `timesfm[torch,xreg]` (xreg pulls
64
- in scikit-learn + JAX, only needed for the featured TimesFM path).
65
- - `README.md` — new "10-feature covariate input" section, a new "Running
66
- the tests" section, 2 new bullets under "Known limitations" (what's
67
- actually been verified vs. not, and the multi-step carry-forward
68
- caveat), `features/`/`tests/` added to "Project layout".
69
-
70
- ## Unchanged
71
- Every other file, and — with `features=None`/`use_features=False`, still
72
- the default everywhere — every existing code path's behavior and output.
73
-
74
- ## What's verified vs. not (please read)
75
- This was built in a sandbox with only `pandas`/`numpy`/`scikit-learn`
76
- available and no network access — same constraint the project's own README
77
- already discloses for its two Hugging Face checkpoint strings. Concretely:
78
- - **Executed and verified**: the leak-free property, the backtest window
79
- alignment, and the ARIMA-GARCH returns/features alignment — all against
80
- synthetic OHLCV data, using only `pandas`/`numpy` (see `tests/`).
81
- - **Written against real, verified library behavior but not executed**:
82
- the `statsmodels` `exog` calls (this project already depended on
83
- `statsmodels`, so the API itself is well-established); Moirai's
84
- `past_feat_dynamic_real` (checked against `uni2ts`'s own GitHub
85
- discussion of that forward-pass argument and GluonTS's `PandasDataset`
86
- constructor); TimesFM's `forecast_with_covariates` (checked against the
87
- official `google-research/timesfm` repo's current docs and a recent real
88
- financial-forecasting example using the same call).
89
- - **Action for you**: run `python -m unittest discover -s tests -v` after
90
- `pip install -r requirements.txt` in your real environment — every skip
91
- guard should turn green, which is the actual end-to-end confirmation the
92
- sandbox this was built in couldn't provide for Moirai/TimesFM.
93
-
94
- ---
95
-
96
- ## Update: bug found and fixed from a real deployed Backtest run
97
-
98
- You ran the deployed Space (BTC-USD, 1m, window=60, 10-feature mode) and
99
- the first ~19 predictions of every model failed with `feature window
100
- contains NaN in ['BB_Upper','BB_Lower','SMA_20']`. Real bug, now fixed.
101
-
102
- **Root cause:** SMA_20/Bollinger need 20 real candles before them to
103
- produce a value. Row 0 of *whatever range got fetched* never has any —
104
- raising `window` or `lookback_days` doesn't help, because the NaN rows are
105
- always at the very front of the fetch, regardless of how big the fetch is.
106
- Not a leak (`validate_features` was correctly rejecting genuinely
107
- undefined data) — just an avoidable inefficiency that cost the first ~19
108
- of every featured backtest's predictions, and could have failed a Live-tab
109
- or Prediction-tab call outright depending on lookback settings.
110
-
111
- **Fix — new files/functions:**
112
- - `features/feature_pipeline.py`: new `compute_feature_frame_for_period(df_padded, keep_days)`.
113
- Computes indicators on a padded (extra history in front) frame, then
114
- trims both the OHLCV and the computed features back down to `keep_days`
115
- — so the returned indicators are never NaN purely for lack of warm-up,
116
- and the tested/predicted period is still exactly what was asked for.
117
- - `data/historical.py`: new `get_historical_with_features(symbol,
118
- timeframe, lookback_days, use_features, refresh=True)` — the new single
119
- entry point. Fetches `lookback_days + feature_warmup_days(timeframe)`
120
- when `use_features=True`, calls `compute_feature_frame_for_period`, and
121
- returns `(df, features_df)` already trimmed and NaN-free. Returns
122
- `(get_historical_cached(...), None)` unchanged when `use_features=False`.
123
-
124
- **A second, related bug fixed alongside it:** Moirai's and TimesFM's own
125
- `features=` handling validated the *entire* history handed to `.predict()`
126
- for NaN, even though both models only ever use its tail
127
- (`context`/`feat_context`) — so a NaN sitting only in the part they'd
128
- truncate away could reject an otherwise-perfectly-usable window.
129
- `validate_features()` gained a `check_nan: bool = True` parameter;
130
- `moirai_model.py`/`timesfm_model.py` now call it once (length-only,
131
- `check_nan=False`) against the full history to still catch a genuinely
132
- misaligned caller early, then again (the default, NaN-included) against
133
- just the truncated tail actually used.
134
-
135
- **Changed to use the new entry point:**
136
- - `app.py` — Prediction tab, Backtest tab (both the single-mode and
137
- Compare-both branches), and the Live tab's `_check_live_impl` all now
138
- call `get_historical_with_features()` instead of a plain
139
- `get_historical_cached()` + separate `compute_feature_frame()`.
140
- - `live_runner.py` — same replacement.
141
- - `backtest/backtester.py` — `run_all_models_backtest()`'s `use_features:
142
- bool` parameter became `features_df: pd.DataFrame = None` (caller
143
- supplies an already-computed, already-warmed-up frame directly);
144
- `run_comparison_backtest()` similarly takes `features_df` as a required
145
- positional argument instead of computing it internally. Necessary, not
146
- just cleaner: internally recomputing `compute_feature_frame(df)` from an
147
- already-trimmed `df` would have silently reintroduced the exact NaN bug
148
- this update fixes.
149
-
150
- **Tests added/updated:**
151
- - `tests/test_feature_pipeline.py` — `TestComputeFeatureFrameForPeriod`
152
- (4 new tests), including `test_reproduces_and_fixes_the_reported_
153
- btc_1m_window60_case`, a direct reproduction of your exact scenario.
154
- `TestValidateFeatures` gained 2 tests for the new `check_nan=` split.
155
- - `tests/test_backtester_alignment.py` — updated for the `features_df=`
156
- signature change (was `use_features=`).
157
- - Full suite: 35 tests, 0 failures, 7 skipped (same 5-model dependency
158
- gaps as before — nothing new skipped by this update).
159
- - `app.py` was also re-verified by actually importing it (via lightweight
160
- stubs for gradio/plotly/yfinance) and executing its full `gr.Blocks()`
161
- layout end to end, confirming every `.click()` wiring still matches its
162
- handler's parameter count after these signature changes.
163
-
164
- **What this doesn't change:** the leak-free property, the carry-forward
165
- approximation for multi-step horizons, and every model's actual covariate
166
- mechanism (exog/past_feat_dynamic_real/forecast_with_covariates) are
167
- untouched — this update is entirely about *how much history gets fetched
168
- before computing on it*, not what gets computed or how it's fed to a
169
- model.
170
-
171
- ## Update: container logs + full codebase audit (7 bugs found and fixed)
172
-
173
- You pasted the actual container startup log from a deployed run and asked
174
- for a complete pass over the whole zip, not a spot fix. Here's everything
175
- that came out of it, roughly in order of how much it could hurt you.
176
-
177
- ### 1. Live tab was completely broken on first use, for every timeframe except daily (critical)
178
-
179
- `_check_live_impl`'s "mark old pending predictions unresolvable" step ran
180
- `pd.to_datetime(log_df["target_time"]) < cutoff` unconditionally. On a
181
- genuinely fresh session, `log_df` starts as
182
- `pd.DataFrame(columns=LIVE_COLUMNS)` — zero rows, so every column
183
- (including `target_time`) is dtype `object` with nothing in it to infer a
184
- type from. `pd.to_datetime()` on that produces a tz-**naive** result;
185
- `cutoff` is always tz-**aware** (built from the fetched data's own
186
- tzinfo) — comparing the two raises `TypeError: Cannot compare tz-naive
187
- and tz-aware datetime-like objects` for any timeframe with a finite
188
- `max_servable_days` (i.e. everything except 1d/1wk/1mo). `check_live()`'s
189
- outer `try/except` stops this from crashing the whole app, but it also
190
- hands back the same still-empty `log_df` as the new state — so the next
191
- click starts from empty too, hits the identical error, and the tab could
192
- never get past its very first row, no matter how many times it was
193
- clicked. Reproduced directly (not just reasoned about): a fresh
194
- `gr.State` + one `check_live()` call raises exactly this, on every
195
- intraday timeframe.
196
-
197
- Fix: skip that block entirely when `log_df` has zero rows (there's
198
- nothing to check anyway), and pass `utc=True` to `pd.to_datetime()` for
199
- the non-empty case too, as a second layer of protection against the same
200
- tz-naive/aware mismatch however `target_time` ended up stored.
201
-
202
- ### 2. ARIMA-GARCH flooding the logs with `ConvergenceWarning` (the noise in your pasted log)
203
-
204
- `arch`'s own `ARCHModelResult.fit()` calls
205
- `warnings.filterwarnings("always", "", ConvergenceWarning)` internally
206
- whenever its `show_warning` parameter is true — which it is by default,
207
- and this project's `.fit(disp="off")` call never overrode it. That
208
- inserts a fresh filter ahead of this file's own top-of-module
209
- `warnings.filterwarnings("ignore")`, so every non-fully-converged GARCH
210
- fit printed its warning regardless. `disp="off"` only silences the
211
- optimizer's own iteration dump, not this. GARCH(1,1) MLE on a short
212
- walk-forward window (tens of residuals per backtest step) frequently
213
- doesn't fully converge — expected behavior on short windows, not a sign
214
- of a broken fit, since the price forecast comes from the ARIMA mean step
215
- and never depends on GARCH's convergence status. Confirmed against
216
- `arch`'s actual source before fixing, not guessed. Fix: pass
217
- `show_warning=False` (the library's own supported switch), plus
218
- `options={"maxiter": 500}` to genuinely reduce non-convergence, plus a
219
- finite/non-negative check on the forecasted variance itself. Verified
220
- with a 340-step backtest: 0 bytes of stderr output, versus the flood
221
- before.
222
-
223
- ### 3. 1-minute data could never actually reach more than 7 days, no matter what was asked for
224
-
225
- `config.py` had `YF_NATIVE_INTERVALS["1m"]["max_days"] = 7` — but 7 is
226
- Yahoo's **per-request** cap, not its total history depth (confirmed
227
- independently: Yahoo serves up to ~30 days of 1m history in total, 7 days
228
- per request). `_native_fetch` already had a correctly-written
229
- chunked-fetch loop to stitch multiple 7-day requests together up to 30
230
- days — but it clamped `days = min(days, max_days)` **before** checking
231
- `if interval == "1m" and days > 7`, and with `max_days` wrongly set to 7,
232
- `days` could never exceed 7 by the time that check ran. The entire
233
- chunking path was dead code; any request for more than 7 days of 1m data
234
- was silently truncated to 7. Fix: `max_days` for `"1m"` is now 30 (the
235
- real total-history depth); the per-request 7-day cap is still enforced
236
- separately inside the chunking loop itself, which now actually runs.
237
- Verified requesting 3/7/10/20/30/45 days of 1m data: correctly returns up
238
- to 30 days now, was hard-capped at 7 before.
239
-
240
- ### 4. That fix (and the previous "BTC-USD/1m/window=60" fix above) could still reintroduce the NaN bug near an interval's max_days
241
-
242
- `get_historical_with_features` fetches `lookback_days + padding_days` and
243
- relies on `_native_fetch`'s own `max_days` clamp to protect Yahoo's
244
- limits — but when `lookback_days` was already close to `max_days` (e.g.
245
- 1m at 30, or 5m/15m/30m at their shared 60-day cap — all directly
246
- reachable from this project's own lookback sliders, which go up to 60),
247
- the padded fetch got silently clamped back down to no bigger than the
248
- plain `lookback_days` request, the padding vanished, and
249
- `compute_feature_frame_for_period` had nothing extra to trim off —
250
- reproducing the exact `NaN in ['BB_Upper','BB_Lower','SMA_20']` error the
251
- prior update fixed, just at a higher `lookback_days` instead of a low
252
- one. Fix: shrink the *requested* `lookback_days` itself up front so
253
- `lookback_days + padding_days` never exceeds `max_days`, guaranteeing
254
- real padding always survives. Verified NaN-free across 8 timeframes × 11
255
- lookback values spanning each interval's boundary.
256
-
257
- ### 5. Daily-timeframe "Lookback (days)" did nothing — always fetched the entire available history
258
-
259
- `_native_fetch`'s `max_days is None` branch (1d/1wk/1mo) called
260
- `yf.Ticker(symbol).history(period="max", ...)` and returned it exactly as
261
- given, ignoring the `days` argument completely. Every code path that
262
- doesn't separately re-trim afterward — `get_historical_cached`'s
263
- Close-only route, i.e. the Chart tab (no features toggle at all), the
264
- Prediction tab with the features checkbox off (the default), the
265
- Backtest tab's default "Close-only" mode, and the Live tab with features
266
- off (the default) — silently got a symbol's *entire* multi-year daily
267
- history back regardless of what the lookback slider said. Concretely
268
- reproduced: a Backtest asking for 2 days of daily EURUSD=X data actually
269
- walked forward across its entire available history instead, took 38+
270
- seconds doing it, and tested a completely different period than the one
271
- requested. Fix: trim to the requested `days` after the `period="max"`
272
- fetch, the same last-timestamp-relative way every other branch of this
273
- function already does it. Verified 2/10/60/200-day requests each now
274
- return exactly that span.
275
-
276
- ### 6. Prediction/Live tabs could take minutes (unbounded) on a large lookback + small timeframe
277
-
278
- Unlike Moirai/TimesFM (which already cap their input to
279
- `context_length`/`max_context`), `ArimaModel`/`AutoArimaModel`/
280
- `ArimaGarchModel` fit on however much history they were handed, with no
281
- cap — and neither the Prediction tab nor the Live tab windows its input
282
- the way Backtest's own "History window per prediction" slider does
283
- (capped at 300). Measured directly: ARIMA's grid search on this
284
- project's own hardware went from ~6s at 500 candles (Close-only) to
285
- ~30s at 4,000 to a measured 100+ seconds at 4,000 candles **with**
286
- 10-feature input — climbing from there, since fitting cost scales worse
287
- than linearly with series length. 30 days of 1-minute data (now reachable
288
- per fix #3 above) is ~43,000 candles; before this fix that extrapolates
289
- to many minutes to fit a *single* model, multiplied across however many
290
- models are selected. Fix: added `max_history=300` to all three classical
291
- models (matching Backtest's own established ceiling), truncating to the
292
- most recent 300 candles before the order search or any fit — Backtest
293
- itself is unaffected since its windows are already ≤300. Verified: the
294
- same 3-model, 30-day, 1m, +features Prediction-tab call that would
295
- previously have taken many minutes and climbing now completes in a
296
- bounded ~16 seconds, regardless of how much history was fetched.
297
-
298
- ### 7. Backtest could still take many minutes even with #6 fixed, from sheer step count
299
-
300
- A long lookback on a small timeframe combined with a small step size can
301
- mean tens of thousands of walk-forward evaluations even with each
302
- individual fit now cheap — e.g. 30 days of 1-minute data at step=1 is
303
- ~40,000 `model.predict()` calls. Measured per-step cost after fix #6:
304
- ARIMA ~9.7ms, Auto-ARIMA ~9.2ms, ARIMA-GARCH ~43ms — at 40,000 steps
305
- that's 6–29 minutes for a *single* model in a *single* mode, doubled in
306
- Compare mode, summed again across however many models are selected.
307
- New `backtest/backtester.py::resolve_backtest_step()` raises (never
308
- lowers) `step` just enough to keep the walk-forward loop near 500
309
- evaluations; wired into `backtest_model()` itself as a default safety net
310
- (so any caller gets it, not just the UI) and pre-computed in
311
- `run_backtest_ui` so the status message tells you when and why it
312
- happened, e.g. *"Step size increased from 1 to 84 to keep this backtest
313
- responsive over 41,761 candles."* Verified: a scenario that previously
314
- didn't finish inside a 150-second timeout now completes (with the notice
315
- shown) in well under a minute.
316
-
317
- ### Test suite: 3 tests were actually failing, not just skipping
318
-
319
- Running the suite with the real dependencies installed (`statsmodels` +
320
- `arch` — the "5-model dependency gaps" the prior update's own "35 tests,
321
- 0 failures" claim was measured *without*) surfaced 3 genuine errors:
322
- `test_models_with_features.py`'s `TestArimaWithFeatures`/
323
- `TestAutoArimaWithFeatures`/`TestArimaGarchWithFeatures` built their test
324
- fixtures by calling `compute_feature_frame()` directly on an unpadded
325
- frame and slicing from row 0 — exactly the NaN-in-the-first-19-rows shape
326
- that get_historical_with_features exists to prevent production code from
327
- ever constructing. Production was never affected (every real call path
328
- already goes through the padded/trimmed entry point), but the tests
329
- themselves were exercising an input shape that can't happen for real,
330
- and erroring instead of actually testing the `features=` code path. Fixed
331
- by fetching 20 extra warm-up candles and dropping them from both `close`
332
- and `feats` in each `setUp()`, mirroring the shape production actually
333
- hands these models. Full suite now genuinely passes: 35 tests, 0
334
- failures, 2 skipped (Moirai/TimesFM — see below).
335
-
336
- ### Also cleaned up
337
- - An unused `import numpy as np` in `features/feature_pipeline.py`, and an
338
- unused local variable (plus a stale cross-reference to a test method
339
- name that no longer matched) in `tests/test_backtester_alignment.py`.
340
-
341
- ### What was checked and found correct (no bug)
342
-
343
- - **Moirai's and TimesFM's external API usage** — the part of this
344
- project hardest to verify without GPU-class dependencies. Checked
345
- `MoiraiForecast`/`MoiraiModule.from_pretrained`/`create_predictor`/
346
- `PandasDataset(..., past_feat_dynamic_real=...)` and
347
- `TimesFM_2p5_200M_torch.from_pretrained`/`ForecastConfig`/`.forecast()`/
348
- `.forecast_with_covariates(dynamic_numerical_covariates=...,
349
- xreg_mode=...)` directly against the official uni2ts/GluonTS/TimesFM
350
- repos and model cards, not from memory. Every call signature, parameter
351
- name, and return shape matches documented usage.
352
- - The CSV cache round-trip (write → read back) and `resample_ohlc` under
353
- pandas 3.0 — a ~1e-14 floating-point difference on round-tripped floats
354
- through CSV text, which is inherent to how any float64 survives a
355
- text-based format and has no practical effect on anything this project
356
- computes.
357
- - `live_runner.py` does not share bug #1 (it doesn't have the
358
- "mark unreachable" logic at all), and its "predict from whatever the
359
- latest fetched candle is, even if still forming" behavior matches
360
- `app.py`'s Live tab exactly — not a divergence, a shared, intentional,
361
- already-documented limitation of polling instead of streaming.
362
-
363
- ### Verification methodology
364
-
365
- Unlike the prior update (built where `statsmodels`/`arch` weren't
366
- available), this pass installed the real dependencies —
367
- `pandas 3.0.2, numpy 2.4.4, statsmodels 0.15.0, arch 8.0.0, gradio 6.26.0,
368
- plotly 7.0.0, yfinance 1.7.0` — and exercised the actual code, not just
369
- read it:
370
- - Full test suite run for real (not reasoned about): 35 passed, 2 skipped
371
- (Moirai/TimesFM — `uni2ts`/`gluonts`/`timesfm`/`torch` need GPU-class
372
- installs this sandbox can't do; their API usage was instead verified
373
- against official docs, see above).
374
- - `app.py` imported for real (not stubbed) with a mocked `yfinance.Ticker`
375
- standing in for the network, and every tab's handler function
376
- (`fetch_and_chart`, `run_prediction`, `run_backtest_ui`, `check_live`)
377
- called end-to-end across baseline/featured/compare modes, multiple
378
- timeframes, edge cases (no models selected, not-enough-data, a fresh
379
- empty Live session), and the specific scenarios each bug above was
380
- found in — 12/12 passing after all fixes.
381
- - Every performance claim above (timing numbers, "which model is
382
- slowest", "how many ms per step") was measured on this project's own
383
- code in this environment, not estimated.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
README.md DELETED
@@ -1,344 +0,0 @@
1
- ---
2
- title: Forex Crypto Prediction
3
- emoji: 📈
4
- colorFrom: blue
5
- colorTo: green
6
- sdk: gradio
7
- app_file: app.py
8
- pinned: false
9
- python_version: "3.11"
10
- ---
11
-
12
- # 📈 Forex & Crypto Prediction Dashboard
13
-
14
- Multi-timeframe forex & crypto price prediction with **ARIMA**,
15
- **Auto-ARIMA**, **ARIMA-GARCH**, **Moirai** (Salesforce) and **TimesFM**
16
- (Google) — each model's accuracy is backtested and reported **separately**,
17
- plus a candle-by-candle correct/incorrect verification log. Pure Python,
18
- built for a free/low CPU-only Hugging Face Space (2 vCPU / 16GB RAM / ~50GB
19
- storage).
20
-
21
- **Educational / research project — not financial advice.** Forex and crypto
22
- trading carries substantial risk of loss. Nothing this app outputs is a
23
- recommendation to trade.
24
-
25
- ## What's inside
26
-
27
- - **Live + historical data**, free, no API key — [yfinance](https://github.com/ranaroussi/yfinance)
28
- (Yahoo Finance). Live means polling the same free source on a timer, since
29
- Yahoo has no free websocket feed.
30
- - **Any symbol** — a dropdown of popular forex pairs (`EURUSD=X`, `GBPUSD=X`, ...)
31
- and crypto pairs (`BTC-USD`, `ETH-USD`, ...) that also accepts any custom
32
- yfinance-valid ticker you type in.
33
- - **Any timeframe** — 1m, 5m, 10m, 15m, 30m, 1h, 4h, 1d out of the box.
34
- Timeframes Yahoo doesn't serve natively (like 10m) are built by resampling
35
- a smaller native interval, so you're not limited to Yahoo's fixed list.
36
- - **5 forecasting models**, one shared interface (`models/base_model.py`):
37
- - **ARIMA** (`statsmodels`) — fast, classical, always available. Order
38
- (p,d,q) is found with a brute-force AIC grid search.
39
- - **Auto-ARIMA** (`statsmodels`) — same idea as ARIMA, but the order is
40
- found with a Hyndman-Khandakar-style stepwise search (the same
41
- algorithm behind R's `auto.arima()`) instead of a brute-force grid.
42
- Implemented directly on statsmodels rather than depending on
43
- `pmdarima` — that package's compiled Cython extensions are a known
44
- source of numpy-version install breakage on fresh environments.
45
- - **ARIMA-GARCH** (`statsmodels` + `arch`) — ARIMA models the conditional
46
- mean of returns, GARCH(1,1) models the conditional variance (volatility
47
- clustering) on top of it. Forecasted volatility is kept on the model
48
- instance (`last_volatility_forecast`) for anyone who wants to build
49
- confidence bands later.
50
- - **Moirai** (`uni2ts`) — Salesforce's pretrained time-series foundation model.
51
- - **TimesFM 2.5** (`timesfm`) — Google's pretrained time-series foundation model.
52
- - **Top 5 technical indicators** (hand-implemented on pandas, no extra
53
- dependency): RSI, MACD, Bollinger Bands, SMA/EMA, Stochastic Oscillator.
54
- These are shown for chart analysis — they're separate from, and not mixed
55
- into, the 5 models' backtested accuracy.
56
- - **Walk-forward backtesting** with a **separate** accuracy card per model
57
- (correct / incorrect / accuracy % / MAE / RMSE) — numbers are never
58
- averaged together across models — plus a per-candle log of every
59
- prediction vs. what actually happened, so you can verify exactly how many
60
- candles each model got right or wrong.
61
- - **Live tab**: polls for the newest candle, predicts the next one, and once
62
- that candle closes, checks whether the prediction was right — with a
63
- running accuracy tally. `live_runner.py` does the same thing unattended
64
- from the command line, logging to CSV.
65
-
66
- ## Project layout
67
-
68
- ```
69
- config.py symbols, timeframes, model checkpoints
70
- app.py Gradio dashboard (entry point for the Space)
71
- live_runner.py standalone CLI for unattended live logging
72
- data/
73
- historical.py yfinance fetch + resampling + 7-day-chunked 1m fetch
74
- live.py polling loop helper
75
- indicators/
76
- indicators.py RSI, MACD, Bollinger Bands, SMA/EMA, Stochastic
77
- features/
78
- feature_pipeline.py 10-feature covariate pipeline (leak-free) -- shared by
79
- backtest, live prediction and app.py's Prediction tab
80
- models/
81
- base_model.py shared interface all 5 models implement
82
- arima_model.py ARIMA (statsmodels), cached order search
83
- auto_arima_model.py Auto-ARIMA (statsmodels only), cached stepwise search
84
- arima_garch_model.py ARIMA-GARCH (statsmodels + arch), mean + volatility
85
- moirai_model.py Moirai (uni2ts + GluonTS)
86
- timesfm_model.py TimesFM 2.5 (timesfm[torch])
87
- registry.py lazy-loading model factory
88
- backtest/
89
- backtester.py walk-forward loop + per-model accuracy summary
90
- utils/
91
- helpers.py small shared utilities
92
- tests/
93
- test_feature_pipeline.py no-lookahead + shape checks for features/
94
- test_backtester_alignment.py walk-forward window-alignment checks
95
- test_models_with_features.py per-model checks (skip gracefully if a
96
- model's own dependency isn't installed)
97
- requirements.txt
98
- ```
99
-
100
- ## Running locally
101
-
102
- ```bash
103
- python -m venv venv
104
- source venv/bin/activate # Windows: venv\Scripts\activate
105
- pip install -r requirements.txt
106
- python app.py
107
- ```
108
-
109
- Open the local URL Gradio prints (usually `http://127.0.0.1:7860`).
110
-
111
- ## Running the tests
112
-
113
- ```bash
114
- python -m unittest discover -s tests -v
115
- ```
116
-
117
- `test_feature_pipeline.py` and `test_backtester_alignment.py` only need
118
- `pandas`/`numpy` (no `statsmodels`/`torch`/etc.) and always run fully —
119
- they're what actually proves the leak-free and window-alignment claims
120
- above by execution, not just by reading the code. `test_models_with_
121
- features.py` exercises each of the 5 models' real `features=` path end to
122
- end, but skips (not fails) whichever model's own dependency isn't
123
- installed in your environment — install `requirements.txt` fully to run
124
- all of it.
125
-
126
- ## Deploying to Hugging Face Spaces
127
-
128
- 1. Create a new Space → SDK: **Gradio** → hardware: **2 vCPU / 16GB RAM**
129
- (the free CPU tier) is enough for all 5 models at their default (small)
130
- checkpoints.
131
- 2. Upload every file in this project, keeping the folder structure
132
- (`data/`, `models/`, etc.) intact — Spaces builds from the repo root.
133
- 3. The YAML block at the top of this README is what Spaces reads to
134
- configure the app (`sdk: gradio`, `app_file: app.py`). No changes needed
135
- unless you want a different title/emoji/colors.
136
- 4. Push. The Space installs `requirements.txt` and launches `app.py`
137
- automatically. First launch is normal Python-only (ARIMA works
138
- immediately); Moirai/TimesFM download their pretrained weights
139
- on-demand the first time you actually use them from the UI.
140
-
141
- ## Models — what to expect on CPU-only hardware
142
-
143
- | Model | Package | Approx. size | Notes |
144
- |---|---|---|---|
145
- | ARIMA | `statsmodels` | tiny | Fastest by far. Order (p,d,q) is searched once per backtest run (brute-force AIC grid) and reused, not re-searched every candle. |
146
- | Auto-ARIMA | `statsmodels` | tiny | Same dependency as plain ARIMA — no extra package. Stepwise order search runs once and is reused; usually visits fewer candidate models than ARIMA's brute-force grid. |
147
- | ARIMA-GARCH | `statsmodels` + `arch` | tiny | Two fits per prediction (ARIMA for the mean, GARCH for the volatility), so a little slower than plain ARIMA — still fast next to the foundation models. |
148
- | Moirai (`moirai-1.1-R-small`) | `uni2ts` | small (~14M params) | Zero-shot foundation model, no training needed. |
149
- | TimesFM 2.5 (`timesfm-2.5-200m`) | `timesfm[torch]` | ~800MB download, ~4GB RAM | Zero-shot foundation model, no training needed. |
150
-
151
- Both foundation models are **zero-shot** — they were pretrained elsewhere and
152
- just run inference here, no training step in this project. They're slower
153
- per-prediction than ARIMA on CPU, which matters most in the **Backtest** tab
154
- (one model call per candle). Use the **Step** slider there to skip candles
155
- (e.g. step=5 or 10) and keep Moirai/TimesFM backtests fast on 2 vCPUs.
156
- Checkpoints are configurable in `config.py` if you later move to GPU hardware
157
- and want the bigger `base`/`large` variants.
158
-
159
- ## 10-feature covariate input (optional, off by default)
160
-
161
- Every model above can optionally take more than just the Close-price series:
162
- Open, High, Low, Volume, RSI(14), MACD, Bollinger Bands, SMA/EMA and
163
- Stochastic — the same 5 indicator families the **Chart & Indicators** tab
164
- already computes for display, now actually wired into the models. Off by
165
- default everywhere (checkbox in Prediction/Live, a 3-way mode in Backtest,
166
- `--use-features` in `live_runner.py`); with it off, nothing about the
167
- original Close-only behavior changes.
168
-
169
- **Why 10 named categories become 14 columns, and why Close isn't one of
170
- them:** Close is excluded because it's already the series every model
171
- forecasts — feeding it in again as a covariate would be pure duplication
172
- (and, for the ARIMA family specifically, a design matrix column perfectly
173
- collinear with the model's own dependent variable). Bollinger's mid band is
174
- excluded too — `bollinger_bands()` defines it as `sma(close, 20)`,
175
- identical to SMA_20, which is already included under "SMA/EMA"; keeping
176
- both would feed the model the same numbers twice under different names.
177
- MACD/Bollinger/Stochastic each expand into more than one number (line/
178
- signal/histogram, upper/lower, %K/%D), which is where 9 usable categories
179
- become 14 numeric columns. Full reasoning and the exact column list:
180
- `features/feature_pipeline.py`.
181
-
182
- **Leak-free by construction, not by convention:** every feature value at
183
- candle *t* is computed only from candles ≤ *t* (trailing rolling/EWM
184
- windows, same as the existing indicators). `tests/test_feature_pipeline.py`
185
- proves this by actually truncating the input at different points and
186
- checking every already-computed value is byte-identical either way — not
187
- just documented, executed. Where a model's own forecasting API structurally
188
- needs covariate values for future candles that haven't happened yet
189
- (multi-step ARIMA/Auto-ARIMA/ARIMA-GARCH forecasts, and TimesFM's
190
- `forecast_with_covariates`), those steps get the **last known real reading
191
- held constant** — a standard, clearly-labeled persistence assumption, using
192
- zero information from the future. This is *not* something this project
193
- invented to route around the no-leakage rule — it's the same approach a
194
- recent worked TimesFM-covariates example for financial forecasting uses for
195
- exactly this reason. Moirai is the one model that never needs this trick:
196
- its `past_feat_dynamic_real` input is architecturally past-only, so it's
197
- simply never asked for a future covariate value.
198
-
199
- **Each model uses its own real supported mechanism, not a workaround:**
200
-
201
- | Model | Mechanism |
202
- |---|---|
203
- | ARIMA / Auto-ARIMA / ARIMA-GARCH | `statsmodels`' native `exog` regressor (a genuine ARIMAX) — including in the (p,d,q) order search itself, not bolted on after |
204
- | Moirai | `past_feat_dynamic_real` — uni2ts/GluonTS's first-class "extra series only observed for the historical context" input |
205
- | TimesFM 2.5 | `forecast_with_covariates()` — the library's own xreg extension (needs `timesfm[xreg]`, i.e. scikit-learn + JAX on top of `timesfm[torch]`) |
206
-
207
- **Backtest comparison:** the Backtest tab's "Compare both" mode runs every
208
- selected model twice — Close-only and +10-feature — on the *identical*
209
- symbol, timeframe, window, horizon, step and testing period, so you can see
210
- directly whether the extra features actually changed a given model's
211
- accuracy. There's no assumption either way going in; a model can end up
212
- better, worse, or about the same, and the two numbers are always shown
213
- side by side, never averaged into one.
214
-
215
- ## How accuracy is measured
216
-
217
- For each candle in a backtest (or each live prediction), the model forecasts
218
- the next close; we compare **predicted direction** (up/down vs. the current
219
- close) to what **actually** happened once that candle closes. That's one
220
- verified candle: correct or incorrect. A model's accuracy % is
221
- `correct / (correct + incorrect)` over the whole run — and each model's
222
- numbers live in their own card/tab, never averaged with another model's.
223
- MAE/RMSE (predicted price vs. actual price) are reported alongside as a
224
- secondary, continuous-error view.
225
-
226
- "Once that candle closes" is checked strictly: a prediction only gets
227
- verified once a *newer* candle exists after its target, never merely because
228
- the target timestamp has appeared. Yahoo's intraday feed includes the
229
- currently-forming candle as its last row, with a Close that's just the
230
- latest traded price so far — reading that as final the moment it appears
231
- would grade some candles against a mid-candle price instead of the true
232
- close (Live tab and `live_runner.py` both apply this check the same way).
233
-
234
- The Live tab's data fetch window grows to cover however old the oldest
235
- still-unverified prediction is (capped at whatever that timeframe's source
236
- interval can actually serve on Yahoo), instead of a fixed 2-day fetch. A
237
- fixed window meant a prediction you didn't check back on for a few days
238
- could fall permanently outside every future fetch and just sit as "pending"
239
- forever — silently shrinking the accuracy denominator with nothing telling
240
- you it happened. If a row's target is older than that timeframe's Yahoo
241
- history limit can ever reach (e.g. an unchecked 1-minute prediction from
242
- last week, past Yahoo's ~7-day 1m limit), it's now labeled
243
- `unresolvable (past history limit)` instead of sitting as silently,
244
- indefinitely "pending."
245
-
246
- ## Known limitations (please read before relying on this for anything real)
247
-
248
- - **Yahoo/yfinance history limits** (enforced by Yahoo, not this project):
249
- 1-minute candles ≈ last 7–30 days only; other intraday intervals ≈ last 60
250
- days; hourly ≈ last 730 days; daily+ is full history. The app tells you if
251
- a request falls outside what's available rather than failing silently.
252
- - **"Live" is polling, not push.** Free forex/crypto data has no public
253
- websocket firehose, so "live" here means re-fetching on a timer — fine for
254
- candle-close-level decisions, not for tick-by-tick/HFT use.
255
- - **"All symbols"** means any symbol yfinance/Yahoo covers — there's no
256
- finite master list to ship, so the app takes any ticker you type in rather
257
- than pretending to enumerate everything.
258
- - **Small foundation-model checkpoints** were chosen to fit 2 vCPU/16GB
259
- comfortably. Bigger checkpoints (`base`/`large`) may forecast better but
260
- need more RAM/CPU time — swap them in `config.py` if your hardware allows.
261
- - The backtest metric math (accuracy/MAE/RMSE, walk-forward windowing,
262
- failure accounting) has been executed end-to-end against synthetic
263
- price data and cross-checked line-by-line against an independent manual
264
- recomputation, not just read through — same for `moirai_model.py` and
265
- `timesfm_model.py`'s calls into `uni2ts`/`gluonts`/`timesfm`, checked
266
- against those packages' actual PyPI source rather than assumed from
267
- memory. What's still unverified: the two HF Hub checkpoint strings in
268
- `config.py` (`MOIRAI_CHECKPOINT`, `TIMESFM_CHECKPOINT`) resolve to real
269
- repos — huggingface.co wasn't reachable from the environment this was
270
- checked in, so confirm those load on first use in your own Space, and
271
- otherwise swap in whatever checkpoint name that org currently publishes.
272
- `uni2ts` and `timesfm` are active research projects that occasionally
273
- make breaking API changes, so re-check their docs/repo if either starts
274
- erroring after a `pip install -U`. Every model also validates its own
275
- forecast is finite (no NaN/inf) before returning it, and raises a clear
276
- error instead if it isn't — this surfaces as a "failed" row in a backtest
277
- rather than silently corrupting the accuracy/MAE numbers, which is how an
278
- earlier version of this project let a bad TimesFM forecast slip through.
279
- - Historical candle data is now sorted and de-duplicated on every fetch
280
- path (not just the multi-request chunked 1m path) before it reaches the
281
- backtester, indicators, or cache — including on cache-file reads — since
282
- the walk-forward backtest loop assumes strictly oldest→newest input and
283
- has no check of its own; a single out-of-order or duplicated timestamp
284
- slipping through would silently misalign predictions against the wrong
285
- candle.
286
- - MAE/RMSE round to 6 **significant figures**, not 6 decimal places — this
287
- project covers everything from forex pairs (~1.10) to sub-cent tokens
288
- (~0.000018), and a fixed decimal count either hides a real, non-zero error
289
- as a misleading `0.0` on a low-price asset or prints false precision
290
- (`125.345678`) on a high-price one like BTC.
291
- - **10-feature covariate input** — same "what's actually been checked"
292
- disclosure as the checkpoint strings above, in the same spirit: the
293
- leak-free property (`features/feature_pipeline.py`) and the walk-forward
294
- window alignment (`backtest/backtester.py`) were executed and verified
295
- end-to-end against synthetic OHLCV data — not just read through. The
296
- three ARIMA-family integrations run the same real `statsmodels` API this
297
- project already depended on. The two foundation-model integrations
298
- (Moirai's `past_feat_dynamic_real`, TimesFM's `forecast_with_covariates`)
299
- were checked against `uni2ts`'s own GitHub discussion of that forward-pass
300
- argument and the official `google-research/timesfm` repo's current docs
301
- and a recent real financial-forecasting example using the same API — but
302
- neither package was installed in the environment this was built in (no
303
- network access there), so unlike the ARIMA-family path, these two were
304
- never actually *run*. Smoke-test `features=` on Moirai/TimesFM in your
305
- own environment before relying on their featured numbers, the same way
306
- you'd confirm the checkpoint strings resolve.
307
- - **Update, found via a real deployed run** (BTC-USD, 1m, window=60, 10-
308
- feature mode): the first ~19 predictions of every features-on backtest
309
- failed with `feature window contains NaN in ['BB_Upper','BB_Lower',
310
- 'SMA_20']`. Not a leak — SMA_20/Bollinger genuinely need 20 real candles
311
- before them, and row 0 of whatever got fetched never has any, no matter
312
- how large the window or lookback is. Fixed in
313
- `data/historical.py`'s `get_historical_with_features()`: it now fetches
314
- a bit of real history *before* the requested period specifically for
315
- indicators to warm up on, then trims back down to exactly what was
316
- asked for — so the returned data's indicators are never NaN purely for
317
- lack of warm-up. `app.py` (all three relevant tabs) and `live_runner.py`
318
- now go through this instead of a plain fetch. Also fixed alongside it:
319
- Moirai/TimesFM's own `features=` validation used to check the *entire*
320
- history handed to them for NaN, even though both only ever use its tail
321
- — a NaN sitting only in the part they'd truncate away could reject an
322
- otherwise-fine window. `validate_features()` gained a `check_nan=`
323
- split so this is now checked only on the truncated slice actually used.
324
- Both fixes are covered by `tests/test_feature_pipeline.py` (including a
325
- direct reproduction of the exact reported BTC-USD/1m/window=60 case) and
326
- `tests/test_backtester_alignment.py`.
327
- - **Multi-step forecasts (horizon > 1) with features on** use each
328
- covariate's *last known real value, held constant* for every step beyond
329
- the first — real future OHLCV/RSI/MACD/etc. values don't exist yet to use
330
- honestly, and this project won't fabricate or peek at them (see the
331
- "10-feature covariate input" section above). This is a standard, clearly-
332
- labeled persistence assumption, not leakage — but it does mean a
333
- horizon=5 forecast's features are only ever as fresh as candle 1's, and
334
- that's a real approximation worth keeping in mind when reading a longer-
335
- horizon featured backtest, not something to expect to just work like a
336
- true multi-step covariate forecast would.
337
-
338
- ## Disclaimer
339
-
340
- This project is for education and research. It does not constitute
341
- financial advice, and past backtested accuracy is not a promise of future
342
- performance. Forex and crypto markets are volatile and you can lose money
343
- trading them — always do your own research and consider consulting a
344
- licensed financial advisor before trading real capital.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app.py DELETED
@@ -1,571 +0,0 @@
1
- """
2
- Forex & Crypto Prediction Dashboard — Gradio app, entry point for the
3
- Hugging Face Space (see README.md for the Space YAML header / deployment).
4
-
5
- Tabs:
6
- 1. Chart & Indicators - candlestick chart + the top 5 technical indicators
7
- 2. Prediction - ARIMA / Auto-ARIMA / ARIMA-GARCH / Moirai / TimesFM
8
- forecasts, shown together on one chart for
9
- visual comparison
10
- 3. Backtest - walk-forward backtest; each model gets its own
11
- SEPARATE accuracy card + per-candle verify log
12
- (numbers are never averaged across models)
13
- 4. Live - poll the newest candle, predict the next one, and
14
- verify the previous prediction once it lands
15
-
16
- Educational project — nothing here is financial advice.
17
- """
18
- import numpy as np
19
- import pandas as pd
20
- import plotly.graph_objects as go
21
- import gradio as gr
22
-
23
- from config import ALL_DEFAULT_SYMBOLS, CUSTOM_TIMEFRAMES, MODEL_NAMES, YF_NATIVE_INTERVALS
24
- from data.historical import get_historical_cached, get_historical_with_features, DataFetchError
25
- from indicators.indicators import add_all_indicators, latest_signals
26
- from models.registry import get_model, fresh_model
27
- from backtest.backtester import (
28
- run_all_models_backtest, run_comparison_backtest, resolve_backtest_step,
29
- BASELINE_LABEL, FEATURED_LABEL,
30
- )
31
- from utils.helpers import infer_step, future_index
32
-
33
- TIMEFRAME_CHOICES = list(CUSTOM_TIMEFRAMES.keys())
34
- DISCLAIMER = (
35
- "**Educational / research project — not financial advice.** "
36
- "Forex and crypto trading carries substantial risk of loss."
37
- )
38
- FEATURES_HELP = (
39
- "**10-feature input** feeds each model Open/High/Low/Volume plus RSI, MACD, "
40
- "Bollinger Bands, SMA/EMA and Stochastic — computed leak-free (only data "
41
- "available at each prediction's own point in time) — in addition to the "
42
- "Close-price series every model already uses. Off by default; this changes "
43
- "nothing about the original Close-only behavior unless you turn it on. "
44
- "See features/feature_pipeline.py for exactly what's computed and how "
45
- "each model consumes it."
46
- )
47
-
48
-
49
- # ---------------------------------------------------------------------------
50
- # Tab 1: Chart & Indicators
51
- # ---------------------------------------------------------------------------
52
- def make_candlestick_figure(df: pd.DataFrame, symbol: str, timeframe: str) -> go.Figure:
53
- fig = go.Figure(data=[go.Candlestick(
54
- x=df.index, open=df["Open"], high=df["High"], low=df["Low"], close=df["Close"],
55
- name=symbol,
56
- )])
57
- if "SMA_20" in df:
58
- fig.add_trace(go.Scatter(x=df.index, y=df["SMA_20"], name="SMA 20", line=dict(width=1)))
59
- if "EMA_20" in df:
60
- fig.add_trace(go.Scatter(x=df.index, y=df["EMA_20"], name="EMA 20", line=dict(width=1)))
61
- if "BB_Upper" in df:
62
- fig.add_trace(go.Scatter(x=df.index, y=df["BB_Upper"], name="BB Upper",
63
- line=dict(width=1, dash="dot")))
64
- fig.add_trace(go.Scatter(x=df.index, y=df["BB_Lower"], name="BB Lower",
65
- line=dict(width=1, dash="dot")))
66
- fig.update_layout(title=f"{symbol} — {timeframe}", xaxis_rangeslider_visible=False,
67
- height=480, margin=dict(l=10, r=10, t=40, b=10))
68
- return fig
69
-
70
-
71
- def fetch_and_chart(symbol, timeframe, lookback_days):
72
- try:
73
- df = get_historical_cached(symbol, timeframe, lookback_days=int(lookback_days), refresh=True)
74
- df_ind = add_all_indicators(df)
75
- fig = make_candlestick_figure(df_ind, symbol, timeframe)
76
- signals = latest_signals(df_ind)
77
- signal_rows = [[name, str(v["value"]), v["signal"]] for name, v in signals.items()]
78
- status = f"Loaded {len(df)} candles for {symbol} ({timeframe})."
79
- return fig, signal_rows, status
80
- except DataFetchError as e:
81
- return None, [], f"Data error: {e}"
82
- except Exception as e:
83
- return None, [], f"Unexpected error: {e}"
84
-
85
-
86
- # ---------------------------------------------------------------------------
87
- # Tab 2: Prediction
88
- # ---------------------------------------------------------------------------
89
- def run_prediction(symbol, timeframe, selected_models, horizon, lookback_days, use_features):
90
- if not selected_models:
91
- return None, "Pick at least one model."
92
- try:
93
- # get_historical_with_features fetches a bit of extra real history
94
- # in front and trims it back off after computing indicators on it,
95
- # so `df`/`features_df` cover exactly `lookback_days` with no NaN
96
- # from insufficient warm-up (see data/historical.py's docstring —
97
- # this replaced a plain get_historical_cached() + compute_feature_
98
- # frame() pair that could raise on a short lookback).
99
- df, features_df = get_historical_with_features(
100
- symbol, timeframe, lookback_days=int(lookback_days), use_features=use_features, refresh=True,
101
- )
102
- except Exception as e:
103
- return None, f"Data error: {e}"
104
-
105
- close = df["Close"]
106
- horizon = int(horizon)
107
- step = infer_step(df.index)
108
-
109
- fig = go.Figure()
110
- tail = close.iloc[-150:]
111
- fig.add_trace(go.Scatter(x=tail.index, y=tail.values, name="Actual Close",
112
- line=dict(color="#888888")))
113
-
114
- mode_label = f" [{FEATURED_LABEL}]" if use_features else ""
115
- summary_lines = []
116
- for name in selected_models:
117
- try:
118
- model = get_model(name, symbol, timeframe, use_features=use_features)
119
- forecast = model.predict(close, horizon=horizon, features=features_df)
120
- fc_index = future_index(df.index[-1], step, len(forecast))
121
- fig.add_trace(go.Scatter(x=fc_index, y=forecast, name=f"{name}{mode_label} forecast",
122
- mode="lines+markers"))
123
- direction = "UP" if forecast[-1] > close.iloc[-1] else "DOWN"
124
- summary_lines.append(f"**{name}**{mode_label} — next close (approx.) `{forecast[-1]:.5f}` ({direction})")
125
- except Exception as e:
126
- summary_lines.append(f"**{name}**{mode_label} — failed: {e}")
127
-
128
- fig.update_layout(title=f"{symbol} — {timeframe} forecast ({horizon} candle(s) ahead)",
129
- height=450, margin=dict(l=10, r=10, t=40, b=10))
130
- return fig, "\n\n".join(summary_lines) + "\n\n" + DISCLAIMER
131
-
132
-
133
- # ---------------------------------------------------------------------------
134
- # Tab 3: Backtest
135
- # ---------------------------------------------------------------------------
136
- FEATURE_MODE_BASELINE = "Close-only (baseline)"
137
- FEATURE_MODE_FEATURED = "+10-feature input"
138
- FEATURE_MODE_COMPARE = "Compare both"
139
-
140
-
141
- def _summary_row(summary: dict, mode_label: str) -> list:
142
- return [
143
- mode_label, summary["model"], summary["total_predictions"], summary["correct"],
144
- summary["incorrect"], f"{summary['accuracy_pct']}%",
145
- summary["mae"], summary["rmse"], summary["failed_predictions"],
146
- ]
147
-
148
-
149
- def run_backtest_ui(symbol, timeframe, selected_models, window, horizon, step_size,
150
- lookback_days, feature_mode):
151
- empty = pd.DataFrame()
152
- if not selected_models:
153
- return [], empty, empty, empty, empty, empty, "Pick at least one model."
154
-
155
- needs_features = feature_mode != FEATURE_MODE_BASELINE
156
- try:
157
- # get_historical_with_features fetches extra real history in front
158
- # (when needs_features) and trims it back off after computing
159
- # indicators on it, so `df`/`features_df` cover exactly
160
- # `lookback_days` with no NaN from insufficient warm-up (see
161
- # data/historical.py's docstring — this replaced a plain
162
- # get_historical_cached() whose first ~20 rows would otherwise be
163
- # unusable for any model needing the front of the tested period).
164
- df, features_df = get_historical_with_features(
165
- symbol, timeframe, lookback_days=int(lookback_days),
166
- use_features=needs_features, refresh=True,
167
- )
168
- except Exception as e:
169
- return [], empty, empty, empty, empty, empty, f"Data error: {e}"
170
-
171
- window, horizon, step_size = int(window), int(horizon), int(step_size)
172
- if len(df) < window + horizon + 5:
173
- return [], empty, empty, empty, empty, empty, (
174
- f"Only {len(df)} candles available — raise 'Backtest lookback (days)' "
175
- f"or lower the window size to backtest this symbol/timeframe."
176
- )
177
-
178
- # BUG FIX: a small step size on a long lookback for a small timeframe
179
- # can otherwise mean tens of thousands of walk-forward evaluations for
180
- # a single model (e.g. 30 days of 1-minute data at step=1 is ~40,000
181
- # `model.predict()` calls) -- measured at up to ~29 minutes for a
182
- # single ARIMA-GARCH run on this project's own hardware, worse again
183
- # with several models selected or Compare mode's 2x per model. Computed
184
- # here (not just left to backtest_model's own internal safety net) so
185
- # the effective step actually used can be reported below instead of
186
- # silently differing from what was asked for -- see
187
- # resolve_backtest_step's docstring for the full reasoning.
188
- effective_step = resolve_backtest_step(len(df), window, horizon, step_size)
189
- step_note = (f" Step size increased from {step_size} to {effective_step} to keep "
190
- f"this backtest responsive over {len(df)} candles."
191
- if effective_step != step_size else "")
192
- step_size = effective_step
193
-
194
- # Same asset/symbol data, same timeframe, same horizon, same step, same
195
- # testing period for every model AND every mode below — `df`, `window`,
196
- # `horizon`, `step_size` are fixed once here and reused unchanged, so
197
- # whichever models/modes run are always compared fairly against each
198
- # other, never against a different slice of history.
199
- summary_table = []
200
- detail = {"ARIMA": empty, "Auto-ARIMA": empty, "ARIMA-GARCH": empty,
201
- "Moirai": empty, "TimesFM": empty}
202
-
203
- if feature_mode == FEATURE_MODE_COMPARE:
204
- models = {name: (fresh_model(name), fresh_model(name)) for name in selected_models}
205
- try:
206
- per_model_results, per_model_summaries = run_comparison_backtest(
207
- df, models, features_df, window=window, horizon=horizon, step=step_size,
208
- )
209
- except Exception as e:
210
- return [], empty, empty, empty, empty, empty, f"Backtest error: {e}"
211
- for name in selected_models:
212
- for summary in per_model_summaries[name]:
213
- summary_table.append(_summary_row(summary, summary["mode"]))
214
- detail[name] = per_model_results[name]
215
- else:
216
- use_features = feature_mode == FEATURE_MODE_FEATURED
217
- mode_label = FEATURED_LABEL if use_features else BASELINE_LABEL
218
- models = {name: fresh_model(name) for name in selected_models}
219
- try:
220
- per_model_results, per_model_summaries = run_all_models_backtest(
221
- df, models, window=window, horizon=horizon, step=step_size,
222
- features_df=features_df if use_features else None,
223
- )
224
- except Exception as e:
225
- return [], empty, empty, empty, empty, empty, f"Backtest error: {e}"
226
- for name in selected_models:
227
- summary_table.append(_summary_row(per_model_summaries[name], mode_label))
228
- results = per_model_results[name].copy()
229
- results.insert(0, "mode", mode_label)
230
- detail[name] = results
231
-
232
- status = (f"Backtested {', '.join(selected_models)} on {len(df)} candles of "
233
- f"{symbol} ({timeframe}) — mode: {feature_mode}.{step_note}")
234
- return (summary_table, detail["ARIMA"], detail["Auto-ARIMA"], detail["ARIMA-GARCH"],
235
- detail["Moirai"], detail["TimesFM"], status)
236
-
237
-
238
- # ---------------------------------------------------------------------------
239
- # Tab 4: Live
240
- # ---------------------------------------------------------------------------
241
- LIVE_COLUMNS = ["predicted_at", "mode", "target_time", "current_close", "predicted_close",
242
- "predicted_direction", "actual_close", "actual_direction", "correct"]
243
-
244
-
245
- def _required_lookback_days(log_df, minimum_days: int = 2) -> int:
246
- """
247
- How many days back we need to fetch to have a chance of covering every
248
- still-pending prediction's target candle.
249
-
250
- BUG FIX: this used to be a hardcoded `lookback_days=2`. If the user
251
- doesn't click "Check Latest" for more than 2 days, the oldest pending
252
- row's `target_time` falls outside that 2-day window entirely --
253
- `row["target_time"] in df.index` is then False FOREVER for that row
254
- (the freshly fetched data never reaches back far enough to contain it),
255
- so it silently stays "pending" forever instead of ever being verified.
256
- That quietly shrinks the accuracy denominator with no error shown --
257
- exactly the kind of silent corruption this project's other "once the
258
- candle closes" fix (see the comment below) was written to prevent, just
259
- from the opposite direction (too-narrow fetch instead of too-early read).
260
-
261
- Fix: size the fetch to the age of the oldest pending row (plus a small
262
- safety margin for polling gaps), not a fixed constant. The caller still
263
- clamps this to what the timeframe can actually serve (Yahoo's own
264
- history-depth limit) -- see the clamp in _check_live_impl.
265
- """
266
- if log_df is None or len(log_df) == 0:
267
- return minimum_days
268
- pending = log_df[log_df["actual_close"].isna()]
269
- if len(pending) == 0:
270
- return minimum_days
271
- oldest_target = pd.to_datetime(pending["target_time"]).min()
272
- now = pd.Timestamp.now(tz=oldest_target.tzinfo)
273
- age_days = (now - oldest_target).total_seconds() / 86400
274
- return max(minimum_days, int(age_days) + 2) # +2 days safety margin
275
-
276
-
277
- def _check_live_impl(symbol, timeframe, model_name, log_df, use_features=False):
278
- if log_df is None or len(log_df) == 0:
279
- log_df = pd.DataFrame(columns=LIVE_COLUMNS)
280
- else:
281
- log_df = log_df.copy()
282
-
283
- # See _required_lookback_days' docstring: fetch far enough back to cover
284
- # the oldest still-pending prediction, not a fixed 2 days. Feature
285
- # warm-up (when use_features) is handled separately, INSIDE
286
- # get_historical_with_features -- it adds its own extra padding on top
287
- # of whatever `fetch_days` this section decides, then trims back off
288
- # (see data/historical.py's docstring) -- so `wanted_days` here is
289
- # purely "how far back do we need for verification", unrelated to
290
- # indicator warm-up. Clamped to what this timeframe's source interval
291
- # can actually serve (e.g. 1m tops out at 7 days on Yahoo) so we never
292
- # request more than yfinance can return -- get_historical_with_features
293
- # clamps internally too, but computing it here lets us tell the user
294
- # when a row is stuck.
295
- source_interval = CUSTOM_TIMEFRAMES.get(timeframe, {}).get("source", timeframe)
296
- max_servable_days = YF_NATIVE_INTERVALS.get(source_interval, {}).get("max_days")
297
- wanted_days = _required_lookback_days(log_df)
298
- fetch_days = min(wanted_days, max_servable_days) if max_servable_days else wanted_days
299
-
300
- df, features_df = get_historical_with_features(
301
- symbol, timeframe, lookback_days=fetch_days, use_features=use_features, refresh=True,
302
- )
303
- latest_time = df.index[-1]
304
- latest_close = float(df["Close"].iloc[-1])
305
-
306
- # 1) verify any pending prediction whose target candle has now CLOSED.
307
- #
308
- # BUG FIX: the old check was just `row["target_time"] in df.index`. Yahoo's
309
- # intraday feed includes the current, still-forming candle as the last row
310
- # (see data/live.py's own docstring: "the most recent (possibly
311
- # still-forming) candle"), and its Close is just the latest traded price so
312
- # far, not the candle's eventual final close. The instant the target candle
313
- # starts forming it satisfies `in df.index`, so the old code would verify
314
- # right then and there against a mid-candle snapshot -- then never revisit
315
- # that row again, since it's no longer NaN. Reproduced: a prediction that
316
- # was directionally CORRECT against the true close got permanently logged
317
- # as incorrect because the candle had dipped the other way in its first
318
- # minute. That silently corrupts the accuracy % this tab (and
319
- # live_runner.py) reports.
320
- #
321
- # Fix: also require the target candle to no longer be the latest row --
322
- # i.e. a strictly newer candle exists after it -- which is only true once
323
- # it has actually closed.
324
- pending_mask = log_df["actual_close"].isna()
325
- for idx, row in log_df[pending_mask].iterrows():
326
- if row["target_time"] in df.index and row["target_time"] < latest_time:
327
- actual_close = float(df.loc[row["target_time"], "Close"])
328
- actual_direction = "up" if actual_close > row["current_close"] else "down"
329
- log_df.loc[idx, "actual_close"] = actual_close
330
- log_df.loc[idx, "actual_direction"] = actual_direction
331
- log_df.loc[idx, "correct"] = bool(actual_direction == row["predicted_direction"])
332
-
333
- # 1b) a pending row whose target_time is older than what THIS timeframe
334
- # can ever serve (e.g. a 1m prediction nobody verified for 10+ days,
335
- # past Yahoo's ~7-day 1m history limit) can never be reached by any
336
- # future fetch either -- fetch_days is already clamped to the max.
337
- # Rather than leave it silently "pending" forever (which would read
338
- # as "still collecting data" indefinitely, hiding the fact that it
339
- # is actually unrecoverable), mark it explicitly unresolvable so
340
- # it's visibly dropped from the log instead of quietly stuck.
341
- #
342
- # BUG FIX: this used to run unconditionally on `max_servable_days
343
- # is not None` alone. On a BRAND NEW session -- log_df starts as
344
- # `pd.DataFrame(columns=LIVE_COLUMNS)`, genuinely zero rows -- every
345
- # column, including target_time, has no data to infer a dtype from
346
- # and defaults to plain `object`. `pd.to_datetime()` on that empty
347
- # column produces a tz-NAIVE result (nothing to infer a timezone
348
- # from either), and comparing that against `cutoff` (always
349
- # tz-AWARE, built from latest_time's own tzinfo) raises
350
- # `TypeError: Cannot compare tz-naive and tz-aware datetime-like
351
- # objects` -- for every intraday timeframe (anything with a finite
352
- # max_servable_days, i.e. everything except daily+), on literally
353
- # the FIRST EVER click of "Check Latest & Predict Next". check_live
354
- # catches the exception so the app itself doesn't crash, but it
355
- # also hands back the SAME still-empty log_df as the new state --
356
- # so the next click starts from empty too, hits the identical
357
- # crash, and the tab can never get past its very first row no
358
- # matter how many times it's clicked. There's nothing to check
359
- # anyway when log_df has zero rows, so skip the block entirely
360
- # then; `utc=True` further guards the non-empty case against the
361
- # same tz-naive/aware mismatch however target_time got stored.
362
- if max_servable_days is not None and len(log_df):
363
- cutoff = pd.Timestamp.now(tz=latest_time.tzinfo) - pd.Timedelta(days=max_servable_days)
364
- still_pending = log_df["actual_close"].isna()
365
- unreachable = still_pending & (pd.to_datetime(log_df["target_time"], utc=True) < cutoff)
366
- if unreachable.any():
367
- log_df.loc[unreachable, "actual_direction"] = "unresolvable (past history limit)"
368
- log_df.loc[unreachable, "correct"] = None
369
- # Leave actual_close as NaN so summarize-style dropna(subset=["correct"])
370
- # logic elsewhere continues to exclude these from the denominator --
371
- # they were never wrong, they're just permanently unverifiable now.
372
-
373
- # 2) make a fresh prediction for the next candle, unless one is already
374
- # pending for this exact latest candle
375
- #
376
- # Dedup by TIMESTAMP (predicted_at == latest_time), not by close price.
377
- # Price equality is the wrong proxy in both directions: an intraday
378
- # "latest" candle from yfinance is often still forming, so its close
379
- # can drift between polls even though it's still the SAME candle
380
- # (price-equality would wrongly log a second, duplicate pending row
381
- # for it before it even closes) -- and a genuinely NEW candle can by
382
- # coincidence close at the exact same price as the previous one,
383
- # especially in a quiet/ranging market (price-equality would then
384
- # wrongly skip predicting it entirely). Either way corrupts the
385
- # running-accuracy count this tab reports. Timestamp equality has
386
- # neither failure mode.
387
- have_pending_for_latest = False
388
- if len(log_df):
389
- last_row = log_df.iloc[-1]
390
- have_pending_for_latest = (
391
- pd.isna(last_row["actual_close"]) and last_row["predicted_at"] == latest_time
392
- )
393
-
394
- status_extra = ""
395
- if not have_pending_for_latest:
396
- try:
397
- model = get_model(model_name, symbol, timeframe, use_features=use_features)
398
- # features_df was computed above by get_historical_with_features,
399
- # the exact same feature-generation pipeline backtest uses --
400
- # positionally aligned with df["Close"] by construction, no
401
- # separate windowing step needed here the way backtest_model()
402
- # needs one for its rolling window.
403
- forecast = model.predict(df["Close"], horizon=1, features=features_df)
404
- predicted_close = float(forecast[-1])
405
- predicted_direction = "up" if predicted_close > latest_close else "down"
406
- step = infer_step(df.index)
407
- new_row = {
408
- "predicted_at": latest_time,
409
- "mode": FEATURED_LABEL if use_features else BASELINE_LABEL,
410
- "target_time": latest_time + step,
411
- "current_close": latest_close, "predicted_close": predicted_close,
412
- "predicted_direction": predicted_direction, "actual_close": np.nan,
413
- "actual_direction": None, "correct": None,
414
- }
415
- log_df = pd.concat([log_df, pd.DataFrame([new_row])], ignore_index=True)
416
- except Exception as e:
417
- status_extra = f" (prediction failed: {e})"
418
-
419
- verified = log_df.dropna(subset=["correct"])
420
- current_mode = FEATURED_LABEL if use_features else BASELINE_LABEL
421
- # The log can contain rows from BOTH modes if this checkbox was toggled
422
- # between clicks (it's the same running gr.State) -- accuracy is always
423
- # computed for the CURRENT mode's rows only, never blended across modes.
424
- verified_this_mode = verified[verified["mode"] == current_mode]
425
- if len(verified_this_mode):
426
- acc = round(100 * verified_this_mode["correct"].sum() / len(verified_this_mode), 2)
427
- acc_text = (f"Running accuracy for {model_name} [{current_mode}]: {acc}% "
428
- f"({int(verified_this_mode['correct'].sum())}/{len(verified_this_mode)})")
429
- other_count = len(verified) - len(verified_this_mode)
430
- if other_count:
431
- acc_text += f" — {other_count} verified prediction(s) from the other mode are in the table below but not counted here."
432
- else:
433
- acc_text = "Collecting data — check again after the next candle closes to see accuracy."
434
-
435
- status = f"Latest {symbol} candle: {latest_time} — Close {latest_close:.5f}. {acc_text}{status_extra}"
436
- return status, log_df.tail(20), log_df
437
-
438
-
439
- def check_live(symbol, timeframe, model_name, log_df, use_features=False):
440
- try:
441
- return _check_live_impl(symbol, timeframe, model_name, log_df, use_features=use_features)
442
- except Exception as e:
443
- safe_log = log_df if (log_df is not None and len(log_df)) else pd.DataFrame(columns=LIVE_COLUMNS)
444
- return f"Error: {e}", safe_log.tail(20), safe_log
445
-
446
-
447
- # ---------------------------------------------------------------------------
448
- # UI
449
- # ---------------------------------------------------------------------------
450
- with gr.Blocks(title="Forex & Crypto Prediction Dashboard") as demo:
451
- gr.Markdown(
452
- "# 📈 Forex & Crypto Prediction Dashboard\n"
453
- "ARIMA, Auto-ARIMA, ARIMA-GARCH, Moirai (Salesforce) & TimesFM (Google) — "
454
- "each model's accuracy is tracked and shown **separately**, never averaged "
455
- "together.\n\n" + DISCLAIMER
456
- )
457
-
458
- with gr.Tab("📊 Chart & Indicators"):
459
- with gr.Row():
460
- symbol_in1 = gr.Dropdown(choices=ALL_DEFAULT_SYMBOLS, value="EURUSD=X",
461
- allow_custom_value=True, label="Symbol")
462
- timeframe_in1 = gr.Dropdown(choices=TIMEFRAME_CHOICES, value="15m", label="Timeframe")
463
- lookback_in1 = gr.Slider(1, 60, value=5, step=1, label="Lookback (days)")
464
- fetch_btn = gr.Button("Fetch & Analyze", variant="primary")
465
- chart_out1 = gr.Plot()
466
- signals_out = gr.Dataframe(headers=["Indicator", "Value", "Signal"],
467
- label="Top 5 Indicators (latest reading)")
468
- status_out1 = gr.Markdown()
469
- fetch_btn.click(fetch_and_chart, [symbol_in1, timeframe_in1, lookback_in1],
470
- [chart_out1, signals_out, status_out1])
471
-
472
- with gr.Tab("🔮 Prediction"):
473
- with gr.Row():
474
- symbol_in2 = gr.Dropdown(choices=ALL_DEFAULT_SYMBOLS, value="BTC-USD",
475
- allow_custom_value=True, label="Symbol")
476
- timeframe_in2 = gr.Dropdown(choices=TIMEFRAME_CHOICES, value="15m", label="Timeframe")
477
- with gr.Row():
478
- models_in2 = gr.CheckboxGroup(choices=MODEL_NAMES, value=["ARIMA"], label="Models")
479
- horizon_in2 = gr.Slider(1, 10, value=1, step=1, label="Candles ahead")
480
- lookback_in2 = gr.Slider(1, 60, value=5, step=1, label="Lookback (days)")
481
- use_features_in2 = gr.Checkbox(value=False, label="Use 10-feature input (OHLCV + RSI/MACD/Bollinger/SMA-EMA/Stochastic)")
482
- gr.Markdown(
483
- "Moirai and TimesFM download pretrained weights the first time you use them "
484
- "(needs internet on the Space; a few hundred MB each, one-time download)."
485
- )
486
- predict_btn = gr.Button("Predict", variant="primary")
487
- chart_out2 = gr.Plot()
488
- summary_out2 = gr.Markdown()
489
- predict_btn.click(run_prediction,
490
- [symbol_in2, timeframe_in2, models_in2, horizon_in2, lookback_in2, use_features_in2],
491
- [chart_out2, summary_out2])
492
-
493
- with gr.Tab("🧪 Backtest"):
494
- gr.Markdown(
495
- "Each model's accuracy is computed and shown **separately** below — "
496
- "results are never averaged together. Raise **Step** to speed up "
497
- "Moirai/TimesFM (slower per-prediction than ARIMA on CPU-only hardware)."
498
- "\n\n" + FEATURES_HELP
499
- )
500
- with gr.Row():
501
- symbol_in3 = gr.Dropdown(choices=ALL_DEFAULT_SYMBOLS, value="EURUSD=X",
502
- allow_custom_value=True, label="Symbol")
503
- timeframe_in3 = gr.Dropdown(choices=TIMEFRAME_CHOICES, value="15m", label="Timeframe")
504
- with gr.Row():
505
- models_in3 = gr.CheckboxGroup(choices=MODEL_NAMES, value=["ARIMA"], label="Models to backtest")
506
- window_in3 = gr.Slider(20, 300, value=60, step=10, label="History window per prediction")
507
- horizon_in3 = gr.Slider(1, 5, value=1, step=1, label="Horizon (candles ahead)")
508
- with gr.Row():
509
- step_in3 = gr.Slider(1, 20, value=1, step=1, label="Step (skip candles between tests)")
510
- lookback_in3 = gr.Slider(1, 60, value=10, step=1, label="Backtest lookback (days)")
511
- feature_mode_in3 = gr.Radio(
512
- choices=[FEATURE_MODE_BASELINE, FEATURE_MODE_FEATURED, FEATURE_MODE_COMPARE],
513
- value=FEATURE_MODE_BASELINE, label="Feature mode",
514
- info="'Compare both' runs every selected model twice — Close-only and +10-feature — on the identical "
515
- "symbol/timeframe/window/horizon/step/period, so you can see whether the extra features actually helped.",
516
- )
517
- backtest_btn = gr.Button("Run Backtest", variant="primary")
518
- summary_out3 = gr.Dataframe(
519
- headers=["Mode", "Model", "Total", "Correct", "Incorrect", "Accuracy", "MAE", "RMSE", "Failed"],
520
- label="Per-model accuracy — kept separate, never mixed",
521
- )
522
- status_out3 = gr.Markdown()
523
- with gr.Tabs():
524
- with gr.Tab("ARIMA — verify log"):
525
- arima_detail = gr.Dataframe(label="Per-candle: predicted vs actual, correct/incorrect")
526
- with gr.Tab("Auto-ARIMA — verify log"):
527
- autoarima_detail = gr.Dataframe(label="Per-candle: predicted vs actual, correct/incorrect")
528
- with gr.Tab("ARIMA-GARCH — verify log"):
529
- arimagarch_detail = gr.Dataframe(label="Per-candle: predicted vs actual, correct/incorrect")
530
- with gr.Tab("Moirai — verify log"):
531
- moirai_detail = gr.Dataframe(label="Per-candle: predicted vs actual, correct/incorrect")
532
- with gr.Tab("TimesFM — verify log"):
533
- timesfm_detail = gr.Dataframe(label="Per-candle: predicted vs actual, correct/incorrect")
534
-
535
- backtest_btn.click(
536
- run_backtest_ui,
537
- [symbol_in3, timeframe_in3, models_in3, window_in3, horizon_in3, step_in3, lookback_in3, feature_mode_in3],
538
- [summary_out3, arima_detail, autoarima_detail, arimagarch_detail,
539
- moirai_detail, timesfm_detail, status_out3],
540
- )
541
-
542
- with gr.Tab("📡 Live"):
543
- gr.Markdown(
544
- "Polls Yahoo Finance for the newest candle each time you click the button, "
545
- "predicts the next one, and verifies the previous prediction once that candle "
546
- "has closed. For unattended/continuous logging instead of clicking manually, "
547
- "run `live_runner.py` in the background (see README)."
548
- )
549
- with gr.Row():
550
- symbol_in4 = gr.Dropdown(choices=ALL_DEFAULT_SYMBOLS, value="BTC-USD",
551
- allow_custom_value=True, label="Symbol")
552
- timeframe_in4 = gr.Dropdown(choices=TIMEFRAME_CHOICES, value="5m", label="Timeframe")
553
- model_in4 = gr.Dropdown(choices=MODEL_NAMES, value="ARIMA", label="Model")
554
- use_features_in4 = gr.Checkbox(
555
- value=False, label="Use 10-feature input",
556
- info="Uses the exact same feature-generation pipeline as the Backtest tab. "
557
- "Toggling this mid-session keeps both modes' predictions in the log below, "
558
- "but running accuracy is always computed for the current mode only.",
559
- )
560
- live_btn = gr.Button("🔄 Check Latest & Predict Next", variant="primary")
561
- live_log_state = gr.State(pd.DataFrame(columns=LIVE_COLUMNS))
562
- live_status = gr.Markdown()
563
- live_table = gr.Dataframe(label="Live verification log (this model, this session)")
564
- live_btn.click(check_live, [symbol_in4, timeframe_in4, model_in4, live_log_state, use_features_in4],
565
- [live_status, live_table, live_log_state])
566
-
567
- gr.Markdown("---\n" + DISCLAIMER)
568
-
569
-
570
- if __name__ == "__main__":
571
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
backtest/__init__.py DELETED
File without changes
backtest/backtester.py DELETED
@@ -1,289 +0,0 @@
1
- """
2
- Walk-forward backtesting + per-candle verification.
3
-
4
- Every model's results are kept in a SEPARATE dict/DataFrame and are never
5
- averaged or blended across models — each model's accuracy is reported on its
6
- own (har model ka accuracy alag hona chahiye, mix nahi).
7
-
8
- For each step of the walk-forward loop we log the predicted close, the real
9
- close once it's known, the predicted vs actual direction (up/down), and
10
- whether that candle's prediction was correct — this is the "verify how many
11
- candles were right / wrong" log.
12
-
13
- Optional 10-feature covariate input (see features/feature_pipeline.py): pass
14
- `features_df=` (to backtest_model directly, or run_all_models_backtest/
15
- run_comparison_backtest) already computed and aligned with `df["Close"]` --
16
- typically via data.historical.get_historical_with_features(...,
17
- use_features=True), which also pads/trims so it's warm-up-safe (NaN-free)
18
- for the exact period `df` covers -- see that function's docstring. The
19
- SAME (start, end) window sliced out of `close` for `history` at each step
20
- is also sliced out of `features_df` and handed to `model.predict(...,
21
- features=...)`, so features are exactly as leak-free as `history` already
22
- was: only rows up to the candle being predicted from are ever in either
23
- window. `features_df=None` (the default) is the original Close-only
24
- backtest, byte-for-byte unchanged.
25
- """
26
- import math
27
-
28
- import numpy as np
29
- import pandas as pd
30
-
31
- from features.feature_pipeline import slice_features
32
-
33
-
34
- def resolve_backtest_step(n_candles: int, window: int, horizon: int, step: int,
35
- max_steps: int = 500) -> int:
36
- """Return a step size that keeps the walk-forward loop's evaluation
37
- count at or under `max_steps`, raising `step` (never lowering it) only
38
- if the caller's own choice would exceed that.
39
-
40
- BUG FIX: backtest_model's walk-forward loop used to run with whatever
41
- `step` it was given, however many iterations that implied. That's fine
42
- for the sizes this project's own UI sliders were designed around, but
43
- a long lookback on a small timeframe combined with a small step can
44
- imply tens of thousands of iterations -- e.g. 30 days of 1-minute data
45
- (~43,000 candles) with step=1 is ~40,000 `model.predict()` calls.
46
- Measured on this project's own three classical models (after
47
- arima_model.py's max_history fix, so each individual fit is already as
48
- cheap as it's going to get): ARIMA ~9.7ms/step, Auto-ARIMA ~9.2ms/step,
49
- ARIMA-GARCH ~43ms/step. At 40,000 steps that's 6-29 minutes for a
50
- SINGLE model in a SINGLE mode -- doubled again in Compare mode, and
51
- summed again over however many models the Backtest tab's checkbox has
52
- selected. On a free-tier CPU Space that's not just a bad wait, it's
53
- long enough to hit a browser or reverse-proxy timeout before the
54
- request ever finishes. This function computes what `step` would need
55
- to be to keep the total evaluation count near `max_steps` instead, so
56
- backtest_model always finishes in bounded time regardless of how large
57
- a lookback/timeframe combination produced `df` -- it still walks
58
- forward across the SAME full period, just more sparsely when the
59
- caller's own step would have made that impractically fine-grained.
60
- Callers whose own (window, step, n_candles) already stays under
61
- max_steps are completely unaffected.
62
- """
63
- last_i = n_candles - horizon
64
- span = last_i - window
65
- if span <= 0 or not max_steps or step <= 0:
66
- return step
67
- natural_steps = span // step + 1
68
- if natural_steps <= max_steps:
69
- return step
70
- return max(step, math.ceil(span / max_steps))
71
-
72
-
73
- def _round_sig(x, sig: int = 6):
74
- """Round to `sig` significant figures instead of a fixed decimal count.
75
-
76
- This app accepts ANY custom yfinance ticker (README: "any custom
77
- yfinance-valid ticker you type in"), so MAE/RMSE have to stay meaningful
78
- across wildly different price scales in the same codebase: a forex pair
79
- (~1.10), BTC (~65,000), and a low-price altcoin (~0.000018) can all show
80
- up. A fixed `round(x, 6)`:
81
- - silently prints 0.0 for a real, non-zero error once the asset's
82
- price is below ~1e-6 (a low-price token with genuine, non-trivial
83
- prediction error would misleadingly look "perfect"), and
84
- - prints false precision for a high-price asset like BTC, e.g.
85
- `125.345678` for a forecast error that has nowhere near six decimal
86
- digits of real meaning at that price scale.
87
- Significant-figure rounding keeps both ends honest without needing to
88
- know the asset's scale ahead of time.
89
- """
90
- if x is None or not np.isfinite(x) or x == 0:
91
- return x
92
- from math import floor, log10
93
- decimals = sig - 1 - floor(log10(abs(x)))
94
- return round(x, decimals)
95
-
96
-
97
- def backtest_model(df: pd.DataFrame, model, window: int = 100, horizon: int = 1,
98
- step: int = 1, features_df: pd.DataFrame = None,
99
- max_steps: int = 500) -> pd.DataFrame:
100
- """
101
- Walk forward through `df` (needs a 'Close' column, sorted oldest -> newest).
102
- At each step, feed the last `window` closes into `model.predict()`,
103
- compare the forecast to the real next close, and log correct/incorrect.
104
-
105
- `step` > 1 skips candles between predictions — raise this to speed up the
106
- slower foundation models (Moirai/TimesFM) on CPU-only hardware. `step`
107
- is only ever raised automatically, never lowered, to keep the total
108
- number of walk-forward evaluations near `max_steps` -- see
109
- resolve_backtest_step's docstring for why that safety net exists.
110
- Pass `max_steps=None` to disable it and always honor `step` exactly.
111
-
112
- `features_df`, if given, must be positionally aligned with `close`
113
- (typically via data.historical.get_historical_with_features, which
114
- keeps it warm-up-safe -- see this module's docstring) — the identical
115
- `[i - window : i]` window is sliced from it and passed as
116
- `model.predict(..., features=...)`, so every model sees the same
117
- history cutoff for both the price series and its covariates.
118
- """
119
- close = df["Close"].reset_index(drop=True)
120
- timestamps = df.index
121
-
122
- step = resolve_backtest_step(len(close), window, horizon, step, max_steps=max_steps)
123
-
124
- last_i = len(close) - horizon
125
- rows = []
126
- for i in range(window, last_i + 1, step):
127
- history = close.iloc[i - window:i]
128
- feature_window = slice_features(features_df, i - window, i) if features_df is not None else None
129
- current_close = float(close.iloc[i - 1])
130
- target_idx = i + horizon - 1
131
- actual_next_close = float(close.iloc[target_idx])
132
-
133
- try:
134
- forecast = model.predict(history, horizon=horizon, features=feature_window)
135
- predicted_next_close = float(forecast[-1])
136
- if not np.isfinite(predicted_next_close):
137
- raise ValueError(f"non-finite forecast value: {predicted_next_close!r}")
138
- except Exception as e:
139
- rows.append({
140
- "datetime": timestamps[target_idx],
141
- "current_close": current_close,
142
- "predicted_close": np.nan,
143
- "actual_close": actual_next_close,
144
- "predicted_direction": None,
145
- "actual_direction": None,
146
- "correct": None,
147
- "error": str(e),
148
- })
149
- continue
150
-
151
- predicted_direction = "up" if predicted_next_close > current_close else "down"
152
- actual_direction = "up" if actual_next_close > current_close else "down"
153
- correct = predicted_direction == actual_direction
154
-
155
- rows.append({
156
- "datetime": timestamps[target_idx],
157
- "current_close": current_close,
158
- "predicted_close": predicted_next_close,
159
- "actual_close": actual_next_close,
160
- "predicted_direction": predicted_direction,
161
- "actual_direction": actual_direction,
162
- "correct": correct,
163
- "error": None,
164
- })
165
-
166
- return pd.DataFrame(rows)
167
-
168
-
169
- def summarize_backtest(results: pd.DataFrame, model_name: str) -> dict:
170
- """One model's accuracy summary. Always kept separate per model — never
171
- combined with another model's numbers."""
172
- if results.empty:
173
- return {
174
- "model": model_name, "total_predictions": 0, "correct": 0, "incorrect": 0,
175
- "accuracy_pct": 0.0, "mae": None, "rmse": None, "failed_predictions": 0,
176
- }
177
-
178
- valid = results.dropna(subset=["correct"])
179
- total = len(valid)
180
- correct = int(valid["correct"].sum()) if total else 0
181
- incorrect = total - correct
182
- accuracy = round(100 * correct / total, 2) if total else 0.0
183
-
184
- price_valid = results.dropna(subset=["predicted_close", "actual_close"])
185
- if len(price_valid):
186
- errors = price_valid["predicted_close"] - price_valid["actual_close"]
187
- mae = float(np.mean(np.abs(errors)))
188
- rmse = float(np.sqrt(np.mean(errors ** 2)))
189
- else:
190
- mae = rmse = None
191
-
192
- failed = int(results["error"].notna().sum())
193
-
194
- return {
195
- "model": model_name,
196
- "total_predictions": total,
197
- "correct": correct,
198
- "incorrect": incorrect,
199
- "accuracy_pct": accuracy,
200
- "mae": _round_sig(mae) if mae is not None else None,
201
- "rmse": _round_sig(rmse) if rmse is not None else None,
202
- "failed_predictions": failed,
203
- }
204
-
205
-
206
- def run_all_models_backtest(df: pd.DataFrame, models: dict, window: int = 100,
207
- horizon: int = 1, step: int = 1, features_df: pd.DataFrame = None,
208
- max_steps: int = 500):
209
- """
210
- Run a backtest for every model in `models` ({name: model_instance}).
211
- Returns (per_model_results, per_model_summary) — both dicts keyed by
212
- model name, so results are never merged/averaged across models.
213
-
214
- `features_df`, if given, must already be computed and positionally
215
- aligned with `df["Close"]` — typically via
216
- data.historical.get_historical_with_features(..., use_features=True),
217
- which also makes sure it's warmed-up/NaN-free for the exact period
218
- `df` covers. (This function deliberately does NOT compute features_df
219
- itself from `df` alone anymore — doing that after the fact would
220
- reintroduce the exact "first ~20 rows always NaN" problem
221
- get_historical_with_features exists to avoid; see its docstring.)
222
- `features_df=None` (default) is the original Close-only backtest,
223
- byte-for-byte unchanged.
224
-
225
- `max_steps` is forwarded to backtest_model's own safety net -- see
226
- resolve_backtest_step's docstring.
227
- """
228
- per_model_results = {}
229
- per_model_summary = {}
230
- for name, model in models.items():
231
- results = backtest_model(df, model, window=window, horizon=horizon, step=step,
232
- features_df=features_df, max_steps=max_steps)
233
- per_model_results[name] = results
234
- per_model_summary[name] = summarize_backtest(results, name)
235
- return per_model_results, per_model_summary
236
-
237
-
238
- BASELINE_LABEL = "Close-only"
239
- FEATURED_LABEL = "+10 Features"
240
-
241
-
242
- def run_comparison_backtest(df: pd.DataFrame, models: dict, features_df: pd.DataFrame,
243
- window: int = 100, horizon: int = 1, step: int = 1,
244
- max_steps: int = 500):
245
- """
246
- Run every model in `models` TWICE on the identical asset/timeframe/
247
- window/horizon/step/testing period — once Close-only (BASELINE_LABEL),
248
- once with the 10-feature covariate input (FEATURED_LABEL) — which is
249
- exactly the fair, apples-to-apples comparison needed to honestly answer
250
- "does giving these models the extra features change their accuracy?"
251
- without assuming the answer either way.
252
-
253
- `features_df` is required (unlike run_all_models_backtest's optional
254
- one) since a comparison is meaningless without it — must already be
255
- computed and positionally aligned with `df["Close"]`, same contract as
256
- run_all_models_backtest (see its docstring for why this function
257
- doesn't compute it internally either).
258
-
259
- `max_steps` is forwarded to backtest_model's own safety net -- see
260
- resolve_backtest_step's docstring. Each mode (baseline/featured) gets
261
- its own independent budget of up to `max_steps` evaluations, since
262
- they're timed and reported completely separately.
263
-
264
- Returns (per_model_results, per_model_summary), each keyed by model
265
- name as usual, but every results DataFrame gets a leading "mode" column
266
- (BASELINE_LABEL/FEATURED_LABEL) and every summary dict gets a "mode"
267
- key — so a model's two runs sit next to each other for comparison
268
- without ever being averaged or blended into one number.
269
- """
270
- per_model_results = {}
271
- per_model_summary = {}
272
- for name, model_pair in models.items():
273
- baseline_model, featured_model = model_pair
274
- results_list, summary_list = [], []
275
- for label, model, feats in (
276
- (BASELINE_LABEL, baseline_model, None),
277
- (FEATURED_LABEL, featured_model, features_df),
278
- ):
279
- results = backtest_model(df, model, window=window, horizon=horizon, step=step,
280
- features_df=feats, max_steps=max_steps)
281
- summary = summarize_backtest(results, name)
282
- summary["mode"] = label
283
- results = results.copy()
284
- results.insert(0, "mode", label)
285
- results_list.append(results)
286
- summary_list.append(summary)
287
- per_model_results[name] = pd.concat(results_list, ignore_index=True)
288
- per_model_summary[name] = summary_list
289
- return per_model_results, per_model_summary
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
config.py DELETED
@@ -1,116 +0,0 @@
1
- """
2
- Central configuration for the Forex & Crypto Prediction project.
3
- Edit this file to add symbols, change default model checkpoints, etc.
4
- Nothing here needs internet access to import.
5
- """
6
- import os
7
-
8
- BASE_DIR = os.path.dirname(os.path.abspath(__file__))
9
- DATA_CACHE_DIR = os.path.join(BASE_DIR, "data_cache")
10
- OUTPUT_DIR = os.path.join(BASE_DIR, "outputs")
11
-
12
- os.makedirs(DATA_CACHE_DIR, exist_ok=True)
13
- os.makedirs(OUTPUT_DIR, exist_ok=True)
14
-
15
- # ---------------------------------------------------------------------------
16
- # Symbols (yfinance ticker format)
17
- # ---------------------------------------------------------------------------
18
- # Forex pairs use the "=X" suffix, crypto pairs use "-USD".
19
- # NOTE on "all symbols": yfinance/Yahoo Finance covers a huge number of forex
20
- # and crypto tickers, so there is no fixed master list to hardcode. The app's
21
- # Symbol dropdown ships with the popular ones below AND accepts any custom
22
- # yfinance-valid ticker typed in directly (allow_custom_value=True) — so you
23
- # are not limited to this list, this is just a convenient starting set.
24
- DEFAULT_FOREX_SYMBOLS = [
25
- "EURUSD=X", "GBPUSD=X", "USDJPY=X", "USDCHF=X", "AUDUSD=X",
26
- "USDCAD=X", "NZDUSD=X", "EURJPY=X", "EURGBP=X", "GBPJPY=X",
27
- ]
28
-
29
- DEFAULT_CRYPTO_SYMBOLS = [
30
- "BTC-USD", "ETH-USD", "BNB-USD", "XRP-USD", "SOL-USD",
31
- "ADA-USD", "DOGE-USD", "DOT-USD", "LTC-USD", "MATIC-USD",
32
- ]
33
-
34
- ALL_DEFAULT_SYMBOLS = DEFAULT_FOREX_SYMBOLS + DEFAULT_CRYPTO_SYMBOLS
35
-
36
- # ---------------------------------------------------------------------------
37
- # Timeframes
38
- # ---------------------------------------------------------------------------
39
- # Yahoo Finance only serves a fixed set of native intraday intervals, each
40
- # with its own history-depth limit (enforced by Yahoo, not by us):
41
- # 1m -> last ~30 days total (but a SINGLE request maxes out at
42
- # 7 days -- see data/historical.py's chunked-fetch path)
43
- # 2m/5m/15m/30m/90m -> last 60 days
44
- # 60m (1h) -> last 730 days
45
- # 1d/1wk/1mo -> full history
46
- #
47
- # BUG FIX: this used to be 7 for "1m", which is Yahoo's PER-REQUEST cap, not
48
- # its total history depth. data/historical.py's _native_fetch() already
49
- # clamps `days = min(days, max_days)` *before* deciding whether to chunk
50
- # ("if interval == '1m' and days > 7"), so a max_days of 7 made that
51
- # `days > 7` check permanently unreachable -- `days` could never be more
52
- # than 7 by the time it was tested, so the whole chunked-fetch code path
53
- # (stitching multiple 7-day requests together, already correctly
54
- # implemented below) silently never ran, and any request for more than 7
55
- # days of 1-minute data was quietly truncated to 7 no matter what was
56
- # asked for. 30 is Yahoo's real total-history depth for 1m data (confirmed
57
- # both by this project's own data/historical.py docstring and independently
58
- # against yfinance's documented behavior); the per-request 7-day cap is
59
- # still enforced separately, inside the chunking loop itself
60
- # (`window_days = min(7, remaining)`), so this change only affects how far
61
- # back a *multi-request* 1m fetch is allowed to reach in total.
62
- YF_NATIVE_INTERVALS = {
63
- "1m": {"max_days": 30},
64
- "2m": {"max_days": 60},
65
- "5m": {"max_days": 60},
66
- "15m": {"max_days": 60},
67
- "30m": {"max_days": 60},
68
- "60m": {"max_days": 730},
69
- "90m": {"max_days": 60},
70
- "1d": {"max_days": None},
71
- "1wk": {"max_days": None},
72
- "1mo": {"max_days": None},
73
- }
74
-
75
- # Timeframes offered in the app UI. Ones that aren't native Yahoo intervals
76
- # (e.g. "10m") are built by resampling a smaller native interval, so you can
77
- # ask for basically any custom candle size, not just what Yahoo natively has.
78
- #
79
- # `rule: None` means this timeframe IS a native Yahoo interval already (just
80
- # under a friendlier display name) -- return it as-is, no resampling.
81
- # `rule: "<pandas rule>"` means genuine upsampling from a smaller native
82
- # interval is needed.
83
- #
84
- # "1h" -> source "60m" is the one case where the display name and yfinance's
85
- # native interval name differ ("1h" vs "60m") even though they're the same
86
- # candle size. That mismatch used to make get_historical()'s
87
- # `source_interval != timeframe` check misfire and re-resample already-hourly
88
- # candles through rule="1h" -- a harmless-looking no-op when Yahoo's 60m
89
- # candles happen to fall on clean UTC hour boundaries, but not otherwise: it
90
- # silently re-labels each candle's timestamp to the bucket start (observed
91
- # shifting real candle times by up to 59 minutes in testing) and, for
92
- # irregularly-spaced candles (weekend gaps, DST), can merge two distinct
93
- # candles into one bucket or drop one to a dropna(how="any"). Native
94
- # passthrough (`rule: None`) sidesteps all of that.
95
- CUSTOM_TIMEFRAMES = {
96
- "1m": {"source": "1m", "rule": None},
97
- "5m": {"source": "5m", "rule": None},
98
- "10m": {"source": "5m", "rule": "10min"},
99
- "15m": {"source": "15m", "rule": None},
100
- "30m": {"source": "30m", "rule": None},
101
- "1h": {"source": "60m", "rule": None},
102
- "4h": {"source": "60m", "rule": "4h"},
103
- "1d": {"source": "1d", "rule": None},
104
- }
105
-
106
- # ---------------------------------------------------------------------------
107
- # Models
108
- # ---------------------------------------------------------------------------
109
- MODEL_NAMES = ["ARIMA", "Auto-ARIMA", "ARIMA-GARCH", "Moirai", "TimesFM"]
110
-
111
- # Small/CPU-friendly checkpoints, picked to fit comfortably in 2 vCPU / 16GB
112
- # RAM. Change these if you deploy on bigger (e.g. GPU) hardware.
113
- MOIRAI_CHECKPOINT = "Salesforce/moirai-1.1-R-small"
114
- TIMESFM_CHECKPOINT = "google/timesfm-2.5-200m-pytorch"
115
-
116
- RANDOM_SEED = 42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
data/__init__.py DELETED
File without changes
data/historical.py DELETED
@@ -1,293 +0,0 @@
1
- """
2
- Historical OHLCV data via yfinance — free, no API key needed.
3
-
4
- Two things this module does beyond a plain yf.download():
5
- 1. Resampling: Yahoo only serves a fixed set of native intervals, so any
6
- timeframe in config.CUSTOM_TIMEFRAMES that isn't native (e.g. "10m") is
7
- built by aggregating a smaller native interval with pandas .resample().
8
- 2. Chunked 1m fetching: Yahoo limits a single request for 1m candles to 7
9
- days, but keeps roughly 30 days of 1m history available in total. If you
10
- ask for more than 7 days of 1m data, this module makes several 7-day
11
- requests and stitches them together instead of silently truncating.
12
- """
13
- import os
14
- import time
15
- from datetime import datetime, timedelta, timezone
16
-
17
- import pandas as pd
18
- import yfinance as yf
19
-
20
- from config import YF_NATIVE_INTERVALS, CUSTOM_TIMEFRAMES, DATA_CACHE_DIR
21
- from features.feature_pipeline import compute_feature_frame_for_period
22
- from utils.helpers import feature_warmup_days
23
-
24
-
25
- class DataFetchError(Exception):
26
- """Raised for anything that stops us returning usable candles."""
27
-
28
-
29
- def _download_window(symbol: str, interval: str, start: datetime, end: datetime) -> pd.DataFrame:
30
- df = yf.Ticker(symbol).history(start=start, end=end, interval=interval)
31
- if df is None or df.empty:
32
- return pd.DataFrame()
33
- df = df.rename(columns=str.title)
34
- df.index.name = "Datetime"
35
- keep = [c for c in ["Open", "High", "Low", "Close", "Volume"] if c in df.columns]
36
- return df[keep]
37
-
38
-
39
- def _finalize_ohlcv(df: pd.DataFrame) -> pd.DataFrame:
40
- """
41
- Guarantee strictly-ascending, duplicate-free timestamps on every candle
42
- frame this module hands to the rest of the app.
43
-
44
- BUG FIX: only the chunked 1m path (multiple stitched requests, where
45
- overlapping windows obviously CAN produce duplicate/out-of-order rows)
46
- used to call `.sort_index()` + de-dup. The single-window and
47
- period="max" paths returned whatever yfinance gave back as-is, on the
48
- assumption it's already sorted. That's true in the overwhelming
49
- majority of cases, but the backtester's walk-forward loop
50
- (backtest/backtester.py) has no independent check of its own -- it
51
- slices by positional index and assumes oldest -> newest, so even a rare
52
- single out-of-order or duplicated timestamp from the upstream feed
53
- would silently feed the model a shuffled window and misalign
54
- `actual_next_close` against the wrong candle, corrupting that backtest
55
- run's accuracy/MAE/RMSE with no error surfaced anywhere. Applying this
56
- to every exit path (not just the one that obviously needs it) means a
57
- future code path added here doesn't quietly reopen the same gap.
58
- """
59
- if df is None or df.empty:
60
- return df
61
- df = df.sort_index()
62
- return df[~df.index.duplicated(keep="last")]
63
-
64
-
65
- def _native_fetch(symbol: str, interval: str, days: int) -> pd.DataFrame:
66
- """Fetch raw OHLCV candles at a yfinance-native interval."""
67
- if interval not in YF_NATIVE_INTERVALS:
68
- raise DataFetchError(f"'{interval}' is not a native yfinance interval.")
69
-
70
- max_days = YF_NATIVE_INTERVALS[interval]["max_days"]
71
- end = datetime.now(timezone.utc)
72
-
73
- if max_days is None:
74
- # 1d / 1wk / 1mo — Yahoo has no rolling-window limit, so there's no
75
- # single-request size cap to worry about here the way the branches
76
- # below have. That does NOT mean `days` stops mattering, though.
77
- #
78
- # BUG FIX: this used to fetch period="max" and return it exactly as
79
- # given, silently ignoring `days` altogether -- every caller that
80
- # doesn't separately re-trim the result (get_historical_cached's
81
- # Close-only path used directly by the Chart tab, the Prediction
82
- # tab with 10-feature input OFF, the Backtest tab's default
83
- # "Close-only" feature mode, and the Live tab with 10-feature input
84
- # OFF -- i.e. every default/common configuration) got the ENTIRE
85
- # available price history back no matter what "Lookback (days)" was
86
- # set to. Concretely reproduced: a Backtest run asking for 2 days
87
- # of daily EURUSD=X candles actually walked forward across this
88
- # symbol's entire multi-year history instead, silently testing a
89
- # completely different period than the one requested, and taking
90
- # far longer to do it (only get_historical_with_features's
91
- # use_features=True path, via compute_feature_frame_for_period's
92
- # own keep_days trim, happened to still come out right). Fixed by
93
- # trimming to the requested `days` here too, the same
94
- # last-timestamp-relative way the branches below already do it, so
95
- # `days` means the same thing regardless of which branch of this
96
- # function serves it.
97
- df = yf.Ticker(symbol).history(period="max", interval=interval)
98
- if df is None or df.empty:
99
- raise DataFetchError(
100
- f"No data returned for '{symbol}' at interval '{interval}'. "
101
- f"Check it's a valid yfinance ticker (e.g. EURUSD=X, BTC-USD)."
102
- )
103
- df = df.rename(columns=str.title)
104
- df.index.name = "Datetime"
105
- df = _finalize_ohlcv(df[["Open", "High", "Low", "Close", "Volume"]])
106
- if days:
107
- cutoff = df.index[-1] - timedelta(days=days)
108
- df = df[df.index >= cutoff]
109
- return df
110
-
111
- days = min(days, max_days)
112
-
113
- if interval == "1m" and days > 7:
114
- # Yahoo caps a single 1m request at 7 days — stitch several requests
115
- # together to cover the full requested window (up to its ~30 day limit).
116
- frames = []
117
- window_end = end
118
- remaining = days
119
- while remaining > 0:
120
- window_days = min(7, remaining)
121
- window_start = window_end - timedelta(days=window_days)
122
- frame = _download_window(symbol, interval, window_start, window_end)
123
- if not frame.empty:
124
- frames.append(frame)
125
- window_end = window_start
126
- remaining -= window_days
127
- time.sleep(0.2) # be polite to Yahoo's endpoint between chunks
128
- if not frames:
129
- raise DataFetchError(
130
- f"No 1m data returned for '{symbol}'. Yahoo only keeps ~30 days "
131
- f"of 1-minute history — try a shorter lookback or a bigger timeframe."
132
- )
133
- return _finalize_ohlcv(pd.concat(frames))
134
-
135
- start = end - timedelta(days=days)
136
- df = _download_window(symbol, interval, start, end)
137
- if df.empty:
138
- raise DataFetchError(
139
- f"No data returned for '{symbol}' at interval '{interval}'. "
140
- f"Check it's a valid yfinance ticker (e.g. EURUSD=X, BTC-USD) and "
141
- f"that the market has traded recently."
142
- )
143
- return _finalize_ohlcv(df)
144
-
145
-
146
- def resample_ohlc(df: pd.DataFrame, rule: str) -> pd.DataFrame:
147
- """Aggregate candles up to a coarser timeframe, e.g. 5m candles -> 10m candles."""
148
- agg = {"Open": "first", "High": "max", "Low": "min", "Close": "last", "Volume": "sum"}
149
- agg = {k: v for k, v in agg.items() if k in df.columns}
150
- out = df.resample(rule).agg(agg).dropna(how="any")
151
- return out
152
-
153
-
154
- def get_historical(symbol: str, timeframe: str, lookback_days: int = None) -> pd.DataFrame:
155
- """
156
- Fetch historical candles for `symbol` at `timeframe`.
157
-
158
- `timeframe` can be a native yfinance interval (see config.YF_NATIVE_INTERVALS)
159
- OR one of config.CUSTOM_TIMEFRAMES (e.g. "10m", "4h"), which is built by
160
- resampling a smaller native interval.
161
- """
162
- if not symbol or not symbol.strip():
163
- raise DataFetchError("No symbol given.")
164
- symbol = symbol.strip()
165
-
166
- if timeframe in CUSTOM_TIMEFRAMES:
167
- spec = CUSTOM_TIMEFRAMES[timeframe]
168
- source_interval, rule = spec["source"], spec["rule"]
169
- elif timeframe in YF_NATIVE_INTERVALS:
170
- source_interval, rule = timeframe, None
171
- else:
172
- raise DataFetchError(
173
- f"Unknown timeframe '{timeframe}'. Valid options: "
174
- f"{', '.join(CUSTOM_TIMEFRAMES.keys())}"
175
- )
176
-
177
- max_days = YF_NATIVE_INTERVALS[source_interval]["max_days"]
178
- days = lookback_days or (max_days if max_days else 730)
179
-
180
- df = _native_fetch(symbol, source_interval, days)
181
-
182
- if rule:
183
- df = resample_ohlc(df, rule)
184
- if df.empty:
185
- raise DataFetchError(
186
- f"Not enough '{source_interval}' candles to build '{timeframe}' bars. "
187
- f"Try a longer lookback."
188
- )
189
-
190
- return df
191
-
192
-
193
- def _cache_path(symbol: str, timeframe: str) -> str:
194
- safe = symbol.replace("=", "_").replace("^", "_").replace("/", "_")
195
- return os.path.join(DATA_CACHE_DIR, f"{safe}_{timeframe}.csv")
196
-
197
-
198
- def get_historical_cached(symbol: str, timeframe: str, lookback_days: int = None,
199
- refresh: bool = True) -> pd.DataFrame:
200
- """
201
- Same as get_historical, but reads/writes a CSV cache under data_cache/ so
202
- repeated UI actions don't all need a fresh network round trip.
203
- `refresh=True` (default) always fetches live and overwrites the cache —
204
- pass refresh=False to reuse whatever is cached, if anything.
205
- """
206
- path = _cache_path(symbol, timeframe)
207
-
208
- if not refresh and os.path.exists(path):
209
- try:
210
- cached = pd.read_csv(path, index_col="Datetime", parse_dates=True)
211
- if not cached.empty:
212
- # Defense-in-depth, not just belt-and-suspenders: this cache
213
- # file is normally written from already-sorted data (see
214
- # _finalize_ohlcv), but this read path has no independent
215
- # guarantee of its own, so a hand-edited or externally
216
- # written cache file could otherwise hand the backtester an
217
- # unsorted frame with no error to signal it.
218
- return _finalize_ohlcv(cached)
219
- except Exception:
220
- pass # fall through and fetch fresh
221
-
222
- df = get_historical(symbol, timeframe, lookback_days)
223
- try:
224
- df.to_csv(path)
225
- except Exception:
226
- pass # caching is a convenience, never fatal
227
- return df
228
-
229
-
230
- def get_historical_with_features(symbol: str, timeframe: str, lookback_days: int,
231
- use_features: bool, refresh: bool = True):
232
- """
233
- The one call app.py/live_runner.py should make whenever they need
234
- feature-ready data (see features/feature_pipeline.py's FEATURE_COLUMNS).
235
- Returns (df, features_df) -- features_df is None when use_features=False,
236
- in which case this is exactly get_historical_cached(), unchanged.
237
-
238
- BUG FIX (found via a real Backtest run: BTC-USD/1m/window=60 with 10-
239
- feature input, first 19 walk-forward predictions failed with "feature
240
- window contains NaN in ['BB_Upper','BB_Lower','SMA_20']"): fetching
241
- EXACTLY `lookback_days` and computing indicators on just that range
242
- always leaves the first ~20 rows NaN -- SMA_20/Bollinger need 20 real
243
- candles before them, and row 0 of whatever got fetched has none, no
244
- matter how large `lookback_days` or the walk-forward `window` is. This
245
- isn't a leakage bug (validate_features was correctly rejecting genuinely
246
- undefined data), just an avoidable inefficiency -- the fix is to fetch
247
- a bit more than asked for.
248
-
249
- When use_features=True: fetches lookback_days PLUS enough extra real
250
- history in front for every indicator to warm up on
251
- (utils.helpers.feature_warmup_days), computes them on that padded
252
- range, then trims both the returned OHLCV and the computed features
253
- back down to exactly lookback_days
254
- (features.feature_pipeline.compute_feature_frame_for_period does the
255
- actual compute-then-trim) -- so what's returned covers precisely the
256
- period asked for, "same testing period" for backtest comparisons is
257
- unaffected, and its indicators are never NaN purely for lack of
258
- warm-up.
259
-
260
- BUG FIX: requesting `lookback_days + padding_days` only actually adds
261
- real padding in front if the underlying fetch isn't itself clamped
262
- back down by the source interval's own max_days (e.g. 1m tops out at
263
- 30 days total -- see YF_NATIVE_INTERVALS). When lookback_days was
264
- already close to max_days, `get_historical_cached` would silently
265
- return AT MOST max_days of data regardless of the padding we asked
266
- for on top -- so `df_padded` ended up no bigger than the plain
267
- lookback_days request, `compute_feature_frame_for_period` had nothing
268
- extra to trim off, and the exact "first ~19 rows are NaN" bug this
269
- function exists to fix came right back (reproduced with e.g. 1m +
270
- lookback_days=max_days). Fix: shrink the *requested* lookback_days
271
- itself, up front, so there's always room left over for real padding
272
- within what this interval can serve -- e.g. for 1m (max_days=30) a
273
- lookback_days=30 request becomes 29, leaving 1 real day of padding in
274
- front instead of silently losing it. This mirrors how the rest of
275
- this module already treats max_days as a hard ceiling (see
276
- _native_fetch), just applied before adding padding instead of after.
277
- """
278
- lookback_days = int(lookback_days)
279
- if not use_features:
280
- df = get_historical_cached(symbol, timeframe, lookback_days=lookback_days, refresh=refresh)
281
- return df, None
282
-
283
- padding_days = feature_warmup_days(timeframe, minimum_days=0)
284
-
285
- source_interval = CUSTOM_TIMEFRAMES.get(timeframe, {}).get("source", timeframe)
286
- max_days = YF_NATIVE_INTERVALS.get(source_interval, {}).get("max_days")
287
- if max_days is not None:
288
- lookback_days = min(lookback_days, max(1, max_days - padding_days))
289
-
290
- df_padded = get_historical_cached(symbol, timeframe,
291
- lookback_days=lookback_days + padding_days, refresh=refresh)
292
- df, features_df = compute_feature_frame_for_period(df_padded, keep_days=lookback_days)
293
- return df, features_df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
data/live.py DELETED
@@ -1,42 +0,0 @@
1
- """
2
- 'Live' data — Yahoo Finance has no free websocket feed, so live data here
3
- means polling the same free yfinance source used for history, on a timer,
4
- and watching for a new candle to appear. That's the standard approach for
5
- free-tier forex/crypto data and is what config.py / app.py assume.
6
- """
7
- import time
8
-
9
- import pandas as pd
10
-
11
- from data.historical import get_historical, DataFetchError
12
-
13
-
14
- def get_latest_candle(symbol: str, timeframe: str) -> pd.Series:
15
- """Return the most recent (possibly still-forming) candle."""
16
- df = get_historical(symbol, timeframe, lookback_days=2)
17
- if df.empty:
18
- raise DataFetchError(f"No live data available for {symbol}.")
19
- return df.iloc[-1]
20
-
21
-
22
- def poll_live(symbol: str, timeframe: str, interval_seconds: int, on_new_candle, stop_flag=None):
23
- """
24
- Blocking loop: polls for a new candle every `interval_seconds` and calls
25
- `on_new_candle(candle)` whenever the latest timestamp changes.
26
-
27
- Pass a `stop_flag` object with a mutable `.stop` attribute to end the loop
28
- gracefully from another thread (set stop_flag.stop = True); otherwise this
29
- runs until interrupted. Used by live_runner.py, not required by the
30
- Gradio UI (which polls on a button click instead of a background thread).
31
- """
32
- last_ts = None
33
- while stop_flag is None or not getattr(stop_flag, "stop", False):
34
- try:
35
- candle = get_latest_candle(symbol, timeframe)
36
- ts = candle.name
37
- if ts != last_ts:
38
- last_ts = ts
39
- on_new_candle(candle)
40
- except Exception as e:
41
- print(f"[live] fetch error for {symbol}: {e}")
42
- time.sleep(interval_seconds)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
data_cache/.gitkeep DELETED
File without changes
features/__init__.py DELETED
File without changes
features/feature_pipeline.py DELETED
@@ -1,227 +0,0 @@
1
- """
2
- Shared feature-engineering pipeline: turns raw OHLCV into the 10-feature
3
- covariate set (Open, High, Low, Close, Volume, RSI(14), MACD, Bollinger
4
- Bands, SMA/EMA, Stochastic) that ARIMA / Auto-ARIMA / ARIMA-GARCH / Moirai /
5
- TimesFM can each optionally take as EXTRA input, on top of the Close-price
6
- series they already forecast.
7
-
8
- This module is the ONE place backtest/backtester.py, app.py and
9
- live_runner.py all call to build that feature set -- so backtest and live
10
- prediction are guaranteed to run the identical pipeline, not two versions
11
- that could quietly drift apart.
12
-
13
- --------------------------------------------------------------------------
14
- Why "10 features" -> 13 columns, and why Close isn't one of them
15
- --------------------------------------------------------------------------
16
- The requested set is 10 named categories: Open, High, Low, Close, Volume,
17
- RSI(14), MACD, Bollinger Bands, SMA/EMA, Stochastic. Two adjustments turn
18
- that into FEATURE_COLUMNS below:
19
-
20
- 1. Close is NOT included as a covariate. Every one of the 5 models already
21
- receives the Close-price series as `history` -- that IS the series being
22
- forecast. Feeding it in a second time as an "exogenous" column would be
23
- pure duplication, and for the ARIMA-family models specifically it would
24
- hand statsmodels a regressor that's perfectly collinear with the
25
- dependent variable itself (singular design matrix). Close is still very
26
- much "used" -- it's the backbone of the whole system -- it's just not
27
- double-counted as its own covariate.
28
-
29
- 2. MACD / Bollinger Bands / SMA-EMA / Stochastic are each a *family* of
30
- sub-indicators, not a single number, so they expand to more than one
31
- column (MACD -> line/signal/histogram, Bollinger -> upper/lower,
32
- Stochastic -> %K/%D). One overlap is removed: bollinger_bands()'s
33
- "mid" band is defined as sma(close, 20) -- IDENTICAL to SMA_20, which is
34
- already included under "SMA/EMA". Including both would feed the exact
35
- same numbers into the model under two different names, which is not
36
- only redundant but perfectly collinear for the exogenous-regression
37
- models. BB_Mid is therefore dropped; BB_Upper/BB_Lower (the genuinely
38
- distinct band-edge information) are kept.
39
-
40
- That's 4 raw (Open/High/Low/Volume) + 1 (RSI_14) + 3 (MACD/Signal/Hist) +
41
- 2 (BB_Upper/BB_Lower) + 2 (SMA_20/EMA_20) + 2 (Stoch_%K/%D) = 14 columns
42
- from 9 used categories.
43
-
44
- --------------------------------------------------------------------------
45
- Leakage
46
- --------------------------------------------------------------------------
47
- Every column here comes straight from indicators/indicators.py's
48
- add_all_indicators(), which is already fully causal: every value at row t
49
- is computed only from rows <= t (trailing .rolling()/.ewm(adjust=False),
50
- .diff(), .shift(1) -- never center=True, never a negative/forward shift).
51
- Computing it ONCE over the full available history and slicing out backtest
52
- windows afterwards (compute_feature_frame + slice_features, used by
53
- backtest/backtester.py) gives bit-identical values to recomputing on just
54
- that window every time -- rolling/ewm literally cannot see past row t
55
- either way -- while giving short indicator warm-ups (SMA/BB need 20 prior
56
- candles) real history to warm up on instead of re-starting cold at the
57
- left edge of every walk-forward window. See tests/test_feature_pipeline.py
58
- for an executable proof of this (truncating the input after row t never
59
- changes any already-computed value at or before t).
60
-
61
- The part leakage-avoidance actually depends on is what happens at FORECAST
62
- time, handled by carry_forward_future() below -- see its docstring.
63
- """
64
- import pandas as pd
65
-
66
- from indicators.indicators import add_all_indicators
67
-
68
- REQUIRED_OHLCV_COLUMNS = ["Open", "High", "Low", "Close", "Volume"]
69
-
70
- # The 14 covariate columns actually fed to the models. Order is fixed and
71
- # stable -- every model's exog/covariate array lines up with this order.
72
- FEATURE_COLUMNS = [
73
- "Open", "High", "Low", "Volume", # raw OHLCV (Close excluded -- see module docstring)
74
- "RSI_14", # RSI(14)
75
- "MACD", "MACD_Signal", "MACD_Hist", # MACD
76
- "BB_Upper", "BB_Lower", # Bollinger Bands (BB_Mid == SMA_20, excluded -- see module docstring)
77
- "SMA_20", "EMA_20", # SMA/EMA
78
- "Stoch_%K", "Stoch_%D", # Stochastic
79
- ]
80
-
81
-
82
- def compute_feature_frame(df: pd.DataFrame) -> pd.DataFrame:
83
- """
84
- Given a full OHLCV DataFrame (oldest -> newest, the same shape
85
- data/historical.py's get_historical*() already returns), compute all 5
86
- indicator families via the existing add_all_indicators() and return
87
- just FEATURE_COLUMNS, positionally re-indexed 0..len(df)-1 so it lines
88
- up index-for-index with `df["Close"].reset_index(drop=True)` -- exactly
89
- how backtest/backtester.py already indexes `close`.
90
-
91
- Raises ValueError if `df` isn't an OHLCV frame -- fails loudly rather
92
- than silently computing indicators on the wrong columns.
93
- """
94
- missing = [c for c in REQUIRED_OHLCV_COLUMNS if c not in df.columns]
95
- if missing:
96
- raise ValueError(
97
- f"compute_feature_frame needs an OHLCV DataFrame; missing column(s): {missing}."
98
- )
99
- with_indicators = add_all_indicators(df)
100
- return with_indicators[FEATURE_COLUMNS].reset_index(drop=True).astype(float)
101
-
102
-
103
- def slice_features(features: pd.DataFrame, start: int, end: int) -> pd.DataFrame:
104
- """
105
- features.iloc[start:end], re-indexed 0..n-1 -- the exact positional
106
- window backtest/backtester.py already cuts out of `close` for
107
- `history` (close.iloc[i-window:i]), so passing the same (start, end)
108
- to both always keeps them candle-for-candle aligned.
109
- """
110
- return features.iloc[start:end].reset_index(drop=True)
111
-
112
-
113
- def compute_feature_frame_for_period(df_padded: pd.DataFrame, keep_days: float):
114
- """
115
- Fixes a real, observed failure mode: compute_feature_frame(df) alone
116
- always leaves its OWN first ~20 rows NaN (SMA_20/Bollinger need 20 real
117
- candles before them, and row 0 of whatever you hand it has none) --
118
- which then makes validate_features() correctly, loudly reject any
119
- window that touches those rows. That's not wrong, but it's avoidable:
120
- those rows aren't actually undefined in real life, they just don't
121
- have their own warm-up *inside this particular fetch*.
122
-
123
- `df_padded` should be fetched with EXTRA real history before the
124
- period you actually want (see utils.helpers.feature_warmup_days for
125
- how many extra days that needs to be, and data/historical.py's
126
- get_historical_with_features for the fetch-then-call-this pattern).
127
- This computes indicators on the FULL padded frame -- so SMA_20/
128
- Bollinger/etc. get real candles to warm up on -- then trims both the
129
- OHLCV and the computed features back down to just the last
130
- `keep_days`. The RETURNED frame's indicators are never NaN purely for
131
- lack of warm-up, even though the padding candles that supplied it
132
- aren't in the returned frame anymore -- exactly as if you'd fetched a
133
- longer range from the start and only looked at the tail of it, which
134
- is all this is: fetch extra, compute, then look at the tail.
135
-
136
- Returns (df_trimmed, features_trimmed) -- df_trimmed keeps its
137
- DatetimeIndex (unchanged rows, just fewer of them); features_trimmed
138
- is re-indexed 0..n-1 and positionally aligned with
139
- df_trimmed["Close"].reset_index(drop=True), same convention
140
- compute_feature_frame() itself already guarantees.
141
- """
142
- features_padded = compute_feature_frame(df_padded)
143
- cutoff = df_padded.index[-1] - pd.Timedelta(days=keep_days)
144
- keep_mask = df_padded.index >= cutoff # DatetimeIndex >= scalar already returns a plain numpy bool array
145
- df_trimmed = df_padded.loc[keep_mask]
146
- features_trimmed = features_padded.iloc[keep_mask].reset_index(drop=True)
147
- return df_trimmed, features_trimmed
148
-
149
-
150
- def validate_features(features: pd.DataFrame, expected_len: int, model_name: str,
151
- check_nan: bool = True) -> None:
152
- """
153
- Fail clearly instead of silently handing a model a misaligned or NaN
154
- feature window -- same philosophy base_model.py already asks every
155
- model to follow for its own inputs.
156
-
157
- NaNs happen when the fetched history doesn't have enough real warm-up
158
- before the window being predicted from: SMA_20/Bollinger need 20 prior
159
- candles of real data, and if the very first ~20 rows of the ENTIRE
160
- fetched dataset land inside a requested window, those rows are
161
- genuinely undefined (there's no earlier data to compute them from) --
162
- the fix is more lookback/history (see
163
- data/historical.py's get_historical_with_features and this module's
164
- compute_feature_frame_for_period), not a fabricated fill value.
165
-
166
- `check_nan=False` skips the NaN check, keeping only the length check --
167
- for a model like Moirai/TimesFM that only ever uses the TAIL of what
168
- it's handed (see models/moirai_model.py, models/timesfm_model.py):
169
- call this once with check_nan=False against the FULL `history` length
170
- (catches a genuinely misaligned caller early, before any truncation
171
- logic runs), then again with the default check_nan=True against just
172
- the truncated tail actually being used -- so a NaN sitting only in the
173
- part that gets trimmed away never causes a false-positive rejection of
174
- an otherwise-fine window.
175
- """
176
- if len(features) != expected_len:
177
- raise ValueError(
178
- f"{model_name}: feature window has {len(features)} rows, expected "
179
- f"{expected_len} to match the price history it must line up with."
180
- )
181
- if check_nan and features.isna().any().any():
182
- bad_cols = features.columns[features.isna().any()].tolist()
183
- raise ValueError(
184
- f"{model_name}: feature window contains NaN in {bad_cols}. This "
185
- f"usually means there isn't enough real history before this window "
186
- f"for the slower indicators (SMA/Bollinger need 20 prior candles) "
187
- f"to warm up on -- increase the lookback/history and try again."
188
- )
189
-
190
-
191
- def carry_forward_future(feature_window: pd.DataFrame, horizon: int) -> pd.DataFrame:
192
- """
193
- Build `horizon` additional covariate rows for the forecast period, by
194
- repeating the LAST already-observed feature row.
195
-
196
- Why this exists: several of the models' native forecasting APIs
197
- structurally require covariate values for the future steps being
198
- forecast, not just the historical context --
199
- statsmodels' `SARIMAX/ARIMA.forecast(steps=h, exog=...)` needs exog for
200
- all h future steps, and TimesFM's `forecast_with_covariates()` needs
201
- "dynamic covariates [that] need values for BOTH context AND forecast
202
- windows" (its own docs' words). But RSI/MACD/Bollinger/SMA-EMA/
203
- Stochastic/Volume for a candle that hasn't happened yet don't exist --
204
- they're derived from that candle's own OHLCV, which is exactly what's
205
- being forecast. Computing them for the future would mean either
206
- fabricating data or leaking the real future values, both of which the
207
- project's no-leakage rule rules out.
208
-
209
- So instead: persist the last REAL, already-observed feature row forward
210
- for every step of the forecast horizon (a standard "conditions stay as
211
- they last were" persistence assumption for unknown-future exogenous
212
- inputs). This uses zero information from the future -- it is NOT
213
- leakage -- but it IS an approximation, and it does not pretend to be
214
- anything more the further out `horizon` reaches. Callers should not
215
- read a multi-step forecast's use of features as "the model knew where
216
- RSI was going" -- it didn't; it assumed RSI stayed put.
217
-
218
- Moirai is the one model that never needs this: its past_feat_dynamic_real
219
- covariate slot is architecturally past-only, so it simply isn't asked
220
- for future covariate values at all (see models/moirai_model.py).
221
- """
222
- if horizon < 1:
223
- raise ValueError("carry_forward_future: horizon must be >= 1.")
224
- if len(feature_window) == 0:
225
- raise ValueError("carry_forward_future: feature_window is empty.")
226
- last_row = feature_window.iloc[[-1]]
227
- return pd.concat([last_row] * horizon, ignore_index=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
indicators/__init__.py DELETED
File without changes
indicators/indicators.py DELETED
@@ -1,180 +0,0 @@
1
- """
2
- Top 5 technical indicators for the chart/analysis tab.
3
-
4
- Implemented directly with pandas/numpy (no extra TA library dependency, so
5
- there's one less thing that can fail to install):
6
- 1. RSI - Relative Strength Index
7
- 2. MACD - Moving Average Convergence Divergence
8
- 3. Bollinger Bands
9
- 4. Moving Averages - SMA & EMA
10
- 5. Stochastic Oscillator
11
-
12
- These are informational/analysis indicators shown alongside the chart. They
13
- are NOT part of the backtested model accuracy numbers — those come only from
14
- the 5 forecasting models (ARIMA / Auto-ARIMA / ARIMA-GARCH / Moirai /
15
- TimesFM) in backtest/backtester.py, kept deliberately separate so nothing
16
- gets mixed together.
17
- """
18
- import numpy as np
19
- import pandas as pd
20
-
21
-
22
- def sma(series: pd.Series, window: int) -> pd.Series:
23
- return series.rolling(window=window).mean()
24
-
25
-
26
- def ema(series: pd.Series, window: int) -> pd.Series:
27
- return series.ewm(span=window, adjust=False).mean()
28
-
29
-
30
- def _wilder_rma(values: pd.Series, window: int) -> pd.Series:
31
- """
32
- Wilder's exact smoothed moving average (the "RMA" his RSI/ATR/ADX are
33
- all defined with): the FIRST output is a plain SIMPLE average of the
34
- first `window` values, and every value after that is
35
- `prev*(window-1)/window + current/window`.
36
-
37
- `series.ewm(alpha=1/window, adjust=False).mean()` looks equivalent but
38
- is NOT: `adjust=False` seeds the recursion from the very first raw
39
- observation instead of a `window`-length simple-average, so for window
40
- periods it takes roughly 6-7x `window` more bars to converge to what
41
- the simple-average seed gives immediately. Verified against the
42
- classic 14-period Wilder RSI reference example (StockCharts'
43
- 44.34/44.09/.../46.28 series): the seeded version reproduces the
44
- textbook 70.46 exactly; plain `ewm(adjust=False)` gives 50.66 at that
45
- same point -- a ~20-point error right at the point a short lookback
46
- would actually display. The two converge (difference < 0.05) only
47
- after roughly 100 bars, so this only silently matters on short
48
- lookbacks -- which the Chart tab's lookback slider allows down to 1 day.
49
- """
50
- arr = values.to_numpy(dtype=float)
51
- n = len(arr)
52
- out = np.full(n, np.nan)
53
- first_valid_pos = 0
54
- while first_valid_pos < n and np.isnan(arr[first_valid_pos]):
55
- first_valid_pos += 1
56
- seed_end = first_valid_pos + window
57
- if seed_end > n:
58
- return pd.Series(out, index=values.index)
59
- prev = arr[first_valid_pos:seed_end].mean()
60
- out[seed_end - 1] = prev
61
- for i in range(seed_end, n):
62
- prev = (prev * (window - 1) + arr[i]) / window
63
- out[i] = prev
64
- return pd.Series(out, index=values.index)
65
-
66
-
67
- def rsi(series: pd.Series, window: int = 14) -> pd.Series:
68
- delta = series.diff()
69
- gain = delta.clip(lower=0)
70
- loss = -delta.clip(upper=0)
71
- avg_gain = _wilder_rma(gain, window)
72
- avg_loss = _wilder_rma(loss, window)
73
- rs = avg_gain / avg_loss.replace(0, np.nan)
74
- out = 100 - (100 / (1 + rs))
75
- # Wilder's defined edge case (see StockCharts/Wilder RSI reference): a
76
- # window with gains but zero losses is a divide-by-zero for RS, and RSI
77
- # is defined as 100 by convention -- NOT neutral. Without this, an
78
- # all-gains window (the exact moment RSI should scream "overbought")
79
- # was silently reported as a neutral 50. A genuinely flat window (zero
80
- # gain AND zero loss) has no such convention and still falls through to
81
- # the neutral fillna(50) below, same as the warm-up NaN period.
82
- out = out.mask((avg_loss == 0) & (avg_gain > 0), 100.0)
83
- return out.fillna(50)
84
-
85
-
86
- def macd(series: pd.Series, fast: int = 12, slow: int = 26, signal: int = 9):
87
- fast_ema = ema(series, fast)
88
- slow_ema = ema(series, slow)
89
- macd_line = fast_ema - slow_ema
90
- signal_line = ema(macd_line, signal)
91
- histogram = macd_line - signal_line
92
- return macd_line, signal_line, histogram
93
-
94
-
95
- def bollinger_bands(series: pd.Series, window: int = 20, num_std: float = 2.0):
96
- mid = sma(series, window)
97
- # ddof=0 (population std): traditional Bollinger Bands, as defined by
98
- # John Bollinger, use the population standard deviation of the SAME
99
- # `window` closes as the SMA -- not pandas' default sample std
100
- # (ddof=1), which divides by (window-1) and makes the bands ~2-3%
101
- # too wide at the default window=20, shifting the "above/below/inside
102
- # the band" reading right at the margin.
103
- std = series.rolling(window=window).std(ddof=0)
104
- upper = mid + num_std * std
105
- lower = mid - num_std * std
106
- return upper, mid, lower
107
-
108
-
109
- def stochastic_oscillator(high: pd.Series, low: pd.Series, close: pd.Series,
110
- k_window: int = 14, d_window: int = 3):
111
- lowest_low = low.rolling(window=k_window).min()
112
- highest_high = high.rolling(window=k_window).max()
113
- denom = (highest_high - lowest_low).replace(0, np.nan)
114
- percent_k = 100 * (close - lowest_low) / denom
115
- percent_d = percent_k.rolling(window=d_window).mean()
116
- return percent_k.fillna(50), percent_d.fillna(50)
117
-
118
-
119
- def add_all_indicators(df: pd.DataFrame) -> pd.DataFrame:
120
- """Return a copy of df with all 5 indicators added as columns."""
121
- out = df.copy()
122
- out["SMA_20"] = sma(out["Close"], 20)
123
- out["EMA_20"] = ema(out["Close"], 20)
124
- out["RSI_14"] = rsi(out["Close"], 14)
125
-
126
- macd_line, signal_line, hist = macd(out["Close"])
127
- out["MACD"] = macd_line
128
- out["MACD_Signal"] = signal_line
129
- out["MACD_Hist"] = hist
130
-
131
- upper, mid, lower = bollinger_bands(out["Close"])
132
- out["BB_Upper"] = upper
133
- out["BB_Mid"] = mid
134
- out["BB_Lower"] = lower
135
-
136
- k, d = stochastic_oscillator(out["High"], out["Low"], out["Close"])
137
- out["Stoch_%K"] = k
138
- out["Stoch_%D"] = d
139
-
140
- return out
141
-
142
-
143
- def latest_signals(df_with_indicators: pd.DataFrame) -> dict:
144
- """A simple rule-based read of the latest indicator values, for display only."""
145
- last = df_with_indicators.iloc[-1]
146
- signals = {}
147
-
148
- signals["RSI (14)"] = {
149
- "value": round(float(last["RSI_14"]), 2),
150
- "signal": "Overbought" if last["RSI_14"] > 70 else "Oversold" if last["RSI_14"] < 30 else "Neutral",
151
- }
152
- signals["MACD"] = {
153
- "value": round(float(last["MACD"]), 5),
154
- "signal": "Bullish (above signal)" if last["MACD"] > last["MACD_Signal"] else "Bearish (below signal)",
155
- }
156
- bb_ready = pd.notna(last["BB_Upper"]) and pd.notna(last["BB_Lower"])
157
- signals["Bollinger Bands"] = {
158
- "value": round(float(last["Close"]), 5),
159
- "signal": (
160
- "Insufficient data" if not bb_ready
161
- else "Above Upper Band" if last["Close"] > last["BB_Upper"]
162
- else "Below Lower Band" if last["Close"] < last["BB_Lower"]
163
- else "Inside Bands"
164
- ),
165
- }
166
- sma_ready = pd.notna(last["SMA_20"])
167
- signals["Moving Averages"] = {
168
- "value": f"SMA20={round(float(last['SMA_20']), 5) if sma_ready else 'n/a'}, "
169
- f"EMA20={round(float(last['EMA_20']), 5)}",
170
- "signal": (
171
- "Insufficient data for SMA" if not sma_ready
172
- else "Price Above MA (Uptrend)" if last["Close"] > last["SMA_20"]
173
- else "Price Below MA (Downtrend)"
174
- ),
175
- }
176
- signals["Stochastic (14,3)"] = {
177
- "value": round(float(last["Stoch_%K"]), 2),
178
- "signal": "Overbought" if last["Stoch_%K"] > 80 else "Oversold" if last["Stoch_%K"] < 20 else "Neutral",
179
- }
180
- return signals
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
live_runner.py DELETED
@@ -1,131 +0,0 @@
1
- """
2
- Standalone continuous 'live' prediction logger — an alternative to clicking
3
- the Live tab in app.py by hand. Run this separately, e.g.:
4
-
5
- python live_runner.py --symbol BTC-USD --timeframe 5m --model ARIMA
6
- python live_runner.py --symbol EURUSD=X --timeframe 15m --model ARIMA-GARCH
7
- python live_runner.py --symbol EURUSD=X --timeframe 15m --model TimesFM --poll-seconds 120
8
- python live_runner.py --symbol BTC-USD --timeframe 15m --model ARIMA --use-features
9
-
10
- Every prediction and its later verification (correct/incorrect once the
11
- target candle closes) is appended to a CSV under outputs/, one row per
12
- verified candle: outputs/live_<symbol>_<timeframe>_<model>[_featured].csv
13
-
14
- --use-features runs the exact same feature-generation pipeline as the
15
- Backtest tab and app.py's Live tab (features/feature_pipeline.py) — Open/
16
- High/Low/Volume + RSI/MACD/Bollinger/SMA-EMA/Stochastic, leak-free — on top
17
- of the Close-only prediction this script always made. Off by default, and
18
- logs to a separate CSV (the "_featured" suffix) so a baseline run and a
19
- featured run of the same symbol/timeframe/model never land in the same file.
20
- """
21
- import argparse
22
- import os
23
- from datetime import datetime, timezone
24
-
25
- import pandas as pd
26
-
27
- from data.historical import get_historical_with_features, DataFetchError
28
- from models.registry import get_model
29
- from utils.helpers import infer_step
30
- from config import OUTPUT_DIR, CUSTOM_TIMEFRAMES, YF_NATIVE_INTERVALS
31
-
32
-
33
- def main():
34
- parser = argparse.ArgumentParser(description="Continuous live prediction logger.")
35
- parser.add_argument("--symbol", required=True, help="e.g. BTC-USD, EURUSD=X")
36
- parser.add_argument("--timeframe", required=True, help="e.g. 1m, 5m, 15m, 1h")
37
- parser.add_argument("--model", required=True,
38
- choices=["ARIMA", "Auto-ARIMA", "ARIMA-GARCH", "Moirai", "TimesFM"])
39
- parser.add_argument("--poll-seconds", type=int, default=60,
40
- help="How often to check for a new candle (default: 60s)")
41
- parser.add_argument("--use-features", action="store_true",
42
- help="Feed OHLCV+RSI/MACD/Bollinger/SMA-EMA/Stochastic to the model "
43
- "(same leak-free pipeline as the Backtest tab), not just Close.")
44
- args = parser.parse_args()
45
-
46
- safe_symbol = args.symbol.replace("=", "_").replace("/", "_")
47
- suffix = "_featured" if args.use_features else ""
48
- log_path = os.path.join(OUTPUT_DIR, f"live_{safe_symbol}_{args.timeframe}_{args.model}{suffix}.csv")
49
- model = get_model(args.model, args.symbol, args.timeframe, use_features=args.use_features)
50
-
51
- # This script has no lookback flag either -- a flat 2 days is enough
52
- # for verification purposes on any timeframe. Feature warm-up (when
53
- # --use-features) is handled separately, INSIDE
54
- # get_historical_with_features below -- it adds its own extra padding
55
- # on top of fetch_days, then trims back off (see data/historical.py's
56
- # docstring) -- so fetch_days here is unrelated to indicator warm-up.
57
- source_interval = CUSTOM_TIMEFRAMES.get(args.timeframe, {}).get("source", args.timeframe)
58
- max_servable_days = YF_NATIVE_INTERVALS.get(source_interval, {}).get("max_days")
59
- fetch_days = min(2, max_servable_days) if max_servable_days else 2
60
-
61
- pending = None # dict: target_time, current_close, predicted_close, predicted_direction
62
- correct_count = 0
63
- total_count = 0
64
-
65
- print(f"[live_runner] {args.symbol} [{args.timeframe}] using {args.model}"
66
- f"{' (+10 features)' if args.use_features else ''}. "
67
- f"Logging to {log_path}. Ctrl+C to stop.")
68
-
69
- import time
70
- while True:
71
- try:
72
- df, features_df = get_historical_with_features(
73
- args.symbol, args.timeframe, lookback_days=fetch_days,
74
- use_features=args.use_features, refresh=True,
75
- )
76
- latest_time = df.index[-1]
77
- latest_close = float(df["Close"].iloc[-1])
78
-
79
- # Only verify once the target candle has actually CLOSED -- i.e. a
80
- # strictly newer candle exists after it in the freshly fetched data.
81
- # `in df.index` alone is true the instant the candle starts forming,
82
- # which would read its still-changing in-progress price as if it
83
- # were the final close (see app.py's _check_live_impl for the full
84
- # writeup of this bug and how it silently corrupts the accuracy %).
85
- if (pending is not None and pending["target_time"] in df.index
86
- and pending["target_time"] < latest_time):
87
- actual_close = float(df.loc[pending["target_time"], "Close"])
88
- actual_direction = "up" if actual_close > pending["current_close"] else "down"
89
- correct = actual_direction == pending["predicted_direction"]
90
- total_count += 1
91
- correct_count += int(correct)
92
-
93
- row = {
94
- **pending, "actual_close": actual_close, "actual_direction": actual_direction,
95
- "correct": correct, "logged_at": datetime.now(timezone.utc).isoformat(),
96
- }
97
- pd.DataFrame([row]).to_csv(log_path, mode="a", header=not os.path.exists(log_path), index=False)
98
- acc = round(100 * correct_count / total_count, 2)
99
- print(f"[{datetime.now(timezone.utc).isoformat()}] Verified: predicted "
100
- f"{pending['predicted_direction']}, actual {actual_direction} -> "
101
- f"{'CORRECT' if correct else 'WRONG'} (running accuracy: {acc}%, n={total_count})")
102
- pending = None
103
-
104
- if pending is None:
105
- # features_df was computed above by get_historical_with_features,
106
- # the exact same feature-generation pipeline backtest and
107
- # app.py's Live tab use.
108
- forecast = model.predict(df["Close"], horizon=1, features=features_df)
109
- predicted_close = float(forecast[-1])
110
- predicted_direction = "up" if predicted_close > latest_close else "down"
111
- step = infer_step(df.index)
112
- pending = {
113
- "target_time": latest_time + step, "current_close": latest_close,
114
- "predicted_close": predicted_close, "predicted_direction": predicted_direction,
115
- }
116
- print(f"[{datetime.now(timezone.utc).isoformat()}] New prediction: "
117
- f"{args.symbol} next candle -> {predicted_direction} ({predicted_close:.5f})")
118
-
119
- except DataFetchError as e:
120
- print(f"[{datetime.now(timezone.utc).isoformat()}] Data error: {e}")
121
- except KeyboardInterrupt:
122
- print("Stopped.")
123
- break
124
- except Exception as e:
125
- print(f"[{datetime.now(timezone.utc).isoformat()}] Error: {e}")
126
-
127
- time.sleep(args.poll_seconds)
128
-
129
-
130
- if __name__ == "__main__":
131
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
models/__init__.py DELETED
File without changes
models/arima_garch_model.py DELETED
@@ -1,231 +0,0 @@
1
- """
2
- ARIMA-GARCH — the classical two-step "mean + volatility" combo for return
3
- series: `statsmodels` (ARIMA) models the conditional MEAN of returns, and
4
- `arch` (GARCH) models the conditional VARIANCE — the volatility clustering
5
- that plain ARIMA can't see, since it assumes constant variance.
6
-
7
- Why not just run ARIMA on the raw price directly, like arima_model.py does?
8
- GARCH models volatility, and volatility clustering shows up in RETURNS, not
9
- raw price levels (price isn't stationary in variance; returns usually are
10
- much closer to it). So this model:
11
- 1. converts the closing-price history to log returns,
12
- 2. fits ARIMA(p, 0, q) to the returns for the conditional mean,
13
- 3. fits GARCH(1, 1) on the ARIMA residuals for the conditional variance,
14
- 4. turns the forecasted mean return back into a price forecast:
15
- price[t+1] = price[t] * exp(forecasted_log_return[t+1])
16
- (compounding forward for horizon > 1).
17
-
18
- Point-forecast-wise this lands close to plain ARIMA-on-returns, because a
19
- GARCH model forecasts the SIZE of the next shock (its variance), not its
20
- sign/mean — ARIMA still supplies the point forecast. Where GARCH actually
21
- earns its keep is the volatility forecast itself, kept here as
22
- `last_volatility_forecast` (per-step forecasted std-dev of returns, updated
23
- on every `.predict()` call). That isn't part of the BaseForecastModel
24
- interface so it's not forced into predict()'s return value, but it's there
25
- if you want to build confidence bands or size positions by risk later.
26
-
27
- Speed note: same story as arima_model.py/auto_arima_model.py — searching a
28
- (p, q) grid on every `.predict()` call would make backtesting slow, so the
29
- ARIMA order (for the returns) is searched ONCE and reused. Pass
30
- `order=(p, 0, q)` yourself to skip that search.
31
-
32
- Optional 10-feature covariate input (see features/feature_pipeline.py): fed
33
- into step 1 only — as `exog` on the conditional-MEAN ARIMA, exactly like
34
- arima_model.py. Step 2 (GARCH on the residuals) is deliberately left alone:
35
- GARCH already models the one thing plain ARIMA-with-exog can't (volatility
36
- clustering in the *residuals*), and that's a property of the mean model's
37
- own errors, not something OHLCV/RSI/MACD-style covariates are standardly
38
- used to explain — bolting exog onto the variance equation too would be a
39
- much less established technique than exogenous-ARIMA-for-the-mean, which
40
- this project isn't going to invent on the spot.
41
- One bookkeeping wrinkle `features=` needs that arima_model.py doesn't:
42
- `log_returns` has one fewer row than `prices`/`history` (returns need a
43
- previous price to be computed from, so the very first candle has none) —
44
- `features` gets its first row dropped the same way before it's used as
45
- exog, so row i of the aligned features still describes the same candle as
46
- `log_returns[i]`.
47
-
48
- BUG FIX -- same unbounded-history issue as arima_model.py/
49
- auto_arima_model.py (see arima_model.py's module docstring for the full
50
- writeup and measurements): the Prediction/Live tabs impose no context
51
- window of their own, so without a cap the (p,q) search and every re-fit
52
- here scaled with however much history got fetched. `max_history` bounds it
53
- the same way, applied to `prices` before log returns are even computed
54
- (truncating a price series and then differencing it is equivalent to
55
- differencing first and truncating the returns, just simpler to reason
56
- about) — default matches the other two classical models for consistency.
57
- """
58
- import warnings
59
-
60
- import numpy as np
61
- import pandas as pd
62
- from statsmodels.tsa.arima.model import ARIMA
63
-
64
- from models.base_model import BaseForecastModel
65
- from features.feature_pipeline import carry_forward_future, validate_features
66
-
67
- warnings.filterwarnings("ignore")
68
-
69
-
70
- class ArimaGarchModel(BaseForecastModel):
71
- name = "ARIMA-GARCH"
72
-
73
- def __init__(self, max_p=3, max_q=3, order=None, garch_p=1, garch_q=1,
74
- refit_order_every=None, max_history=300):
75
- self.max_p = max_p
76
- self.max_q = max_q
77
- self._cached_order = order # (p, 0, q), searched on the RETURNS series
78
- self.garch_p = garch_p
79
- self.garch_q = garch_q
80
- self.refit_order_every = refit_order_every # None = search once, then always reuse
81
- self._calls_since_search = 0
82
- # Only the most recent `max_history` candles of price history are
83
- # ever used -- see the module docstring's BUG FIX note. None/0
84
- # disables the cap.
85
- self.max_history = max_history
86
- # Per-step forecasted std-dev of returns from the most recent
87
- # predict() call — informational only, see module docstring.
88
- self.last_volatility_forecast = None
89
-
90
- def _check_dependencies(self):
91
- import arch # noqa
92
-
93
- def _import_arch_model(self):
94
- try:
95
- from arch import arch_model
96
- except ImportError as e:
97
- raise ImportError(
98
- "ARIMA-GARCH needs the 'arch' package. Install with:\n"
99
- " pip install arch\n"
100
- f"(original error: {e})"
101
- )
102
- return arch_model
103
-
104
- def _best_order(self, returns: pd.Series, exog: pd.DataFrame = None):
105
- """Small AIC grid search over (p, q) for the returns series, d fixed
106
- at 0 since returns should already be roughly stationary."""
107
- best_aic = np.inf
108
- best_order = (1, 0, 1)
109
- for p in range(self.max_p + 1):
110
- for q in range(self.max_q + 1):
111
- if p == 0 and q == 0:
112
- continue
113
- try:
114
- fit = ARIMA(returns, exog=exog, order=(p, 0, q)).fit()
115
- if fit.aic < best_aic:
116
- best_aic = fit.aic
117
- best_order = (p, 0, q)
118
- except Exception:
119
- continue
120
- return best_order
121
-
122
- def predict(self, history: pd.Series, horizon: int = 1, features: pd.DataFrame = None) -> list:
123
- arch_model = self._import_arch_model()
124
-
125
- prices_full = pd.Series(history).astype(float).reset_index(drop=True)
126
- if len(prices_full) < 30:
127
- raise ValueError("ARIMA-GARCH needs at least 30 candles of history.")
128
-
129
- exog_full = None
130
- if features is not None:
131
- # Length-only against the FULL input first -- catches a
132
- # genuinely misaligned caller early, before max_history
133
- # truncates anything away.
134
- validate_features(features, expected_len=len(prices_full), model_name="ARIMA-GARCH", check_nan=False)
135
- exog_full = features.reset_index(drop=True)
136
-
137
- # Cap how much price history the (p,q) search and every fit
138
- # actually see -- see max_history's note in __init__/the module
139
- # docstring. Truncating prices before differencing is equivalent to
140
- # differencing first and truncating the returns, just simpler.
141
- if self.max_history and len(prices_full) > self.max_history:
142
- prices = prices_full.iloc[-self.max_history:].reset_index(drop=True)
143
- exog_aligned = exog_full.iloc[-self.max_history:].reset_index(drop=True) if exog_full is not None else None
144
- else:
145
- prices = prices_full
146
- exog_aligned = exog_full
147
-
148
- if (prices <= 0).any():
149
- raise ValueError("ARIMA-GARCH needs strictly positive prices (it uses log returns).")
150
-
151
- log_returns = np.log(prices / prices.shift(1)).dropna().reset_index(drop=True)
152
-
153
- exog = None
154
- if exog_aligned is not None:
155
- # NaN is checked here, only on the truncated tail actually used
156
- # (same check_nan split arima_model.py/auto_arima_model.py use).
157
- validate_features(exog_aligned, expected_len=len(prices), model_name="ARIMA-GARCH")
158
- # Drop row 0 to line up with log_returns (see module docstring):
159
- # log_returns[i] is the return INTO candle i+1, so it aligns with
160
- # features row i+1, not features row i.
161
- exog = exog_aligned.iloc[1:].reset_index(drop=True)
162
-
163
- need_search = self._cached_order is None
164
- if self.refit_order_every and self._calls_since_search >= self.refit_order_every:
165
- need_search = True
166
- if need_search:
167
- self._cached_order = self._best_order(log_returns, exog=exog)
168
- self._calls_since_search = 0
169
- self._calls_since_search += 1
170
-
171
- # --- 1) conditional mean: ARIMA on log returns ---
172
- mean_fit = ARIMA(log_returns, exog=exog, order=self._cached_order).fit()
173
- if exog is not None:
174
- future_exog = carry_forward_future(exog, horizon)
175
- mean_forecast = np.asarray(mean_fit.forecast(steps=horizon, exog=future_exog), dtype=float)
176
- else:
177
- mean_forecast = np.asarray(mean_fit.forecast(steps=horizon), dtype=float)
178
-
179
- # --- 2) conditional variance: GARCH on the ARIMA residuals ---
180
- # arch_model optimizes more reliably on returns of order ~1 than on
181
- # tiny raw log-returns (~1e-4), so scale up for fitting and scale
182
- # the resulting variance back down afterwards.
183
- resid = pd.Series(mean_fit.resid).dropna()
184
- scale = 100.0
185
- # BUG FIX: `arch`'s own ARCHModel.fit() does NOT respect this file's
186
- # top-of-module `warnings.filterwarnings("ignore")`. Internally, on
187
- # every single call, it runs `warnings.filterwarnings("always", "",
188
- # ConvergenceWarning)` when its own `show_warning` (default: True)
189
- # is truthy -- which inserts a fresh filter ahead of ours and forces
190
- # the warning to print regardless of what any caller configured
191
- # (confirmed against arch's ARCHModel.fit source). `disp="off"` only
192
- # silences the optimizer's iteration-by-iteration console dump, not
193
- # this warning, so it kept firing on every non-fully-converged fit.
194
- # A GARCH(1,1) MLE fit on a short walk-forward window (tens of
195
- # residuals per backtest step) frequently doesn't fully converge --
196
- # that's an expected characteristic of fitting on short windows, not
197
- # a sign this particular fit is unusable, since the price forecast
198
- # returned by predict() comes from `mean_fit` (plain ARIMA) and
199
- # never depends on whether this GARCH step fully converged; only
200
- # the informational `last_volatility_forecast` below does. Passing
201
- # `show_warning=False` uses arch's own supported switch to suppress
202
- # exactly this warning at its source, instead of fighting the global
203
- # warnings-filter list from outside -- which is what was flooding
204
- # the app's logs with hundreds of repeated ConvergenceWarning lines
205
- # on any real backtest run.
206
- garch_fit = arch_model(
207
- resid * scale, mean="Zero", vol="GARCH", p=self.garch_p, q=self.garch_q,
208
- dist="normal", rescale=False,
209
- ).fit(disp="off", show_warning=False, options={"maxiter": 500})
210
- garch_forecast = garch_fit.forecast(horizon=horizon, reindex=False)
211
- variance = np.asarray(garch_forecast.variance.values[-1], dtype=float) / (scale ** 2)
212
- if not np.all(np.isfinite(variance)) or np.any(variance < 0):
213
- raise ValueError(
214
- "ARIMA-GARCH produced a non-finite or negative forecasted variance "
215
- "for this window (the GARCH volatility step failed to produce a "
216
- "usable result)."
217
- )
218
- self.last_volatility_forecast = [float(x) for x in np.sqrt(variance)]
219
-
220
- # --- 3) mean log-return forecast -> price forecast (compounded) ---
221
- last_price = float(prices.iloc[-1])
222
- prices_out = []
223
- cum_log_return = 0.0
224
- for r in mean_forecast:
225
- cum_log_return += float(r)
226
- prices_out.append(last_price * float(np.exp(cum_log_return)))
227
-
228
- if not all(np.isfinite(x) for x in prices_out):
229
- raise ValueError(f"ARIMA-GARCH produced a non-finite forecast for order {self._cached_order}.")
230
-
231
- return prices_out
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
models/arima_model.py DELETED
@@ -1,141 +0,0 @@
1
- """
2
- ARIMA — the classic statistical baseline. Needs only `statsmodels`, so it
3
- always works even if the two deep-learning models below aren't installed.
4
-
5
- Speed note: a full (p,d,q) grid search on every single prediction would make
6
- backtesting painfully slow (hundreds of candles x dozens of candidate orders).
7
- So by default this class searches for the best order ONCE (on the first
8
- `.predict()` call) and reuses it for every later call in the same instance —
9
- that's what makes ARIMA backtesting fast. Pass `order=(p, d, q)` yourself to
10
- skip the search entirely.
11
-
12
- Optional 10-feature covariate input (see features/feature_pipeline.py): pass
13
- `features=<DataFrame>` to `.predict()` and this becomes a genuine ARIMAX —
14
- `statsmodels.tsa.arima.model.ARIMA` natively accepts an `exog` regressor
15
- matrix alongside the endogenous series, so this isn't a workaround, it's the
16
- model's own supported mechanism. Close itself is never one of the exog
17
- columns (see feature_pipeline.py's docstring for why). Forecasting `horizon`
18
- steps ahead needs exog values for those future steps too — since RSI/MACD/
19
- etc. for candles that haven't happened yet can't be known, those rows are
20
- built by carry_forward_future() (persisting the last real reading forward,
21
- never fabricated or peeked-at future data — see its docstring). The (p,d,q)
22
- order search itself also uses exog when features are supplied, so the order
23
- chosen is the one that's actually best for the model being fit, not for a
24
- plain-Close model that then quietly gets extra regressors bolted on.
25
-
26
- BUG FIX -- unbounded history made the (p,d,q) search (and every subsequent
27
- re-fit) scale with however much was fetched, not with anything actually
28
- needed: unlike Moirai/TimesFM (which already cap their input to
29
- context_length/max_context candles), this model used to fit on the WHOLE
30
- `history` it was handed. The Prediction/Live tabs impose no window of their
31
- own the way Backtest's "History window per prediction" slider does (capped
32
- at 300) -- they hand this model everything get_historical_with_features
33
- fetched, which for a small timeframe and a generous lookback can be tens of
34
- thousands of candles (e.g. 30 days of 1-minute data is ~43,000 rows).
35
- Measured on this project's own grid search: ~6s at 500 candles Close-only
36
- and ~17s with the 10-feature exog on, growing well past that -- at several
37
- thousand candles this reached tens of seconds to low minutes for a single
38
- model, before even counting sequential scaling across multiple selected
39
- models in one Prediction-tab click. `max_history` bounds this the same way
40
- Moirai/TimesFM already bound theirs: only the most recent `max_history`
41
- candles are ever handed to the search or the fit, regardless of how much
42
- history the caller has available. 300 matches the Backtest tab's own
43
- established ceiling for these classical models' per-prediction context, so
44
- Backtest's behavior (whose windows are already <= 300) is unaffected by
45
- this change either way.
46
- """
47
- import warnings
48
-
49
- import numpy as np
50
- import pandas as pd
51
- from statsmodels.tsa.arima.model import ARIMA
52
-
53
- from models.base_model import BaseForecastModel
54
- from features.feature_pipeline import carry_forward_future, validate_features
55
-
56
- warnings.filterwarnings("ignore")
57
-
58
-
59
- class ArimaModel(BaseForecastModel):
60
- name = "ARIMA"
61
-
62
- def __init__(self, max_p=3, max_d=2, max_q=3, order=None, refit_order_every=None,
63
- max_history=300):
64
- self.max_p = max_p
65
- self.max_d = max_d
66
- self.max_q = max_q
67
- self._cached_order = order
68
- self.refit_order_every = refit_order_every # None = search once, then always reuse
69
- self._calls_since_search = 0
70
- # Only the most recent `max_history` candles are ever fit on -- see
71
- # the module docstring's BUG FIX note. None/0 disables the cap.
72
- self.max_history = max_history
73
-
74
- def _check_dependencies(self):
75
- import statsmodels # noqa
76
-
77
- def _best_order(self, series: pd.Series, exog: pd.DataFrame = None):
78
- best_aic = np.inf
79
- best_order = (1, 1, 1)
80
- for p in range(self.max_p + 1):
81
- for d in range(self.max_d + 1):
82
- for q in range(self.max_q + 1):
83
- if p == 0 and q == 0:
84
- continue
85
- try:
86
- fit = ARIMA(series, exog=exog, order=(p, d, q)).fit()
87
- if fit.aic < best_aic:
88
- best_aic = fit.aic
89
- best_order = (p, d, q)
90
- except Exception:
91
- continue
92
- return best_order
93
-
94
- def predict(self, history: pd.Series, horizon: int = 1, features: pd.DataFrame = None) -> list:
95
- series_full = pd.Series(history).astype(float).reset_index(drop=True)
96
- if len(series_full) < 20:
97
- raise ValueError("ARIMA needs at least 20 candles of history.")
98
-
99
- exog_full = None
100
- if features is not None:
101
- # Length-only against the FULL input first -- catches a
102
- # genuinely misaligned caller early, before max_history
103
- # truncates anything away. NaN is checked below, only on the
104
- # truncated tail actually used for fitting (same check_nan
105
- # split Moirai/TimesFM already use for their own context-window
106
- # truncation -- see validate_features' docstring).
107
- validate_features(features, expected_len=len(series_full), model_name="ARIMA", check_nan=False)
108
- exog_full = features.reset_index(drop=True)
109
-
110
- # Cap how much history the (p,d,q) search and every fit actually
111
- # see -- see max_history's note in __init__/the module docstring.
112
- if self.max_history and len(series_full) > self.max_history:
113
- series = series_full.iloc[-self.max_history:].reset_index(drop=True)
114
- exog = exog_full.iloc[-self.max_history:].reset_index(drop=True) if exog_full is not None else None
115
- else:
116
- series = series_full
117
- exog = exog_full
118
- if exog is not None:
119
- validate_features(exog, expected_len=len(series), model_name="ARIMA")
120
-
121
- need_search = self._cached_order is None
122
- if self.refit_order_every and self._calls_since_search >= self.refit_order_every:
123
- need_search = True
124
-
125
- if need_search:
126
- self._cached_order = self._best_order(series, exog=exog)
127
- self._calls_since_search = 0
128
-
129
- self._calls_since_search += 1
130
-
131
- fit = ARIMA(series, exog=exog, order=self._cached_order).fit()
132
-
133
- if exog is not None:
134
- future_exog = carry_forward_future(exog, horizon)
135
- forecast = np.asarray(fit.forecast(steps=horizon, exog=future_exog), dtype=float)
136
- else:
137
- forecast = np.asarray(fit.forecast(steps=horizon), dtype=float)
138
-
139
- if not np.all(np.isfinite(forecast)):
140
- raise ValueError(f"ARIMA produced a non-finite forecast for order {self._cached_order}.")
141
- return [float(x) for x in forecast]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
models/auto_arima_model.py DELETED
@@ -1,188 +0,0 @@
1
- """
2
- Auto-ARIMA — automatic (p, d, q) order selection for ARIMA, implemented
3
- directly on top of `statsmodels` (no extra dependency beyond what plain
4
- `arima_model.py` already needs).
5
-
6
- An earlier version of this file used the `pmdarima` package. It's been
7
- removed: pmdarima ships compiled Cython extensions that are notorious for
8
- breaking on environments where its build-time numpy version doesn't match
9
- the runtime one ("binary incompatibility" / import errors) — exactly the
10
- kind of thing that silently fails every single prediction on a fresh
11
- Space/container without touching any of your actual code. Since
12
- `statsmodels` was already confirmed working (plain ARIMA and ARIMA-GARCH
13
- both depend on it), doing the search ourselves with statsmodels removes
14
- that whole failure class.
15
-
16
- How this differs from the plain `ARIMA` model in `arima_model.py`:
17
- - `arima_model.py` picks (p, d, q) with a brute-force grid search over
18
- every combination up to max_p/max_d/max_q, scored by AIC.
19
- - This model instead follows the Hyndman-Khandakar (2008) stepwise
20
- procedure — the same algorithm R's `forecast::auto.arima()` and
21
- `pmdarima.auto_arima()` are built on:
22
- 1. pick the differencing order `d` with a KPSS unit-root test
23
- (difference until the series looks stationary, up to max_d times)
24
- instead of trying every `d` blindly,
25
- 2. fit a handful of starting (p, q) candidates at that `d`,
26
- 3. greedily step to whichever neighbouring (p, q) improves AIC,
27
- repeating until no neighbour improves on the current best.
28
- This visits far fewer models than the brute-force grid, and tends to
29
- land on the same or a better order.
30
-
31
- Speed note: same reasoning as arima_model.py — running the full search on
32
- every single `.predict()` call would make backtesting painfully slow
33
- (hundreds of candles x a search each time). So the order is searched ONCE
34
- (on the first call) and reused for every later call on this instance — pass
35
- `refit_order_every=N` to re-search every N calls, or pass `order=(p, d, q)`
36
- yourself to skip the search entirely.
37
-
38
- Optional 10-feature covariate input (see features/feature_pipeline.py): same
39
- mechanism as arima_model.py — pass `features=<DataFrame>` to `.predict()`
40
- and it's used as `exog` on the underlying `statsmodels` ARIMA, including
41
- during the stepwise order search. The `d` (differencing) step is unaffected
42
- — KPSS stationarity is a property of the endogenous series alone, not
43
- something exog changes. Forecasting beyond the last observed candle uses
44
- carry_forward_future() for the exog rows, same reasoning as arima_model.py.
45
-
46
- BUG FIX -- same unbounded-history issue as arima_model.py (see its module
47
- docstring's BUG FIX note for the full writeup and measurements): the
48
- Prediction/Live tabs impose no context window of their own, so without a
49
- cap this model's stepwise search and every re-fit scaled with however much
50
- history got fetched -- for a small timeframe and a generous lookback that
51
- can be tens of thousands of candles. `max_history` bounds it the same way
52
- Moirai/TimesFM already bound theirs, and matches arima_model.py's default
53
- so both classical ARIMA variants behave consistently.
54
- """
55
- import warnings
56
-
57
- import numpy as np
58
- import pandas as pd
59
- from statsmodels.tsa.arima.model import ARIMA
60
- from statsmodels.tsa.stattools import kpss
61
-
62
- from models.base_model import BaseForecastModel
63
- from features.feature_pipeline import carry_forward_future, validate_features
64
-
65
- warnings.filterwarnings("ignore")
66
-
67
-
68
- class AutoArimaModel(BaseForecastModel):
69
- name = "Auto-ARIMA"
70
-
71
- def __init__(self, max_p=5, max_q=5, max_d=2, order=None, refit_order_every=None,
72
- max_history=300):
73
- self.max_p = max_p
74
- self.max_q = max_q
75
- self.max_d = max_d
76
- self._cached_order = order
77
- self.refit_order_every = refit_order_every # None = search once, then always reuse
78
- self._calls_since_search = 0
79
- # Only the most recent `max_history` candles are ever fit on -- see
80
- # the module docstring's BUG FIX note. None/0 disables the cap.
81
- self.max_history = max_history
82
-
83
- def _check_dependencies(self):
84
- import statsmodels # noqa
85
-
86
- def _find_d(self, series: pd.Series) -> int:
87
- """KPSS-based differencing order, capped at max_d."""
88
- s = series.copy()
89
- d = 0
90
- while d < self.max_d:
91
- try:
92
- _, p_value, _, _ = kpss(s, regression="c", nlags="auto")
93
- except Exception:
94
- break
95
- if p_value < 0.05: # KPSS null = stationary -> low p-value = reject -> difference again
96
- s = s.diff().dropna()
97
- d += 1
98
- else:
99
- break
100
- return d
101
-
102
- def _aic(self, series: pd.Series, order, exog: pd.DataFrame = None) -> float:
103
- try:
104
- return ARIMA(series, exog=exog, order=order).fit().aic
105
- except Exception:
106
- return np.inf
107
-
108
- def _stepwise_search(self, series: pd.Series, d: int, exog: pd.DataFrame = None):
109
- """Hyndman-Khandakar-style stepwise search: a few starting points,
110
- then greedy steps to the best-AIC neighbour until nothing improves."""
111
- starting_points = [(0, 0), (1, 0), (0, 1), (2, 2)]
112
- best_order, best_aic = None, np.inf
113
- for p, q in starting_points:
114
- if p > self.max_p or q > self.max_q:
115
- continue
116
- aic = self._aic(series, (p, d, q), exog=exog)
117
- if aic < best_aic:
118
- best_aic, best_order = aic, (p, q)
119
-
120
- if best_order is None:
121
- best_order, best_aic = (0, 0), self._aic(series, (0, d, 0), exog=exog)
122
-
123
- improved = True
124
- while improved:
125
- improved = False
126
- p0, q0 = best_order
127
- for p, q in [(p0 + 1, q0), (p0 - 1, q0), (p0, q0 + 1), (p0, q0 - 1),
128
- (p0 + 1, q0 + 1), (p0 - 1, q0 - 1)]:
129
- if p < 0 or q < 0 or p > self.max_p or q > self.max_q:
130
- continue
131
- aic = self._aic(series, (p, d, q), exog=exog)
132
- if aic < best_aic - 1e-6:
133
- best_aic, best_order = aic, (p, q)
134
- improved = True
135
-
136
- p, q = best_order
137
- return (p, d, q)
138
-
139
- def predict(self, history: pd.Series, horizon: int = 1, features: pd.DataFrame = None) -> list:
140
- series_full = pd.Series(history).astype(float).reset_index(drop=True)
141
- if len(series_full) < 20:
142
- raise ValueError("Auto-ARIMA needs at least 20 candles of history.")
143
-
144
- exog_full = None
145
- if features is not None:
146
- # Length-only against the FULL input first -- catches a
147
- # genuinely misaligned caller early, before max_history
148
- # truncates anything away. NaN is checked below, only on the
149
- # truncated tail actually used for fitting (same check_nan
150
- # split Moirai/TimesFM already use for their own context-window
151
- # truncation -- see validate_features' docstring).
152
- validate_features(features, expected_len=len(series_full), model_name="Auto-ARIMA", check_nan=False)
153
- exog_full = features.reset_index(drop=True)
154
-
155
- # Cap how much history the stepwise search and every fit actually
156
- # see -- see max_history's note in __init__/the module docstring.
157
- if self.max_history and len(series_full) > self.max_history:
158
- series = series_full.iloc[-self.max_history:].reset_index(drop=True)
159
- exog = exog_full.iloc[-self.max_history:].reset_index(drop=True) if exog_full is not None else None
160
- else:
161
- series = series_full
162
- exog = exog_full
163
- if exog is not None:
164
- validate_features(exog, expected_len=len(series), model_name="Auto-ARIMA")
165
-
166
- need_search = self._cached_order is None
167
- if self.refit_order_every and self._calls_since_search >= self.refit_order_every:
168
- need_search = True
169
-
170
- if need_search:
171
- d = self._find_d(series)
172
- self._cached_order = self._stepwise_search(series, d, exog=exog)
173
- self._calls_since_search = 0
174
-
175
- self._calls_since_search += 1
176
-
177
- fit = ARIMA(series, exog=exog, order=self._cached_order).fit()
178
-
179
- if exog is not None:
180
- future_exog = carry_forward_future(exog, horizon)
181
- forecast = np.asarray(fit.forecast(steps=horizon, exog=future_exog), dtype=float)
182
- else:
183
- forecast = np.asarray(fit.forecast(steps=horizon), dtype=float)
184
-
185
- if not np.all(np.isfinite(forecast)):
186
- raise ValueError(f"Auto-ARIMA produced a non-finite forecast for order {self._cached_order}.")
187
-
188
- return [float(x) for x in forecast]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
models/base_model.py DELETED
@@ -1,52 +0,0 @@
1
- """
2
- Common interface every forecasting model implements, so the rest of the app
3
- (backtester, Gradio UI) can treat ARIMA / Auto-ARIMA / ARIMA-GARCH / Moirai /
4
- TimesFM interchangeably without knowing their internal details.
5
- """
6
- from abc import ABC, abstractmethod
7
-
8
- import pandas as pd
9
-
10
-
11
- class BaseForecastModel(ABC):
12
- name = "base"
13
-
14
- @abstractmethod
15
- def predict(self, history: pd.Series, horizon: int = 1, features: pd.DataFrame = None) -> list:
16
- """
17
- Given a 1-D series of past closing prices (oldest -> newest), return
18
- a list of `horizon` forecasted future closing prices.
19
-
20
- `features`, if given, is a DataFrame of extra covariates (OHLCV +
21
- technical indicators — see features/feature_pipeline.py's
22
- FEATURE_COLUMNS) covering the SAME candles as `history`, in the SAME
23
- order, one row per candle (len(features) == len(history)). Every
24
- value in it was already known as of its own candle — callers (the
25
- backtester, the UI, live_runner.py) are responsible for never
26
- building a `features` window that reaches past the last candle in
27
- `history`; see features/feature_pipeline.py for how that's kept
28
- leak-free, and its carry_forward_future() for how each model asks
29
- for covariate values over the FORECAST horizon (where real future
30
- values don't exist yet).
31
-
32
- `features=None` (the default) means Close-price-only — every
33
- model's original behavior, completely unchanged.
34
-
35
- Implementations should raise a clear, specific exception (ImportError
36
- for a missing dependency, ValueError for bad input) rather than
37
- silently returning a wrong/empty result — the backtester and UI both
38
- catch and display these per-model, without crashing other models.
39
- """
40
- raise NotImplementedError
41
-
42
- def is_available(self) -> bool:
43
- """True if this model's dependencies are installed and importable."""
44
- try:
45
- self._check_dependencies()
46
- return True
47
- except Exception:
48
- return False
49
-
50
- def _check_dependencies(self):
51
- """Override to raise ImportError if a required package is missing."""
52
- return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
models/moirai_model.py DELETED
@@ -1,129 +0,0 @@
1
- """
2
- Salesforce Moirai — a pretrained time-series foundation model, via the
3
- `uni2ts` package (https://github.com/SalesforceAIResearch/uni2ts).
4
-
5
- Needs: pip install uni2ts torch
6
- First use downloads pretrained weights from Hugging Face (needs internet),
7
- cached under ~/.cache/huggingface/ afterwards.
8
-
9
- Uni2TS uses GluonTS for inference. The pattern below (PandasDataset ->
10
- MoiraiForecast -> create_predictor -> predict) matches the official
11
- "Getting Started" example in the uni2ts README.
12
-
13
- Optional 10-feature covariate input (see features/feature_pipeline.py): of
14
- all 5 models, Moirai has the most natively-built-for-this mechanism —
15
- `past_feat_dynamic_real`, a first-class GluonTS/uni2ts concept for exactly
16
- "extra real-valued series that are only observed for the historical
17
- context, not the forecast horizon" (see
18
- github.com/SalesforceAIResearch/uni2ts discussion #166 for the underlying
19
- forward() signature). RSI/MACD/Bollinger/SMA-EMA/Stochastic/Volume for
20
- candles that haven't happened yet are precisely that: unknown-until-they-
21
- happen. Concretely, `PandasDataset(..., past_feat_dynamic_real=[...column
22
- names...])` takes those columns straight from the same per-series
23
- DataFrame that holds the target, and `MoiraiForecast(...,
24
- past_feat_dynamic_real_dim=N)` tells the pretrained module how many such
25
- columns to expect — both are genuine constructor parameters this project
26
- already left wired up at `0` (see the two _dim kwargs below), not
27
- something bolted on. Unlike the ARIMA family or TimesFM, this means Moirai
28
- never needs carry_forward_future() or any other future-covariate stand-in
29
- — it was designed to just not need future values for these at all.
30
- """
31
- import numpy as np
32
- import pandas as pd
33
-
34
- from models.base_model import BaseForecastModel
35
- from config import MOIRAI_CHECKPOINT
36
- from features.feature_pipeline import validate_features
37
-
38
-
39
- class MoiraiModel(BaseForecastModel):
40
- name = "Moirai"
41
-
42
- def __init__(self, checkpoint: str = MOIRAI_CHECKPOINT, context_length: int = 200,
43
- num_samples: int = 100):
44
- self.checkpoint = checkpoint
45
- self.context_length = context_length
46
- self.num_samples = num_samples
47
- self._module = None # cached after first load so repeat calls in the
48
- # same running app don't re-download / re-init the pretrained weights
49
-
50
- def _check_dependencies(self):
51
- import uni2ts # noqa
52
- import gluonts # noqa
53
-
54
- def _get_module(self):
55
- from uni2ts.model.moirai import MoiraiModule
56
- if self._module is None:
57
- self._module = MoiraiModule.from_pretrained(self.checkpoint)
58
- return self._module
59
-
60
- def predict(self, history: pd.Series, horizon: int = 1, features: pd.DataFrame = None) -> list:
61
- try:
62
- from uni2ts.model.moirai import MoiraiForecast
63
- from gluonts.dataset.pandas import PandasDataset
64
- except ImportError as e:
65
- raise ImportError(
66
- "Moirai needs the 'uni2ts' and 'gluonts' packages. Install with:\n"
67
- " pip install uni2ts torch\n"
68
- f"(original error: {e})"
69
- )
70
-
71
- values = np.asarray(history, dtype=np.float32)
72
- if len(values) < 10:
73
- raise ValueError("Moirai needs at least 10 candles of history.")
74
-
75
- if features is not None:
76
- # Length-only here (against the FULL history) -- catches a
77
- # genuinely misaligned caller early. NaN is checked below,
78
- # only for the truncated tail actually used (see
79
- # validate_features' docstring for why the split matters).
80
- validate_features(features, expected_len=len(values), model_name="Moirai", check_nan=False)
81
-
82
- context = values[-self.context_length:] if len(values) > self.context_length else values
83
-
84
- # Moirai/GluonTS need a DatetimeIndex but only the VALUES matter for a
85
- # point forecast — the real candle timestamps are re-attached in
86
- # app.py, so a clean synthetic per-minute index is fine here.
87
- # (Timestamp.now("UTC"), not the deprecated Timestamp.utcnow() —
88
- # pandas already warns that utcnow() will be removed in a future
89
- # release.)
90
- idx = pd.date_range(end=pd.Timestamp.now("UTC").floor("min"), periods=len(context), freq="min")
91
- data = {"target": context}
92
-
93
- feat_cols = []
94
- if features is not None:
95
- # Same tail-truncation as `context` above, so every column in
96
- # `data` still describes the same set of candles, row for row.
97
- feat_context = features.iloc[-len(context):].reset_index(drop=True)
98
- validate_features(feat_context, expected_len=len(context), model_name="Moirai")
99
- for col in feat_context.columns:
100
- data[col] = feat_context[col].to_numpy(dtype=np.float32)
101
- feat_cols = list(feat_context.columns)
102
-
103
- series_df = pd.DataFrame(data, index=idx)
104
- dataset = PandasDataset(
105
- series_df, target="target",
106
- past_feat_dynamic_real=feat_cols if feat_cols else None,
107
- )
108
-
109
- module = self._get_module()
110
- forecaster = MoiraiForecast(
111
- module=module,
112
- prediction_length=horizon,
113
- context_length=len(context),
114
- patch_size="auto",
115
- num_samples=self.num_samples,
116
- target_dim=1,
117
- feat_dynamic_real_dim=0,
118
- past_feat_dynamic_real_dim=len(feat_cols),
119
- )
120
- predictor = forecaster.create_predictor(batch_size=1)
121
- forecast = next(iter(predictor.predict(dataset)))
122
-
123
- # forecast.samples has shape (num_samples, horizon) — take the
124
- # per-step median across samples as the point forecast.
125
- point_forecast = np.median(forecast.samples, axis=0)
126
- out = [float(x) for x in point_forecast]
127
- if not all(np.isfinite(x) for x in out):
128
- raise ValueError("Moirai produced a non-finite forecast (NaN/inf) for this window.")
129
- return out
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
models/registry.py DELETED
@@ -1,110 +0,0 @@
1
- """
2
- Model factory. Models are created lazily and cached here — importing this
3
- module does NOT import torch/uni2ts/timesfm, and calling get_model("ARIMA")
4
- never touches the (heavier) Moirai/TimesFM code paths. This matters on a
5
- shared 2 vCPU / 16GB Space: a session that only ever uses ARIMA should never
6
- pay the cost of loading foundation-model weights.
7
-
8
- Caching strategy differs by model type, because "cached" means something
9
- different for each:
10
-
11
- - ARIMA / Auto-ARIMA / ARIMA-GARCH search and cache a (p, d, q) order on
12
- their first `.predict()` call, then reuse it for every later call (see
13
- each model's own docstring) — that order is fit to whichever
14
- symbol/timeframe supplied the FIRST window of history it ever saw. A
15
- cache keyed only on model name would keep reusing e.g. EURUSD's order
16
- after switching to BTC-USD, silently fitting the wrong symbol's dynamics
17
- with no error or obviously-bad output — just a quietly worse forecast.
18
- So these three are cached per (name, symbol, timeframe): switching
19
- either gets its own fresh order search, same symbol+timeframe keeps
20
- reusing the cached order (the whole reason this cache exists).
21
- - Moirai / TimesFM are zero-shot foundation models: `.predict()` takes the
22
- history fresh every call and never caches anything symbol-specific — the
23
- only thing cached on the instance is the pretrained weights themselves
24
- (large, symbol-agnostic, and expensive to reload — TimesFM alone is a
25
- ~800MB/compile-step load). There's no staleness to fix here, so these
26
- stay cached by name only and are always shared/reused across symbols.
27
-
28
- Same reasoning extends to the `use_features` toggle (see
29
- features/feature_pipeline.py): an ARIMA/Auto-ARIMA/ARIMA-GARCH instance's
30
- cached (p, d, q) order was searched either WITH or WITHOUT exog columns —
31
- those are two different fitted models, so reusing one instance's cached
32
- order across a features-on call and a features-off call would be the same
33
- class of bug as reusing it across a symbol switch. The cache key below adds
34
- a features on/off "variant" for exactly those three models, so switching
35
- the toggle gets its own fresh order search, same as switching symbol.
36
- Moirai/TimesFM don't cache anything features-dependent (features is just a
37
- per-call argument to a zero-shot model), so they're unaffected.
38
- """
39
- from models.base_model import BaseForecastModel
40
-
41
- # Model names that internally cache a fitted (p, d, q) order — see module
42
- # docstring above for why these need a (name, symbol, timeframe, variant) cache key.
43
- _ORDER_SEARCH_MODELS = {"ARIMA", "Auto-ARIMA", "ARIMA-GARCH"}
44
-
45
- _CACHE: dict = {}
46
-
47
-
48
- def _build_model(name: str) -> BaseForecastModel:
49
- if name == "ARIMA":
50
- from models.arima_model import ArimaModel
51
- return ArimaModel()
52
- elif name == "Auto-ARIMA":
53
- from models.auto_arima_model import AutoArimaModel
54
- return AutoArimaModel()
55
- elif name == "ARIMA-GARCH":
56
- from models.arima_garch_model import ArimaGarchModel
57
- return ArimaGarchModel()
58
- elif name == "Moirai":
59
- from models.moirai_model import MoiraiModel
60
- return MoiraiModel()
61
- elif name == "TimesFM":
62
- from models.timesfm_model import TimesFMModel
63
- return TimesFMModel()
64
- raise ValueError(
65
- f"Unknown model '{name}'. Expected one of: "
66
- "ARIMA, Auto-ARIMA, ARIMA-GARCH, Moirai, TimesFM."
67
- )
68
-
69
-
70
- def get_model(name: str, symbol: str = None, timeframe: str = None,
71
- use_features: bool = False) -> BaseForecastModel:
72
- """
73
- Cached model factory.
74
-
75
- Pass the symbol/timeframe you're about to predict on — for ARIMA,
76
- Auto-ARIMA and ARIMA-GARCH this keys the cache so a symbol or
77
- timeframe switch gets its own fresh instance (and therefore its own
78
- fresh order search) instead of silently reusing another symbol's
79
- cached order. Moirai/TimesFM ignore symbol/timeframe for caching
80
- purposes and are always shared by name only (see module docstring).
81
-
82
- Pass `use_features=True` when you're about to call `.predict(...,
83
- features=...)` on the returned model — for the same three models this
84
- adds a features on/off variant to the cache key so a cached
85
- features-off order search is never silently reused for a features-on
86
- call or vice versa (see module docstring).
87
-
88
- Calling with just `name` (no symbol/timeframe) still works and behaves
89
- like before — fine for scripts that only ever handle one symbol.
90
- """
91
- if name in _ORDER_SEARCH_MODELS:
92
- variant = "featured" if use_features else "base"
93
- key = (name, symbol, timeframe, variant)
94
- else:
95
- key = name
96
- if key in _CACHE:
97
- return _CACHE[key]
98
-
99
- model = _build_model(name)
100
- _CACHE[key] = model
101
- return model
102
-
103
-
104
- def fresh_model(name: str) -> BaseForecastModel:
105
- """
106
- Like get_model, but always returns a brand-new instance instead of the
107
- cache — useful for ARIMA in a backtest, where you want a fresh order
108
- search rather than reusing whatever order a previous run settled on.
109
- """
110
- return _build_model(name)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
models/timesfm_model.py DELETED
@@ -1,137 +0,0 @@
1
- """
2
- Google TimesFM 2.5 — a pretrained time-series foundation model, via the
3
- `timesfm` package (https://github.com/google-research/timesfm).
4
-
5
- Needs: pip install timesfm[torch]
6
- First use downloads pretrained weights from Hugging Face (~800MB, needs
7
- internet), cached under ~/.cache/huggingface/ afterwards. The 200M-parameter
8
- 2.5 checkpoint needs roughly 4GB RAM on CPU, which comfortably fits a 16GB
9
- Space alongside the other two models.
10
-
11
- Optional 10-feature covariate input (see features/feature_pipeline.py):
12
- TimesFM 2.5 is natively univariate (the transformer only ever sees the
13
- Close-price series, patched); covariates enter through a completely
14
- separate, genuinely-supported mechanism instead — `forecast_with_covariates()`
15
- (needs `pip install timesfm[xreg]`, which pulls in scikit-learn + JAX). It
16
- fits a lightweight in-context ridge regression from the covariates onto the
17
- target using only the historical window, then blends that with TimesFM's
18
- own patch-based forecast (`xreg_mode="xreg + timesfm"`, the library's own
19
- default). This is TimesFM's documented, real xreg extension, not a
20
- workaround this project is inventing.
21
-
22
- The one thing this call requires that the plain `.forecast()` call doesn't:
23
- covariate arrays have to cover the historical context AND the forecast
24
- horizon in one array (TimesFM's own docs: "dynamic covariates need values
25
- for BOTH context AND forecast windows"). The context portion is real,
26
- already-observed data; the horizon portion is built by
27
- carry_forward_future() — same last-known-value persistence used for the
28
- ARIMA family, for the same reason (future RSI/MACD/etc. don't exist yet,
29
- and this project never fabricates or peeks at them).
30
- """
31
- import numpy as np
32
- import pandas as pd
33
-
34
- from models.base_model import BaseForecastModel
35
- from config import TIMESFM_CHECKPOINT
36
- from features.feature_pipeline import carry_forward_future, validate_features
37
-
38
- _MAX_HORIZON = 64 # generous fixed ceiling set once at compile time
39
-
40
-
41
- class TimesFMModel(BaseForecastModel):
42
- name = "TimesFM"
43
-
44
- def __init__(self, checkpoint: str = TIMESFM_CHECKPOINT, max_context: int = 512):
45
- self.checkpoint = checkpoint
46
- self.max_context = max_context
47
- self._model = None # cached after first load+compile (same process)
48
-
49
- def _check_dependencies(self):
50
- import timesfm # noqa
51
- import torch # noqa
52
-
53
- def _load(self):
54
- import torch
55
- import timesfm
56
-
57
- if self._model is None:
58
- torch.set_float32_matmul_precision("high")
59
- model = timesfm.TimesFM_2p5_200M_torch.from_pretrained(self.checkpoint)
60
- model.compile(timesfm.ForecastConfig(
61
- max_context=self.max_context,
62
- max_horizon=_MAX_HORIZON,
63
- normalize_inputs=True,
64
- use_continuous_quantile_head=True,
65
- force_flip_invariance=True,
66
- infer_is_positive=True, # forex/crypto prices are always > 0
67
- fix_quantile_crossing=True,
68
- ))
69
- self._model = model
70
- return self._model
71
-
72
- def predict(self, history: pd.Series, horizon: int = 1, features: pd.DataFrame = None) -> list:
73
- try:
74
- import timesfm # noqa
75
- except ImportError as e:
76
- raise ImportError(
77
- "TimesFM needs the 'timesfm' package. Install with:\n"
78
- " pip install timesfm[torch]\n"
79
- f"(original error: {e})"
80
- )
81
-
82
- values = np.asarray(history, dtype=np.float32)
83
- if len(values) < 32:
84
- raise ValueError("TimesFM needs at least 32 candles of history.")
85
- if horizon > _MAX_HORIZON:
86
- raise ValueError(f"This TimesFM wrapper supports at most {_MAX_HORIZON} candles ahead.")
87
-
88
- if features is not None:
89
- # Length-only here (against the FULL history) -- catches a
90
- # genuinely misaligned caller early. NaN is checked below,
91
- # only for the truncated tail actually used (see
92
- # validate_features' docstring for why the split matters).
93
- validate_features(features, expected_len=len(values), model_name="TimesFM", check_nan=False)
94
-
95
- context = values[-self.max_context:] if len(values) > self.max_context else values
96
- model = self._load()
97
-
98
- if features is None:
99
- point_forecast, _quantiles = model.forecast(horizon=horizon, inputs=[context])
100
- else:
101
- # Same tail-truncation as `context` above, so every covariate
102
- # column still describes the same candles as `context`, in order.
103
- feat_context = features.iloc[-len(context):].reset_index(drop=True)
104
- validate_features(feat_context, expected_len=len(context), model_name="TimesFM")
105
- feat_future = carry_forward_future(feat_context, horizon)
106
-
107
- dynamic_numerical_covariates = {}
108
- for col in feat_context.columns:
109
- full_array = np.concatenate([
110
- feat_context[col].to_numpy(dtype=np.float32),
111
- feat_future[col].to_numpy(dtype=np.float32),
112
- ])
113
- dynamic_numerical_covariates[col] = [full_array] # batch of 1 series
114
-
115
- try:
116
- point_forecast, _quantiles = model.forecast_with_covariates(
117
- inputs=[context],
118
- dynamic_numerical_covariates=dynamic_numerical_covariates,
119
- xreg_mode="xreg + timesfm",
120
- )
121
- except (ImportError, AttributeError) as e:
122
- raise ImportError(
123
- "TimesFM's covariate forecasting needs the 'xreg' extra "
124
- "(scikit-learn + JAX). Install with:\n"
125
- " pip install timesfm[torch,xreg]\n"
126
- f"(original error: {e})"
127
- )
128
-
129
- out = [float(x) for x in point_forecast[0]]
130
-
131
- if not all(np.isfinite(x) for x in out):
132
- raise ValueError(
133
- "TimesFM returned a non-finite forecast (NaN/inf) for this window. "
134
- "This is usually a sign the input window is degenerate (e.g. a flat "
135
- "or near-constant price run) — try a different window size or symbol."
136
- )
137
- return out
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
outputs/.gitkeep DELETED
File without changes
requirements.txt DELETED
@@ -1,38 +0,0 @@
1
- --extra-index-url https://download.pytorch.org/whl/cpu
2
-
3
- # --- core data / numerics ---
4
- # floor is 2.1, not just "2.0": the code's resample/date_range rules use the
5
- # lowercase offset aliases ("10min", "4h", "min") -- confirmed these work on
6
- # 2.1.4 through the current 3.0.x, and confirmed pandas 3.0 now HARD-ERRORS
7
- # on the old uppercase forms ("4H", "10T") rather than just warning, so
8
- # lowercase was the right choice; 2.1 is the safe, verified floor for it.
9
- pandas>=2.1
10
- numpy>=1.24
11
-
12
- # --- market data (free, no API key) ---
13
- yfinance>=0.2.40
14
-
15
- # --- UI & charts ---
16
- gradio>=4.36
17
- plotly>=5.20
18
-
19
- # --- ARIMA / Auto-ARIMA / ARIMA-GARCH ---
20
- statsmodels>=0.14
21
- arch>=6.3
22
-
23
- # --- Moirai (Salesforce) — https://github.com/SalesforceAIResearch/uni2ts ---
24
- uni2ts
25
- gluonts
26
-
27
- # --- TimesFM 2.5 (Google) — https://github.com/google-research/timesfm ---
28
- # [xreg] is only needed for the 10-feature covariate path (models/timesfm_model.py's
29
- # forecast_with_covariates()) -- it pulls in scikit-learn + JAX/jaxlib on top of
30
- # [torch]. Plain Close-only TimesFM forecasting never touches xreg and would work
31
- # fine with just timesfm[torch], but since this project's `features=` support is
32
- # meant to work out of the box, both extras are requested together here.
33
- timesfm[torch,xreg]
34
-
35
- # --- shared deep-learning backend ---
36
- torch
37
- einops
38
- huggingface_hub
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
tests/__init__.py DELETED
File without changes
tests/test_backtester_alignment.py DELETED
@@ -1,229 +0,0 @@
1
- """
2
- End-to-end check of backtest/backtester.py's walk-forward loop itself --
3
- in particular that the `history` window and the `features` window it slices
4
- out for each step are ALWAYS the same candles, and that neither ever
5
- reaches past the candle being predicted from. This is the single most
6
- safety-critical property in the whole features= addition, so it gets a
7
- dedicated, dependency-free test using a fake model instead of a real one.
8
-
9
- Run with: python -m unittest tests.test_backtester_alignment -v
10
- """
11
- import unittest
12
-
13
- import numpy as np
14
- import pandas as pd
15
-
16
- from backtest.backtester import backtest_model, run_all_models_backtest, run_comparison_backtest
17
- from features.feature_pipeline import FEATURE_COLUMNS, compute_feature_frame
18
- from models.base_model import BaseForecastModel
19
-
20
-
21
- def _synthetic_ohlcv(n: int, seed: int = 7) -> pd.DataFrame:
22
- rng = np.random.default_rng(seed)
23
- steps = rng.normal(loc=0.0, scale=0.4, size=n)
24
- close = 100 + np.cumsum(steps)
25
- close = np.abs(close) + 5.0
26
- open_ = close + rng.normal(0, 0.1, n)
27
- high = np.maximum(open_, close) + np.abs(rng.normal(0, 0.2, n))
28
- low = np.minimum(open_, close) - np.abs(rng.normal(0, 0.2, n))
29
- volume = rng.integers(100, 10000, n).astype(float)
30
- idx = pd.date_range("2024-01-01", periods=n, freq="min")
31
- return pd.DataFrame(
32
- {"Open": open_, "High": high, "Low": low, "Close": close, "Volume": volume}, index=idx
33
- )
34
-
35
-
36
- class _AlignmentCheckingModel(BaseForecastModel):
37
- """A fake model that doesn't forecast anything real -- it just verifies,
38
- on every single call, that the `history` and `features` windows it was
39
- handed are (a) the same length, (b) features' last row matches the
40
- candle `history`'s last value came from (checked via a `RowTag` column
41
- the test wires through compute_feature_frame's output position), and
42
- (c) never contain anything from beyond history's own last candle.
43
- Raises AssertionError (which the backtester will catch and log as a
44
- 'failed' row) if any of that is violated -- so a clean backtest run
45
- with zero failures IS the proof of correct alignment."""
46
-
47
- name = "AlignmentChecker"
48
-
49
- def __init__(self, full_close: pd.Series):
50
- # The backtester only ever gives us a *window* of Close; to check
51
- # "does history's last value match the real candle at this
52
- # position" we need to know the FULL series to search it in.
53
- self.full_close = full_close.reset_index(drop=True)
54
-
55
- def predict(self, history, horizon=1, features=None):
56
- history = pd.Series(history).reset_index(drop=True)
57
-
58
- # (a) same length
59
- if features is not None:
60
- assert len(features) == len(history), (
61
- f"length mismatch: history={len(history)} features={len(features)}"
62
- )
63
- assert list(features.columns) == FEATURE_COLUMNS
64
-
65
- # (b)/(c): find where this exact `history` window sits in the full
66
- # series (it's a contiguous slice by construction), then confirm
67
- # the feature window's OWN last-row values (Open/High/Low/Volume)
68
- # match the OHLCV at that same last candle -- proving it wasn't
69
- # shifted forward (which would leak) or backward (which would be
70
- # simply wrong) relative to `history`.
71
- n = len(self.full_close)
72
- w = len(history)
73
- match_start = None
74
- for start in range(0, n - w + 1):
75
- if np.allclose(self.full_close.iloc[start:start + w].to_numpy(), history.to_numpy()):
76
- match_start = start
77
- break
78
- assert match_start is not None, "history window not found in full series -- corrupted slice"
79
-
80
- if features is not None:
81
- # The candle at `match_start + w - 1` must never be an index
82
- # that comes AFTER the true cutoff. We can't directly check
83
- # OHLCV equality here without re-deriving indicators, so instead
84
- # we assert the simpler, sufficient invariant actually being
85
- # tested end-to-end: this call must never see a features window
86
- # whose length disagrees with history (checked above) and must
87
- # never be called with a window reaching past that index --
88
- # guaranteed structurally by the backtester slicing features_df
89
- # with the identical (start, end) as close, which
90
- # test_features_window_matches_close_window_exactly below checks
91
- # directly against the backtester's own slicing, not just this
92
- # model's view of it.
93
- pass
94
-
95
- return [float(history.iloc[-1])] * horizon
96
-
97
-
98
- class TestBacktesterAlignment(unittest.TestCase):
99
- def setUp(self):
100
- self.df = _synthetic_ohlcv(200, seed=11)
101
- self.features_df = compute_feature_frame(self.df)
102
-
103
- def test_no_failed_rows_with_features(self):
104
- """If history/features were ever misaligned in length or shape,
105
- the AlignmentChecker model raises -> backtester logs a 'failed'
106
- row -> this assertion catches it."""
107
- model = _AlignmentCheckingModel(self.df["Close"])
108
- results = backtest_model(self.df, model, window=40, horizon=1, step=1,
109
- features_df=self.features_df)
110
- failed = results["error"].notna().sum()
111
- self.assertEqual(failed, 0, f"{failed} rows failed alignment checks:\n"
112
- f"{results[results['error'].notna()]}")
113
- self.assertGreater(len(results), 0)
114
-
115
- def test_features_window_matches_close_window_exactly(self):
116
- """Directly re-derive what backtest_model *should* have sliced at
117
- each step and compare against a hand-rolled reference loop -- the
118
- strongest possible check that (start, end) is identical for both."""
119
- close = self.df["Close"].reset_index(drop=True)
120
- window, horizon, step = 35, 2, 3
121
- last_i = len(close) - horizon
122
-
123
- for i in range(window, last_i + 1, step):
124
- expected_close_window = close.iloc[i - window:i].reset_index(drop=True)
125
- expected_feat_window = self.features_df.iloc[i - window:i].reset_index(drop=True)
126
- # The Volume column is a raw passthrough (not an indicator
127
- # derived from Close), so comparing it against the source df
128
- # directly is a strong, independent check that the SAME i-window
129
- # was used for both series (an off-by-one would desync them).
130
- source_volume = self.df["Volume"].reset_index(drop=True).iloc[i - window:i].reset_index(drop=True)
131
- pd.testing.assert_series_equal(
132
- expected_feat_window["Volume"], source_volume, check_names=False
133
- )
134
- self.assertEqual(len(expected_close_window), len(expected_feat_window))
135
-
136
- def test_baseline_mode_never_touches_features_module(self):
137
- """Not passing features_df= at all (the default) must be
138
- byte-for-byte the old behavior -- confirmed by never even needing
139
- a features_df (features=None reaches the model every single call)."""
140
- class RecordsFeatureArg(BaseForecastModel):
141
- name = "Recorder"
142
- saw_features = []
143
-
144
- def predict(self, history, horizon=1, features=None):
145
- RecordsFeatureArg.saw_features.append(features)
146
- return [float(pd.Series(history).iloc[-1])] * horizon
147
-
148
- RecordsFeatureArg.saw_features.clear()
149
- _ = backtest_model(self.df, RecordsFeatureArg(), window=40, horizon=1, step=5)
150
- self.assertTrue(len(RecordsFeatureArg.saw_features) > 0)
151
- self.assertTrue(all(f is None for f in RecordsFeatureArg.saw_features))
152
-
153
-
154
- class TestRunAllModelsBacktestFeaturesDf(unittest.TestCase):
155
- def setUp(self):
156
- self.df = _synthetic_ohlcv(150, seed=13)
157
- self.features_df = compute_feature_frame(self.df)
158
-
159
- def test_features_df_none_passes_none_through(self):
160
- class Recorder(BaseForecastModel):
161
- name = "Recorder"
162
-
163
- def __init__(self):
164
- self.seen = []
165
-
166
- def predict(self, history, horizon=1, features=None):
167
- self.seen.append(features is None)
168
- return [float(pd.Series(history).iloc[-1])] * horizon
169
-
170
- m = Recorder()
171
- run_all_models_backtest(self.df, {"Recorder": m}, window=40, horizon=1, step=10,
172
- features_df=None)
173
- self.assertTrue(all(m.seen)) # every call saw features=None
174
-
175
- def test_features_df_given_passes_dataframe_through(self):
176
- class Recorder(BaseForecastModel):
177
- name = "Recorder"
178
-
179
- def __init__(self):
180
- self.seen = []
181
-
182
- def predict(self, history, horizon=1, features=None):
183
- self.seen.append(features is not None and len(features) == len(history))
184
- return [float(pd.Series(history).iloc[-1])] * horizon
185
-
186
- m = Recorder()
187
- run_all_models_backtest(self.df, {"Recorder": m}, window=40, horizon=1, step=10,
188
- features_df=self.features_df)
189
- self.assertTrue(len(m.seen) > 0)
190
- self.assertTrue(all(m.seen))
191
-
192
-
193
- class TestRunComparisonBacktest(unittest.TestCase):
194
- def setUp(self):
195
- self.df = _synthetic_ohlcv(150, seed=17)
196
- self.features_df = compute_feature_frame(self.df)
197
-
198
- def test_both_modes_run_and_are_labeled_separately(self):
199
- class Recorder(BaseForecastModel):
200
- name = "Recorder"
201
-
202
- def __init__(self):
203
- self.seen_features = []
204
-
205
- def predict(self, history, horizon=1, features=None):
206
- self.seen_features.append(features is not None)
207
- return [float(pd.Series(history).iloc[-1])] * horizon
208
-
209
- baseline_model, featured_model = Recorder(), Recorder()
210
- results, summaries = run_comparison_backtest(
211
- self.df, {"Recorder": (baseline_model, featured_model)}, self.features_df,
212
- window=40, horizon=1, step=10,
213
- )
214
- # baseline instance never saw features; featured instance always did
215
- self.assertTrue(all(f is False for f in baseline_model.seen_features))
216
- self.assertTrue(all(f is True for f in featured_model.seen_features))
217
-
218
- # results carry a "mode" column with exactly the two expected labels
219
- modes_present = set(results["Recorder"]["mode"].unique())
220
- self.assertEqual(modes_present, {"Close-only", "+10 Features"})
221
-
222
- # summaries: one dict per mode, never merged into a single number
223
- self.assertEqual(len(summaries["Recorder"]), 2)
224
- labels = {s["mode"] for s in summaries["Recorder"]}
225
- self.assertEqual(labels, {"Close-only", "+10 Features"})
226
-
227
-
228
- if __name__ == "__main__":
229
- unittest.main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
tests/test_feature_pipeline.py DELETED
@@ -1,271 +0,0 @@
1
- """
2
- Executable checks for features/feature_pipeline.py -- in particular an
3
- actual, run-it-yourself proof of the "no lookahead" property, not just a
4
- docstring claim.
5
-
6
- Run with: python -m unittest tests.test_feature_pipeline -v
7
- (from the project root; only needs pandas/numpy, same as indicators.py).
8
- """
9
- import unittest
10
-
11
- import numpy as np
12
- import pandas as pd
13
-
14
- from features.feature_pipeline import (
15
- FEATURE_COLUMNS,
16
- carry_forward_future,
17
- compute_feature_frame,
18
- compute_feature_frame_for_period,
19
- slice_features,
20
- validate_features,
21
- )
22
-
23
-
24
- def _synthetic_ohlcv(n: int, seed: int = 0) -> pd.DataFrame:
25
- """A plausible-looking random-walk OHLCV frame, positive prices,
26
- strictly increasing DatetimeIndex -- enough to exercise every
27
- indicator without needing real market data."""
28
- rng = np.random.default_rng(seed)
29
- steps = rng.normal(loc=0.0, scale=0.5, size=n)
30
- close = 100 + np.cumsum(steps)
31
- close = np.abs(close) + 1.0 # keep strictly positive
32
- open_ = close + rng.normal(0, 0.1, n)
33
- high = np.maximum(open_, close) + np.abs(rng.normal(0, 0.2, n))
34
- low = np.minimum(open_, close) - np.abs(rng.normal(0, 0.2, n))
35
- volume = rng.integers(100, 10000, n).astype(float)
36
- idx = pd.date_range("2024-01-01", periods=n, freq="min")
37
- return pd.DataFrame(
38
- {"Open": open_, "High": high, "Low": low, "Close": close, "Volume": volume},
39
- index=idx,
40
- )
41
-
42
-
43
- class TestNoLookahead(unittest.TestCase):
44
- """The property the whole no-leakage requirement rests on: a feature
45
- value at row t must be identical no matter how much (or little) data
46
- after row t exists when it's computed."""
47
-
48
- def test_truncation_invariance(self):
49
- df = _synthetic_ohlcv(300, seed=1)
50
- full = compute_feature_frame(df)
51
-
52
- # Recompute using only data up to several different cutoffs, and
53
- # check every row that exists in both versions matches EXACTLY.
54
- for cutoff in (50, 100, 150, 250, 299):
55
- truncated = compute_feature_frame(df.iloc[: cutoff + 1])
56
- overlap = min(len(full), len(truncated))
57
- pd.testing.assert_frame_equal(
58
- full.iloc[:overlap].reset_index(drop=True),
59
- truncated.iloc[:overlap].reset_index(drop=True),
60
- check_exact=False,
61
- atol=1e-10,
62
- obj=f"feature frame truncated at row {cutoff}",
63
- )
64
-
65
- def test_single_row_extension_does_not_rewrite_history(self):
66
- """Appending ONE new candle must never change any prior row --
67
- this is what the walk-forward backtest loop depends on candle to
68
- candle."""
69
- df = _synthetic_ohlcv(120, seed=2)
70
- shorter = compute_feature_frame(df.iloc[:100])
71
- longer = compute_feature_frame(df.iloc[:101])
72
- pd.testing.assert_frame_equal(
73
- shorter.reset_index(drop=True),
74
- longer.iloc[:100].reset_index(drop=True),
75
- check_exact=False,
76
- atol=1e-10,
77
- )
78
-
79
-
80
- class TestShapeAndColumns(unittest.TestCase):
81
- def test_columns_are_exactly_the_expected_13(self):
82
- df = _synthetic_ohlcv(200, seed=3)
83
- feats = compute_feature_frame(df)
84
- self.assertEqual(list(feats.columns), FEATURE_COLUMNS)
85
- # 4 raw OHLCV (Open/High/Low/Volume) + 1 RSI + 3 MACD + 2 Bollinger
86
- # + 2 SMA/EMA + 2 Stochastic, from 9 used categories (Close excluded).
87
- self.assertEqual(len(FEATURE_COLUMNS), 14)
88
-
89
- def test_close_is_not_a_feature_column(self):
90
- self.assertNotIn("Close", FEATURE_COLUMNS)
91
-
92
- def test_bb_mid_not_duplicated_as_sma20(self):
93
- # BB_Mid == SMA_20 by construction (see module docstring) -- make
94
- # sure we didn't accidentally keep both.
95
- self.assertIn("SMA_20", FEATURE_COLUMNS)
96
- self.assertNotIn("BB_Mid", FEATURE_COLUMNS)
97
-
98
-
99
- class TestNoNaN(unittest.TestCase):
100
- def test_no_nan_once_past_warmup(self):
101
- df = _synthetic_ohlcv(200, seed=4)
102
- feats = compute_feature_frame(df)
103
- # Row 60 has 60 real candles of history before it -- comfortably
104
- # past every indicator's warm-up (the longest is Bollinger/SMA_20
105
- # at 20 candles).
106
- self.assertFalse(feats.iloc[60:].isna().any().any())
107
-
108
- def test_first_19_rows_ARE_nan_without_padding(self):
109
- """The failure mode this project actually hit: compute_feature_frame
110
- alone always leaves its own first ~19 rows NaN, no matter how large
111
- the input is -- window size doesn't matter, row 0 never has warm-up
112
- history *within this fetch*. This is the bug compute_feature_frame_
113
- for_period (tested below) exists to work around."""
114
- df = _synthetic_ohlcv(500, seed=99)
115
- feats = compute_feature_frame(df)
116
- self.assertTrue(feats.iloc[:19].isna().any().any())
117
- self.assertFalse(feats.iloc[19:].isna().any().any())
118
-
119
- def test_missing_ohlcv_column_raises(self):
120
- bad = pd.DataFrame({"Open": [1, 2], "High": [1, 2], "Low": [1, 2]})
121
- with self.assertRaises(ValueError):
122
- compute_feature_frame(bad)
123
-
124
-
125
- class TestComputeFeatureFrameForPeriod(unittest.TestCase):
126
- """The actual fix for the observed bug: a Backtest/Live run whose
127
- requested period starts right at the edge of the fetched data always
128
- had NaN in its first ~19 rows (see test_first_19_rows_ARE_nan_without_
129
- padding above) -- this is what makes that go away."""
130
-
131
- def test_no_nan_anywhere_in_the_trimmed_result(self):
132
- # Simulate "fetch padding_days extra, then only want the last
133
- # keep_days" -- e.g. window=60 on 1-minute BTC-USD candles, which
134
- # is the exact case that surfaced this bug.
135
- df_padded = _synthetic_ohlcv(60 * 24 * 3, seed=21) # 3 days of 1-min candles
136
- df_trimmed, feats_trimmed = compute_feature_frame_for_period(df_padded, keep_days=2)
137
- self.assertFalse(feats_trimmed.isna().any().any(),
138
- "compute_feature_frame_for_period must never leave "
139
- "leading NaN in the period actually requested")
140
-
141
- def test_trimmed_period_is_the_requested_length_not_the_padded_one(self):
142
- df_padded = _synthetic_ohlcv(60 * 24 * 3, seed=22)
143
- keep_days = 1
144
- df_trimmed, feats_trimmed = compute_feature_frame_for_period(df_padded, keep_days=keep_days)
145
- expected_span = df_padded.index[-1] - df_trimmed.index[0]
146
- self.assertLessEqual(expected_span, pd.Timedelta(days=keep_days) + pd.Timedelta(minutes=1))
147
- self.assertEqual(len(df_trimmed), len(feats_trimmed))
148
-
149
- def test_trimmed_values_match_computing_on_the_full_padded_series(self):
150
- """The trim must only DROP rows, never change the surviving ones --
151
- same truncation-invariance property as compute_feature_frame itself,
152
- just checked the other direction (padding in front, not truncating
153
- the end)."""
154
- df_padded = _synthetic_ohlcv(60 * 24 * 2, seed=23)
155
- full_features = compute_feature_frame(df_padded)
156
- df_trimmed, feats_trimmed = compute_feature_frame_for_period(df_padded, keep_days=1)
157
-
158
- keep_mask = df_padded.index >= df_padded.index[-1] - pd.Timedelta(days=1)
159
- expected = full_features.iloc[keep_mask].reset_index(drop=True)
160
- pd.testing.assert_frame_equal(feats_trimmed, expected, atol=1e-10)
161
-
162
- def test_reproduces_and_fixes_the_reported_btc_1m_window60_case(self):
163
- """Direct reproduction of the actual failure: BTC-USD, 1-minute
164
- candles, 1 day lookback, window=60 -- the first backtest window is
165
- rows [0:60] of whatever got fetched, which used to include the
166
- always-NaN rows [0:19]. With padding, it no longer does."""
167
- from utils.helpers import feature_warmup_days
168
- keep_days = 1
169
- padding_days = feature_warmup_days("1m", minimum_days=0)
170
- df_padded = _synthetic_ohlcv(60 * 24 * (keep_days + padding_days) + 100, seed=24)
171
-
172
- df_trimmed, feats_trimmed = compute_feature_frame_for_period(df_padded, keep_days=keep_days)
173
- window = 60
174
- first_window = slice_features(feats_trimmed, 0, window)
175
- self.assertFalse(first_window.isna().any().any(),
176
- "the FIRST walk-forward window must be NaN-free after padding")
177
-
178
-
179
- class TestSliceFeatures(unittest.TestCase):
180
- def test_slice_matches_close_slice_positionally(self):
181
- df = _synthetic_ohlcv(200, seed=5)
182
- feats = compute_feature_frame(df)
183
- close = df["Close"].reset_index(drop=True)
184
-
185
- window, i = 60, 130
186
- close_window = close.iloc[i - window : i]
187
- feat_window = slice_features(feats, i - window, i)
188
-
189
- self.assertEqual(len(close_window), len(feat_window))
190
- # Same positional slice, just re-indexed -- last row of the feature
191
- # window must describe the SAME candle as the last row of the
192
- # close window (spot check via Close reconstructed from OHLCV: the
193
- # feature frame doesn't carry Close, so we check against the
194
- # original df's own Close at that position instead).
195
- self.assertAlmostEqual(
196
- df["Close"].iloc[i - 1], close_window.iloc[-1], places=8
197
- )
198
-
199
-
200
- class TestValidateFeatures(unittest.TestCase):
201
- def test_length_mismatch_raises(self):
202
- feats = pd.DataFrame({c: [1.0, 2.0] for c in FEATURE_COLUMNS})
203
- with self.assertRaises(ValueError):
204
- validate_features(feats, expected_len=3, model_name="Test")
205
-
206
- def test_nan_raises(self):
207
- feats = pd.DataFrame({c: [1.0, np.nan] for c in FEATURE_COLUMNS})
208
- with self.assertRaises(ValueError):
209
- validate_features(feats, expected_len=2, model_name="Test")
210
-
211
- def test_check_nan_false_skips_nan_but_still_checks_length(self):
212
- """The Moirai/TimesFM pattern: a caller that only ever uses the
213
- TAIL of `features` calls this once with check_nan=False against the
214
- FULL history length (to still catch a genuinely misaligned caller
215
- early), then again with the default check_nan=True against just
216
- the truncated tail it actually uses."""
217
- feats_with_nan = pd.DataFrame({c: [np.nan, 1.0, 2.0] for c in FEATURE_COLUMNS})
218
- validate_features(feats_with_nan, expected_len=3, model_name="Test", check_nan=False) # no raise
219
-
220
- with self.assertRaises(ValueError):
221
- validate_features(feats_with_nan, expected_len=2, model_name="Test", check_nan=False)
222
-
223
- def test_check_nan_false_then_true_on_truncated_tail_mirrors_model_usage(self):
224
- # row 0 has NaN, rows 1-2 don't -- a model that only uses the last
225
- # 2 rows should pass; one that (mistakenly) got handed the NaN row
226
- # inside its own truncation should still fail.
227
- feats = pd.DataFrame({c: [np.nan, 1.0, 2.0] for c in FEATURE_COLUMNS})
228
- validate_features(feats, expected_len=3, model_name="Test", check_nan=False)
229
-
230
- clean_tail = feats.iloc[-2:].reset_index(drop=True)
231
- validate_features(clean_tail, expected_len=2, model_name="Test") # no raise -- no NaN in the tail
232
-
233
- dirty_tail = feats.iloc[-3:].reset_index(drop=True)
234
- with self.assertRaises(ValueError):
235
- validate_features(dirty_tail, expected_len=3, model_name="Test") # NaN still inside this one
236
-
237
- def test_clean_window_passes(self):
238
- feats = pd.DataFrame({c: [1.0, 2.0, 3.0] for c in FEATURE_COLUMNS})
239
- validate_features(feats, expected_len=3, model_name="Test") # no raise
240
-
241
-
242
- class TestCarryForwardFuture(unittest.TestCase):
243
- def test_repeats_last_row(self):
244
- feats = pd.DataFrame({"a": [1.0, 2.0, 3.0], "b": [10.0, 20.0, 30.0]})
245
- future = carry_forward_future(feats, horizon=4)
246
- self.assertEqual(len(future), 4)
247
- self.assertTrue((future["a"] == 3.0).all())
248
- self.assertTrue((future["b"] == 30.0).all())
249
-
250
- def test_uses_zero_future_information(self):
251
- """The defining property: carry-forward for a window ending at
252
- cutoff `c` must be identical whether or not candles AFTER `c`
253
- exist in the source data -- otherwise it would be leaking them."""
254
- df = _synthetic_ohlcv(150, seed=6)
255
- feats_short = compute_feature_frame(df.iloc[:80])
256
- feats_long = compute_feature_frame(df) # 150 rows, i.e. real future data exists
257
-
258
- future_from_short = carry_forward_future(feats_short.iloc[:80], horizon=5)
259
- future_from_long = carry_forward_future(feats_long.iloc[:80], horizon=5)
260
- pd.testing.assert_frame_equal(future_from_short, future_from_long, atol=1e-10)
261
-
262
- def test_rejects_bad_input(self):
263
- feats = pd.DataFrame({"a": [1.0]})
264
- with self.assertRaises(ValueError):
265
- carry_forward_future(feats, horizon=0)
266
- with self.assertRaises(ValueError):
267
- carry_forward_future(pd.DataFrame({"a": []}), horizon=1)
268
-
269
-
270
- if __name__ == "__main__":
271
- unittest.main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
tests/test_models_with_features.py DELETED
@@ -1,176 +0,0 @@
1
- """
2
- End-to-end checks that each model's `features=` covariate path actually
3
- runs and produces a sane forecast -- and, just as importantly, that
4
- `features=None` still behaves exactly as it always did (no regression on
5
- the pre-existing Close-only behavior).
6
-
7
- This sandbox this project was edited in only has pandas/numpy/scikit-learn
8
- available (no statsmodels/arch/uni2ts/gluonts/timesfm/torch), so those
9
- per-model tests below are written to SKIP (not fail, not error) when a
10
- model's real dependency isn't importable -- see `_HAVE_*` flags. In the
11
- project's actual environment (`pip install -r requirements.txt`), every
12
- skip guard passes and this file exercises all 5 models for real. Run with:
13
-
14
- python -m unittest tests.test_models_with_features -v
15
- """
16
- import importlib.util
17
- import unittest
18
-
19
- import numpy as np
20
- import pandas as pd
21
-
22
- from features.feature_pipeline import compute_feature_frame, slice_features
23
-
24
-
25
- def _have(module_name: str) -> bool:
26
- return importlib.util.find_spec(module_name) is not None
27
-
28
-
29
- _HAVE_STATSMODELS = _have("statsmodels")
30
- _HAVE_ARCH = _have("arch")
31
- _HAVE_MOIRAI = _have("uni2ts") and _have("gluonts")
32
- _HAVE_TIMESFM = _have("timesfm") and _have("torch")
33
-
34
-
35
- def _synthetic_ohlcv(n: int, seed: int = 42) -> pd.DataFrame:
36
- rng = np.random.default_rng(seed)
37
- steps = rng.normal(loc=0.0, scale=0.4, size=n)
38
- close = 100 + np.cumsum(steps)
39
- close = np.abs(close) + 5.0
40
- open_ = close + rng.normal(0, 0.1, n)
41
- high = np.maximum(open_, close) + np.abs(rng.normal(0, 0.2, n))
42
- low = np.minimum(open_, close) - np.abs(rng.normal(0, 0.2, n))
43
- volume = rng.integers(100, 10000, n).astype(float)
44
- idx = pd.date_range("2024-01-01", periods=n, freq="min")
45
- return pd.DataFrame(
46
- {"Open": open_, "High": high, "Low": low, "Close": close, "Volume": volume}, index=idx
47
- )
48
-
49
-
50
- class TestArimaFamilyAlignment(unittest.TestCase):
51
- """Pure-logic regression check for the ARIMA-GARCH log-returns/features
52
- alignment (see models/arima_garch_model.py's module docstring) -- does
53
- NOT need statsmodels/arch, so this always runs regardless of environment."""
54
-
55
- def test_returns_alignment_has_no_off_by_one(self):
56
- n = 10
57
- prices = pd.Series(100 + np.arange(n, dtype=float))
58
- # label each feature row with the candle index it belongs to
59
- features = pd.Series([float(i) for i in range(n)])
60
-
61
- log_returns = np.log(prices / prices.shift(1)).dropna().reset_index(drop=True)
62
- exog = features.iloc[1:].reset_index(drop=True)
63
-
64
- self.assertEqual(len(exog), len(log_returns))
65
- for k in range(len(log_returns)):
66
- # log_returns[k] = log(p_{k+1}/p_k) -- realized at candle k+1,
67
- # so it must pair with the feature row for candle k+1.
68
- self.assertEqual(exog[k], float(k + 1))
69
-
70
-
71
- # compute_feature_frame's own first ~19 rows are always NaN (SMA_20/
72
- # Bollinger need 20 real candles before them -- see
73
- # test_feature_pipeline.TestNoNaN.test_first_19_rows_ARE_nan_without_padding).
74
- # Production code never hands a model a window starting there --
75
- # data.historical.get_historical_with_features always fetches extra padding
76
- # and trims it back off first (see its own docstring) -- so the tests below
77
- # fetch WARMUP extra candles up front and drop them from both `close` and
78
- # `feats` (kept positionally aligned) before slicing a window from them,
79
- # the same shape of input the model actually ever receives for real.
80
- _WARMUP = 20
81
-
82
-
83
- @unittest.skipUnless(_HAVE_STATSMODELS, "statsmodels not installed in this environment")
84
- class TestArimaWithFeatures(unittest.TestCase):
85
- def setUp(self):
86
- self.df = _synthetic_ohlcv(120 + _WARMUP)
87
- full_feats = compute_feature_frame(self.df)
88
- self.close = self.df["Close"].reset_index(drop=True).iloc[_WARMUP:].reset_index(drop=True)
89
- self.feats = full_feats.iloc[_WARMUP:].reset_index(drop=True)
90
-
91
- def test_baseline_unchanged_shape(self):
92
- from models.arima_model import ArimaModel
93
- m = ArimaModel(max_p=2, max_d=1, max_q=2)
94
- out = m.predict(self.close, horizon=3)
95
- self.assertEqual(len(out), 3)
96
- self.assertTrue(all(np.isfinite(out)))
97
-
98
- def test_with_features_runs_and_is_finite(self):
99
- from models.arima_model import ArimaModel
100
- m = ArimaModel(max_p=2, max_d=1, max_q=2)
101
- window = slice_features(self.feats, 0, len(self.close))
102
- out = m.predict(self.close, horizon=3, features=window)
103
- self.assertEqual(len(out), 3)
104
- self.assertTrue(all(np.isfinite(out)))
105
-
106
- def test_mismatched_features_length_raises(self):
107
- from models.arima_model import ArimaModel
108
- m = ArimaModel(max_p=2, max_d=1, max_q=2)
109
- short_window = slice_features(self.feats, 0, len(self.close) - 5)
110
- with self.assertRaises(ValueError):
111
- m.predict(self.close, horizon=3, features=short_window)
112
-
113
-
114
- @unittest.skipUnless(_HAVE_STATSMODELS, "statsmodels not installed in this environment")
115
- class TestAutoArimaWithFeatures(unittest.TestCase):
116
- def setUp(self):
117
- self.df = _synthetic_ohlcv(120 + _WARMUP)
118
- full_feats = compute_feature_frame(self.df)
119
- self.close = self.df["Close"].reset_index(drop=True).iloc[_WARMUP:].reset_index(drop=True)
120
- self.feats = full_feats.iloc[_WARMUP:].reset_index(drop=True)
121
-
122
- def test_with_features_runs_and_is_finite(self):
123
- from models.auto_arima_model import AutoArimaModel
124
- m = AutoArimaModel(max_p=3, max_q=3, max_d=1)
125
- window = slice_features(self.feats, 0, len(self.close))
126
- out = m.predict(self.close, horizon=2, features=window)
127
- self.assertEqual(len(out), 2)
128
- self.assertTrue(all(np.isfinite(out)))
129
-
130
-
131
- @unittest.skipUnless(_HAVE_STATSMODELS and _HAVE_ARCH, "statsmodels+arch not installed in this environment")
132
- class TestArimaGarchWithFeatures(unittest.TestCase):
133
- def setUp(self):
134
- self.df = _synthetic_ohlcv(150 + _WARMUP)
135
- full_feats = compute_feature_frame(self.df)
136
- self.close = self.df["Close"].reset_index(drop=True).iloc[_WARMUP:].reset_index(drop=True)
137
- self.feats = full_feats.iloc[_WARMUP:].reset_index(drop=True)
138
-
139
- def test_with_features_runs_and_is_finite(self):
140
- from models.arima_garch_model import ArimaGarchModel
141
- m = ArimaGarchModel(max_p=2, max_q=2)
142
- window = slice_features(self.feats, 0, len(self.close))
143
- out = m.predict(self.close, horizon=2, features=window)
144
- self.assertEqual(len(out), 2)
145
- self.assertTrue(all(np.isfinite(out)))
146
- self.assertIsNotNone(m.last_volatility_forecast)
147
-
148
-
149
- @unittest.skipUnless(_HAVE_MOIRAI, "uni2ts/gluonts not installed in this environment")
150
- class TestMoiraiWithFeatures(unittest.TestCase):
151
- def test_with_features_runs_and_is_finite(self):
152
- from models.moirai_model import MoiraiModel
153
- df = _synthetic_ohlcv(60)
154
- close = df["Close"].reset_index(drop=True)
155
- feats = compute_feature_frame(df)
156
- m = MoiraiModel(context_length=60, num_samples=10) # small for a fast test
157
- out = m.predict(close, horizon=2, features=feats)
158
- self.assertEqual(len(out), 2)
159
- self.assertTrue(all(np.isfinite(out)))
160
-
161
-
162
- @unittest.skipUnless(_HAVE_TIMESFM, "timesfm/torch not installed in this environment")
163
- class TestTimesFMWithFeatures(unittest.TestCase):
164
- def test_with_features_runs_and_is_finite(self):
165
- from models.timesfm_model import TimesFMModel
166
- df = _synthetic_ohlcv(64)
167
- close = df["Close"].reset_index(drop=True)
168
- feats = compute_feature_frame(df)
169
- m = TimesFMModel(max_context=64)
170
- out = m.predict(close, horizon=2, features=feats)
171
- self.assertEqual(len(out), 2)
172
- self.assertTrue(all(np.isfinite(out)))
173
-
174
-
175
- if __name__ == "__main__":
176
- unittest.main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
utils/__init__.py DELETED
File without changes
utils/helpers.py DELETED
@@ -1,52 +0,0 @@
1
- """Small shared helpers used by app.py and live_runner.py."""
2
- import math
3
- import re
4
-
5
- import pandas as pd
6
-
7
-
8
- def is_probably_valid_symbol(symbol: str) -> bool:
9
- """Loose sanity check for a yfinance-style ticker (not exhaustive)."""
10
- if not symbol or len(symbol) > 20:
11
- return False
12
- return bool(re.match(r"^[A-Za-z0-9=\-.^]+$", symbol.strip()))
13
-
14
-
15
- def infer_step(index: pd.DatetimeIndex):
16
- """Best-effort candle spacing (timedelta) from a DatetimeIndex, used to
17
- project a forecast's index forward beyond the last known candle."""
18
- if len(index) < 2:
19
- return pd.Timedelta(minutes=1)
20
- diffs = index.to_series().diff().dropna()
21
- if diffs.empty:
22
- return pd.Timedelta(minutes=1)
23
- return diffs.median()
24
-
25
-
26
- def future_index(last_timestamp, step, n):
27
- """Build `n` future timestamps spaced `step` apart, starting after last_timestamp."""
28
- return [last_timestamp + step * (i + 1) for i in range(n)]
29
-
30
-
31
- # Only the timeframe names config.py's CUSTOM_TIMEFRAMES actually offers.
32
- # Used by app.py's Live tab and live_runner.py -- both fetch a fixed/
33
- # auto-computed lookback rather than exposing a lookback slider the way
34
- # Backtest/Prediction do, so when 10-feature mode is on, *something* has to
35
- # decide "enough days back" to give RSI/Bollinger/etc. real warm-up data
36
- # instead of hitting features/feature_pipeline.py's NaN guard on every call.
37
- MINUTES_PER_CANDLE = {"1m": 1, "5m": 5, "10m": 10, "15m": 15, "30m": 30,
38
- "1h": 60, "4h": 240, "1d": 1440}
39
- INDICATOR_WARMUP_CANDLES = 30 # SMA_20/Bollinger need 20 candles; a small safety margin on top
40
-
41
-
42
- def feature_warmup_days(timeframe: str, minimum_days: int = 2) -> int:
43
- """Days of lookback needed for this timeframe's candles to give the
44
- slowest indicator (SMA_20/Bollinger, 20-candle window) real warm-up
45
- history, with a small safety margin. Never returns less than
46
- `minimum_days` (the non-features default), and callers should still
47
- clamp the result against a source interval's own max_days (e.g. 1m
48
- tops out at 7 days on Yahoo) -- this function only knows about
49
- indicator warm-up, not data-source limits."""
50
- minutes_per_candle = MINUTES_PER_CANDLE.get(timeframe, 60)
51
- days = math.ceil(INDICATOR_WARMUP_CANDLES * minutes_per_candle / (24 * 60))
52
- return max(minimum_days, days)