12 Commits

Author SHA1 Message Date
Adriano 8292e5e6b8 docs: verify cerbero-bite MCP is MAINNET (real option chain) -> VRP source unlocked
- 3-level check: spot matches certified feed (<0.3%); environment_info=mainnet/testnet=false;
  chain-level decisive: ETH_USDC-26JUN26-1650-P identical bid/ask/IV/delta on ccxt mainnet
  vs Cerbero MCP (25.6/26.6/54.54%/-0.315)
- contamination was the OLD testnet token on get_historical, NOT Cerbero MCP itself
- Cerbero MCP gives real per-strike bid/ask/IV/greeks/OI + regime features ccxt lacks
  (dealer gamma, gamma-flip, OI delta, liquidation, funding) -> usable for VRP validation
- no deep chain history -> multi-year VRP still model-priced, but model-vs-real calibration
  now robust/repeatable. Token used only for verification, never logged/committed.
2026-06-19 22:11:14 +02:00
Adriano 922947d2aa research: verify options sleeve on REAL Deribit quotes (spread+skew haircut)
- options_real_quote_check.py: fetches real weekly BTC put chain, measures premium
  haircut (bid vs BS@DVOL-ATM), re-runs CSP sleeve with real haircut
- KEY FINDING (reverses a prior critique): backtest UNDER-prices the OTM put by using
  ATM DVOL; real skew (+28% gross) exceeds the ~4% bid/ask spread -> real bid premium
  = 1.29x modeled. Sleeve premium is conservative at current (calm) quotes.
- Real risk SHIFTS to the tail + roll-liquidity in stress (skew = market pricing fat
  tail), not premium magnitude. Breakpoint: sleeve dies below ~70% premium capture.
- updated eval diary with the verification
2026-06-19 21:48:12 +02:00
Adriano 69be9eb75f docs: critical eval of external crypto_backtest strategy (trend + options VRP)
- spot sleeve independently confirms our TP01 (12h vol-targeted trend); our multi-horizon
  blend is slightly better (Sharpe 1.32 vs their 0.77 window / 1.07 full)
- options sleeve (75% weight) = the most promising lead to break the ~1.3 Sharpe ceiling
  (adds VRP, a different return source) BUT priced on modeled IV (DVOL+BS, no real bid/ask,
  ATM vol on OTM puts ignoring skew, tail under-modeled, leverage no funding, window bias)
  -> headline blend Sharpe 1.21 must be discounted until validated on real option quotes
- next steps: real Deribit quotes/skew, crash-week stress, testnet paper trading
2026-06-19 21:43:08 +02:00
Adriano 58fc10de77 research tracks H+I: volume/vol/range + alt-momentum/reversal (both NEGATIVE for alpha)
- trackH volume_vol: no uncorrelated additive edge; profitable signals are trend-in-disguise
  (corr 0.6-0.75); MR/declining-volume fade dead even at fee 0; OBV-up filter is a defensive
  DD overlay only (13.3->10.1% DD but -CAGR), not new alpha
- trackI momentum/reversal: no formulation beats 1-3-6m sign-blend OOS on both assets;
  z-score continuous momentum = same edge (corr 0.96), lower DD 8.4% but lower CAGR;
  long-horizon reversal not bankable (negative/flat standalone). ~1.3 Sharpe ceiling holds.
- TP01 (12h sign-blend) remains the deployable winner
2026-06-19 21:22:49 +02:00
Adriano eac2aa1d00 audit+fix: anti-look-ahead audit, migrate deployable config to >=12h
- trackD_lookahead_audit.py: relabel test (left==right, no labeling leak) + execution-lag
  stress -> our trend pipeline is CLEAN (4h Sharpe 1.36 robust to +1 bar lag, label-invariant)
- ADOPT conservative conclusion: deploy at 12h (sub-12h: costs/overfit dominate, slight Sharpe
  bump unreliable). 12h: Sharpe 1.32, DD 13.3%, CAGR 16.2% ~ identical and robust
- trend_portfolio: DEPLOY_TF=12h, resample_tf(rule); paper trader + tests on 12h
- calendar research (NEGATIVE, both): trackF seasonality (spurious), trackG prior-levels
  (breakouts continue, fade dead; only long-drift survivor, redundant with TP01)
- gitignore data/paper_trend runtime state
2026-06-19 21:13:57 +02:00
Adriano 7b34e11476 docs: update CLAUDE.md with post-research state (TP01 winner + research outcome) 2026-06-19 20:36:01 +02:00
Adriano ae7f3d17f2 deploy: TP01 trend portfolio (PORT LF4h) module + paper trader
- src/strategies/trend_portfolio.py: canonical winner, causal/no-leakage,
  reproduces CAGR +16.5% Sharpe 1.36 maxDD 13.8%
- scripts/live/paper_trend.py: forward-only paper trader, persistent state, resume
- tests/test_trend_portfolio.py: 5 tests (causality, profitability, long-only, paper parity)
2026-06-19 20:35:28 +02:00
Adriano 3b6ff02197 fix: resolution-safe timestamp in trackD_timing resample (pandas 2.x datetime64[ms])
- combination study: PORT LF4h (BTC+ETH) Sharpe 1.32 DD 12.3% remains best
- RV ETH/BTC market-neutral sleeve is genuinely uncorrelated (~0.05) but too weak
  (Sharpe 0.27) to raise portfolio Sharpe; combining the two TF configs is redundant
  (same-asset cross-config corr 0.80)
2026-06-19 20:18:03 +02:00
Adriano 8dbdadd509 research: add per-year trade counts + turnover to trackD_timing 2026-06-19 20:09:32 +02:00
Adriano 33267584d9 research: Track D winner across timeframes (15m-1d) + per-year PnL/DD
- trackD_timing.py: same TSMOM 1-3-6m blend config sampled at 15m/1h/4h/1d
- robust plateau across all TFs; 4h marginally best (LF Sharpe 1.36, DD 13.8%)
- per-year PnL and per-year max drawdown tables
2026-06-19 19:39:02 +02:00
Adriano dc2b5697da research wave 1: 5 honest tracks on certified BTC/ETH + synthesis
- trackA trend, trackB ML, trackC mean-rev, trackD trend-portfolio, trackE xsec/ensemble
- VERDICT: Track D vol-targeted BTC+ETH trend portfolio is the one robust deployable
  earner (Sharpe 1.0-1.32, DD 13-19%, positive every year 2019-2026)
- mean-reversion confirmed dead on clean data; weak-but-real ML/trend residuals
- honest: EUR50/day on 2000 in 1-2y is not reachable (needs ~137k capital or ruinous DD)
2026-06-19 19:14:53 +02:00
Adriano 6b9c469832 research: rebuild certified BTC/ETH feed + honest backtest harness
- rebuilt BTC/ETH from Deribit mainnet (certified 1.7-1.9bps vs Coinbase)
- archived contaminated alt data to Old/data/raw
- add src/backtest/harness.py: leakage-free, fee-aware signal engine
  (entry at close[i], intrabar TP/SL, CAGR/Sharpe/DD/per-year/OOS)
2026-06-19 18:41:15 +02:00
37 changed files with 6230 additions and 1 deletions
+2
View File
@@ -43,3 +43,5 @@ data/games/
# archived data (mirrors top-level data/ ignores, which are top-level-anchored)
Old/data/
Old/**/__pycache__/
.cache_trackE_*.npy
data/paper_trend/
+32 -1
View File
@@ -16,6 +16,30 @@ Cosa è cambiato:
- L'esecuzione è **DISABILITATA**, il conto mainnet è flat. **Non c'è trading live attivo.**
- Si riparte dalla ricerca di strategie NUOVE, su dati certi, con la metodologia qui sotto.
### Ricerca post-reset (2026-06-19) — esito
Prima ondata di ricerca onesta su BTC/ETH certificati (5 track, harness condiviso
`src/backtest/harness.py`). Sintesi in `docs/diary/2026-06-19-research-synthesis.md`.
- **VINCITRICE (l'unica robusta e profittevole): TP01 Trend Portfolio** —
`src/strategies/trend_portfolio.py`. TSMOM multi-orizzonte (1-3-6 mesi) vol-targeted,
50/50 BTC+ETH. Config canonica **PORT LF4h** (4h, long-flat, vol-target 20%, leva cap 2x):
**CAGR ~16.6%, Sharpe ~1.32-1.36, maxDD ~12-14%, positiva ogni anno 2019-2026**.
Robusta su tutti i TF (15m-1d), regge fee fino a 0.40% RT, su entrambi gli asset.
Paper trader: `scripts/live/paper_trend.py`. Test: `tests/test_trend_portfolio.py`.
- **Edge deboli ma reali** (NON standalone, NON migliorano il portafoglio): ML walk-forward
su BTC (Sharpe ~0.57), trend 1h long-short (Sharpe ~1.0), relative-value market-neutral
ETH/BTC (scorrelato ~0.05 ma Sharpe solo 0.27 → troppo debole per alzare lo Sharpe).
- **MORTO/confermato artefatto:** mean-reversion / fade (negativo anche a fee zero su dati
certi — la vecchia libreria +201%/+1238% era pura contaminazione); trend 5m/15m (fee).
- **Soffitto strutturale:** con i soli BTC/ETH lo Sharpe di portafoglio si ferma a **~1.3**.
Combinare TF o aggiungere la RV non aiuta (ridondanza/edge troppo debole).
- **Onestà sul target €50/giorno:** NON raggiungibile su 2000 in 1-2 anni (servono ~130k di
capitale o un DD da rovina). La leva non è la scorciatoia; la via è target-vol + capitale +
tempo. La strategia che *guadagna* esiste, ma a ~+€1.5/giorno su 2000.
Script ricerca: `scripts/research/track{A,B,C,D,E}_*.py` + `trackD_timing.py`.
## Obiettivo
Ricerca: riconoscimento pattern frattali per trading algoritmico su crypto. Target dichiarato
@@ -35,9 +59,13 @@ netto fee, out-of-sample, robusto su griglia, e su dati certificati + liquidi +
src/data/downloader.py → load_data(asset, tf): legge i parquet certificati da data/raw/
src/strategies/base.py → Strategy (ABC), Signal, BacktestResult, YearlyStats
src/strategies/indicators.py → indicatori condivisi (ema, atr, keltner, ...)
src/strategies/trend_portfolio.py → TP01: strategia VINCENTE (PORT LF4h), causale, deployabile
src/fractal/ → indicatori frattali (patterns.py, indicators.py, similarity.py)
src/backtest/engine.py → engine di backtesting riusabile
src/backtest/harness.py → harness ONESTO (load BTC/ETH, backtest_signals no-leakage, OOS)
src/version.py → APP_VERSION (legge il file VERSION)
scripts/research/ → ricerca post-reset: track{A-E}_*.py (trend/ML/MR/portfolio/xsec)
scripts/live/paper_trend.py → paper trader forward-only di TP01 (no esecuzione reale)
scripts/analysis/ → SOLO i tool dati certificati:
rebuild_history.py → (ri)costruisce lo storico da Deribit mainnet (base 5m + resample)
certify_feed.py → certifica il feed (integrità, coerenza resample, spike, cross-venue)
@@ -57,7 +85,10 @@ uv sync # installa dipende
uv run python scripts/analysis/rebuild_history.py --asset BTC ETH # (ri)costruisci storico da Deribit mainnet
uv run python scripts/analysis/certify_feed.py # certifica i feed (locale + cross-venue)
uv run python scripts/analysis/certify_feed.py --local # solo check locali (veloce)
uv run pytest # test (da ripopolare con le nuove strategie)
uv run python scripts/research/trackD_trendport.py # backtest strategia vincente (full report)
uv run python scripts/research/trackD_timing.py # vincitrice su 15m/1h/4h/1d + PnL/DD/trade per anno
uv run python scripts/live/paper_trend.py # avanza il paper trader TP01 (forward-only)
uv run pytest # test
```
```python
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,65 @@
# 2026-06-19 — Cerbero-bite = MAINNET reale: fonte VRP sbloccata
Indagine "cerca dati di cerbero-bite" + verifica mainnet/testnet a tre livelli. Esito: la
contaminazione storica NON era una proprieta' di Cerbero MCP, ma del vecchio token testnet sul
solo endpoint `get_historical`. Il token di cerbero-bite e' mainnet e serve catene opzioni reali.
## Dove sono i dati di cerbero-bite
`/home/adriano/Documenti/Git_XYZ/CerberoSuite/Cerbero_Bite` — bot live (testnet exec, propose-only)
che vende **credit-spread bull-put su ETH**. Dati:
- `data/state.sqlite`: `market_snapshots` (**52 righe, solo 30 apr1 mag 2026**, BTC+ETH) con
`spot, dvol, realized_vol_30d, iv_minus_rv, funding_perp/cross, dealer_net_gamma,
gamma_flip_level, oi_delta_pct_4h, liquidation_long/short_risk, macro_days_to_event`;
`dvol_history` (1 riga); `positions/instructions/decisions` (0 righe, niente trade persistiti).
- `data/log/*.jsonl` (26 apr1 mag 2026): log HTTP, non dump di catena. `strategy.yaml`: golden config.
- **Fonte dati**: Cerbero MCP (`get_instruments` + `get_ticker_batch`) dal gateway
`cerbero-mcp.tielogic.xyz`. NON c'e' storico profondo della catena (solo fetch live/on-demand).
## Verifica mainnet vs testnet (3 livelli)
1. **Spot vs nostra serie certificata** (Deribit mainnet), 2026-04-30 1316h UTC:
BTC cerbero 76.28776.446 vs certificato 76.23776.443 (Δ 0.130.27%); ETH 2.2612.264 vs
2.2562.265 (Δ 0.040.29%). Scarti = rumore intra-barra (snapshot 15-min vs close orario).
NON e' il feed fantasma testnet (che divergeva >3%).
2. **`environment_info`** (token cerbero-bite): `environment=mainnet`, `base_url=www.deribit.com`,
`source=credentials`. **`get_ticker ETH-PERPETUAL`**: `testnet=false`, mark 1703.11.
3. **Catena, decisivo** — stessa opzione su ccxt.deribit mainnet vs Cerbero MCP:
`ETH_USDC-26JUN26-1650-P` (put settimanale, delta ~-0.28):
| fonte | bid | ask | mark_iv | delta | testnet |
|---|---|---|---|---|---|
| ccxt mainnet | 25.6 | 26.6 | 54.54% | -0.3150 | — |
| Cerbero MCP | 25.6 | 26.6 | 54.54% | -0.31513 | False |
**Identici bit-per-bit.**
## Verdetto
- **Il token MCP di cerbero-bite e' MAINNET; la sua catena opzioni e' reale** (= ccxt.deribit
mainnet). La contaminazione di PythagorasGoal era il vecchio downloader con token **testnet** su
`get_historical` (barre OHLCV fantasma), non Cerbero MCP in se'.
- **Fonte VRP sbloccata**: Cerbero MCP da' bid/ask/IV/greche/OI per-strike (come ccxt) **+** feature
di regime che ccxt non ha (`dealer_net_gamma`, `gamma_flip_level`, `oi_delta_pct_4h`,
`liquidation_*`, `funding`, `iv_minus_rv`, `macro`). Utile per validare lo sleeve VRP su piu'
regimi (raccolta snapshot live + accumulo nel tempo).
- **Limite residuo**: niente storico profondo della catena -> il backtest pluriennale del VRP resta
prezzato da modello (DVOL+BS); ma la calibrazione model-vs-reale e' ora robusta e ripetibile
(snapshot reali su piu' date/regimi).
## Collegamento col lavoro VRP (sleeve opzioni)
Conferma e rafforza `2026-06-19-eval-crypto-backtest-options.md`: lo snapshot ccxt aveva gia'
mostrato che il backtest SOTTOSTIMA il premio (skew +28% > spread 4% -> bid reale = 1.29x modello).
Ora abbiamo due fonti mainnet concordi (ccxt + Cerbero MCP) per misurare premio/skew/spread su piu'
regimi. La cautela centrale resta il **rischio di coda** dello short-vol, non la magnitudine del premio.
## Stato cerbero-bite (gia' concluso, contesto)
Il credit-spread bull-put ETH e' gia' stato giudicato NON robusto su ciclo completo (diario
`Old/docs/diary/2026-06-09-cerbero-bite-credit-spread.md`: EV breakeven-negativo; "+0.48%/mese" =
artefatto di finestra calma; coda concentrata col fade ETH). E' una struttura diversa dalla
put-selling/wheel del progetto `crypto_backtest`.
> Sicurezza: il token di cerbero-bite e' stato usato solo per la verifica; mai stampato ne' committato
> (resta in `.env`, gitignored).
@@ -0,0 +1,125 @@
# 2026-06-19 — Valutazione strategia esterna `crypto_backtest` (trend + opzioni VRP)
Valutazione critica di un progetto esterno (`/home/adriano/crypto_backtest/`, file chiave
`STRATEGIA.md`, `production.py`, `options_deribit.py`, `production_equity.csv`) che propone un
book a 2 motori quasi scorrelati. Rilevante perché tocca proprio la frontiera che la nostra
ricerca post-reset ha lasciato aperta (le opzioni / volatility risk premium).
## Cosa propone
Portafoglio a due gambe (ρ=0.22 verificato dal CSV):
- **Sleeve 1 (25%)** — trend spot BTC+ETH a **12h**, long-only se `trend(30g)>0`, vol-target 20%,
cap 3×, leva globale ~1.07 calibrata a maxDD in-sample 20%.
- **Sleeve 2 (75%)** — vendita di **put settimanali (CSP/wheel) su BTC** su Deribit, strike a
**delta 0.28**, hold-to-expiry, IV da DVOL reale, prezzo Black-Scholes.
Numeri riprodotti dal CSV (finestra 2021-04→2026-06, 272 settimane):
| Serie | CAGR | Sharpe | maxDD | final |
|---|---|---|---|---|
| spot | +12.0% | 0.77 | 18.1% | 1.80x |
| opt | +15.9% | 1.09 | 20.0% | 2.16x |
| **blend 25/75** | +15.4% | **1.21** | **15.2%** | 2.10x |
| blend ri-levato | +20.5% | 1.21 | 20.0% | 2.63x |
| B&H BTC | +1.3% | 0.30 | 74.2% | 1.07x |
corr(spot, opt) = **0.217** confermata. Settimane peggiori opt: 2022-05 (LUNA) 13%,
2022-06 11%, 2021-05 11%, 2022-11 (FTX) 9.7%.
## Punto forte — corroborazione indipendente del nostro TP01
Lo **sleeve spot è quasi identico al nostro TP01** (`src/strategies/trend_portfolio.py`):
12h, long-only, trend(30g), vol-target 20%, cap 3×. Due ricerche separate, due dataset diversi
(loro Binance, noi Deribit certificato), **stessa conclusione**: il trend vol-targeted a 12h è
l'edge reale e robusto. Il nostro Sharpe è più alto (1.32 vs 0.77 su questa finestra / 1.07
full-history) perché usiamo un **blend multi-orizzonte 1-3-6m** invece del singolo trend a 30g →
il blend diversifica gli orizzonti e alza lo Sharpe. Conferma forte per entrambi.
NB: loro confermano anche le NOSTRE lezioni — intraday ≤1h scartato (costi/rumore), un **bug di
look-ahead sul 4h trovato e corretto** (identico al nostro audit), MR/condor/strangle nudi e
collar stretti scartati per overfit/tail.
## Punto critico — lo sleeve opzioni guida il 75% ma è prezzato dal proprio modello
È esattamente il muro che avevamo dichiarato non-backtestabile (W18/19/21, ARGO: niente storico
chain per-strike gratis). Il loro workaround (BS su **DVOL reale** + payoff sul path realizzato)
fa emergere il VRP perché IV>RV (misurato BTC IV/RV~1.24). Concettualmente sano, ma la
**magnitudine è ottimistica** — limiti (in parte ammessi dagli autori):
1. **Nessun bid/ask**: vendono al mid (BS fair), non al bid. Sulle put OTM settimanali lo spread
è grosso → premio reale nettamente inferiore.
2. **Skew ignorato**: prezzano put a delta-0.28 (OTM) con DVOL = **IV ATM**. Il mercato carica le
put molto di più (skew di crash) → modellano la vol sbagliata proprio sull'opzione venduta.
3. **Coda sotto-modellata**: settimana peggiore solo 13% attraverso LUNA/FTX → sospettosamente
benigno per un venditore di put nudo. Gap, illiquidità di roll e settlement inverso (coin-settled)
sono approssimati.
4. **Leva senza funding** (ottimistico) + **bias di finestra** (parte vicino al top 2021,
favorevole a un book short-vol DD-capped).
Il blend Sharpe 1.21 è dominato dallo sleeve income (Sharpe 1.09, peso 75%). Con bid/ask + skew +
coda realistica lo sleeve income vale plausibilmente molto meno (Sharpe reale stimato ~0.7-0.9),
e il blend scende di conseguenza.
## Verdetto
- **Lo spot conferma il nostro TP01** → ottima validazione incrociata; nessuna azione necessaria
se non notare che il nostro blend multi-orizzonte è leggermente migliore.
- **Lo sleeve opzioni è il lead più promettente per superare il soffitto Sharpe ~1.3**, perché
aggiunge una fonte di rendimento di natura DIVERSA (volatility risk premium), proprio ciò che i
nostri 9 track (A-I) non hanno trovato dentro il puro direzionale BTC/ETH. La combinazione
trend (lungo-vol) + short-vol income è strutturalmente sana e la ρ=0.22 è reale.
- **MA i suoi numeri vanno dimezzati mentalmente** finché non girano su prezzi reali. Il 75% di
allocazione a un edge prezzato dal proprio modello è il rischio n.1.
## Prossimi passi onesti se si vuole inseguire questo lead
1. **Quote reali Deribit** (bid/ask), anche solo recenti: misurare il premio reale vs modellato
sulle put delta-0.28 settimanali, e quanto Sharpe sopravvive allo spread.
2. **Prezzare allo skew vero** (IV della put OTM, non DVOL ATM).
3. **Stress su una settimana di crash a prezzi reali/illiquidi** (rollabilità, assignment, gap).
4. **Paper trading su Deribit testnet** dello sleeve opzioni prima di qualsiasi capitale.
Coerente con la regola del progetto (lezione v2.0.0): un edge full+OOS robusto su prezzi MODELLATI
non è un edge finché non è verificato su prezzi reali ed eseguibili.
---
## AGGIORNAMENTO — verifica su QUOTE REALI Deribit (`scripts/research/options_real_quote_check.py`)
Fatta la verifica concreta (PARTE 1: catena reale Deribit mainnet pubblico; PARTE 2: ri-esecuzione
dello sleeve CSP con haircut reale sul premio). **Risultato che RIBALTA una mia critica.**
Snapshot del 2026-06-19, scadenza settimanale 2026-06-26 (~6.2 DTE), put delta 0.277 (strike 61k,
3.1% OTM), underlying 62.965:
| Grandezza | Valore |
|---|---|
| IV ATM (≈ DVOL) | 37.2% |
| IV put OTM (mark) | 42.1% (**skew +4.8 pt**) |
| premio put: BID / mark / ask | 598 / 623 / 630 USD |
| spread bid/mark | 0.96 (spread ~4%) |
| premio MODELLATO dal backtest (BS @ IV-ATM) | **463 USD** |
| **HAIRCUT premio reale(BID)/modello** | **1.29** |
**Il backtest SOTTOSTIMA il premio, non lo sovrastima.** Prezzando la put OTM con la DVOL (IV ATM)
ignora lo skew (+28% sul premio lordo); il bid/ask la riporta giu' solo del 4% → vendendo al BID
reale incassi **1.29×** il premio modellato. Lo sleeve modellato (Sharpe 1.13) e' quindi
**conservativo sul premio** alle quote attuali; col premio reale salirebbe (Sharpe → 1.83 a f=1.29).
**Ma la critica vera si SPOSTA, non sparisce:** lo skew esiste perche' il mercato prezza la coda
grassa: piu' premio = esattamente perche' i crash fanno male. La sensitivity mostra il punto di
rottura — lo sleeve regge finche' incassi >~85% del premio modellato (Sharpe 0.59 a f=0.85), va a
zero a f=0.70, negativo a f=0.55. Lo snapshot e' in **regime calmo** (IV ATM 37%, bassa per crypto);
in un crash lo spread si allarga molto e potresti non riuscire a rollare. Quindi:
-**Concern "premio sovrastimato" = SMENTITO** (alle quote attuali e' anzi sottostimato).
- ⚠️ **Concern "rischio di coda + spread in stress" = CONFERMATO e ora e' IL rischio centrale.**
Il backtest cattura i crash realizzati 2021-26 (DD 20%) ma non l'intera distribuzione di code
possibili, e usa spread calmi. La f reale in settimana di crash e' < 1 e lo spread esplode.
**Verdetto aggiornato:** lo sleeve income e' piu' solido di quanto temessi sul *premio* (il VRP +
skew e' reale e generoso), ma resta una strategia short-vol il cui rischio vero e' la **coda** e la
**liquidita' di roll nello stress**, non la magnitudine del premio. Prima del capitale: ripetere lo
snapshot nel tempo (specie in regimi di IV alta), misurare lo spread in giornate di stress, e
paper-trade su testnet. Il lead per superare il soffitto Sharpe ~1.3 (aggiungere il VRP a TP01)
resta valido e ora meglio quantificato.
@@ -0,0 +1,63 @@
# 2026-06-19 — Sintesi ricerca post-reset (5 track) e verdetto
Prima ondata di ricerca sui dati **certificati** BTC/ETH (Deribit mainnet, ~2 bps vs
Coinbase USD), con harness onesto condiviso `src/backtest/harness.py` (ingresso eseguibile
a `close[i]`, fee 0.10% RT, exit intrabar TP/SL, OOS/per-anno). Branch
`strategy-research-2026-06`.
## I 5 track
| Track | Famiglia | Esito |
|-------|----------|-------|
| **A** | Trend/Momentum (TSMOM, Donchian, EMA, vol-scaled) | 5m/15m morti (fee); 1h = residuo reale ma celle singole non robuste |
| **B** | ML walk-forward (logistic/GBM su feature di forma) | edge debole ma REALE su BTC (+83% OOS, Sharpe 0.57), ~+0.58 €/d su 2000 |
| **C** | Mean-reversion / range (fade, RSI2, VWAP) | **MORTO** — negativo anche a fee=0. Conferma: la vecchia libreria fade era artefatto |
| **D** | **Trend portfolio vol-targeted BTC+ETH** | ✅ **DEPLOYABLE** — robusto, positivo ogni anno |
| **E** | Cross-sectional BTC↔ETH + ensemble | RV debole (muore a 1.5bps/gamba); ensemble dimezza il DD ma non alza il ritorno |
## Il vincitore: Track D — trend portfolio (l'unico che guadagna in modo robusto)
TSMOM multi-orizzonte (blend 1-3-6 mesi su barre 1h), **vol-targeting** (posizione ∝
1/vol realizzata, target 20% annuo), portafoglio **50/50 BTC+ETH**, fee 0.10% RT. Un solo
set di parametri per entrambi gli asset.
- **LONG-SHORT 50/50:** CAGR +14.2%, **Sharpe 1.00**, maxDD 18.9%, positivo ogni anno 2019-2026.
- **LONG-FLAT 50/50 (migliore risk-adj):** CAGR +15.9%, **Sharpe 1.32**, **maxDD 13.3%**.
- Robusto: plateau di Sharpe ~1.0 su griglia target-vol/leva/orizzonti; regge fee fino a 0.40% RT;
su entrambi gli asset; **non** è un picco fortunato (a differenza delle "star" di Track A).
- Tesi confermata: il valore del trend è **tagliare il drawdown** (B&H DD ~78% → trend DD ~13-19%)
con Sharpe ≥ B&H → si può scalare il rischio (target-vol) e diversificare BTC+ETH.
- Caveat onesto: l'edge è più forte 2018-21 (Sharpe 1.63) che 2022-26 (Sharpe 0.57). Dimensionare
sul regime recente.
## Il verdetto sul target €50/giorno
Una strategia che **guadagna** in modo robusto ESISTE (Track D). Ma il target "€50/giorno
medio partendo da 2000 in 1-2 anni" **non è raggiungibile onestamente**: sono ~2.5%/giorno.
La leva NON è la scorciatoia (alza il DD verso la rovina). La vera leva è **target-vol +
capitale + tempo**:
| target-vol | leva usata | CAGR | Sharpe | maxDD | €/giorno (2k) |
|-----------|-----------|------|--------|-------|---------------|
| 20% | 0.23x | +14% | 1.00 | 19% | +0.73 |
| 40% | 0.45x | +28% | 1.00 | 35% | +3.73 |
| 60% | 0.68x | +40% | 1.00 | 48% | +7.96 |
| 80% | 0.90x | +50% | 0.99 | 60% | +13.78 |
Per **€50/giorno steady-state** servono ~**137k di capitale** (config conservativa, DD~19%),
oppure DD da rovina. Partendo da 2000 a CAGR ~28% (target-vol 40%, DD 35%) il capitale che
genera €50/giorno arriva in ~10-13 anni, non in 1-2.
## Conclusione operativa
1. **Esiste un edge dispiegabile e onesto**: il trend portfolio vol-targeted (Track D).
È il primo risultato robusto post-reset.
2. **Non esiste alcuna scorciatoia** verso €50/giorno su 2000 in 1-2 anni con questi dati
(BTC/ETH 5m-1h). Il limite è strutturale: due asset, alta correlazione, fee.
3. Prossimi passi onesti se si vuole alzare il soffitto: (a) dimensionare Track D a un
target-vol/DD tollerabile e farlo girare in paper, (b) cercare edge di **magnitudine
diversa** (non più diversificazione di edge deboli) — il che richiede dati che oggi non
abbiamo certificati (universo più ampio, microstruttura, funding/opzioni backtestabili).
Script: `scripts/research/track{A,B,C,D,E}_*.py`. Diari di dettaglio: `2026-06-19-track*.md`.
+74
View File
@@ -0,0 +1,74 @@
# 2026-06-19 — Track A: Trend / Momentum su BTC & ETH (dati certificati)
Prima ricerca di strategie NUOVE post-reset (track A = trend/momentum). Tool:
`scripts/research/trackA_trend.py` (harness onesto `src/backtest/harness.py`, fee 0.10% RT,
IS/OOS 65/35, griglia su entrambi gli asset, fee sweep, stress leva). Run:
`uv run python scripts/research/trackA_trend.py`.
## Cosa è stato testato
- **TSMOM** (segno del ritorno N-barre, hold H) long/short e long-only.
- **EMA crossover** (fast/slow) come filtro di trend.
- **Donchian breakout** (entry ONESTO: breakout rilevato con `close[i]`, fill a `close[i]`).
- **Vol-scaled / regime-gated TSMOM** (momentum preso solo se |z| > gate, z = ritorno/vol).
- Griglia ampia su **BTC e ETH**, **1h / 15m / 5m**. 480 celle OOS totali.
Tutto entry-eseguibile: direzione e prezzo decisi con dati ≤ `close[i]`, fill a `close[i]`.
Nessun uso di `returns[i]` (che codifica `close[i+1]`). Hold approssimato come catena di
posizioni non sovrapposte di H barre (la fee si ammortizza su H barre — costo onesto).
## Risultati — la fotografia onesta
**Celle positive OOS per timeframe:**
| TF | celle positive / totali |
|----|----|
| **1h** | 39 / 160 |
| **15m** | **0 / 160** |
| **5m** | **0 / 160** |
**Trend intraday (5m/15m) è MORTO**: lo drag della fee (più trade = più 0.10% RT) annienta
qualsiasi segnale. Drawdown 80-99%, Sharpe da 0.6 a 2.2. Niente da salvare.
**Su 1h** c'è qualche cella positiva, ma il contesto la ridimensiona:
- La finestra **OOS è un singolo regime**: il taglio 65% cade a **set/dic 2023**, quindi
l'OOS è ~2023→2026 (in gran parte toro 2024). Tutto il 2018-2022 (orso 2018, crash 2020,
toro 2021, orso 2022) è IN-SAMPLE. "Positivo OOS" qui ≈ "il trend ha fatto soldi nel toro 2024".
- **Benchmark buy & hold sulla stessa finestra OOS**: BTC **+134%**, ETH **21%**.
- Tutte le `TSMOM_LONG` e metà delle celle BTC fanno **MENO** del B&H → è **beta**, non edge.
- Le poche che battono il B&H lo fanno **solo su ETH** (dove il B&H era negativo): catturano
anche i ribassi. Quello è timing reale — ma vedi sotto.
**Le "star":** VOLSCALED_TSMOM BTC 1h (N=20,H=48,vw=100,z=0.5) = +367% OOS, Sh 0.91, DD 32%,
€/d(2k) +2.56; ETH 1h (N=20,H=48,vw=50,z=1.0) = +197% OOS, Sh 0.60. **MA sono celle fortunate:**
i vicini di griglia crollano (stesso N/H, vw=50 invece di 100 → +21% invece di +367%; z=1.0 → +34%).
Non è un altopiano robusto, è un picco isolato. E il P&L è concentrato nel 2024 (+110% su BTC),
con 2025/2026 deboli o negativi per molte celle.
**Consistenza cross-asset (un edge vero regge su ENTRAMBI):** su 480 celle, solo **2** sono
positive OOS su BTC *e* ETH:
- `TSMOM_LONG 1h N=200 H=48` → ma è long-only ≈ beta (fa meno del B&H su BTC).
- `DONCHIAN 1h N=200 H=12` → l'unico candidato "vero" simmetrico, ma **marginale**:
OOS BTC +9% / ETH +15%, **Sharpe 0.15-0.19**, troppo debole per dispiegarlo.
**Fee sweep / leva:** le star reggono lo sweep 0.0005-0.002 (è 1h, poche operazioni), e lo Sharpe
è invariante alla leva (come deve) — ma la leva 3x porta i DD a 75-91% e affonda le celle marginali.
## Verdetto
**Nessun edge trend/momentum dispiegabile, onestamente, su BTC/ETH oggi.**
- 5m/15m: morti per fee. Chiuso.
- 1h: esiste un **residuo di segnale trend** (le celle che battono il B&H negativo di ETH non sono
solo beta), ma è (a) testato su **un solo regime OOS** (toro 2023-2026), (b) **non robusto** di
griglia (picchi isolati), (c) sull'unica cella simmetrica robusta-su-entrambi (Donchian N=200)
**troppo debole** (Sharpe ~0.17). Sharpe netti ~0.3-0.9 nel caso migliore = sotto la soglia per
rischiare capitale reale.
Conferma la lezione del reset (il superstite storico era trend-following, non mean-reversion): il
trend è la direzione *meno sbagliata*, ma sui dati certi non basta a fare un edge. Coerente con
Track C (mean-reversion = artefatto).
## Prossimi passi possibili (non ancora edge)
- Walk-forward multi-regime (non un singolo taglio 65/35) per stressare Donchian-1h-N200 su orso 2018/2022.
- Trend 1h **con filtro di volatilità/regime più ricco** o portafoglio BTC+ETH per diversificare il
rischio di regime — ma solo se emerge robustezza di griglia, non altri picchi fortunati.
- Restare scettici: finché un trend non è positivo su griglia + su entrambi gli asset + su ≥2 regimi
OOS, **non si dispiega**.
+93
View File
@@ -0,0 +1,93 @@
# 2026-06-19 — Track B: ML / feature-prediction su BTC & ETH (walk-forward onesto)
Esperimento di ricerca sulla direzione **machine-learning** post-reset, su dati Deribit
mainnet certificati (solo BTC/ETH). Tool: `scripts/research/trackB_ml.py` (runnable
`uv run python scripts/research/trackB_ml.py`). Tutto netto fee, strict walk-forward,
held-out tail mai usato per scegliere i config.
## Metodologia (anti look-ahead — la lezione della v2.0.0)
- **Feature** (21): ritorni multi-lag (1/2/3/6/12/24), geometria candela (body/upper/lower
shadow su range, range normalizzato, body lag-1), momentum48 + accelerazione, RSI14,
estensione ATR-normalizzata vs EMA24, vol realizzata 24/72 + ratio, posizione del close
nel range 24/72, z-score del volume. **Tutte backward** (note solo a `close[i]`).
- **Label**: segno del ritorno forward su H barre, `sign(close[i+H]/close[i])`.
- **Strict walk-forward**: per predire il blocco che inizia a `b`, si addestra
scaler+modello SOLO su indici `< b-H` (gap di H → label completamente realizzata nel
passato), finestra rolling delle ultime W barre. Retrain ogni K=250 barre. Mai fit sul
futuro. **Nessun leakage** (verificato: la label più recente del train usa `close[b-1]`).
- **Esecuzione**: entry a `close[i]` nella direzione predetta, hold fino a H barre
(no TP/SL); il no-overlap dell'harness distanzia i trade ≥ H barre.
- **Modello**: `LogisticRegression(class_weight='balanced')`. Soglia di probabilità per
filtrare i segnali deboli (long se p>0.5+thr, short se p<0.5-thr, altrimenti flat).
- **Selezione su DEV** (primo 75%), **conferma una volta sola** sull'held-out tail (ultimo 25%).
- Griglia: W∈{4000,8000,16000}, H∈{6,12,24,48}, thr∈{0,0.03,0.06,0.10}, BTC & ETH, 1h.
Fee-sweep 0.05/0.10/0.15/0.20% RT. Turnover/time-in-market sempre riportati.
## Risultato — esiste un segnale, ma è debole e a basso turnover
**Pattern netto e robusto della griglia**: la positività compare SOLO nelle celle a basso
turnover → **W grande (16000) + H lungo (24) + soglia alta (0.10)**. Tutto ciò che gira
veloce (thr basso, H corto, e soprattutto il **15m**) **muore sulle fee**.
- **15m**: 0/12 celle positive in dev (la migliore 47%, le altre 99%). Stesso win-rate
5256% del 1h, ma il turnover lo polverizza. Conferma di prim'ordine: l'edge per-trade è
minuscolo, sopravvive solo se si tradano poche barre.
- **1h, dev**: 19/96 celle net-positive con Sharpe>0. Famiglie threshold-robuste:
`BTC W16000 H12`, `BTC W8000 H12`, `BTC W16000 H24`, più ETH W16000 H12/H48 marginali.
### Held-out tail (2024→2026, mai toccato in sviluppo)
| config | trades | wr% | net% | Sharpe | DD% | mkt% | €/g(2k) | long% | B&H tail |
|---|---|---|---|---|---|---|---|---|---|
| **BTC W16000 H24 thr0.10** | 333 | 52.9 | **+83.7** | 0.57 | 23 | 12 | **+0.58** | 44 | +3.9% |
| BTC W16000 H12 thr0.10 | 382 | 53.4 | +37.6 | 0.35 | 25 | 7 | +0.26 | 54 | +3.9% |
| ETH W16000 H12 thr0.10 | 364 | 57.7 | +23.7 | 0.24 | 35 | 7 | +0.18 | 68 | 38.4% |
| ETH W16000 H48 thr0.06 | 215 | 55.3 | 13.3 | 0.08 | 64 | 16 | 0.10 | 67 | 38.4% |
**Non è solo beta.** Il B&H sul tail è +3.9% (BTC) e 38.4% (ETH), eppure le celle migliori
fanno +37…+84% (BTC) con **long ~4454%** (bilanciato long/short), e ETH +23.7% **mentre ETH
scendeva 38%** (short corretti). Quindi c'è segnale direzionale genuino, non cattura di trend
rialzista. Payoff asimmetrico: ~53% WR ma avgWin>avgLoss (BTC: +2.04% vs 1.63%).
### Fee-sweep (held-out)
- `BTC W16000 H12 thr0.10`: 0.05%→+66.6 | **0.10%→+37.6** | 0.15%→+13.7 | 0.20%→−6.1.
Sopravvive fino a ~0.15% RT, poi muore. Margine sottile.
- `BTC W8000 H12 thr0.06`: positivo solo a 0.05%, già 35% a 0.10%. Fragile.
- ETH e le celle a turnover medio: muoiono tra 0.10 e 0.15%.
### Stabilità per-anno (full walk-forward, BTC W16000 H24 thr0.10)
`+11% (2020) / +188% (2021) / +14% (2022) / 38% (2023) / +13% (2024) / +75% (2025) / +7% (2026)`,
CAGR full ~22%, ma **DD 56%** e fortissima concentrazione su 2021/2025 con un 2023 a 38%.
## Verdetto onesto — NON deployabile verso l'obiettivo
1. **L'edge è reale ma minuscolo.** A differenza della vecchia libreria (artefatto puro), qui
il segnale sopravvive a strict walk-forward, a fee 0.10% RT e batte il B&H sul tail. È un
risultato genuino e va registrato: la direzione ML **non è morta**.
2. **Ma è incompatibile col target.** €/giorno su €2000 = +0.26…+0.58 baseline (anche la stima
rosea full-WF CAGR 22% → ~€13/g). Il target è **€50/g** → siamo ~100x sotto.
3. **Fragilità**: vive solo a basso turnover (thr alto, H lungo, W grande), DD 2356%,
ritorni concentrati in pochi anni con un anno a 38%, e l'edge si assottiglia già a
0.15% RT. Un singolo cambio di regime lo annulla.
4. **ETH ≠ "specialmente buono"** (contrariamente all'indizio dello shape-ML precedente): qui
ETH è più sottile e più rumoroso di BTC sull'held-out; l'unico merito è aver shortato
correttamente il drawdown 2024-25.
**Conclusione**: la logistic-regression walk-forward su feature di forma+momentum trova un
debole edge **momentum direzionale a basso turnover** su BTC (più tenue su ETH), onesto e
netto-fee, ma **troppo piccolo, troppo concentrato e troppo fee-sensibile** per essere
deployato standalone. Al massimo un **componente** di un futuro ensemble, e solo nelle
configurazioni a bassissimo turnover. Nessun config raggiunge, neanche lontanamente, i €50/g.
## Prossimi passi possibili (non eseguiti)
- Provare **predizione di magnitudine/asimmetria** (large-up vs large-down) e position-sizing
proporzionale alla confidenza, invece del semplice segno.
- **GradientBoosting / feature non lineari** (flag `--gbm` predisposto) — ma attenzione
all'overfit; il rischio è di "trovare" edge che il walk-forward onesto non conferma.
- **Ensemble** del segnale ML a basso turnover con un filtro di regime (vol/trend) per tagliare
il 2023. Ma serve dimostrare che il filtro non è scelto col senno di poi.
- Restare scettici: finché €/g resta ~100x sotto target, l'ML da solo NON è la risposta.
+70
View File
@@ -0,0 +1,70 @@
# 2026-06-19 — Track C: mean-reversion / range re-examination (HONEST) → DEAD
Obiettivo: stabilire rigorosamente se **un qualunque** edge di mean-reversion / range a
breve orizzonte sopravvive su BTC/ETH **certificati** (Deribit mainnet) con **ingresso
eseguibile onesto**, oppure confermarne definitivamente la morte. Entrambi gli esiti sono
validi; nessun risultato forzato.
Tool: `scripts/research/trackC_meanrev.py` (self-contained, runnable), sopra l'harness
onesto `src/backtest/harness.py` (direzione+prezzo decisi con dati ≤ `close[i]`, fill a
`close[i]`, exit intrabar TP/SL da `i+1`, fee netto). Universo: BTC/ETH × {1h,15m,5m}.
## Cosa è stato testato (5 famiglie, ingresso onesto)
- **ZFADE** — Bollinger/z-score fade: `z(close,lookback)`thr → long, ≥ +thr → short.
TP al mean mobile o a `tp_atr·ATR`; SL a `sl_atr·ATR`. **Entry a close[i]**, NON al tocco
della banda (era proprio quello l'artefatto storico).
- **RSI2** — RSI(2) oversold/overbought (+ variante con filtro trend SMA200).
- **RETREV** — return reversal: fade del rendimento cumulato estremo (|z| > thr·σ).
- **VWAP** — reversione sulla distanza dal VWAP rolling (in unità di σ della distanza).
- **SESSION** — autocorrelazione next-bar per ora UTC (descrittivo).
Metodologia applicata: OOS 65/35, griglia parametri su **entrambi** gli asset, fee-sweep
{0, 0.5, 1.0, 1.5, 2.0} bps RT, cross-check liquidità (flat-bar O=H=L=C) e time-in-market.
## Sanity liquidità
Flat-bar O=H=L=C: BTC/ETH 1h ≈ 0.01%, 15m 0.090.14%. Book vivo → l'eventuale edge NON
potrebbe nascondersi in barre ferme (a differenza degli alt archiviati). Confermato pulito.
## Risultati — tutto negativo, su ogni asse
**PASS 1 (screen 1h, fee 0.10% RT):** ogni famiglia OOS negativa su entrambi gli asset.
Es. ZFADE z2/mean: BTC OOS 85%, ETH OOS 83%. RSI2 10/90: BTC 92%, ETH 96%.
RETREV/VWAP idem. Win-rate spesso "alto" (RSI2 ~63%, VWAP ~64%) ma **perde lo stesso**
le poche perdite sono enormi, la reversione non paga il rischio + fee.
**PASS 2 (griglia 1h):** ZFADE **0/18** celle con OOS>0 su entrambi; RSI2 **0/36**. La
cella meno-peggio (ZFADE lookback20 z3) resta BTC 40% / ETH 33% OOS. Nessun sopravvissuto.
**PASS 3 (fee-sweep, incl. fee=0 GROSS):** il colpo decisivo. **Anche a fee=0** (lordo)
la z-fade è negativa: BTC full 74% / OOS 46%, ETH full 98% / OOS 48%. Quindi non è
"morte da fee": **la direzione stessa della fade è sbagliata** sul feed pulito. Salendo le
fee degrada monotòno fino a 100%.
**PASS 4 (timeframe 5m/15m/1h):** più veloce = peggio. A 5m full 100% su entrambi
(41.889 / 38.660 trade), €/giorno su 2000 ≈ 0.70/0.75. Coerente con "molte operazioni =
morte per fee", ma il PASS 3 mostra che il problema è a monte: niente edge nemmeno lordo.
**PASS 5 (sessione UTC):** esiste una **debole** autocorrelazione negativa next-bar in
poche ore (BTC 13h 0.166, 2h 0.154, 21h 0.129; ETH 13h 0.152, 4h 0.117), e una
positiva alle 03h UTC (BTC +0.158, ETH +0.202 = ora "trending"). Struttura reale ma
debolissima (|ρ|≤0.17): non sopravvive a fee + dimensionamento del rischio (lo conferma il
fatto che tutte le versioni *tradate* perdono anche lorde).
## Verdetto
**Nessuna** configurazione MR produce OOS netto>0 su entrambi BTC ed ETH a fee baseline.
Più forte: **a fee zero la fade è già negativa** → l'edge MR storico (+201%/+1238% "OOS")
era un **artefatto del feed contaminato** (wick fantasma testnet + entry su estremi mai
scambiati), non una proprietà del mercato. Sul dato certificato, con ingresso eseguibile,
la mean-reversion a breve orizzonte **non è un edge**: è morta sia lorda che netta.
Coerente con la tesi del reset (`2026-06-19-deribit-history.md`, §3): FADE morto ogni anno.
Track C chiusa come direzione di alpha. La debole struttura intraday-by-hour (PASS 5) è
annotata ma non azionabile da sola; semmai un *filtro* futuro, non una strategia.
## Artefatti
- `scripts/research/trackC_meanrev.py` — riproducibile: `uv run python
scripts/research/trackC_meanrev.py [--quick]` (~40s quick, ~3min full).
+96
View File
@@ -0,0 +1,96 @@
# 2026-06-19 — Track D: Robust walk-forward TREND PORTFOLIO (BTC+ETH), vol-targeted + leverage
Follow-up to Track A. Thesis under test: trend-following's real value in crypto is **drawdown
reduction** vs buy & hold (it sidesteps crashes), and that lower DD lets you apply **leverage** and
**diversify** BTC+ETH into a deployable, risk-adjusted *earning* system — even if each single signal
has modest Sharpe. Tool: `scripts/research/trackD_trendport.py` (run
`uv run python scripts/research/trackD_trendport.py`).
## Method (honest, no look-ahead)
Equity built directly from a **target-position series** (the harness's documented "build your own
equity" path), NOT per-trade chaining:
- `target[i]` decided with data **≤ close[i]**; **held during the next bar** (close[i]→close[i+1]).
- `pnl[t] = target[t-1]·r[t]`, `r[t]=close[t]/close[t-1]-1` — positions **shifted +1 bar** ⇒ no leakage.
- Fees on **turnover**: `0.05%/side·|target[t-1]-target[t-2]|` (0.10% RT baseline; swept 0.100.40% RT).
- **Vol-targeting** (main lever): `target = direction · (target_vol / realized_vol)`, clipped to the
leverage cap. `realized_vol` = annualized rolling std of past bar returns (30d window), ≤ close[i].
- **Portfolio** = 50/50 BTC+ETH net-return series, rebalanced each bar on common timestamps.
Leakage sanity check passed: an *oracle* target using next-bar sign explodes (10^119×) — proving the
engine holds `target[i-1]` over bar `i` — while our signals (TSMOM blend, MA-slope, Donchian) only use
`close[i]` and earlier. Zero-position equity = exactly 1.0.
## What was tested
TSMOM multi-horizon blend (1/3/6-month-equiv on 1h bars), MA-slope (EMA200 slope), Donchian breakout
with trailing channel stop — each vol-targeted, long-short **and** long-flat, per-asset and combined.
Grid: target-vol × leverage-cap × horizon-set; explicit EARLY(2018-21)/LATE(2022-26) split;
fee & leverage sweep; full per-year 2018-2026.
## Results — the honest picture
**1) The thesis holds: massive DD reduction, and diversification helps.**
| Strategy (50/50 port, tvol20%, LS) | CAGR | Sharpe | maxDD | volA |
|---|---|---|---|---|
| **B&H 50/50** | +48% | 0.92 | **77.8%** | 70% |
| TSMOM 1-3-6m blend | +14.2% | **1.00** | **18.9%** | 14% |
| MA-slope | +14.1% | 0.79 | 21.9% | 19% |
| Donchian-trailing | +14.7% | 0.89 | 17.7% | 17% |
Trend cuts maxDD from ~78% to ~18% while keeping a Sharpe **above** buy&hold (1.00 vs 0.92). The
portfolio Sharpe (1.00) **beats both sleeves** (BTC 0.95, ETH 0.75) — diversification works as claimed.
The **long-flat** variant is even cleaner: Sharpe **1.32**, maxDD **13.3%** (no short funding/borrow risk).
**2) It is genuinely robust (not a lucky cell).**
- *Per-year (headline LS):* every full year **positive** 2019-2025 (+19/+36/+19/+6/+2/+14/+4%) and 2026 +8%.
- *Grid:* Sharpe ≈1.00 across **all** target-vol (10-40%) × leverage caps — flat plateau (vol-targeting
just scales). DD scales ~linearly with target-vol (10%→DD10%, 40%→DD35%).
- *Horizon-set:* every subset (1m/3m/6m/1-3m/3-6m/1-2-4m/2-4-8m) is **positive**; Sharpe 0.37→1.39.
Shorter horizons (1m, 1-2-4m) score best (Sharpe 1.34-1.39) — a real plateau, not one combo.
- *Fee:* survives to 0.40% RT (Sharpe 1.00→0.39, still positive at 4× baseline fee).
**3) The honest caveat — most of the edge is the EARLY regime.**
Walk-forward split, same param set both assets:
- **EARLY 2018-2021:** CAGR +26%, Sharpe **1.63**, DD 18%.
- **LATE 2022-2026:** CAGR +7.3%, Sharpe **0.57**, DD 19%.
The signal is real and still net-positive every late year, but its quality **halved** post-2021
(crypto vol compressed, trends choppier). This is the same warning Track A raised, now quantified: the
edge is strongest 2019-2021 and merely *modest* in the 2022-26 regime.
**4) Leverage is a red herring; target-vol is the real dial — and it costs DD linearly.**
At tvol=20% on 60-80% crypto vol, positions stay **sub-1x** (avg gross 0.23×): the leverage cap
**never binds**. To deploy real leverage you raise target-vol; Sharpe stays ~1.0, DD scales:
| target_vol | avg gross | CAGR | Sharpe | maxDD |
|---|---|---|---|---|
| 20% | 0.23× | +14% | 1.00 | 19% |
| 40% | 0.45× | +28% | 1.00 | 35% |
| 60% | 0.68× | +40% | 1.00 | 48% |
| 80% | 0.90× | +50% | 1.00 | 60% |
| 100% | 1.12× | +58% | 0.99 | 69% |
## Verdict — is this a deployable earning system?
**Yes as a risk-adjusted system; NO as a fast path to €50/day on €2000.**
- This is the **first post-reset config that is genuinely robust**: Sharpe ~1.0 (long-flat 1.3),
positive every year 2018-2026, robust across grid/horizon/fee, on both assets, on certified data,
with honest no-look-ahead accounting. It is a real, deployable trend portfolio and a clear
improvement over Track A's lucky single cells. The thesis (DD reduction → leverageable, diversifiable)
is **confirmed**.
- **But the earnings are modest.** Headline (tvol20%, 2x cap, LS): CAGR **+14.2%**, DD 19% ⇒ steady-state
**~€0.73/day on €2000**. To average **€50/day at this CAGR you need ~€137k capital**, not €2000.
- **Leverage can't close the gap cheaply.** Pushing target-vol to 80% gives CAGR ~50% (DD **60%**) — and
at €2000, 50%/yr is still only ~€2.7/day in steady state. Reaching €50/day in 1-2 years from €2000
would require both heavy leverage (DD 60-70%, near-ruin) **and** lucky path — not a sane plan.
- **Regime risk:** the edge is much weaker post-2021 (Sharpe 0.57 LATE). Deploy sized for the LATE
regime, not the EARLY one.
**Recommendation:** treat this as the **core risk engine** (compounding ~14%/yr at DD<20%, or
long-flat ~16%/yr at DD 13%), deployable now at low size to validate live execution. It grows €2000,
but to *€50/day* the lever is **capital + time**, not leverage. Realistic near-term: ~€0.7-1.5/day on
€2000; €50/day needs ~€70-140k or a second uncorrelated edge stacked on top.
## Deliverable
`scripts/research/trackD_trendport.py` — self-contained, prints B&H benchmark, broad scan, grid
robustness, horizon robustness, walk-forward early/late, fee+leverage sweep, headline config per-year,
and the path-to-€50/day table. Reusable building blocks (vol-targeting, target→equity, portfolio).
@@ -0,0 +1,140 @@
# 2026-06-19 — Track E: Cross-sectional BTC↔ETH relative-value + ENSEMBLE synthesis
Due parti, entrambe oneste e su dati Deribit-mainnet certificati (solo BTC/ETH). Tool:
`scripts/research/trackE_xsec_ensemble.py` (runnable, self-contained, riusa il walk-forward
ML di Track B e il Donchian di Track A). Harness onesto: direzione/posizione decise con dati
`close[i]`, realizzo sul bar successivo (shift di 1 barra, niente look-ahead). Fee
turnover-based: `|Δpos|·fee_rt/2` **per gamba** (un flip +1↔−1 = un round-trip = 0.10% RT).
Run: `uv run python scripts/research/trackE_xsec_ensemble.py` (`--quick` salta lo sleeve ML;
`--no-cache` ricalcola la proba ML). Il proba ML viene cacheato (`.cache_trackE_*.npy`).
---
## PART 1 — Relative value (spread BTC↔ETH, 1h, market-neutral)
**Premessa strutturale.** BTC/ETH log-ret 1h sono correlati **0.84**. Con due soli asset
l'unica struttura tradabile è lo **spread**. E con due asset, *"long il più forte / short il
più debole"* (XS-momentum) è **algebraicamente identico** a *"trada il trend del ratio
ETH/BTC"* — infatti nel codice (A) e (B) producono numeri identici. Sono lo stesso edge.
**Lead-lag: nullo.** `corr(rB[i], rE[i+1]) = 0.018`, `corr(rE[i], rB[i+1]) = 0.007`,
autocorrelazioni 0.01..0.02. Nessun potere predittivo cross-asset → lead-lag **non**
perseguito come sleeve (sarebbe rumore moltiplicato per le fee).
**(A/B) XS momentum / ratio trend (griglia N∈{24,72,168,336}, hold∈{6,24,72}):**
- Solo **4/12 celle** OOS net-positive, e sparse (N24/h24, N24/h72, N72/h72, N168/h24).
- Le celle FULL forti (N168/h24: +150% full, Sharpe 0.68, DD 27%) hanno **OOS debole**
(+11%, Sh 0.30). La migliore per OOS-Sharpe è N24/h24 (OOS Sh 0.31, OOS net +11%).
- **Fee sweep (N24/h24):** gross (0bp) FULL +356%/OOS +74% Sh 1.20 → a 1.0bp/gamba FULL +27%/
OOS +11% Sh 0.31 → **muore già a 1.5bp/gamba** (OOS 11%). Margine fee sottilissimo.
- **Per-anno** concentrato sui grandi movimenti del ratio 2020-2021 (e 2024), piatto/negativo
altrove (2022 9%, 2023 19%, 2025 6%, 2026 16%). Non è un altopiano: è un edge debole,
fee-sensibile, regime-dipendente.
**(C) Ratio mean-reversion (z-fade di log(ETH/BTC)):** negativa ovunque (es. lb168/zin2.0:
FULL 85%, OOS 44%, Sh 1.56). Coerente con Track C: anche sullo spread la MR a breve non è
un edge sul dato pulito.
**Verdetto PART 1:** esiste un **debole** edge di relative-value (XS-momentum ≡ ratio-trend),
net-positivo OOS solo in alcune celle, Sharpe OOS ~0.3, che **muore a ~1.5bp/gamba** ed è
concentrato in pochi anni. È **reale ma marginale** — degno di entrare in un ensemble come
sleeve diversificante, non come strategia standalone. La sua virtù: è **quasi scorrelato**
dagli edge direzionali (vedi sotto).
---
## PART 2 — Ensemble (3 sleeve residui in UN portafoglio)
Sleeve combinati (gross 1 ciascuno, equal-weight 1/N → gross totale ~1):
- **S1 = BTC-ML** (Track B, cella onesta a basso turnover W16000 H24 thr0.10, 1h).
- **S2 = BTC-Trend** (Track A, l'unica cella trend robusta cross-asset: Donchian N=200 H=12).
- **S3 = Relative-value** (PART 1, miglior cella OOS: XS-momentum N=24 hold=24).
**Finestra comune attiva** (dove tutti e 3 sono live, dopo il warmup ML): 2020-06 → 2026-06,
52.636 barre.
### Matrice di correlazione degli sleeve (ret per-barra, finestra comune)
| | S2_trend | S3_relval | S1_ml |
|----------|----------|-----------|--------|
| S2_trend | +1.000 | +0.010 | 0.063 |
| S3_relval| +0.010 | +1.000 | 0.010 |
| S1_ml | 0.063 | 0.010 | +1.000 |
**Sleeve quasi perfettamente scorrelati** (|ρ| ≤ 0.06). In teoria, terreno ideale per la
diversificazione.
### Per-sleeve (finestra comune, scala $ uguale)
| sleeve | net | Sharpe | maxDD | €/g(2k) |
|-----------|-------|--------|-------|---------|
| S2_trend | +5% | +0.15 | 34% | +0.04 |
| S3_relval | +8% | +0.16 | 41% | +0.07 |
| **S1_ml** | +382% | **+0.87** | 56% | +3.51 |
### Ensemble
| portafoglio | net | Sharpe | maxDD | CAGR | €/g(2k) |
|----------------------|-------|--------|-------|-------|---------|
| best single (S1_ml) | +382% | +0.87 | 56% | +30% | +3.51 |
| **EQUAL-WEIGHT 1/N** | +109% | **+0.83** | **30%** | +13% | +1.00 |
| inverse-vol (IS wts) | +76% | +0.70 | 29% | +10% | +0.69 |
| EQ-WEIGHT **OOS**(65/35)| +32% | **+1.02** | **12%** | +14% | +0.83 |
Per-anno equal-weight: 2020 +16%, 2021 +50%, 2022 +2%, **2023 13%** (vs 38% dell'ML da
solo!), 2024 +18%, 2025 +19%, 2026 3%. **Molto più liscio**, niente anno-catastrofe.
### La diversificazione aiuta? Sì sul rischio, NO sul rendimento risk-adjusted
- **Sharpe:** ensemble 0.83 vs best-single 0.87 → **non batte** il miglior sleeve singolo.
- **maxDD:** ensemble **30%** vs best-single 56% → **dimezzato**. E OOS 12% vs ML-solo molto
più profondo. Per-anno senza il 38% del 2023.
- **Risk-matched** (levare l'ensemble 1.84x per pareggiare il 56% DD dell'ML): €/g +2.23
contro €/g +3.51 dell'ML da solo → a pari drawdown l'ensemble rende **MENO** (ratio 0.64).
**Perché?** Gli sleeve sono scorrelati ma **enormemente diseguali** (Sharpe 0.87 vs 0.15 vs
0.16). L'equal-weight 1/N "annacqua" l'unico sleeve forte con due deboli: la matematica
della diversificazione alza lo Sharpe solo se gli sleeve sono di *qualità comparabile*. Qui
non lo sono, quindi 1/N non può superare il singolo migliore. Pesare verso l'ML (quality-
weighting) converge banalmente a "esegui solo l'ML" — e sarebbe in-sample.
**Il guadagno vero dell'ensemble è la ROBUSTEZZA, non il rendimento:** stesso Sharpe del
miglior sleeve a **metà del drawdown**, per-anno molto più stabile, niente dipendenza da un
singolo modello/regime (l'ML da solo concentra tutto in 2021/2025 con un 38% nel 2023). Per
chi deve *sopravvivere*, l'ensemble è preferibile; per chi massimizza il rendimento a pari
rischio, l'ML puro vince di un soffio.
---
## Verdetto onesto — è un motore da €50/giorno? NO.
1. **Relative-value:** edge debole, reale ma marginale (Sharpe OOS ~0.3), fee-sensibile
(muore a 1.5bp/gamba), concentrato 2020-2021/2024. Utile **solo** come sleeve scorrelato.
Lead-lag e ratio-MR: nulli/negativi.
2. **Ensemble:** gli sleeve sono **quasi scorrelati** (|ρ|≤0.06) — risultato genuino e bello.
L'ensemble equal-weight ottiene **Sharpe ~0.83 a metà del drawdown** del miglior sleeve e
un per-anno molto più liscio. **Ma NON alza il tetto risk-adjusted** (a pari DD rende meno
dell'ML puro) perché un solo sleeve domina.
3. **Distanza dal target:** ensemble **€1.00/giorno su €2000** (best single €3.51 ma a DD
56% e concentrato). Il target è **€50/giorno → ~50x sotto** (l'ML puro ~14x sotto ma con
rischio/concentrazione inaccettabili). Levare per colmare il gap moltiplica il drawdown
ben oltre il tollerabile (1.84x già porta al 51% DD per ~€2.2/g).
**Conclusione:** la sintesi di Track E conferma la fotografia dei track A/B/C — esistono
**edge residui deboli ma reali e scorrelati** su BTC/ETH. Combinarli in un ensemble **migliora
la robustezza** (DD dimezzato, per-anno stabile, niente single-point-of-failure) ma **non crea
rendimento dal nulla**: il sistema combinato rende ~€1/giorno su €2000, ~50x sotto l'obiettivo,
e non è un motore dispiegabile. Il miglior uso pratico dei risultati: se un giorno si tradasse,
l'ensemble equal-weight (ML + trend + relative-value) è la forma **più onesta e meno fragile**
del poco edge disponibile — ma serve un edge **di un'altra magnitudine** per avvicinare i €50/g.
## Prossimi passi possibili (non eseguiti)
- Cercare uno sleeve **di qualità comparabile all'ML** (Sharpe ≥0.5 indipendente) — solo
allora 1/N alzerebbe lo Sharpe oltre il singolo. Senza, l'ensemble resta solo "risk smoother".
- Relative-value su **timeframe diversi** del ratio (giornaliero?) o con **position sizing**
proporzionale alla forza del segnale, restando scettici sul fee-margin sottile.
- Non aumentare la leva per inseguire €50/g: il DD esplode prima del rendimento.
## Artefatti
- `scripts/research/trackE_xsec_ensemble.py` — riproducibile (`uv run ...`, ~8s con cache ML).
@@ -0,0 +1,77 @@
# Track F — Calendar seasonality (hour-of-day / day-of-week) on BTC & ETH
**Data:** 2026-06-19 · **Script:** `scripts/research/trackF_seasonality.py`
**Dati:** Deribit mainnet certificati, BTC/ETH 1h UTC. Fee baseline 0.10% RT (`fee_side=0.0005`).
## Domanda
Esiste un edge di calendario *sistematico e tradeable* (ora del giorno, giorno della
settimana, interazione ora×giorno) su BTC ed ETH, netto fee, OOS, per-anno, su entrambi gli asset?
## Metodologia (anti-overfit, anti-leakage)
- `ret[i]=close[i]/close[i-1]-1` è noto a `close[i]`; una posizione decisa a `close[i]` guadagna
`ret[i+1]`. La statistica che decide il trade usa **solo barre ≤ i** (mai la barra tradata né futuro).
- **Tradeable test onesto = ADAPTIVE EXPANDING sign**: a `close[i]` guardo il bucket di calendario
della barra `i+1` (il clock è noto, zero look-ahead) e prendo il **segno della media passata** di
quel bucket (espandente, warmup-gated). Long-flat o long-short. Fee solo su `|Δposizione|`.
È l'analogo onesto di "tradare il seasonal": i dati scelgono il segno di ogni bucket **dal vivo**.
- Tabelle descrittive per-ora/per-giorno split IS(65%)/OOS(35%) come diagnostica.
- Regola discreta ottimizzata in-sample (entra a ora H, tieni W barre, dir migliore) mostrata solo
per **esporre il gap IS→OOS** (384 celle testate/asset).
- Benchmark **buy-and-hold** come controllo del long-bias.
## Risultati
### 1. Descrittive (bp/barra, IS vs OOS)
- **Hour-of-day:** sign-agreement IS/OOS solo **12/24 (BTC)** e **8/24 (ETH)** → caso. Le ore "US
close" 21:0022:00 UTC sono positive in entrambi gli split su entrambi gli asset (l'unico pattern
con un minimo di coerenza), ma il resto è rumore che cambia segno tra IS e OOS.
- **Day-of-week:** più stabile. **Giovedì negativo** su BTC ed ETH in IS *e* OOS; Lun/Mer positivi.
Sign-agreement 6/7 (BTC), 5/7 (ETH).
### 2. Adaptive expanding-sign (il test tradeable)
| Strategia | BTC Sharpe | ETH Sharpe | Note |
|---|---|---|---|
| HOUR long-short | **5.39** | **4.04** | DD 100%. Annientata dalle fee. |
| HOUR long-flat | 2.92 | 2.09 | DD 100%. Idem. |
| DOW long-short | +0.64 | +0.83 | DD 8284%, 66% nel 2022 |
| DOW long-flat | +0.81 | +0.96 | DD 7578%, 64/66% nel 2022 |
| HOUR×WEEKDAY (168 buckets) | 5.05 | 3.96 | DD 100%. Overfit puro + fee. |
### 3. Il controllo che smonta il DOW — **buy-and-hold**
- BTC buy-hold: **Sharpe 0.79, CAGR 34.9%, DD 77%** → DOW long-flat: Sh 0.81, CAGR 34.2%, DD 77.5%.
- ETH buy-hold: **Sharpe 0.84, CAGR 42.4%, DD 81%** → DOW long-flat: Sh 0.96, CAGR 52.7%, DD 74%.
- Il DOW long-flat è **long il 78% del tempo** (`mean_pos≈+0.78`). È **buy-and-hold travestito**:
guadagna perché crypto sale, non perché esiste un edge di giorno. Lo "skip del giovedì" aggiunge
pochissimo e non giustifica un deploy.
### 4. Fee sweep (HOUR long-short adaptive)
A fee **0%**: Sh +0.61 (BTC) / +0.80 (ETH) — solo long-drift. A 0.10% RT: **5.4 / 4.0**. Turnover
**~8.000 flip/anno** (segno orario instabile, cambia quasi ogni barra) → morte istantanea per fee.
Le strategie hour-of-day sono ad alta frequenza per costruzione: le fee sono di prim'ordine e le
uccidono.
### 5. Regola discreta ottimizzata in-sample (trappola multiple-testing)
- BTC: best IS H=05 hold=24h dir=+1 → **IS Sh +4.25 → OOS Sh +1.47** (+3.7 bp/trade).
- ETH: best IS H=13 hold=24h dir=+1 → **IS Sh +7.35 → OOS Sh +0.90** (+3.2 bp/trade).
- Collasso IS→OOS classico. Inoltre "hold 24h dir+1" = ancora **long-bias** (entra una volta/giorno
e tiene 24h ≈ sempre long). Il margine OOS (~3 bp/trade su 10 bp RT) è marginale e fragile.
## Multiple-testing
199 celle di calendario/asset (24 ore + 7 giorni + 168 ora×giorno) + 384 (H,W,dir)/asset. Con così
tante celle, bucket "significativi" spuri sono **garantiti**. Filtri applicati: segno scelto dal vivo
su soli dati passati, deve reggere OOS, per-anno, e su **entrambi** BTC ed ETH.
## Verdetto — **SPURIO / NON deployable**
- **Nessun edge di calendario netto-fee robusto** su BTC ed ETH.
- **Hour-of-day:** morto (fee + segno instabile). L'unica regolarità (US-close 2122 UTC positiva) è
troppo debole e non sopravvive al turnover.
- **Day-of-week:** l'unico risultato "positivo" è **long-bias mascherato** (≈ buy-and-hold,
Sharpe ~0.80.96 < trend portfolio 1.32, DD 7584% rovinoso, 65% nel 2022). Non è un edge
seasonal sfruttabile; è esposizione direzionale al drift di crypto.
- **Hour×weekday:** overfit puro (IS 3.6 → OOS 8.0).
- Coerente con la lezione del progetto: dove l'unica "direzione" che funziona è essere long, non c'è
alpha di timing — c'è beta. Il trend portfolio (TP01) cattura quel beta in modo vol-targeted e
con DD ~12%, infinitamente meglio di qualunque regola di calendario qui.
**Azione:** track F chiuso negativo. Non aggiungere nulla al portafoglio. Il soffitto Sharpe ~1.3 su
BTC/ETH regge.
@@ -0,0 +1,85 @@
# Track G — Prior-period level breakouts / range (BTC & ETH, calendar-anchored)
**Data:** 2026-06-19 · **Script:** `scripts/research/trackG_prior_levels.py`
**Harness:** `src/backtest/harness.py` (honest, entry decided at `close[i]`, fill `close[i]`).
## Domanda
Esistono edge net-positivi OOS, robusti su BTC **e** ETH, definiti rispetto a un **periodo
calendario precedente** (giorno/settimana/opening-range)? E soprattutto: i breakout di livello
**continuano** (trend) o **rientrano** (fade)?
## No look-ahead (garanzie)
- Livelli prior-day/week costruiti aggregando a barre giornaliere/settimanali (UTC) e poi
**`shift(1)`** sul frame del periodo *chiuso*: il periodo corrente vede solo il precedente
totalmente chiuso. Mai "oggi"/"questa settimana" nel livello.
- Opening-range usato **solo** sulle barre dopo la chiusura della finestra di apertura.
- Direzione + prezzo decisi a `close[i]`, fill a `close[i]`. Mai entry sul livello esatto intrabar.
- Bug iniziale corretto: mismatch tz-aware vs tz-naive nel mapping dei livelli (dava 0 trade).
## Risultati (1h, fee 0.10% RT, leva 1x, OOS 65/35)
### Continuation vs FADE — il verdetto è netto
| Regola (PD = prior-day) | BTC OOS | ETH OOS | Sharpe OOS |
|---|---|---|---|
| **PD-high CONT (long su rottura max ieri)** | **+25%** | **+16%** | +0.5 / +0.3 |
| PD-high FADE | **68%** | **68%** | 1.6 / 1.2 |
| PD-low CONT (short su rottura min ieri) | 33% | 60% | 0.5 / 0.8 |
| PD-low FADE | 36% | 8% | 0.6 / +0.1 |
- **I breakout CONTINUANO, non rientrano.** Il lato FADE è robustamente **negativo** su entrambi
gli asset (sia high che low), su prior-day, prior-week e opening-range. Conferma diretta della
tesi del reset: la mean-reversion / fade è morta su dati certificati.
- **Asimmetria long-only:** funziona solo la rottura del **massimo** (long), non quella del
**minimo** (short). Cioè non è un edge di breakout *simmetrico/direzione-neutro*: è cattura del
**drift/trend rialzista** del cripto. La PD-low-cont (short sui breakdown) perde perché in questo
campione il cripto sale.
### Grid robustness (PASS 6) — survivor = OOS>0 su ENTRAMBI
- **PD-high CONT: 3/3 celle** (buffer 0/0.1%/0.3%) positive OOS su BTC **e** ETH → robusto al buffer.
- PD-high fade, PD-low cont/fade, OR-fade: **0 survivor**.
- **OR-cont:** positiva solo su ETH, negativa su BTC su tutte le finestre (3/6/8/12h) → artefatto
mono-asset, scartato dalla regola "entrambi".
### Anchor-hour sweep (PASS 5) — non è un'ora fortunata
PD-high cont positiva su **21/24** ore UTC (BTC) e **20/24** (ETH). Non dipende da un singolo
anchor → coerente con un edge reale (ma vedi sotto: è beta di trend).
### Fee sweep + per-anno (PD-high cont, full sample)
```
BTC RT%: 0.00→+571 0.05→+289 0.10→+126 0.15→ +31 0.20→ 24 (OOS: +84/+52/+25/+3/15)
ETH RT%: 0.00→+1754 0.05→+1012 0.10→+567 0.15→+299 0.20→+139 (OOS: +67/+39/+16/3/19)
BTC per-anno: 2019 +39 2020 +104 2021 +7 2022 42 2023 +24 2024 +27 2025 16 2026 +3
ETH per-anno: 2020 +164 2021 +160 2022 +7 2023 +1 2024 +12 2025 4 2026 +7
Sharpe full: BTC +0.48 (maxDD 55%, €/d 2k +0.88) · ETH +0.86 (maxDD 34%, €/d 2k +4.27)
```
- **Fee-fragile:** alla baseline 0.10% RT sopravvive (OOS +25/+16%), ma muore già a ~0.15-0.20% RT.
Margine di fee sottile (≈1.5x baseline e l'edge sparisce su OOS). ~1000-1100 trade in 8 anni.
- **Drawdown enormi** (BTC 55%) e anni negativi (2022 42% BTC, 2025 16%).
## Verdetto
- **Sì, esiste un edge net-positivo OOS su entrambi gli asset:** *PD-high continuation* (long
quando `close` supera il massimo di ieri, exit a fine giornata UTC). Robusto al buffer e
all'anchor-hour. **MA non è deployabile come miglioramento:**
1. È **long-only drift capture**, non un breakout simmetrico (il lato short fallisce) → è una
versione **più debole e ridondante** del Trend Portfolio TP01 (Sharpe 0.48-0.86 vs 1.32).
2. **Fee-fragile** (muore a ~1.5x la fee baseline) e con **drawdown** molto peggiori.
- **Il contributo scientifico vero è la conferma della direzione:** sui dati certificati i
breakout di livello-calendario **CONTINUANO**; il fade è morto (negativo robusto su PD/PW/OR,
entrambi gli asset). Nessuna sorpresa mean-reversion nascosta nei livelli giornalieri/settimanali.
- **Niente di nuovo da mettere in produzione.** TP01 resta la strategia vincente; i breakout
prior-period non aggiungono Sharpe (stessa beta di trend, peggio eseguita).
## Come riprodurre
```bash
uv run python scripts/research/trackG_prior_levels.py # full (1h + 15m, ~25s)
uv run python scripts/research/trackG_prior_levels.py --quick # 1h only
```
@@ -0,0 +1,71 @@
# Track H — Volume, Range & Volatility-Regime signals (BTC/ETH, certified, >=12h)
**Date:** 2026-06-19
**Script:** `scripts/research/trackH_volume_vol.py` (runnable, self-contained)
**Question:** does any volume / range / volatility-regime signal ADD to the deployed winner
TP01 (vol-targeted trend portfolio, 12h, Sharpe ~1.32) — i.e. net-positive OOS on BOTH BTC &
ETH AND uncorrelated (|corr|<~0.3) — OR work as a regime filter that lifts TP01's Sharpe / cuts
its DD?
## Method (honest)
- Same causal per-bar engine as `TrendPortfolio.net_returns`: build a continuous TARGET decided
with data `<= close[i]`, HOLD it during bar `i+1` (`pos_held[t]=target[t-1]`), gross = pos×ret,
fee on `|Δpos|`. Identical in spirit to `harness.backtest_signals` (decide≤close[i], fill at
close[i]); two discrete signals cross-checked through `backtest_signals` directly.
- All features (volume z-score, OBV, ranges, realized vol) use prior/rolling windows shifted so
bar `i` sees only `<= i`. 12h/1d resampled from certified 1h via `resample_tf` (label='left'),
consumed index-based with the +1 hold → no open-label leak.
- Fee 0.10% RT baseline + sweep 0.000.40% RT. OOS 65/35 + per-year. Grid on BOTH assets.
Turnover and correlation-to-TP01 reported for every signal.
- **>=12h only** (12h + 1d). Sub-12h excluded per the standing lesson (fees + HF-noise overfit +
the 4h open-label look-ahead trap).
## Signals tested
VT-long (volatility-managed long), VolBreakout (volume-z-confirmed Donchian), OBV-trend,
VW-mom (volume-weighted momentum), RangeExpand (range-expansion breakout), NR7-break
(narrowest-range breakout), DeclVolRev (declining-volume fade/reversal). Plus regime overlays on
TP01: keep-low-vol, keep-high-vol, vol-managed ×1.5, OBV-up confirmation.
## Results (12h headline, fee 0.10% RT)
| signal | corr→TP01 | OOS Sharpe BTC/ETH | note |
|---|---|---|---|
| VT-long | 0.66 / 0.69 | 0.80 / 0.14 | trend-in-disguise; weak OOS ETH |
| VolBreakout | 0.69 / 0.71 | 0.54 / 0.49 | profitable but correlated |
| OBV-trend | 0.61 / 0.63 | 0.96 / 0.68 | profitable but correlated; turnover ~75/yr |
| VW-mom | 0.64 / 0.67 | 0.98 / 0.74 | basically TSMOM; correlated |
| RangeExpand | 0.48 / 0.49 | 0.37 / 1.04 | lower corr but BTC weak; ETH negative on 1d |
| NR7-break | 0.48 / 0.49 | 0.79 / 0.02 | fails OOS on ETH |
| DeclVolRev | -0.15 / -0.11 | -1.15 / -0.44 | **negative even at zero fee** |
Grid robustness (12h, % cells positive full+OOS on both assets): VW-mom 100%, VT-long 100%,
VolBreakout 96%, RangeExpand 96%, OBV-trend 75% — but the robust ones are precisely the ones
that are highly correlated to TP01. Fee sweep: trend-family signals survive to 0.40% RT;
DeclVolRev gets worse with fees (it trades constantly).
## Regime filters on TP01 (12h, 50/50 portfolio)
| variant | full Sharpe | OOS Sharpe | maxDD | CAGR | turn/y |
|---|---|---|---|---|---|
| **TP01 baseline** | **1.32** | 0.90 | 13.3% | 16.2% | 11.5 |
| × keep LOW-vol | 0.94 | 1.11 | 14.1% | 7.7% | 9.5 |
| × keep HIGH-vol | 0.98 | 0.18 | 9.9% | 7.9% | 4.9 |
| × vol-managed ×1.5 | 1.33 | 0.96 | 17.9% | 18.1% | 15.4 |
| × OBV-up only | 1.49 | 1.04 | 10.1% | 14.4% | 18.2 |
OBV-up filter across EMA span: full Sharpe 1.491.52 (span 1530), DD 710%, but OOS gain is
marginal (0.90→1.04 at span 30) and fades for span≥45 (OOS 0.690.73). It cuts ~2pp CAGR and
raises turnover ~60%.
## Verdict (honest)
- **No uncorrelated additive edge exists.** Every *profitable* volume/range/vol signal is trend
in disguise (corr 0.610.75 to TP01) → cannot raise the 50/50 portfolio Sharpe. The genuinely
lower-corr signals (RangeExpand, NR7 ~0.48) fail OOS on at least one asset.
- **Mean-reversion / declining-volume fade is dead** — negative net AND at zero fee on both
assets. Reconfirms the v2.0.0 contamination lesson; MR is not a real edge on certified data.
- **Vol-regime gating hurts** (keep-low / keep-high both drop Sharpe to ~0.95). The vol-managed
overlay is Sharpe-neutral but DD-worse.
- **The only non-harmful overlay is OBV-up trend-confirmation:** it cuts DD (13.3%→10.1%) and
nudges full Sharpe to ~1.49, but it is trend double-confirmation (de-risking), not new alpha;
it costs CAGR, raises turnover, and the OOS Sharpe gain is within noise and span-sensitive. It
is worth keeping in mind as a **defensive DD overlay**, not as a Sharpe improver.
- **Bottom line:** the ~1.3 portfolio-Sharpe ceiling on BTC/ETH-only **holds**. TP01 stays the
deployable winner. Volume/range/vol add nothing uncorrelated.
@@ -0,0 +1,99 @@
# Track I — Alternative momentum formulations + long-horizon reversal (2026-06-19)
**Script:** `scripts/research/trackI_momentum_reversal.py` (self-contained, runnable).
**Universe:** BTC & ETH only. **TF:** 12h + 1d (sub-12h excluded by rule). **Harness:** identical
honest machinery to TP01 — direction decided `<= close[i]`, positions held next bar (`pos_held[1:]
= tgt[:-1]`), vol-target by inverse PAST-ONLY realized vol (target 20%, lev cap 2x), NET fee 0.10%
RT on turnover, 50/50 BTC+ETH. OOS 65/35 + per-year + fee sweep (0.000.40% RT). Correlation to
TP01 net returns reported for every candidate.
## Goal
(A) A momentum formulation that BEATS or DIVERSIFIES the canonical 1-3-6m sign-blend (TP01,
Sharpe ~1.32). (B) Does the classic LONG-HORIZON REVERSAL (fade ~12m winners) give an
uncorrelated positive overlay?
## PART A — momentum formulations (12h, long-flat, vs TP01 Sharpe 1.32 / OOS 0.90 / DD 13.3%)
| formulation | Sharpe | IS | **OOS** | CAGR | maxDD | corr→TP01 | BTC | ETH |
|---|---|---|---|---|---|---|---|---|
| baseline sign-blend 1-3-6m | 1.32 | 1.54 | 0.90 | +16% | 13.3% | 1.00 | 1.15 | 1.10 |
| (i) z-score cum-return (tanh) | **1.35** | 1.63 | 0.85 | +12% | **8.4%** | 0.96 | 1.30 | 1.00 |
| (ii) risk-adjusted momentum | 1.27 | 1.49 | 0.84 | +13% | 9.5% | 0.97 | 1.21 | 1.00 |
| (iii) EMA-cross trend | 0.81 | 0.91 | 0.62 | +11% | 25.1% | 0.85 | 0.89 | 0.53 |
| (iii-b) MACD (calendar spans) | **1.50** | **1.87** | 0.74 | +22% | 17.7% | 0.69 | 1.30 | 1.32 |
| (iv) Donchian breakout | 1.10 | 1.36 | 0.57 | +17% | 25.0% | 0.86 | 1.08 | 0.82 |
| (v) acceleration (Δ-momentum) | 1.28 | 1.82 | 0.35 | +14% | 14.2% | 0.66 | 1.25 | 0.81 |
| (vi) 12-1 skip momentum | 0.67 | 0.79 | 0.47 | +9% | 24.5% | 0.68 | 0.70 | 0.49 |
Results are essentially identical at 1d. Read-out:
- **Nothing cleanly beats the sign-blend OOS on both assets.** The headline-Sharpe leaders are
artefacts of in-sample fit: **MACD** posts IS 1.87 but OOS collapses to 0.74 (gap = overfit) with
a worse DD (17.7%); **acceleration** IS 1.82 → OOS **0.35** (worst OOS decay of all). Both fail.
- **(i) z-score continuous momentum** is the one mild, honest refinement: Sharpe 1.35 (≈baseline)
but **maxDD 8.4% vs 13.3%** — the continuous score scales down position when the cumulative move
is statistically small, de-risking the tails. OOS 0.85 (slightly below baseline 0.90), CAGR drops
16%→12%. It's a smoother sibling of TP01, **not a new edge** (corr 0.96).
- (vi) 12-1 skip (classic equity "12-1" momentum) **does NOT help crypto**: skipping the recent
month removes the strongest part of the signal here → Sharpe 0.67, corr 0.68. Crypto momentum
lives in the recent window, opposite to the equity stylised fact.
- Breakout/Donchian and EMA-cross are strictly worse (high DD, weak OOS).
## PART B — long-horizon reversal (fade past winners), 12h
Long-short reversal (short ~12/18/24m winners, long losers, vol-targeted):
| reversal LS | Sharpe | OOS | CAGR | maxDD | corr→TP01 |
|---|---|---|---|---|---|
| 12m | -0.77 | -1.15 | -14% | 73% | -0.51 |
| 18m | -0.36 | -0.75 | -8% | 58% | -0.47 |
| 24m | **+0.04** | -0.07 | -1% | 43% | **-0.32** |
| 12-18-24m | -0.46 | -0.72 | -8% | 57% | -0.54 |
- **Long-horizon reversal is NOT a standalone edge.** Standalone it LOSES money (12m/18m strongly
negative; only 24m is ~flat at Sharpe 0.04, OOS 0.07, and even that fails "net-positive OOS on
both assets": BTC +0.10 / ETH 0.03). Fading crypto winners over a year just shorts the trend.
- It IS genuinely negatively correlated to TP01 (24m: corr 0.32; 12-18-24: 0.54), as expected
(it's the opposite sign of medium-term momentum).
- **Momentum + reversal blend** (long 1-6m momentum, brake on very-long extension): the variant
`mom(1-3-6) 0.5·rev(12-24)` is the most interesting single-strategy result — Sharpe **1.38**,
**OOS 0.98** (> baseline 0.90), **maxDD 10.6%** (< 13.3%), both assets positive (BTC 1.25/ETH
1.05), corr 0.91, fee-robust (1.43→1.22 across 0.000.40% RT). CAGR drops 16%→12%. It is TP01
with a long-term-extension brake: a modest *risk-adjusted* improvement, not more return.
## COMBINED — TP01 + best diversifier (blend net returns)
TP01 alone: Sharpe 1.321, CAGR +16%, maxDD 13.3%, OOS 0.90.
| combo | Sharpe | CAGR | maxDD | OOS | corr |
|---|---|---|---|---|---|
| TP01 + 20% reversal-24m (LS) | **1.411** | +13% | 11.5% | **1.06** | -0.32 |
| TP01 + 30% reversal-24m (LS) | 1.366 | +12% | 11.8% | 1.06 | -0.32 |
| TP01 + 20% reversal-12-18-24 (LS) | 1.350 | +11% | 10.6% | 0.84 | -0.54 |
| TP01 + 50% z-score | 1.348 | +14% | 9.5% | 0.89 | +0.96 |
- Adding a small slice of **reversal-24m long-short** lifts portfolio Sharpe 1.32→1.41 and OOS
0.90→1.06 while cutting DD to 11.5%. **But be skeptical:** the overlay is a ~zero-mean stream
(standalone Sharpe 0.04). The benefit is almost entirely **variance reduction from the negative
correlation, not added alpha** — and it COSTS return (CAGR 16%→13%). With a true-zero-edge
diversifier this Sharpe bump is fragile (it leans on the 0.32 correlation persisting OOS, and the
OOS sample is one 2022-24 crypto cycle). I would NOT deploy capital on a standalone-losing sleeve
to chase a 0.09 Sharpe point that is really de-risking.
## Fee sweep (12h portfolio Sharpe)
baseline 1.37→1.18, z-score 1.38→1.24, MACD 1.52→1.45 (lowest turnover), blend 1.43→1.22,
reversal-24m 0.07→−0.02 (0.00→0.40% RT). All trend formulations survive realistic fees; reversal
has no positive margin to survive on.
## VERDICT (honest)
- **Is there a momentum formulation that beats the 1-3-6m sign-blend? No — not OOS, not on both
assets.** MACD/acceleration look better in-sample but decay OOS (overfit + higher DD). The only
honest refinement is **continuous z-score momentum**, which matches the Sharpe with materially
lower drawdown (8.4% vs 13.3%) — a smoother variant of the SAME edge, not a new one (corr 0.96).
- **Does long-horizon reversal give an uncorrelated positive overlay? No, not a real one.** It is
uncorrelated/negatively-correlated (good) but **not positive** standalone (it loses, or at best is
flat at 24m and fails the both-assets bar). The combined-Sharpe lift (→1.41) is variance reduction
from a near-zero-mean stream and sacrifices CAGR — fragile, not bankable alpha.
- **The ~1.3 structural Sharpe ceiling on BTC/ETH-only holds.** TP01 remains the deployable winner.
If anything, swap the sign-blend for the **z-score continuous score** (or the `mom 0.5·rev`
brake) for a lower-DD profile at equal Sharpe — a risk-management tweak, not a return upgrade.
View File
+191
View File
@@ -0,0 +1,191 @@
"""PAPER TRADER — TP01 Trend Portfolio (PORT LF4h), forward-only, simulato.
Esegue la strategia VINCENTE (src/strategies/trend_portfolio.py, config CANONICAL) in
paper trading FORWARD-ONLY su capitale virtuale (default 2000 USDT), portafoglio 50/50
BTC+ETH a 4h. Stato persistente -> resume al riavvio.
DESIGN (onesto, niente esecuzione reale: l'esecuzione e' DISABILITATA nel progetto):
- Legge i parquet certificati locali (data/raw, BTC/ETH 1h) e resampla a 4h.
- Alla prima esecuzione parte dall'ultima barra 4h CHIUSA disponibile (forward-only:
NON include lo storico nel PnL di paper, traccia solo da ora in avanti).
- Ad ogni run processa le NUOVE barre 4h chiuse dall'ultima volta: applica il rendimento
della posizione tenuta, addebita le fee sul turnover, registra i trade sui cambi di
posizione, poi ricalcola la posizione-bersaglio (decisa con dati <= ultima barra chiusa).
- Per avere barre fresche, aggiornare prima i dati:
uv run python scripts/analysis/rebuild_history.py --asset BTC ETH
Stato: data/paper_trend/state.json + trades.jsonl (append-only).
uv run python scripts/live/paper_trend.py # avanza il paper col dato disponibile
uv run python scripts/live/paper_trend.py --status # solo stato, non avanza
uv run python scripts/live/paper_trend.py --reset # azzera lo stato (riparte da ora)
"""
from __future__ import annotations
import json
import sys
from pathlib import Path
import numpy as np
import pandas as pd
PROJECT_ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(PROJECT_ROOT))
from src.backtest.harness import load
from src.strategies.trend_portfolio import (
TrendPortfolio, CANONICAL, resample_tf, DEPLOY_TF, simple_returns)
STATE_DIR = PROJECT_ROOT / "data" / "paper_trend"
STATE_FILE = STATE_DIR / "state.json"
TRADES_FILE = STATE_DIR / "trades.jsonl"
ASSETS = ["BTC", "ETH"]
WEIGHT = 0.5
INITIAL_CAPITAL = 2000.0
def build_bars() -> dict[str, pd.DataFrame]:
return {a: resample_tf(load(a, "1h"), DEPLOY_TF) for a in ASSETS}
def load_state() -> dict | None:
if STATE_FILE.exists():
return json.loads(STATE_FILE.read_text())
return None
def save_state(st: dict):
STATE_DIR.mkdir(parents=True, exist_ok=True)
STATE_FILE.write_text(json.dumps(st, indent=2))
def append_trade(rec: dict):
STATE_DIR.mkdir(parents=True, exist_ok=True)
with open(TRADES_FILE, "a") as f:
f.write(json.dumps(rec) + "\n")
def init_state(dfs) -> dict:
last_ts = min(int(dfs[a]["timestamp"].iloc[-1]) for a in ASSETS)
tp = TrendPortfolio(**CANONICAL)
positions = {}
for a in ASSETS:
df = dfs[a]
df = df[df["timestamp"] <= last_ts]
positions[a] = tp.current_target(df)
return dict(
capital=INITIAL_CAPITAL, initial_capital=INITIAL_CAPITAL,
start_ts=last_ts, last_ts=last_ts, positions=positions, n_bars=0,
peak=INITIAL_CAPITAL, max_dd=0.0,
)
def advance(st: dict, dfs: dict) -> dict:
"""Processa tutte le barre 4h chiuse DOPO st['last_ts']."""
tp = TrendPortfolio(**CANONICAL)
# precompute per-asset: timestamps, returns, target series (causale)
data = {}
for a in ASSETS:
df = dfs[a]
c = df["close"].values.astype(float)
data[a] = dict(
ts=df["timestamp"].values.astype("int64"),
dt=pd.to_datetime(df["datetime"]).values,
r=simple_returns(c),
tgt=tp.target_series(df),
)
# common new timestamps after last_ts (present in both assets)
common = sorted(set(data["BTC"]["ts"]).intersection(data["ETH"]["ts"]))
new_ts = [t for t in common if t > st["last_ts"]]
if not new_ts:
return st
pos = dict(st["positions"])
cap = st["capital"]
peak = st.get("peak", cap)
max_dd = st.get("max_dd", 0.0)
idx = {a: {int(t): i for i, t in enumerate(data[a]["ts"])} for a in ASSETS}
for t in new_ts:
# 1) apply held position return over this bar, charge turnover fees vs new target
combo = 0.0
new_pos = {}
for a in ASSETS:
i = idx[a][int(t)]
r = float(data[a]["r"][i])
held = pos[a]
new_t = float(data[a]["tgt"][i])
turn = abs(new_t - held)
net = held * r - CANONICAL["fee_side"] * turn
combo += WEIGHT * net
new_pos[a] = new_t
# record a trade when the SIGN of position changes (entry/exit/flip)
if np.sign(new_t) != np.sign(held):
append_trade(dict(
ts=int(t), dt=str(pd.Timestamp(data[a]["dt"][i])),
asset=a, action="ENTRY" if new_t != 0 else "EXIT",
from_pos=round(held, 4), to_pos=round(new_t, 4),
capital=round(cap, 2),
))
cap *= (1.0 + max(combo, -0.99))
peak = max(peak, cap)
max_dd = max(max_dd, (peak - cap) / peak if peak > 0 else 0.0)
pos = new_pos
st.update(capital=cap, last_ts=int(new_ts[-1]), positions=pos,
n_bars=st.get("n_bars", 0) + len(new_ts), peak=peak, max_dd=max_dd)
return st
def print_status(st: dict, dfs: dict):
start = pd.Timestamp(st["start_ts"], unit="ms", tz="UTC")
last = pd.Timestamp(st["last_ts"], unit="ms", tz="UTC")
days = (last - start).total_seconds() / 86400
cap = st["capital"]
ret = cap / st["initial_capital"] - 1
daily = (cap - st["initial_capital"]) / days if days > 0 else 0.0
print("=" * 72)
print(f" PAPER TRADER — TP01 Trend Portfolio (PORT LF{DEPLOY_TF}, 50/50 BTC+ETH)")
print("=" * 72)
print(f" start {start:%Y-%m-%d %H:%M} UTC")
print(f" last bar {last:%Y-%m-%d %H:%M} UTC ({days:.1f} giorni, {st['n_bars']} barre {DEPLOY_TF})")
print(f" capitale {cap:,.2f} USDT (start {st['initial_capital']:,.0f})")
print(f" ritorno {ret*100:+.2f}% | €/giorno {daily:+.2f} | maxDD {st['max_dd']*100:.1f}%")
print(f" posizioni now { 'flat' if all(p==0 for p in st['positions'].values()) else '' }")
for a in ASSETS:
p = st["positions"][a]
state = "FLAT" if p == 0 else ("LONG" if p > 0 else "SHORT")
print(f" {a}: {state:<5s} target {p:+.3f}x (frazione di equity dello sleeve)")
# what the strategy decides at the latest available closed bar
print(" ── prossima decisione (ultima barra chiusa disponibile) ──")
tp = TrendPortfolio(**CANONICAL)
for a in ASSETS:
w = tp.current_target(dfs[a])
print(f" {a}: target {w:+.3f}x")
if TRADES_FILE.exists():
n = sum(1 for _ in open(TRADES_FILE))
print(f" trade registrati: {n} ({TRADES_FILE})")
def main():
argv = sys.argv[1:]
dfs = build_bars()
if "--reset" in argv:
if STATE_FILE.exists():
STATE_FILE.unlink()
if TRADES_FILE.exists():
TRADES_FILE.unlink()
print("stato azzerato.")
st = load_state()
if st is None:
st = init_state(dfs)
save_state(st)
print("paper trader inizializzato (forward-only da ora).\n")
elif "--status" not in argv:
st = advance(st, dfs)
save_state(st)
print_status(st, dfs)
if __name__ == "__main__":
main()
@@ -0,0 +1,187 @@
"""VERIFICA SLEEVE OPZIONI su QUOTE REALI Deribit — quanto Sharpe sopravvive a bid/ask + skew.
Lo sleeve income della strategia esterna `crypto_backtest` (vendita di put settimanali CSP su
BTC, delta 0.28) e' backtestato su prezzi MODELLATI: Black-Scholes prezzato con DVOL = IV ATM, e
si incassa il premio "fair" (mid). Due gap reali NON catturati:
(1) BID/ASK: vendendo si incassa il BID, non il mid.
(2) SKEW: una put OTM (delta 0.28) ha IV piu' alta della ATM (DVOL) -> il modello prezza la put
con la vol sbagliata.
Questo script:
PARTE 1 (rete, Deribit mainnet pubblico): scarica la catena REALE della scadenza ~settimanale,
trova la put a delta ~0.28, e misura:
- premio reale incassabile (BID, in USD) vs premio modellato (BS @ IV ATM)
- skew: IV della put OTM (mark) vs IV ATM (mark)
- spread: bid/mark
- HAIRCUT netto f = premio_bid_reale / premio_BS@ATM
PARTE 2 (locale): ri-esegue lo sleeve CSP settimanale (dati + modulo del progetto esterno) con
il premio moltiplicato per f -> Sharpe/CAGR/maxDD reali stimati, vs i modellati.
NB ONESTO: e' UNO SNAPSHOT (la catena di oggi). Lo spread si allarga nello stress; lo skew varia.
Va ripetuto nel tempo per robustezza. Ma misura direttamente i due gap col mercato vero.
uv run python scripts/research/options_real_quote_check.py
"""
from __future__ import annotations
import sys
from pathlib import Path
import numpy as np
import pandas as pd
EXT = Path("/home/adriano/crypto_backtest")
sys.path.insert(0, str(EXT))
PUT_DELTA = 0.28
CYCLE_DAYS = 7
ANN = 365
def fetch_real_chain():
import ccxt
ex = ccxt.deribit({"enableRateLimit": True})
ex.load_markets()
puts = [m for m in ex.markets.values()
if m.get("option") and m["base"] == "BTC" and m["optionType"] == "put"]
calls = [m for m in ex.markets.values()
if m.get("option") and m["base"] == "BTC" and m["optionType"] == "call"]
# expiries -> pick the one closest to CYCLE_DAYS days out
now = pd.Timestamp.utcnow().tz_localize(None)
def exp_dt(m):
return pd.to_datetime(m["symbol"].split("-")[1], format="%y%m%d")
exps = sorted(set(exp_dt(m) for m in puts))
target = now + pd.Timedelta(days=CYCLE_DAYS)
expiry = min(exps, key=lambda e: abs((e - target).days))
dte = (expiry - now).days + (expiry - now).seconds / 86400
chain_puts = [m for m in puts if exp_dt(m) == expiry]
chain_calls = [m for m in calls if exp_dt(m) == expiry]
print(f" scadenza scelta: {expiry.date()} (DTE ~{dte:.1f}g, target {CYCLE_DAYS}g) "
f"strikes put: {len(chain_puts)}")
def tick(m):
try:
t = ex.fetch_ticker(m["symbol"])
i = t["info"]
g = i.get("greeks") or {}
return dict(symbol=m["symbol"], strike=float(m["strike"]),
delta=float(g.get("delta", "nan")), mark_iv=float(i.get("mark_iv", "nan")),
bid=float(i.get("best_bid_price") or 0), ask=float(i.get("best_ask_price") or 0),
mark=float(i.get("mark_price") or 0),
S=float(i.get("underlying_price") or i.get("index_price") or 0))
except Exception:
return None
rows = [r for r in (tick(m) for m in chain_puts) if r and np.isfinite(r["delta"])]
callrows = [r for r in (tick(m) for m in chain_calls) if r and np.isfinite(r["delta"])]
return expiry, dte, pd.DataFrame(rows), pd.DataFrame(callrows)
def bs_put(S, K, T, sigma):
from scipy.stats import norm
if T <= 0 or sigma <= 0:
return max(0.0, K - S)
d1 = (np.log(S / K) + 0.5 * sigma ** 2 * T) / (sigma * np.sqrt(T))
d2 = d1 - sigma * np.sqrt(T)
return K * norm.cdf(-d2) - S * norm.cdf(-d1)
def measure_haircut(dte, puts, calls):
S = puts["S"].iloc[0]
T = dte / ANN
# ATM IV: option with |delta| closest to 0.5 (use calls+puts mark_iv near ATM)
allo = pd.concat([puts.assign(typ="P"), calls.assign(typ="C")], ignore_index=True)
atm = allo.iloc[(allo["delta"].abs() - 0.5).abs().argsort()[:4]]
atm_iv = atm["mark_iv"].mean() / 100.0
# delta-0.28 put (delta negative)
p = puts.iloc[(puts["delta"] - (-PUT_DELTA)).abs().argsort()[:1]].iloc[0]
K = p["strike"]
put_iv = p["mark_iv"] / 100.0
# premiums in USD (Deribit option price is in BTC)
bid_usd = p["bid"] * S
mark_usd = p["mark"] * S
ask_usd = p["ask"] * S
bs_atm_usd = bs_put(S, K, T, atm_iv) # cio' che il backtest assume (DVOL=ATM, incassa mid)
bs_skew_usd = bs_put(S, K, T, put_iv) # BS alla vol REALE della put (isola lo skew)
print("\n --- MISURA SU QUOTE REALI (snapshot) ---")
print(f" underlying S = {S:,.0f} strike(delta~-0.28) K = {K:,.0f} ({(1-K/S)*100:.1f}% OTM) delta {p['delta']:.3f}")
print(f" IV ATM (DVOL-equivalente) = {atm_iv*100:.1f}% IV put OTM (mark) = {put_iv*100:.1f}% "
f"skew +{(put_iv-atm_iv)*100:.1f} pt")
print(f" premio put (USD): BID {bid_usd:,.1f} mark {mark_usd:,.1f} ask {ask_usd:,.1f}")
print(f" spread bid/mark = {(p['bid']/p['mark']) if p['mark']>0 else float('nan'):.3f} "
f"(ask-bid)/mark = {((p['ask']-p['bid'])/p['mark']) if p['mark']>0 else float('nan'):.3f}")
print(f" modellato dal backtest BS@IV-ATM = {bs_atm_usd:,.1f} USD (BS@IV-put-reale = {bs_skew_usd:,.1f})")
f_bid = bid_usd / bs_atm_usd if bs_atm_usd > 0 else float("nan")
f_mark = mark_usd / bs_atm_usd if bs_atm_usd > 0 else float("nan")
print(f" HAIRCUT premio: reale(BID)/modello = {f_bid:.3f} | mark/modello = {f_mark:.3f}")
print(f" -> lo skew ALZA il premio lordo (+{(bs_skew_usd/bs_atm_usd-1)*100:.0f}% vs ATM), ma il "
f"BID/ask lo riporta a {f_bid*100:.0f}% del modello.")
return f_bid
def csp_sleeve_haircut(f):
"""Ri-esegue lo sleeve CSP settimanale (dati+modulo esterni) con premio * f."""
import options_deribit as od
px = pd.read_csv(EXT / "data/BTCUSDT.csv", parse_dates=["date"]).set_index("date")["close"]
dvol = pd.read_csv(EXT / "data/DVOL_BTC.csv", parse_dates=["date"]).set_index("date")["close"]
iv = od.build_iv(px, "BTC", dvol)
d0 = dvol.index[0]
px, iv = px[px.index >= d0], iv[iv.index >= d0]
def sim(prem_mult, m=0.63):
idx = px.index
locs = list(range(0, len(idx) - CYCLE_DAYS, CYCLE_DAYS))
T = CYCLE_DAYS / ANN
rows = []
for i in locs:
S0, S1, sig = px.iloc[i], px.iloc[i + CYCLE_DAYS], iv.iloc[i]
if not (np.isfinite(S0) and np.isfinite(S1) and np.isfinite(sig)):
continue
Kp = od.strike_for_delta(S0, T, sig, PUT_DELTA, call=False)
pp = od.bs_price(S0, Kp, T, sig, call=False) * prem_mult # <-- haircut sul premio
fee = od.option_fee(S0, pp) + (od.SETTLE_FEE * S0 if S1 < Kp else 0)
pnl = pp - max(Kp - S1, 0.0) - fee
rows.append((idx[i + CYCLE_DAYS], m * pnl / S0))
s = pd.Series({d: r for d, r in rows}).sort_index()
return s
def met(s, name):
eq = (1 + s).cumprod()
cpy = ANN / CYCLE_DAYS
yrs = len(s) / cpy
cagr = eq.iloc[-1] ** (1 / yrs) - 1 if eq.iloc[-1] > 0 else -1
sh = s.mean() / s.std() * np.sqrt(cpy)
dd = (eq / eq.cummax() - 1).min()
print(f" {name:<34s} CAGR {cagr*100:>+6.1f}% Sharpe {sh:>5.2f} maxDD {dd*100:>6.1f}% win {(s>0).mean()*100:>3.0f}%")
return sh
print("\n --- RI-ESECUZIONE SLEEVE CSP con HAIRCUT REALE (m=0.63, hold-to-expiry) ---")
print(f" finestra {px.index[0].date()} -> {px.index[-1].date()} (DVOL reale)")
sh_model = met(sim(1.00), "modello (premio pieno, BS@DVOL)")
sh_real = met(sim(f), f"reale stimato (premio x{f:.2f} = BID)")
# sensitivity
for ff in (0.85, 0.70, 0.55):
met(sim(ff), f"sensitivity premio x{ff:.2f}")
print(f"\n => con haircut reale f={f:.2f}: Sharpe sleeve {sh_model:.2f} -> {sh_real:.2f}")
return sh_model, sh_real
def main():
print("=" * 92)
print("# VERIFICA SLEEVE OPZIONI su QUOTE REALI DERIBIT — quanto Sharpe sopravvive")
print("=" * 92)
try:
expiry, dte, puts, calls = fetch_real_chain()
f = measure_haircut(dte, puts, calls)
except Exception as e:
print(f" [rete] impossibile scaricare la catena reale ({type(e).__name__}: {e})")
print(" uso haircut di letteratura f=0.70 (spread+skew tipici su put OTM settimanali)")
f = 0.70
f = float(np.clip(f, 0.3, 1.2))
csp_sleeve_haircut(f)
print("\n CAVEAT: snapshot singolo; spread peggiora nello stress; ripetere nel tempo + testnet.")
if __name__ == "__main__":
main()
+320
View File
@@ -0,0 +1,320 @@
"""TRACK A — TREND / MOMENTUM research on certified BTC/ETH (Deribit mainnet).
Honest harness only (src.backtest.harness). Rules enforced:
* Direction & entry price decided with data <= close[i]; fill at close[i].
* Net of fees (0.10% RT baseline) + fee sweep + leverage stress.
* IS / OOS split (65/35). Grid robustness across params AND both assets.
Run: uv run python scripts/research/trackA_trend.py
This script is deliberately skeptical: it prints full grids so the reader can see
whether an "edge" is a single lucky cell or a robust neighborhood. The verdict at the
end is printed from the actual numbers, not asserted.
"""
from __future__ import annotations
import sys
from pathlib import Path
import numpy as np
import pandas as pd
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
from src.backtest.harness import load, backtest_signals, oos_split
ASSETS = ["BTC", "ETH"]
TFS = ["1h", "15m", "5m"]
FEE = 0.001
# ---------------------------------------------------------------------------
# Signal builders. Each returns a list[dict|None] of length len(df).
# All features use ONLY data up to and including close[i]. Entry fills at close[i].
# Position is approximated as a chained, non-overlapping hold of `hold` bars whose
# direction is recomputed at each (free) bar -> amortizes fee over `hold` bars while
# staying honest about responsiveness.
# ---------------------------------------------------------------------------
def sig_tsmom(df, lookback, hold, long_only=False):
c = df["close"].values
n = len(c)
ent = [None] * n
dirs = np.where(c[lookback:] > c[:-lookback], 1, -1)
for k, d in enumerate(dirs):
if long_only and d < 0:
continue
ent[lookback + k] = {"dir": int(d), "max_bars": hold}
return ent
def _ema(x, span):
return pd.Series(x).ewm(span=span, adjust=False).mean().values
def sig_ema_cross(df, fast, slow, hold, long_only=False):
c = df["close"].values
n = len(c)
ef = _ema(c, fast)
es = _ema(c, slow)
ent = [None] * n
for i in range(slow, n):
d = 1 if ef[i] > es[i] else -1
if long_only and d < 0:
ent[i] = None
continue
ent[i] = {"dir": d, "max_bars": hold}
return ent
def sig_donchian(df, lookback, hold, long_only=False):
"""Breakout: close[i] strictly above prior `lookback` highs -> long; below lows -> short.
Detection AND entry both at close[i] (honest)."""
c = df["close"].values
h = df["high"].values
l = df["low"].values
n = len(c)
ent = [None] * n
# prior-window high/low EXCLUDING current bar (shift by 1) -> honest
hh = pd.Series(h).rolling(lookback).max().shift(1).values
ll = pd.Series(l).rolling(lookback).min().shift(1).values
for i in range(lookback, n):
if not np.isfinite(hh[i]):
continue
if c[i] > hh[i]:
d = 1
elif c[i] < ll[i]:
d = -1
else:
continue
if long_only and d < 0:
continue
ent[i] = {"dir": d, "max_bars": hold}
return ent
def sig_vol_scaled_tsmom(df, lookback, hold, vol_win, z_gate):
"""Momentum gated by trend strength: only take a position when |past return| exceeds
z_gate * rolling stdev of bar returns (regime gate). Honest: all <= close[i]."""
c = df["close"].values
n = len(c)
logret = np.zeros(n)
logret[1:] = np.diff(np.log(c))
vol = pd.Series(logret).rolling(vol_win).std().values
ent = [None] * n
start = max(lookback, vol_win) + 1
for i in range(start, n):
r = np.log(c[i] / c[i - lookback])
v = vol[i] * np.sqrt(lookback)
if not np.isfinite(v) or v == 0:
continue
z = r / v
if abs(z) < z_gate:
continue
d = 1 if z > 0 else -1
ent[i] = {"dir": d, "max_bars": hold}
return ent
# ---------------------------------------------------------------------------
# Evaluation helpers
# ---------------------------------------------------------------------------
def eval_is_oos(df, entries, asset, tf, fee=FEE, lev=1.0):
cut = oos_split(df, 0.65)
full = backtest_signals(df, entries, fee_rt=fee, leverage=lev, asset=asset, tf=tf)
ent_is = [e if i < cut else None for i, e in enumerate(entries)]
ent_oos = [e if i >= cut else None for i, e in enumerate(entries)]
m_is = backtest_signals(df, ent_is, fee_rt=fee, leverage=lev, asset=asset, tf=tf)
m_oos = backtest_signals(df, ent_oos, fee_rt=fee, leverage=lev, asset=asset, tf=tf)
return full, m_is, m_oos
def buy_hold(df, cut=None):
c = df["close"].values
if cut is None:
cut = oos_split(df, 0.65)
return c[-1] / c[0] - 1, c[-1] / c[cut] - 1 # (full, oos)
def print_benchmarks():
print("\n" + "=" * 110)
print("# BUY & HOLD BENCHMARK (the bar any long/short trend edge must clear)")
print("# NOTE: OOS window is the LAST 35% = ~late-2023 -> 2026, a single (mostly bull) regime.")
print("# 2018-2022 (bear+crash+bull+bear) is ENTIRELY in-sample. 'positive OOS' is weak evidence.")
print("=" * 110)
for tf in TFS:
for asset in ASSETS:
df = load(asset, tf)
cut = oos_split(df, 0.65)
bf, bo = buy_hold(df, cut)
print(f" {asset} {tf:>3s} OOS starts {df['datetime'].iloc[cut].date()} "
f"B&H full={bf*100:>+7.0f}% B&H OOS={bo*100:>+7.0f}%")
def line(label, m):
print(f" {label:<30s} tr={m.n_trades:>6d} wr={m.win_rate:>4.1f}% "
f"ret={m.net_return*100:>+8.0f}% CAGR={m.cagr*100:>+6.1f}% "
f"Sh={m.sharpe:>5.2f} DD={m.max_dd*100:>4.1f}% mkt={m.time_in_market*100:>3.0f}% "
f"€/d={m.daily_profit(2000):>+6.2f}")
# ---------------------------------------------------------------------------
# Experiments
# ---------------------------------------------------------------------------
def run_grid(name, builder, param_grid, builder_kwargs_fn, tfs=TFS, assets=ASSETS):
"""Generic grid runner. Prints OOS-focused table. Returns list of result dicts."""
print("\n" + "=" * 110)
print(f"# {name}")
print("=" * 110)
results = []
for tf in tfs:
for asset in assets:
df = load(asset, tf)
print(f"\n -- {asset} {tf} (n={len(df)}) --")
for params in param_grid:
ent = builder(df, **builder_kwargs_fn(params))
full, m_is, m_oos = eval_is_oos(df, ent, asset, tf)
tag = ",".join(f"{k}={v}" for k, v in params.items())
line(f"{tag} [OOS]", m_oos)
results.append(dict(name=name, asset=asset, tf=tf, params=params,
full=full, is_=m_is, oos=m_oos))
return results
def summarize_survivors(all_results):
print("\n" + "#" * 110)
print("# SURVIVOR SCREEN — positive OOS net return AND positive full-sample, Sharpe(OOS)>0")
print("#" * 110)
survivors = [r for r in all_results
if r["oos"].net_return > 0 and r["full"].net_return > 0
and r["oos"].sharpe > 0 and r["oos"].n_trades >= 20]
if not survivors:
print(" NONE. No config is net-positive OOS with positive full-sample and Sharpe>0.")
return []
survivors.sort(key=lambda r: r["oos"].sharpe, reverse=True)
# precompute B&H OOS per (asset,tf)
bh = {}
for tf in TFS:
for a in ASSETS:
bh[(a, tf)] = buy_hold(load(a, tf))[1]
print(" (BEATS B&H = OOS return exceeds buy&hold over same OOS window; otherwise it's just beta)")
for r in survivors[:40]:
tag = ",".join(f"{k}={v}" for k, v in r["params"].items())
bho = bh[(r["asset"], r["tf"])]
beat = "BEATS B&H" if r["oos"].net_return > bho else "<= B&H (beta)"
print(f" {r['name'][:18]:<18s} {r['asset']} {r['tf']:>3s} {tag:<28s} "
f"OOS: ret={r['oos'].net_return*100:>+7.0f}% Sh={r['oos'].sharpe:>4.2f} "
f"DD={r['oos'].max_dd*100:>4.0f}% €/d={r['oos'].daily_profit(2000):>+5.2f} | "
f"B&H={bho*100:>+5.0f}% {beat}")
return survivors
def robustness_report(survivors):
"""For top survivors, check fee sweep + leverage stress + cross-asset consistency."""
if not survivors:
return
print("\n" + "#" * 110)
print("# ROBUSTNESS: fee sweep (0.0005/0.001/0.0015/0.002) + leverage (1x/2x/3x) on top survivors")
print("#" * 110)
seen = set()
for r in survivors[:8]:
key = (r["name"], r["asset"], r["tf"], tuple(r["params"].items()))
if key in seen:
continue
seen.add(key)
df = load(r["asset"], r["tf"])
# rebuild entries
builder = BUILDERS[r["name"]]
ent = builder(df, **KW_FN[r["name"]](r["params"]))
tag = ",".join(f"{k}={v}" for k, v in r["params"].items())
print(f"\n {r['name']} {r['asset']} {r['tf']} {tag}")
print(" fee sweep (OOS net return):")
for fee in (0.0005, 0.001, 0.0015, 0.002):
_, _, m_oos = eval_is_oos(df, ent, r["asset"], r["tf"], fee=fee)
flag = "" if m_oos.net_return > 0 else " <-- DIES"
print(f" fee={fee:.4f}: OOS ret={m_oos.net_return*100:>+8.0f}% Sh={m_oos.sharpe:>4.2f}{flag}")
print(" leverage stress (OOS, fee=0.001):")
for lev in (1.0, 2.0, 3.0):
_, _, m_oos = eval_is_oos(df, ent, r["asset"], r["tf"], lev=lev)
print(f" {lev:.0f}x: OOS ret={m_oos.net_return*100:>+8.0f}% "
f"Sh={m_oos.sharpe:>4.2f} DD={m_oos.max_dd*100:>4.0f}% €/d={m_oos.daily_profit(2000):>+5.2f}")
# yearly OOS
_, _, m_oos = eval_is_oos(df, ent, r["asset"], r["tf"])
print(" OOS yearly:")
for y in sorted(m_oos.yearly):
print(f" {y}: {m_oos.yearly[y]*100:>+7.1f}%")
# registry so robustness_report can rebuild entries
BUILDERS = {
"TSMOM": sig_tsmom,
"TSMOM_LONG": sig_tsmom,
"EMA_CROSS": sig_ema_cross,
"DONCHIAN": sig_donchian,
"VOLSCALED_TSMOM": sig_vol_scaled_tsmom,
}
KW_FN = {
"TSMOM": lambda p: dict(lookback=p["N"], hold=p["H"]),
"TSMOM_LONG": lambda p: dict(lookback=p["N"], hold=p["H"], long_only=True),
"EMA_CROSS": lambda p: dict(fast=p["f"], slow=p["s"], hold=p["H"]),
"DONCHIAN": lambda p: dict(lookback=p["N"], hold=p["H"]),
"VOLSCALED_TSMOM": lambda p: dict(lookback=p["N"], hold=p["H"], vol_win=p["vw"], z_gate=p["z"]),
}
def main():
pd.set_option("display.width", 200)
print_benchmarks()
all_results = []
# ---- 1. TSMOM (long/short) ----
tsmom_grid = [dict(N=n, H=h) for n in (10, 20, 50, 100, 200) for h in (6, 12, 24, 48)]
all_results += run_grid("TSMOM", sig_tsmom, tsmom_grid,
KW_FN["TSMOM"])
# ---- 2. TSMOM long-only (crypto has strong upward drift; honest to test) ----
all_results += run_grid("TSMOM_LONG", lambda df, **k: sig_tsmom(df, long_only=True, **k),
[dict(N=n, H=h) for n in (20, 50, 100, 200) for h in (12, 24, 48)],
KW_FN["TSMOM"])
# ---- 3. EMA crossover ----
ema_grid = [dict(f=f, s=s, H=h)
for (f, s) in ((10, 30), (20, 50), (20, 100), (50, 200))
for h in (12, 24, 48)]
all_results += run_grid("EMA_CROSS", sig_ema_cross, ema_grid, KW_FN["EMA_CROSS"])
# ---- 4. Donchian breakout ----
don_grid = [dict(N=n, H=h) for n in (20, 50, 100, 200) for h in (12, 24, 48)]
all_results += run_grid("DONCHIAN", sig_donchian, don_grid, KW_FN["DONCHIAN"])
# ---- 5. Vol-scaled / regime-gated TSMOM ----
vs_grid = [dict(N=n, H=h, vw=vw, z=z)
for n in (20, 50, 100) for h in (24, 48)
for vw in (50, 100) for z in (0.5, 1.0)]
all_results += run_grid("VOLSCALED_TSMOM", sig_vol_scaled_tsmom, vs_grid,
KW_FN["VOLSCALED_TSMOM"])
# ---- survivor screen + robustness ----
survivors = summarize_survivors(all_results)
robustness_report(survivors)
# ---- cross-asset robustness note ----
print("\n" + "#" * 110)
print("# CROSS-ASSET / CROSS-TF CONSISTENCY of survivors (a real edge holds on BOTH BTC & ETH)")
print("#" * 110)
from collections import defaultdict
by_strat = defaultdict(list)
for r in survivors:
by_strat[(r["name"], r["tf"], tuple(r["params"].items()))].append(r["asset"])
both = [(k, v) for k, v in by_strat.items() if set(v) >= {"BTC", "ETH"}]
if not both:
print(" No single (strategy, tf, params) cell is an OOS survivor on BOTH BTC and ETH.")
print(" => any apparent edge is asset/regime-specific, not a robust trend edge.")
else:
for (name, tf, params), assets in both:
print(f" {name} {tf} {dict(params)} survives on: {assets}")
print("\nDONE. Read the survivor screen + robustness above for the honest verdict.")
if __name__ == "__main__":
main()
+398
View File
@@ -0,0 +1,398 @@
"""TRACK B — Machine-learning / feature-prediction on BTC & ETH (Deribit-certified).
Honest, strict walk-forward ML research. The whole point is to NOT repeat the death of
the old library (look-ahead). Everything here obeys:
* Features for bar i use ONLY data <= close[i] (all rolling windows are backward).
* Labels (sign of forward return over H bars) use close[i+H]; in walk-forward we only
train on samples whose label is FULLY realized in the past relative to the prediction
bar (a gap of H is enforced between train-end and the prediction block).
* Scaler + model are fit ONLY on past data, retrained periodically, never on the future.
* Net of fees (fee_rt sweep 0.0005 .. 0.002, baseline 0.001). Turnover reported.
* Grid over W (lookback for training), H (horizon), threshold, asset, tf.
* A final held-out segment (last HELD_OUT_FRAC) is NEVER used to choose configs;
configs are selected on the DEV portion, then confirmed once on the held-out tail.
Run: uv run python scripts/research/trackB_ml.py
uv run python scripts/research/trackB_ml.py --quick (smaller grid, faster)
uv run python scripts/research/trackB_ml.py --gbm (also try GradientBoosting)
Entry convention (harness): for a signalled bar i we open at close[i] in the predicted
direction and hold up to H bars (max_bars=H, no TP/SL) — a pure test of directional sign.
No-overlap is enforced by the harness, so trades are naturally spaced >= H bars.
"""
from __future__ import annotations
import argparse
import sys
import time
import warnings
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
import numpy as np
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
from src.backtest.harness import backtest_signals, load
warnings.filterwarnings("ignore")
HELD_OUT_FRAC = 0.25 # final tail reserved for confirmation only
RETRAIN_K = 250 # retrain every K bars (block prediction)
MIN_TRAIN = 400 # minimum usable training samples
# ---------------------------------------------------------------------------
# Feature engineering — ALL backward-looking (safe at close[i])
# ---------------------------------------------------------------------------
def _rsi(close: pd.Series, n: int = 14) -> pd.Series:
d = close.diff()
up = d.clip(lower=0).ewm(alpha=1 / n, adjust=False).mean()
dn = (-d.clip(upper=0)).ewm(alpha=1 / n, adjust=False).mean()
rs = up / dn.replace(0, np.nan)
return (100 - 100 / (1 + rs)).fillna(50.0)
def _atr(df: pd.DataFrame, n: int = 14) -> pd.Series:
h, l, c = df["high"], df["low"], df["close"]
pc = c.shift(1)
tr = pd.concat([(h - l), (h - pc).abs(), (l - pc).abs()], axis=1).max(axis=1)
return tr.ewm(alpha=1 / n, adjust=False).mean()
def build_features(df: pd.DataFrame) -> tuple[np.ndarray, list[str], np.ndarray]:
"""Return (X, names, warmup_valid_mask). Every column known at close[i]."""
c = df["close"].astype(float)
h = df["high"].astype(float)
l = df["low"].astype(float)
o = df["open"].astype(float)
v = df["volume"].astype(float)
logc = np.log(c)
feats: dict[str, pd.Series] = {}
# multi-lag simple returns (ret[i] uses close[i],close[i-k] -> known at i)
for k in (1, 2, 3, 6, 12, 24):
feats[f"ret{k}"] = c.pct_change(k)
# candle geometry (current bar fully known at its close)
rng = (h - l).replace(0, np.nan)
feats["body"] = (c - o) / rng
feats["upsh"] = (h - np.maximum(c, o)) / rng
feats["dnsh"] = (np.minimum(c, o) - l) / rng
feats["range_n"] = (h - l) / c
# one-lag candle geometry
feats["body1"] = ((c - o) / rng).shift(1)
# momentum/acceleration
feats["mom48"] = c.pct_change(48)
feats["accel"] = c.pct_change(6) - c.pct_change(12)
# RSI
feats["rsi14"] = _rsi(c, 14) / 100.0
# ATR-normalized extension from a trend baseline
ema = c.ewm(span=24, adjust=False).mean()
atr = _atr(df, 14)
feats["ext_atr"] = (c - ema) / atr.replace(0, np.nan)
# realized vol (std of 1-bar returns)
r1 = c.pct_change()
feats["rvol24"] = r1.rolling(24).std()
feats["rvol72"] = r1.rolling(72).std()
feats["vol_ratio"] = feats["rvol24"] / feats["rvol72"].replace(0, np.nan)
# position of close within recent window (0=low,1=high)
for w in (24, 72):
lo = l.rolling(w).min()
hi = h.rolling(w).max()
feats[f"pos{w}"] = (c - lo) / (hi - lo).replace(0, np.nan)
# volume z-score
vlog = np.log1p(v)
feats["volz"] = (vlog - vlog.rolling(72).mean()) / vlog.rolling(72).std().replace(0, np.nan)
names = list(feats.keys())
X = np.column_stack([feats[k].to_numpy(dtype=float) for k in names])
valid = np.isfinite(X).all(axis=1)
return X, names, valid
def forward_labels(df: pd.DataFrame, H: int):
"""label[i] = 1 if close[i+H] > close[i] else 0 ; fwd[i] = forward return."""
c = df["close"].to_numpy(float)
n = len(c)
fwd = np.full(n, np.nan)
fwd[: n - H] = c[H:] / c[: n - H] - 1.0
y = (fwd > 0).astype(float)
lab_valid = np.isfinite(fwd)
return y, fwd, lab_valid
# ---------------------------------------------------------------------------
# Strict walk-forward probability
# ---------------------------------------------------------------------------
def walk_forward_proba(X, y, feat_valid, lab_valid, warmup, W, H, K, model_factory):
"""Return proba_up[i] for all i (NaN where not predicted). No leakage:
when predicting block starting at b, training labels must be realized: i + H <= b-1,
i.e. train indices < b - H. Training window is the last W such indices."""
n = len(y)
proba = np.full(n, np.nan)
start = warmup + W + H
b = start
while b < n:
end_block = min(b + K, n)
train_hi = b - H # exclusive; ensures label realized by b-1
train_lo = max(warmup, train_hi - W)
idx = np.arange(train_lo, train_hi)
idx = idx[feat_valid[idx] & lab_valid[idx]]
if len(idx) >= MIN_TRAIN:
ytr = y[idx]
if np.unique(ytr).size == 2:
Xtr = X[idx]
sc = StandardScaler().fit(Xtr)
model = model_factory()
model.fit(sc.transform(Xtr), ytr)
# predict the block (features known at each bar's own close)
blk = np.arange(b, end_block)
fv = feat_valid[blk]
if fv.any():
pb = model.predict_proba(sc.transform(X[blk[fv]]))[:, 1]
proba[blk[fv]] = pb
b = end_block
return proba
def proba_to_entries(proba, threshold, H, n):
"""Long if proba>0.5+thr, short if proba<0.5-thr, else flat. Hold H bars."""
entries = [None] * n
hi = 0.5 + threshold
lo = 0.5 - threshold
for i in range(n):
p = proba[i]
if not np.isfinite(p):
continue
if p > hi:
entries[i] = {"dir": 1, "tp": None, "sl": None, "max_bars": H}
elif p < lo:
entries[i] = {"dir": -1, "tp": None, "sl": None, "max_bars": H}
return entries
def mask_entries(entries, lo, hi):
"""Keep only entries with index in [lo, hi); others -> None (for IS/OOS split)."""
out = [None] * len(entries)
for i in range(lo, min(hi, len(entries))):
out[i] = entries[i]
return out
def trade_stats(df, entries, H):
"""Replicate harness no-overlap to get per-trade gross returns -> avg win/loss + long frac."""
c = df["close"].to_numpy(float)
n = len(c)
grosses = []
dirs = []
busy = -1
for i in range(n):
e = entries[i]
if e is None or i <= busy:
continue
j = min(i + H, n - 1)
g = (c[j] - c[i]) / c[i] * e["dir"]
grosses.append(g)
dirs.append(e["dir"])
busy = j
g = np.array(grosses)
if len(g) == 0:
return 0, 0.0, 0.0, 0.0, 0.0
wins = g[g > 0]
losses = g[g <= 0]
avg_w = wins.mean() if len(wins) else 0.0
avg_l = losses.mean() if len(losses) else 0.0
long_frac = float(np.mean(np.array(dirs) > 0))
return len(g), avg_w, avg_l, g.mean(), long_frac
def buy_hold(df, lo, hi):
"""Buy & hold net return over [lo,hi) bars (beta benchmark)."""
c = df["close"].to_numpy(float)
hi = min(hi, len(c))
if hi - lo < 2:
return 0.0
return c[hi - 1] / c[lo] - 1.0
# ---------------------------------------------------------------------------
# Driver
# ---------------------------------------------------------------------------
def run():
ap = argparse.ArgumentParser()
ap.add_argument("--quick", action="store_true", help="smaller grid (faster)")
ap.add_argument("--gbm", action="store_true", help="also try GradientBoosting on best LR cells")
ap.add_argument("--tf", default="1h")
args = ap.parse_args()
assets = ["BTC", "ETH"]
tf = args.tf
if args.quick:
Ws = [8000]
Hs = [12, 24]
thresholds = [0.0, 0.05, 0.10]
else:
Ws = [4000, 8000, 16000]
Hs = [6, 12, 24, 48]
thresholds = [0.0, 0.03, 0.06, 0.10]
def lr_factory():
return LogisticRegression(C=1.0, max_iter=300, class_weight="balanced")
print("=" * 100)
print(f"TRACK B — walk-forward ML tf={tf} retrain_K={RETRAIN_K} held_out_tail={HELD_OUT_FRAC:.0%}")
print(f" Ws={Ws} Hs={Hs} thresholds={thresholds} model=LogisticRegression(balanced)")
print("=" * 100)
# cache features per asset
cache = {}
for a in assets:
df = load(a, tf)
X, names, fvalid = build_features(df)
warmup = int(np.argmax(fvalid)) if fvalid.any() else 0
cache[a] = (df, X, names, fvalid, warmup)
print(f"features ({len(names)}): {names}\n")
# ---- DEV grid search (configs chosen ONLY on dev portion) ----------------
results = [] # dict rows
t0 = time.time()
for a in assets:
df, X, names, fvalid, warmup = cache[a]
n = len(df)
dev_hi = int(n * (1 - HELD_OUT_FRAC)) # dev = [0, dev_hi), held = [dev_hi, n)
for W in Ws:
for H in Hs:
y, _fwd, lvalid = forward_labels(df, H)
proba = walk_forward_proba(X, y, fvalid, lvalid, warmup, W, H,
RETRAIN_K, lr_factory)
for thr in thresholds:
ent_full = proba_to_entries(proba, thr, H, n)
ent_dev = mask_entries(ent_full, warmup, dev_hi)
m = backtest_signals(df, ent_dev, fee_rt=0.001, asset=a, tf=tf)
nt, aw, al, gmean, lf = trade_stats(df, ent_dev, H)
results.append(dict(asset=a, W=W, H=H, thr=thr, seg="DEV",
m=m, nt=nt, aw=aw, al=al, gmean=gmean,
proba=proba))
print(f" [{a}] dev grid done ({time.time()-t0:.0f}s)")
# print dev table
print("\n--- DEV walk-forward (config selection set) ---")
hdr = f"{'asset':5} {'W':>6} {'H':>3} {'thr':>5} {'trd':>5} {'wr%':>5} {'net%':>8} {'CAGR%':>7} {'Shrp':>6} {'DD%':>5} {'mkt%':>5} {'avgW%':>6} {'avgL%':>6} {'€/d':>6}"
print(hdr)
for r in sorted(results, key=lambda r: -r["m"].sharpe):
m = r["m"]
print(f"{r['asset']:5} {r['W']:>6} {r['H']:>3} {r['thr']:>5.2f} {m.n_trades:>5} "
f"{m.win_rate:>5.1f} {m.net_return*100:>+8.1f} {m.cagr*100:>+7.1f} {m.sharpe:>6.2f} "
f"{m.max_dd*100:>5.1f} {m.time_in_market*100:>5.0f} {r['aw']*100:>+6.2f} {r['al']*100:>+6.2f} "
f"{m.daily_profit(2000):>+6.2f}")
# ---- selection: positive net AND sharpe>0 on dev, then robustness ----------
pos = [r for r in results if r["m"].net_return > 0 and r["m"].sharpe > 0 and r["m"].n_trades >= 30]
pos.sort(key=lambda r: -r["m"].sharpe)
print(f"\n{len(pos)}/{len(results)} dev cells net-positive with Sharpe>0 & >=30 trades.")
# robustness: a config family (asset,W,H) is robust if positive across thresholds
fam = {}
for r in results:
fam.setdefault((r["asset"], r["W"], r["H"]), []).append(r)
robust_fams = []
for key, rs in fam.items():
npos = sum(1 for r in rs if r["m"].net_return > 0 and r["m"].sharpe > 0)
if npos >= max(2, int(0.6 * len(rs))):
robust_fams.append((key, npos, len(rs)))
robust_fams.sort(key=lambda x: -x[1])
print("\nThreshold-robust (asset,W,H) families [>=60% thresholds net+ & Sharpe>0]:")
if not robust_fams:
print(" NONE.")
for key, npos, tot in robust_fams:
print(f" {key}: {npos}/{tot} thresholds positive")
# ---- HELD-OUT confirmation on best robust cells ---------------------------
print("\n" + "=" * 100)
print("HELD-OUT TAIL CONFIRMATION (never used for selection)")
print("=" * 100)
# choose up to 6 best dev cells that belong to a robust family
robust_keys = {k for k, _, _ in robust_fams}
cand = [r for r in pos if (r["asset"], r["W"], r["H"]) in robust_keys][:6]
if not cand:
cand = pos[:6]
if not cand:
print("No positive dev cells to confirm. ML did not beat fees on dev.")
print(hdr)
held_rows = []
for r in cand:
a, W, H, thr = r["asset"], r["W"], r["H"], r["thr"]
df = cache[a][0]
n = len(df)
dev_hi = int(n * (1 - HELD_OUT_FRAC))
ent_full = proba_to_entries(r["proba"], thr, H, n)
ent_held = mask_entries(ent_full, dev_hi, n)
m = backtest_signals(df, ent_held, fee_rt=0.001, asset=a, tf=tf)
nt, aw, al, gmean, lf = trade_stats(df, ent_held, H)
bh = buy_hold(df, dev_hi, n)
held_rows.append((r, m, aw, al, lf, bh))
print(f"{a:5} {W:>6} {H:>3} {thr:>5.2f} {m.n_trades:>5} {m.win_rate:>5.1f} "
f"{m.net_return*100:>+8.1f} {m.cagr*100:>+7.1f} {m.sharpe:>6.2f} {m.max_dd*100:>5.1f} "
f"{m.time_in_market*100:>5.0f} {aw*100:>+6.2f} {al*100:>+6.2f} {m.daily_profit(2000):>+6.2f} "
f"long={lf*100:>3.0f}% B&H={bh*100:>+7.1f}%")
# ---- FEE SWEEP on the held-out winners ------------------------------------
print("\n--- FEE SWEEP (held-out tail) on confirmed cells ---")
fees = [0.0005, 0.001, 0.0015, 0.002]
print(" (B&H = buy&hold over held-out tail; if net% << B&H the 'edge' is just beta)")
for r, _, _, _, _, _ in held_rows[:4]:
a, W, H, thr = r["asset"], r["W"], r["H"], r["thr"]
df = cache[a][0]
n = len(df)
dev_hi = int(n * (1 - HELD_OUT_FRAC))
ent_held = mask_entries(proba_to_entries(r["proba"], thr, H, n), dev_hi, n)
line = f" {a} W{W} H{H} thr{thr:.2f}: "
for f in fees:
m = backtest_signals(df, ent_held, fee_rt=f, asset=a, tf=tf)
line += f"[{f*100:.2f}%]net={m.net_return*100:>+6.1f}% Shrp={m.sharpe:>+4.2f} "
print(line)
# ---- per-year on the single best held-out cell ----------------------------
if held_rows:
held_rows.sort(key=lambda x: -x[1].sharpe)
r, m, aw, al, lf, bh = held_rows[0]
a, W, H, thr = r["asset"], r["W"], r["H"], r["thr"]
print(f"\n--- Per-year (best held-out): {a} W{W} H{H} thr{thr:.2f} ---")
df = cache[a][0]
n = len(df)
dev_hi = int(n * (1 - HELD_OUT_FRAC))
# full walk-forward per-year (dev+held) to see regime stability
mfull = backtest_signals(df, mask_entries(proba_to_entries(r["proba"], thr, H, n),
cache[a][4], n), fee_rt=0.001, asset=a, tf=tf)
mfull.print_summary(f"{a} W{W}H{H}thr{thr:.2f} FULL-WF")
mfull.print_yearly()
print(f"\nTotal runtime {time.time()-t0:.0f}s")
print("\n" + "=" * 100)
print("VERDICT (see docs/diary/2026-06-19-trackB-ml.md for the full write-up)")
print("=" * 100)
print(
" * A weak but REAL low-turnover directional signal exists on BTC (thinner on ETH):\n"
" large train window (W~16000) + long horizon (H~24) + high prob threshold (~0.10).\n"
" * It beats fees at 0.10% RT AND beats buy&hold on the held-out tail with a balanced\n"
" long/short mix (so it is NOT just bull-market beta). Payoff: ~53% WR, avgWin>avgLoss.\n"
" * BUT: high-turnover cells (low thr / short H / 15m) ALL die on fees -> the edge is small.\n"
" Returns concentrate in a few years (2021,2025) with a -38% year (2023); DD 23-56%.\n"
" * EUR/day on 2000 ~= +0.3..+0.6 baseline. Target is 50/day -> ~100x short. NOT deployable\n"
" standalone; at best a small component, and only the lowest-turnover configs are honest."
)
if __name__ == "__main__":
run()
+380
View File
@@ -0,0 +1,380 @@
"""TRACK C — Mean-reversion / range re-examination on CLEAN BTC/ETH (Deribit mainnet).
HONEST harness only. The OLD 'fade' library (Bollinger fade, Donchian fade, return
reversal) was an ARTIFACT of look-ahead + ghost wicks on a contaminated feed; on the
rebuilt+certified data those are negative every year. This script asks, skeptically:
Does ANY short-horizon mean-reversion / range edge survive on clean BTC/ETH with a
genuinely EXECUTABLE entry (direction + price decided with data <= close[i],
fill at close[i]), net of realistic Deribit fees, out-of-sample and grid-robust?
Methodology enforced here:
* Entry decided with data through close[i]; fill at close[i] (harness guarantees it).
No entering "at the band edge" / candle extreme only known intrabar.
* NET fees fee_rt=0.001 baseline + sweep {0.0005, 0.0015, 0.002}.
* OOS 65/35 split + parameter grid across BOTH BTC & ETH.
* Liquidity/plausibility cross-check: time-in-market, avg bars, and whether the edge
concentrates in flat (O=H=L=C heavy) periods.
Run:
uv run python scripts/research/trackC_meanrev.py # full (slow, all TFs)
uv run python scripts/research/trackC_meanrev.py --quick # 1h + 15m only
"""
from __future__ import annotations
import argparse
import sys
import time
from pathlib import Path
import numpy as np
import pandas as pd
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
from src.backtest.harness import load, backtest_signals, oos_split, Metrics
# ===========================================================================
# Indicator helpers — ALL causal: value at index i uses ONLY data through i.
# ===========================================================================
def zscore(close: np.ndarray, lookback: int) -> np.ndarray:
s = pd.Series(close)
ma = s.rolling(lookback).mean()
sd = s.rolling(lookback).std(ddof=0)
z = (s - ma) / sd
return z.values, ma.values, sd.values
def rsi(close: np.ndarray, period: int) -> np.ndarray:
s = pd.Series(close)
d = s.diff()
up = d.clip(lower=0.0)
dn = (-d).clip(lower=0.0)
# Wilder smoothing via ewm alpha=1/period (causal)
ru = up.ewm(alpha=1.0 / period, adjust=False).mean()
rd = dn.ewm(alpha=1.0 / period, adjust=False).mean()
rs = ru / rd.replace(0, np.nan)
out = 100 - 100 / (1 + rs)
return out.values
def atr(df: pd.DataFrame, period: int) -> np.ndarray:
h, l, c = df["high"].values, df["low"].values, df["close"].values
pc = np.roll(c, 1)
pc[0] = c[0]
tr = np.maximum(h - l, np.maximum(np.abs(h - pc), np.abs(l - pc)))
return pd.Series(tr).ewm(alpha=1.0 / period, adjust=False).mean().values
# ===========================================================================
# Signal generators — each returns a list[dict|None] length len(df).
# Direction/levels decided strictly with data through close[i].
# ===========================================================================
def sig_zfade(df, lookback=20, z=2.0, tp_mode="mean", tp_atr=1.0, sl_atr=2.0,
max_bars=24, atr_p=14):
"""Bollinger / z-score fade. z<-thr -> long (reversion up); z>+thr -> short.
TP at the moving mean (tp_mode='mean') or at tp_atr*ATR toward the mean.
SL at sl_atr*ATR beyond entry. Entry at close[i]."""
c = df["close"].values
z_arr, ma, _ = zscore(c, lookback)
a = atr(df, atr_p)
n = len(c)
out = [None] * n
for i in range(lookback, n):
zi = z_arr[i]
if not np.isfinite(zi) or not np.isfinite(a[i]):
continue
px = c[i]
if zi <= -z:
direction = 1
tp = ma[i] if tp_mode == "mean" else px + tp_atr * a[i]
sl = px - sl_atr * a[i] if sl_atr else None
elif zi >= z:
direction = -1
tp = ma[i] if tp_mode == "mean" else px - tp_atr * a[i]
sl = px + sl_atr * a[i] if sl_atr else None
else:
continue
# guardrail: never set TP on wrong side of entry
if direction == 1 and tp <= px:
tp = px + tp_atr * a[i]
if direction == -1 and tp >= px:
tp = px - tp_atr * a[i]
out[i] = {"dir": direction, "tp": tp, "sl": sl, "max_bars": max_bars}
return out
def sig_rsi2(df, period=2, lo=10, hi=90, tp_atr=1.0, sl_atr=2.0, max_bars=12,
atr_p=14, sma_filter=0):
"""RSI(2)-style oversold/overbought reversion. RSI<lo -> long, RSI>hi -> short.
Optional trend filter: only long above SMA(sma_filter), only short below."""
c = df["close"].values
r = rsi(c, period)
a = atr(df, atr_p)
sma = pd.Series(c).rolling(sma_filter).mean().values if sma_filter else None
n = len(c)
out = [None] * n
for i in range(max(period, atr_p, sma_filter), n):
ri = r[i]
if not np.isfinite(ri) or not np.isfinite(a[i]):
continue
px = c[i]
if ri <= lo:
if sma is not None and not (px > sma[i]):
continue
out[i] = {"dir": 1, "tp": px + tp_atr * a[i],
"sl": px - sl_atr * a[i] if sl_atr else None, "max_bars": max_bars}
elif ri >= hi:
if sma is not None and not (px < sma[i]):
continue
out[i] = {"dir": -1, "tp": px - tp_atr * a[i],
"sl": px + sl_atr * a[i] if sl_atr else None, "max_bars": max_bars}
return out
def sig_retrev(df, ret_lb=1, thr_sigma=2.0, vol_lb=50, tp_atr=1.0, sl_atr=2.0,
max_bars=6, atr_p=14):
"""Return reversal: fade an extreme cumulative return over the last ret_lb bars.
Extreme = |ret| > thr_sigma * rolling std of that return. Entry at close[i]."""
c = df["close"].values
s = pd.Series(c)
ret = np.log(s / s.shift(ret_lb))
sd = ret.rolling(vol_lb).std(ddof=0)
a = atr(df, atr_p)
n = len(c)
out = [None] * n
rv = ret.values
sv = sd.values
for i in range(vol_lb + ret_lb, n):
if not np.isfinite(rv[i]) or not np.isfinite(sv[i]) or sv[i] == 0 or not np.isfinite(a[i]):
continue
z = rv[i] / sv[i]
px = c[i]
if z <= -thr_sigma:
out[i] = {"dir": 1, "tp": px + tp_atr * a[i],
"sl": px - sl_atr * a[i] if sl_atr else None, "max_bars": max_bars}
elif z >= thr_sigma:
out[i] = {"dir": -1, "tp": px - tp_atr * a[i],
"sl": px + sl_atr * a[i] if sl_atr else None, "max_bars": max_bars}
return out
def sig_vwap(df, sess_bars=24, thr=2.0, tp_atr=1.0, sl_atr=2.0, max_bars=12, atr_p=14):
"""Rolling-VWAP distance reversion. Distance in std-of-distance units over a
rolling session window. Far above VWAP -> short, far below -> long. Entry close[i]."""
c = df["close"].values
v = df["volume"].values.astype(float)
tp = (df["high"].values + df["low"].values + c) / 3.0
pv = pd.Series(tp * v)
vol = pd.Series(v)
vwap = (pv.rolling(sess_bars).sum() / vol.rolling(sess_bars).sum()).values
dist = pd.Series(c - vwap)
dsd = dist.rolling(sess_bars).std(ddof=0).values
a = atr(df, atr_p)
n = len(c)
out = [None] * n
for i in range(sess_bars * 2, n):
if not np.isfinite(vwap[i]) or not np.isfinite(dsd[i]) or dsd[i] == 0 or not np.isfinite(a[i]):
continue
z = (c[i] - vwap[i]) / dsd[i]
px = c[i]
if z <= -thr:
out[i] = {"dir": 1, "tp": px + tp_atr * a[i],
"sl": px - sl_atr * a[i] if sl_atr else None, "max_bars": max_bars}
elif z >= thr:
out[i] = {"dir": -1, "tp": px - tp_atr * a[i],
"sl": px + sl_atr * a[i] if sl_atr else None, "max_bars": max_bars}
return out
# ===========================================================================
# Evaluation utilities
# ===========================================================================
def flat_fraction(df: pd.DataFrame) -> float:
o, h, l, c = df["open"], df["high"], df["low"], df["close"]
return float(((h == l) & (o == c)).mean())
def run_split(df, sigfn, params, fee_rt=0.001, leverage=1.0):
"""Run full / IS / OOS for a single config. Returns (full, is_, oos)."""
entries = sigfn(df, **params)
cut = oos_split(df, 0.65)
full = backtest_signals(df, entries, fee_rt=fee_rt, leverage=leverage)
df_is = df.iloc[:cut].reset_index(drop=True)
df_oos = df.iloc[cut:].reset_index(drop=True)
is_ = backtest_signals(df_is, sigfn(df_is, **params), fee_rt=fee_rt, leverage=leverage)
oos = backtest_signals(df_oos, sigfn(df_oos, **params), fee_rt=fee_rt, leverage=leverage)
return full, is_, oos
def hdr(title):
print("\n" + "=" * 92)
print(title)
print("=" * 92)
# ===========================================================================
# Main
# ===========================================================================
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--quick", action="store_true", help="1h+15m only (skip slow 5m)")
args = ap.parse_args()
t0 = time.time()
tfs = ["1h", "15m"] if args.quick else ["1h", "15m", "5m"]
assets = ["BTC", "ETH"]
# preload + liquidity sanity
data = {}
hdr("DATA / LIQUIDITY SANITY (flat-bar fraction O=H=L=C; should be ~0 on clean BTC/ETH)")
for a in assets:
for tf in tfs:
df = load(a, tf)
data[(a, tf)] = df
print(f" {a} {tf:>3s}: {len(df):>7d} bars {df['datetime'].iloc[0].date()}"
f"{df['datetime'].iloc[-1].date()} flat={flat_fraction(df)*100:5.2f}%")
# -------------------------------------------------------------------
# PASS 1 — broad screen per family on 1h, both assets (IS/OOS).
# -------------------------------------------------------------------
hdr("PASS 1 — FAMILY SCREEN on 1h (honest entry, fee_rt=0.001, lev=1). "
"Look for OOS>0 on BOTH assets.")
families = {
"ZFADE z2/mean ": (sig_zfade, dict(lookback=20, z=2.0, tp_mode="mean", sl_atr=2.0, max_bars=24)),
"ZFADE z2.5/atr": (sig_zfade, dict(lookback=20, z=2.5, tp_mode="atr", tp_atr=1.5, sl_atr=2.0, max_bars=24)),
"ZFADE z3/mean ": (sig_zfade, dict(lookback=40, z=3.0, tp_mode="mean", sl_atr=3.0, max_bars=48)),
"RSI2 10/90 ": (sig_rsi2, dict(period=2, lo=10, hi=90, tp_atr=1.0, sl_atr=2.0, max_bars=12)),
"RSI2 5/95 ": (sig_rsi2, dict(period=2, lo=5, hi=95, tp_atr=1.5, sl_atr=2.5, max_bars=12)),
"RSI2 +trend ": (sig_rsi2, dict(period=2, lo=10, hi=90, tp_atr=1.0, sl_atr=2.0, max_bars=12, sma_filter=200)),
"RETREV 2sig/6b ": (sig_retrev, dict(ret_lb=1, thr_sigma=2.0, tp_atr=1.0, sl_atr=2.0, max_bars=6)),
"RETREV 3sig/12b": (sig_retrev, dict(ret_lb=3, thr_sigma=3.0, tp_atr=1.5, sl_atr=2.5, max_bars=12)),
"VWAP 2/sess24": (sig_vwap, dict(sess_bars=24, thr=2.0, tp_atr=1.0, sl_atr=2.0, max_bars=12)),
}
for name, (fn, params) in families.items():
line = f" {name} | "
for a in assets:
df = data[(a, "1h")]
full, is_, oos = run_split(df, fn, params)
line += (f"{a}: IS={is_.net_return*100:>+6.0f}% OOS={oos.net_return*100:>+6.0f}% "
f"(tr={oos.n_trades:>4d} wr={oos.win_rate:>4.1f} shrp={oos.sharpe:>+4.1f} "
f"mkt={oos.time_in_market*100:>3.0f}% ab={oos.avg_bars:>4.1f}) ")
print(line)
# -------------------------------------------------------------------
# PASS 2 — parameter GRID on the two most-promising families (z-fade, rsi2),
# require OOS>0 on BOTH assets to count a cell as "surviving".
# -------------------------------------------------------------------
hdr("PASS 2 — GRID ROBUSTNESS (1h). A cell 'survives' only if OOS net>0 on BOTH BTC AND ETH.")
def grid(fn, base, sweep, tf="1h"):
keys = list(sweep.keys())
survivors = []
total = 0
rows = []
from itertools import product
for combo in product(*[sweep[k] for k in keys]):
params = dict(base)
params.update(dict(zip(keys, combo)))
total += 1
res = {}
for a in assets:
_, is_, oos = run_split(data[(a, tf)], fn, params)
res[a] = (is_, oos)
ok = all(res[a][1].net_return > 0 for a in assets)
both_oos = np.mean([res[a][1].net_return for a in assets]) * 100
rows.append((params, res, ok))
if ok:
survivors.append((params, res))
print(f" {fn.__name__}: {len(survivors)}/{total} cells with OOS>0 on BOTH assets")
# show best few by mean OOS
rows.sort(key=lambda r: np.mean([r[1][a][1].net_return for a in assets]), reverse=True)
for params, res, ok in rows[:6]:
tag = "OK " if ok else " -"
pp = {k: params[k] for k in sweep}
s = f" {tag} {pp} | "
for a in assets:
oos = res[a][1]
s += f"{a} OOS={oos.net_return*100:>+6.0f}% (wr={oos.win_rate:>4.1f} shrp={oos.sharpe:>+4.1f}) "
print(s)
return survivors
zsurv = grid(sig_zfade,
dict(tp_mode="mean", max_bars=24),
dict(lookback=[20, 40, 60], z=[2.0, 2.5, 3.0], sl_atr=[2.0, 3.0]))
rsurv = grid(sig_rsi2,
dict(period=2, tp_atr=1.0),
dict(lo=[5, 10, 15], hi=[85, 90, 95], sl_atr=[2.0, 3.0], max_bars=[6, 12]))
# -------------------------------------------------------------------
# PASS 3 — FEE SWEEP on whatever looks least-bad (z-fade z2/mean) to show fee
# sensitivity (MR is high-frequency: fees are first-order).
# -------------------------------------------------------------------
hdr("PASS 3 — FEE SWEEP (z-fade lookback=20 z=2 mean, 1h). fee=0 is GROSS: is there\n"
" ANY edge before fees, or is the fade direction itself wrong on clean data?")
fees = [0.0, 0.0005, 0.001, 0.0015, 0.002]
base = dict(lookback=20, z=2.0, tp_mode="mean", sl_atr=2.0, max_bars=24)
for a in assets:
df = data[(a, "1h")]
line = f" {a}: "
for f in fees:
full, is_, oos = run_split(df, sig_zfade, base, fee_rt=f)
line += f"fee={f*1000:.1f}bp→ full={full.net_return*100:>+6.0f}% OOS={oos.net_return*100:>+6.0f}% "
print(line)
# -------------------------------------------------------------------
# PASS 4 — faster TFs (15m, 5m) on the canonical z-fade, to test the "more MR
# opportunities" hypothesis vs the "fee death" reality.
# -------------------------------------------------------------------
hdr("PASS 4 — z-fade across timeframes (lookback=20 z=2 mean). Faster TF = more fees.")
for tf in tfs:
for a in assets:
df = data[(a, tf)]
full, is_, oos = run_split(df, sig_zfade, base)
print(f" {a} {tf:>3s}: full={full.net_return*100:>+7.0f}% IS={is_.net_return*100:>+7.0f}% "
f"OOS={oos.net_return*100:>+7.0f}% tr={full.n_trades:>5d} wr={full.win_rate:>4.1f}% "
f"shrp={full.sharpe:>+4.1f} mkt={full.time_in_market*100:>3.0f}% €/d={full.daily_profit(2000):>+5.2f}")
# -------------------------------------------------------------------
# PASS 5 — SESSION / overnight effect (UTC hour-of-day) on 1h returns.
# Pure descriptive: is there a systematically mean-reverting hour bucket?
# -------------------------------------------------------------------
hdr("PASS 5 — UTC hour-of-day next-bar return autocorrelation (descriptive, no trade).")
for a in assets:
df = data[(a, "1h")]
c = df["close"].values
ret = pd.Series(np.log(c[1:] / c[:-1])) # ret[k] = log(c[k+1]/c[k])
prev = ret.shift(1)
hours = df["datetime"].dt.hour.values[1:1 + len(ret)]
tmp = pd.DataFrame({"h": hours[:len(ret)], "r": ret.values, "p": prev.values}).dropna()
# autocorr of consecutive bar returns per hour bucket (negative = mean-reverting)
ac = tmp.groupby("h").apply(lambda g: g["r"].corr(g["p"]) if len(g) > 30 else np.nan)
worst = ac.nsmallest(3)
best = ac.nlargest(3)
print(f" {a}: most mean-reverting UTC hours (neg autocorr): "
+ ", ".join(f"{int(h)}h={v:+.3f}" for h, v in worst.items())
+ " | most trending: "
+ ", ".join(f"{int(h)}h={v:+.3f}" for h, v in best.items()))
# -------------------------------------------------------------------
# VERDICT
# -------------------------------------------------------------------
hdr("VERDICT")
n_surv = len(zsurv) + len(rsurv)
if n_surv == 0:
print(" No grid cell produced OOS net>0 on BOTH BTC and ETH at baseline fees.")
print(" => Consistent with the reset thesis: the old MR 'edge' was a feed artifact.")
print(" On clean Deribit data with honest executable entry, short-horizon MR is NOT")
print(" a robust net-positive edge. (See per-pass tables above for the evidence.)")
else:
print(f" {n_surv} grid cell(s) survived OOS>0 on both assets. Inspect above; then stress")
print(" with fee sweep / faster TFs before believing. Surviving configs:")
for params, res in (zsurv + rsurv):
ms = np.mean([res[a][1].net_return for a in assets]) * 100
print(f" {params} meanOOS={ms:+.0f}%")
print(f"\n (elapsed {time.time()-t0:.0f}s)")
if __name__ == "__main__":
main()
+118
View File
@@ -0,0 +1,118 @@
"""ADVERSARIAL LOOK-AHEAD / EXECUTION-LAG AUDIT of the trend portfolio across timeframes.
Motivation (2026-06-19): a look-ahead bug (ffill mixed-timeframe on open-labeled bars) can
inflate sub-daily Sharpe (e.g. 4h to ~1.6 vs a real ~1.1). This audit stress-tests OUR pipeline:
1. EXECUTION LAG: standard book holds the position decided at close[i] during bar i+1.
We re-run with an EXTRA bar of delay (held during i+2) i.e. you cannot trade exactly at
the close; there is one bar of slippage/latency. A genuine slow-trend edge barely moves; a
timing artifact collapses. We sweep lag = 1 (standard) and 2 (conservative).
2. RELABEL TEST: resample with label='left' (open-labeled, our default) vs label='right'
(close-labeled). The realized Sharpe must be (near) identical; a large gap => the labeling
leaks information.
Conclusion target: identify the timeframe below which costs + lag dominate (don't deploy there).
Run: uv run python scripts/research/trackD_lookahead_audit.py
"""
from __future__ import annotations
import sys
from pathlib import Path
import numpy as np
import pandas as pd
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
from src.backtest.harness import load
from src.strategies.trend_portfolio import simple_returns, realized_vol, tsmom_blend
ASSETS = ["BTC", "ETH"]
FEE_SIDE = 0.0005
TARGET_VOL = 0.20
LEVERAGE = 2.0
LONG_ONLY = True
TFS = {"4h": ("4h", 6), "6h": ("6h", 4), "8h": ("8h", 3), "12h": ("12h", 2), "1d": ("1D", 1)}
def resample(df1h: pd.DataFrame, rule: str, label: str) -> pd.DataFrame:
g = df1h.copy()
idx = pd.to_datetime(g["timestamp"], unit="ms", utc=True)
idx.name = "dt"
g.index = idx
out = g.resample(rule, label=label, closed="left").agg(
{"open": "first", "high": "max", "low": "min", "close": "last", "volume": "sum"})
out = out.dropna(subset=["open"])
out["datetime"] = out.index
return out.reset_index(drop=True)
def target_series(c, bpd):
bpy = bpd * 365.25
r = simple_returns(c)
vol = realized_vol(r, 30 * bpd, bpy)
direction = np.clip(tsmom_blend(c, (30 * bpd, 90 * bpd, 180 * bpd)), 0, None) if LONG_ONLY \
else tsmom_blend(c, (30 * bpd, 90 * bpd, 180 * bpd))
scal = np.where((vol > 0) & np.isfinite(vol), TARGET_VOL / vol, 0.0)
tgt = np.clip(direction * scal, -LEVERAGE, LEVERAGE)
tgt[~np.isfinite(tgt)] = 0.0
return tgt, r
def sleeve_net(df, bpd, lag):
"""net[t] uses position decided at close[t-lag] (lag>=1). lag=1 = standard, lag=2 = +1 delay."""
c = df["close"].values.astype(float)
tgt, r = target_series(c, bpd)
pos = np.zeros(len(tgt))
pos[lag:] = tgt[:-lag]
gross = pos * r
turn = np.abs(np.diff(pos, prepend=0.0))
net = gross - FEE_SIDE * turn
net[:lag] = 0.0
return np.clip(net, -0.99, None), pd.to_datetime(df["datetime"])
def portfolio_metrics(dfs, bpd, lag):
series = {}
for a in ASSETS:
net, ts = sleeve_net(dfs[a], bpd, lag)
series[a] = pd.Series(net, index=pd.to_datetime(ts.values))
J = pd.concat(series, axis=1, join="inner").dropna()
combo = 0.5 * J["BTC"].values + 0.5 * J["ETH"].values
bpy = bpd * 365.25
sh = float(np.mean(combo) / np.std(combo) * np.sqrt(bpy)) if np.std(combo) > 0 else 0.0
eq = np.cumprod(1.0 + np.clip(combo, -0.99, None))
dd = float(np.max((np.maximum.accumulate(eq) - eq) / np.maximum.accumulate(eq)))
yrs = (J.index[-1] - J.index[0]).days / 365.25
cagr = eq[-1] ** (1 / yrs) - 1
return sh, dd, cagr
def main():
raw = {a: load(a, "1h") for a in ASSETS}
print("=" * 96)
print("# LOOK-AHEAD / EXECUTION-LAG AUDIT — trend portfolio (long-flat, tvol20, lev2), per timeframe")
print("# lag1 = standard (decision held next bar). lag2 = +1 bar execution delay (conservative).")
print("# left/right = resample label (open vs close). Big gap => labeling leak.")
print("=" * 96)
print(f" {'TF':<5s}{'Sh lag1(L)':>12s}{'Sh lag2(L)':>12s}{'Sh lag1(R)':>12s}"
f"{'CAGR l1':>10s}{'CAGR l2':>10s}{'DD l1':>8s}{'lag-decay':>11s}")
for tf, (rule, bpd) in TFS.items():
dfsL = {a: resample(raw[a], rule, "left") for a in ASSETS}
dfsR = {a: resample(raw[a], rule, "right") for a in ASSETS}
sh1L, dd1, cagr1 = portfolio_metrics(dfsL, bpd, 1)
sh2L, _, cagr2 = portfolio_metrics(dfsL, bpd, 2)
sh1R, _, _ = portfolio_metrics(dfsR, bpd, 1)
decay = (sh1L - sh2L) / sh1L * 100 if sh1L else 0.0
flag = " <-- robust" if sh2L >= 0.9 * sh1L and abs(sh1L - sh1R) < 0.1 else ""
print(f" {tf:<5s}{sh1L:>12.2f}{sh2L:>12.2f}{sh1R:>12.2f}"
f"{cagr1*100:>+9.1f}%{cagr2*100:>+9.1f}%{dd1*100:>7.1f}%{decay:>+10.0f}%{flag}")
print("\n Interpretation:")
print(" - If Sh lag2 << Sh lag1 (big lag-decay), the edge needs to trade AT the close -> sub-TF")
print(" timing artifact / cost-fragile. Robust slow-trend should barely move with +1 bar.")
print(" - If Sh lag1(left) != Sh lag1(right), the bar LABELING leaks -> look-ahead. Should match.")
if __name__ == "__main__":
main()
+176
View File
@@ -0,0 +1,176 @@
"""TRACK D on DIFFERENT TIMEFRAMES — per-year PnL and per-year max drawdown.
Takes the winning config (TSMOM 1-3-6 month blend, vol-target 20%, leverage cap 2x,
50/50 BTC+ETH portfolio) and runs it across timeframes 15m / 1h / 4h / 1d.
Honesty preserved: same building blocks as trackD_trendport.py (positions shifted +1 bar,
fee 0.10% RT on turnover, vol-targeting on past-only realized vol). Horizons are kept
CALENDAR-consistent across TFs (1/3/6 months -> bars = months*30*bars_per_day), so we test
the SAME economic strategy sampled at different frequencies, not different strategies.
4h/1d are RESAMPLED from the certified 1h feed (00:00 UTC boundaries).
Run: uv run python scripts/research/trackD_timing.py
"""
from __future__ import annotations
import sys
from pathlib import Path
import numpy as np
import pandas as pd
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
from src.backtest.harness import load
from scripts.research.trackD_trendport import (
simple_returns, realized_vol, sig_tsmom_blend, build_target,
equity_from_target,
)
ASSETS = ["BTC", "ETH"]
FEE_SIDE = 0.0005 # 0.05%/side = 0.10% RT
TARGET_VOL = 0.20
LEVERAGE = 2.0
# timeframe -> (load_tf, resample_rule_or_None, bars_per_day)
TIMEFRAMES = {
"15m": ("15m", None, 96),
"1h": ("1h", None, 24),
"4h": ("1h", "4h", 6),
"1d": ("1h", "1D", 1),
}
def resample_ohlc(df: pd.DataFrame, rule: str) -> pd.DataFrame:
g = df.copy()
idx = pd.to_datetime(g["timestamp"], unit="ms", utc=True)
idx.name = "dt"
g.index = idx
out = g.resample(rule, label="left", closed="left").agg(
{"open": "first", "high": "max", "low": "min", "close": "last", "volume": "sum"})
out = out.dropna(subset=["open"])
out["datetime"] = out.index
epoch = pd.Timestamp("1970-01-01", tz="UTC")
out["timestamp"] = ((out.index - epoch) // pd.Timedelta(milliseconds=1)).astype("int64")
return out.reset_index(drop=True)[["timestamp", "open", "high", "low", "close", "volume", "datetime"]]
def get_df(tf_key: str, asset: str) -> pd.DataFrame:
load_tf, rule, _ = TIMEFRAMES[tf_key]
df = load(asset, load_tf)
if rule:
df = resample_ohlc(df, rule)
return df
def run_asset(df, bars_per_day, target_vol=TARGET_VOL, leverage=LEVERAGE,
long_only=False, fee_side=FEE_SIDE):
c = df["close"].values.astype(float)
r = simple_returns(c)
bpy = bars_per_day * 365.25
# recompute building blocks at this TF's bar frequency
h1, h3, h6 = 30 * bars_per_day, 90 * bars_per_day, 180 * bars_per_day
vol_win = 30 * bars_per_day
# realized_vol / tsmom use BARS_PER_YEAR from trackD (1h) for annualization of vol;
# we must annualize with THIS tf's bpy -> compute vol locally
vol = pd.Series(r).rolling(vol_win, min_periods=vol_win // 2).std().values * np.sqrt(bpy)
direction = sig_tsmom_blend(c, horizons=(h1, h3, h6))
tgt = build_target(direction, vol, target_vol, leverage, long_only)
equity, net = equity_from_target(tgt, r, fee_side)
# discrete position SIGN for trade counting (entry = sign change to a new non-zero state)
sign = np.sign(tgt)
return dict(net=net, ts=df["datetime"], equity=equity, bpy=bpy, sign=sign, target=tgt)
def portfolio_series(sleeves):
a = pd.Series(sleeves["BTC"]["net"], index=pd.to_datetime(sleeves["BTC"]["ts"].values))
b = pd.Series(sleeves["ETH"]["net"], index=pd.to_datetime(sleeves["ETH"]["ts"].values))
j = pd.concat([a.rename("a"), b.rename("b")], axis=1, join="inner").fillna(0.0)
combo = 0.5 * j["a"].values + 0.5 * j["b"].values
idx = pd.to_datetime(j.index)
equity = np.cumprod(1.0 + np.clip(combo, -0.99, None))
return idx, combo, equity
def overall_metrics(idx, combo, equity, bpy):
rr = combo[np.isfinite(combo)]
sharpe = float(np.mean(rr) / np.std(rr) * np.sqrt(bpy)) if np.std(rr) > 0 else 0.0
peak = np.maximum.accumulate(equity)
dd = float(np.max((peak - equity) / peak))
span_days = (idx[-1] - idx[0]).total_seconds() / 86400
years = span_days / 365.25
total = equity[-1] / equity[0]
cagr = total ** (1 / years) - 1 if years > 0 and total > 0 else -1.0
daily_2k = (2000 * total - 2000) / span_days if span_days > 0 else 0.0
return dict(sharpe=sharpe, max_dd=dd, cagr=cagr, total=total - 1, daily_2k=daily_2k)
def per_year(idx, equity):
"""Return {year: (pnl_pct, maxdd_pct)} where maxdd is the worst drawdown WITHIN the year."""
eq = pd.Series(equity, index=idx)
out = {}
for y, g in eq.groupby(eq.index.year):
if len(g) < 2:
continue
pnl = g.iloc[-1] / g.iloc[0] - 1.0
v = g.values
peak = np.maximum.accumulate(v)
ddy = float(np.max((peak - v) / peak))
out[int(y)] = (float(pnl), ddy)
return out
def trades_per_year(sleeves):
"""Count entries per year, summed across both sleeves. An 'entry' = the position SIGN
changing to a new non-zero value (flat->long, flat->short, or a direction flip)."""
counts: dict[int, int] = {}
for a in ASSETS:
sign = sleeves[a]["sign"]
ts = pd.to_datetime(sleeves[a]["ts"].values)
for i in range(1, len(sign)):
s, prev = sign[i], sign[i - 1]
if s != 0 and s != prev: # entry: from flat or opposite into a non-zero state
counts[ts[i].year] = counts.get(ts[i].year, 0) + 1
return counts
ALL_YEARS = list(range(2018, 2027))
def main():
print("=" * 118)
print("# TRACK D WINNER ACROSS TIMEFRAMES — TSMOM 1-3-6m blend, vol-target 20%, lev 2x, 50/50 BTC+ETH")
print("# fee 0.10% RT on turnover, positions +1 bar (no look-ahead). 4h/1d resampled from certified 1h.")
print("=" * 118)
for mode_long_only, mode_name in ((False, "LONG-SHORT"), (True, "LONG-FLAT")):
print("\n" + "#" * 118)
print(f"# MODE = {mode_name}")
print("#" * 118)
for tf_key in TIMEFRAMES:
bpd = TIMEFRAMES[tf_key][2]
sleeves = {a: run_asset(get_df(tf_key, a), bpd, long_only=mode_long_only)
for a in ASSETS}
idx, combo, equity = portfolio_series(sleeves)
ov = overall_metrics(idx, combo, equity, sleeves["BTC"]["bpy"])
py = per_year(idx, equity)
tpy = trades_per_year(sleeves)
total_trades = sum(tpy.values())
print(f"\n ── TF {tf_key:<3s} │ ret {ov['total']*100:>+8.0f}% CAGR {ov['cagr']*100:>+6.1f}% "
f"Sharpe {ov['sharpe']:>4.2f} maxDD {ov['max_dd']*100:>4.1f}% "
f"€/day(2k) {ov['daily_2k']:>+6.2f} trades {total_trades}")
# per-year PnL / DD / trades rows
print(f" {'PnL %':<8s}" + "".join(
(" . " if y not in py else f"{py[y][0]*100:>+7.0f}") for y in ALL_YEARS))
print(f" {'maxDD %':<8s}" + "".join(
(" . " if y not in py else f"{py[y][1]*100:>7.1f}") for y in ALL_YEARS))
print(f" {'trades':<8s}" + "".join(
(" . " if y not in py else f"{tpy.get(y,0):>7d}") for y in ALL_YEARS))
# year header for reference
print("\n " + "year ".ljust(8) + "".join(f"{y:>7d}" for y in ALL_YEARS))
if __name__ == "__main__":
main()
+460
View File
@@ -0,0 +1,460 @@
"""TRACK D — ROBUST WALK-FORWARD TREND PORTFOLIO (BTC+ETH), vol-targeted + leverage.
Thesis under test: trend-following's real value in crypto is DRAWDOWN REDUCTION vs
buy & hold (it sidesteps crashes). That lower DD lets us apply LEVERAGE and DIVERSIFY
across BTC+ETH to build a deployable, risk-adjusted EARNING system, even if each single
signal has only a modest Sharpe. Question: does a properly-built, anti-overfit trend
portfolio actually EARN robustly across regimes 2018-2026?
METHOD (strict, honest):
* NO LOOK-AHEAD. We build equity directly from a TARGET-POSITION series.
- target[i] is decided using ONLY data <= close[i].
- target[i] is HELD during the next bar (close[i] -> close[i+1]).
- bar return r[t] = close[t]/close[t-1] - 1 (uses close[t], close[t-1]; both <= t).
- pnl on bar t = target[t-1] * r[t] (shift positions by 1 -> no leakage).
- fees: fee_per_side * |target[t-1] - target[t-2]| (turnover cost, charged on rebalances).
This is the harness's documented "build your own equity from a position series" path.
* VOL-TARGETING: position = directional_signal * (target_vol / realized_vol), capped at
leverage. realized_vol uses past returns only (rolling std up to close[i]). This is the
main lever it lets a modest signal run at a controlled risk level.
* WALK-FORWARD / MULTI-REGIME: per-year returns for ALL years 2018-2026. Plus an explicit
EARLY (2018-2021) tune / LATE (2022-2026) confirm split. ONE robust param set, both assets.
* PORTFOLIO: equal-weight BTC+ETH sleeves, rebalanced each bar. Report combined Sharpe/DD/CAGR.
* GRID ROBUSTNESS: chosen config must be positive across a neighborhood AND across regimes.
* FEE & LEVERAGE SWEEP: fee/side 0.0005..0.002 (0.10..0.40% RT); leverage cap 1x..3x.
Run: uv run python scripts/research/trackD_trendport.py
"""
from __future__ import annotations
import sys
from pathlib import Path
import numpy as np
import pandas as pd
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
from src.backtest.harness import load
ASSETS = ["BTC", "ETH"]
TF = "1h"
BARS_PER_YEAR = 24 * 365.25 # 1h bars
FEE_SIDE = 0.0005 # 0.05% per side = 0.10% round trip (Deribit taker)
# horizons in 1h bars ~ 1 / 3 / 6 "months" (30d months)
H1, H3, H6 = 30 * 24, 90 * 24, 180 * 24
# ---------------------------------------------------------------------------
# Core building blocks (all <= close[i])
# ---------------------------------------------------------------------------
def simple_returns(c: np.ndarray) -> np.ndarray:
r = np.zeros(len(c))
r[1:] = c[1:] / c[:-1] - 1.0
return r
def realized_vol(r: np.ndarray, win: int) -> np.ndarray:
"""Annualized realized vol from bar returns up to and including i (no leakage)."""
vol = pd.Series(r).rolling(win, min_periods=win // 2).std().values
return vol * np.sqrt(BARS_PER_YEAR)
def sig_tsmom_blend(c: np.ndarray, horizons=(H1, H3, H6)) -> np.ndarray:
"""Multi-horizon TSMOM: average of sign(close[i]/close[i-h]-1) over horizons -> [-1,1]."""
n = len(c)
acc = np.zeros(n)
cnt = np.zeros(n)
for h in horizons:
s = np.full(n, np.nan)
s[h:] = np.sign(c[h:] / c[:-h] - 1.0)
valid = np.isfinite(s)
acc[valid] += s[valid]
cnt[valid] += 1
out = np.zeros(n)
nz = cnt > 0
out[nz] = acc[nz] / cnt[nz]
return out
def sig_ma_slope(c: np.ndarray, span: int, slope_win: int = 24) -> np.ndarray:
"""Sign of the slope of an EMA: ema[i] vs ema[i-slope_win]. -> {-1,0,+1}."""
ema = pd.Series(c).ewm(span=span, adjust=False).mean().values
n = len(c)
out = np.zeros(n)
out[slope_win:] = np.sign(ema[slope_win:] - ema[:-slope_win])
return out
def sig_donchian_state(c, h, l, n_break: int, n_exit: int) -> np.ndarray:
"""Donchian breakout with trailing (channel) stop, returns a stateful {-1,0,+1} series.
Long when close[i] > prior n_break high; exit/flip via prior n_exit low channel (trailing).
Detection uses prior-window extremes EXCLUDING current bar (shift 1) and close[i] -> honest."""
hh = pd.Series(h).rolling(n_break).max().shift(1).values
ll = pd.Series(l).rolling(n_break).min().shift(1).values
xh = pd.Series(h).rolling(n_exit).max().shift(1).values # trailing exit for shorts
xl = pd.Series(l).rolling(n_exit).min().shift(1).values # trailing exit for longs
n = len(c)
state = np.zeros(n)
pos = 0
for i in range(n):
if not np.isfinite(hh[i]):
state[i] = 0
continue
if pos == 1:
if c[i] < xl[i]:
pos = 0
elif pos == -1:
if c[i] > xh[i]:
pos = 0
if pos == 0:
if c[i] > hh[i]:
pos = 1
elif c[i] < ll[i]:
pos = -1
state[i] = pos
return state
# ---------------------------------------------------------------------------
# Position construction (vol-targeting + leverage cap + long/flat option)
# ---------------------------------------------------------------------------
def build_target(direction: np.ndarray, vol: np.ndarray, target_vol: float,
leverage: float, long_only: bool) -> np.ndarray:
"""target[i] = direction[i] * (target_vol / vol[i]), clipped to [-leverage, leverage].
direction[i] in [-1,1]; vol[i] annualized realized vol (<= close[i]). long_only clips <0 to 0."""
d = direction.copy()
if long_only:
d = np.clip(d, 0, None)
scal = np.where((vol > 0) & np.isfinite(vol), target_vol / vol, 0.0)
tgt = d * scal
tgt = np.clip(tgt, -leverage, leverage)
tgt[~np.isfinite(tgt)] = 0.0
return tgt
def equity_from_target(target: np.ndarray, r: np.ndarray, fee_side: float):
"""Build equity from a target-position series with NO look-ahead.
pos held during bar t = target[t-1]; pnl[t] = target[t-1]*r[t]; fee on turnover."""
n = len(target)
pos_held = np.zeros(n)
pos_held[1:] = target[:-1] # held during bar t = decided at close[t-1]
gross = pos_held * r
turn = np.abs(np.diff(pos_held, prepend=0.0))
net = gross - fee_side * turn
net[0] = 0.0
net = np.clip(net, -0.99, None) # cannot lose more than capital on a bar
equity = np.cumprod(1.0 + net)
return equity, net
# ---------------------------------------------------------------------------
# Metrics
# ---------------------------------------------------------------------------
def metrics(equity: np.ndarray, net: np.ndarray, ts: pd.Series) -> dict:
rr = net[np.isfinite(net)]
sharpe = float(np.mean(rr) / np.std(rr) * np.sqrt(BARS_PER_YEAR)) if np.std(rr) > 0 else 0.0
peak = np.maximum.accumulate(equity)
dd = float(np.max((peak - equity) / peak))
span_days = (ts.iloc[-1] - ts.iloc[0]).total_seconds() / 86400
years = span_days / 365.25
total = equity[-1] / equity[0]
cagr = total ** (1 / years) - 1 if years > 0 and total > 0 else -1.0
eq_s = pd.Series(equity, index=ts)
yearly = {}
for y, g in eq_s.groupby(eq_s.index.year):
if len(g) > 1 and g.iloc[0] > 0:
yearly[int(y)] = float(g.iloc[-1] / g.iloc[0] - 1)
daily_2k = (2000 * total - 2000) / span_days if span_days > 0 else 0.0
return dict(sharpe=sharpe, max_dd=dd, cagr=cagr, total=total - 1,
yearly=yearly, daily_2k=daily_2k, vol_ann=float(np.std(rr) * np.sqrt(BARS_PER_YEAR)))
def avg_gross(target: np.ndarray) -> float:
"""Average absolute position = average gross leverage actually deployed."""
t = target[np.isfinite(target)]
return float(np.mean(np.abs(t))) if len(t) else 0.0
def fmt(m, label):
return (f" {label:<34s} ret={m['total']*100:>+9.0f}% CAGR={m['cagr']*100:>+6.1f}% "
f"Sh={m['sharpe']:>5.2f} DD={m['max_dd']*100:>4.1f}% volA={m['vol_ann']*100:>4.0f}% "
f"€/d(2k)={m['daily_2k']:>+7.2f}")
# ---------------------------------------------------------------------------
# Strategy assembly
# ---------------------------------------------------------------------------
def make_direction(df: pd.DataFrame, kind: str, params: dict) -> np.ndarray:
c = df["close"].values.astype(float)
if kind == "TSMOM":
return sig_tsmom_blend(c, params.get("horizons", (H1, H3, H6)))
if kind == "MASLOPE":
return sig_ma_slope(c, params["span"], params.get("slope_win", 24))
if kind == "DONCHIAN":
h = df["high"].values.astype(float)
l = df["low"].values.astype(float)
return sig_donchian_state(c, h, l, params["n_break"], params["n_exit"])
raise ValueError(kind)
def run_asset(df, kind, params, target_vol, leverage, long_only, fee_side=FEE_SIDE):
c = df["close"].values.astype(float)
r = simple_returns(c)
vol = realized_vol(r, params.get("vol_win", 30 * 24))
direction = make_direction(df, kind, params)
tgt = build_target(direction, vol, target_vol, leverage, long_only)
equity, net = equity_from_target(tgt, r, fee_side)
ts = df["datetime"]
m = metrics(equity, net, ts)
m["target"] = tgt
m["net"] = net
m["ts"] = ts
m["equity"] = equity
return m
def buy_hold(df):
c = df["close"].values.astype(float)
r = simple_returns(c)
equity = np.cumprod(1.0 + np.clip(r, -0.99, None))
return metrics(equity, r, df["datetime"])
# ---------------------------------------------------------------------------
# Portfolio (equal-weight BTC+ETH, rebalanced each bar on common timestamps)
# ---------------------------------------------------------------------------
def portfolio(net_btc_df, net_eth_df, w=(0.5, 0.5)):
"""Combine two per-bar net-return series aligned on common timestamps."""
a = pd.Series(net_btc_df["net"], index=net_btc_df["ts"].values)
b = pd.Series(net_eth_df["net"], index=net_eth_df["ts"].values)
j = pd.concat([a.rename("a"), b.rename("b")], axis=1, join="inner").fillna(0.0)
combo = w[0] * j["a"].values + w[1] * j["b"].values
equity = np.cumprod(1.0 + np.clip(combo, -0.99, None))
ts = pd.Series(pd.to_datetime(j.index))
return metrics(equity, combo, ts)
# ---------------------------------------------------------------------------
# Reporting helpers
# ---------------------------------------------------------------------------
ALL_YEARS = list(range(2018, 2027))
def print_yearly_row(label, m):
cells = []
for y in ALL_YEARS:
v = m["yearly"].get(y)
cells.append(" . " if v is None else f"{v*100:>+6.0f}%")
print(f" {label:<26s} " + " ".join(cells))
def yearly_header():
print(f" {'config':<26s} " + " ".join(f"{y:>7d}" for y in ALL_YEARS))
# ---------------------------------------------------------------------------
# Experiments
# ---------------------------------------------------------------------------
def main():
pd.set_option("display.width", 220)
dfs = {a: load(a, TF) for a in ASSETS}
print("=" * 130)
print("# TRACK D — VOL-TARGETED TREND PORTFOLIO (BTC+ETH, 1h, Deribit certified)")
print("# Equity built from target-position series; positions shifted +1 bar (no look-ahead);")
print("# fee = 0.05%/side (0.10% RT) on turnover. Vol-targeting scales by inverse realized vol.")
print("=" * 130)
print("\n# BUY & HOLD BENCHMARK (the DD/return bar trend must beat on risk-adjusted basis)")
yearly_header()
bh = {}
for a in ASSETS:
bh[a] = buy_hold(dfs[a])
print(fmt(bh[a], f"B&H {a}"))
print_yearly_row(f"B&H {a} yearly", bh[a])
bh_port = portfolio({"net": simple_returns(dfs["BTC"]["close"].values), "ts": dfs["BTC"]["datetime"]},
{"net": simple_returns(dfs["ETH"]["close"].values), "ts": dfs["ETH"]["datetime"]})
print(fmt(bh_port, "B&H 50/50 BTC+ETH"))
print_yearly_row("B&H port yearly", bh_port)
# ----------------------------------------------------------------------
# 1. BROAD SCAN: strategies x vol-target x leverage x long-only, per asset & portfolio
# ----------------------------------------------------------------------
print("\n" + "=" * 130)
print("# 1) BROAD SCAN — per-asset & 50/50 portfolio, vol-target=20%, leverage cap 2x")
print("# (TSMOM 1-3-6m blend / MA-slope / Donchian-trailing; long-short vs long-flat)")
print("=" * 130)
strat_defs = [
("TSMOM", dict(horizons=(H1, H3, H6), vol_win=30 * 24)),
("MASLOPE", dict(span=200, slope_win=48, vol_win=30 * 24)),
("DONCHIAN", dict(n_break=200, n_exit=100, vol_win=30 * 24)),
]
for long_only in (False, True):
mode = "LONG-FLAT" if long_only else "LONG-SHORT"
print(f"\n --- {mode} ---")
for kind, params in strat_defs:
sleeves = {}
for a in ASSETS:
m = run_asset(dfs[a], kind, params, target_vol=0.20, leverage=2.0, long_only=long_only)
sleeves[a] = m
print(fmt(m, f"{kind} {a}"))
port = portfolio(sleeves["BTC"], sleeves["ETH"])
print(fmt(port, f"{kind} PORTFOLIO 50/50"))
print_yearly_row(f"{kind} port yearly", port)
# ----------------------------------------------------------------------
# 2. GRID ROBUSTNESS on the portfolio: vol-target x leverage x vol-window
# using the multi-horizon TSMOM blend (the most diversified trend signal)
# ----------------------------------------------------------------------
print("\n" + "=" * 130)
print("# 2) GRID ROBUSTNESS — TSMOM 1-3-6m blend, 50/50 portfolio (LONG-SHORT)")
print("# Sweep target-vol x leverage-cap. A real config is positive across the neighborhood.")
print("=" * 130)
hdr = " " + "tvol\\lev".ljust(8) + "".join(f"{lev:.0f}x".rjust(26) for lev in (1.0, 1.5, 2.0, 3.0))
print(hdr)
grid = {}
for tvol in (0.10, 0.15, 0.20, 0.30, 0.40):
row = f" {tvol*100:>6.0f}% "
for lev in (1.0, 1.5, 2.0, 3.0):
sleeves = {}
for a in ASSETS:
sleeves[a] = run_asset(dfs[a], "TSMOM",
dict(horizons=(H1, H3, H6), vol_win=30 * 24),
target_vol=tvol, leverage=lev, long_only=False)
port = portfolio(sleeves["BTC"], sleeves["ETH"])
grid[(tvol, lev)] = port
row += f" Sh{port['sharpe']:>4.2f} DD{port['max_dd']*100:>3.0f} C{port['cagr']*100:>+4.0f}"
print(row)
# ----------------------------------------------------------------------
# 3. HORIZON-SET robustness (is the 1-3-6m blend a plateau or a lucky combo?)
# ----------------------------------------------------------------------
print("\n" + "=" * 130)
print("# 3) HORIZON-SET ROBUSTNESS — TSMOM blend, portfolio, tvol=20% lev=2x (LONG-SHORT)")
print("=" * 130)
horizon_sets = {
"1m only": (H1,), "3m only": (H3,), "6m only": (H6,),
"1-3m": (H1, H3), "3-6m": (H3, H6), "1-3-6m": (H1, H3, H6),
"1-2-4m": (30 * 24, 60 * 24, 120 * 24), "2-4-8m": (60 * 24, 120 * 24, 240 * 24),
}
yearly_header()
for name, hs in horizon_sets.items():
sleeves = {a: run_asset(dfs[a], "TSMOM", dict(horizons=hs, vol_win=30 * 24),
target_vol=0.20, leverage=2.0, long_only=False) for a in ASSETS}
port = portfolio(sleeves["BTC"], sleeves["ETH"])
print(fmt(port, f"TSMOM {name}"))
print()
for name, hs in horizon_sets.items():
sleeves = {a: run_asset(dfs[a], "TSMOM", dict(horizons=hs, vol_win=30 * 24),
target_vol=0.20, leverage=2.0, long_only=False) for a in ASSETS}
port = portfolio(sleeves["BTC"], sleeves["ETH"])
print_yearly_row(f"{name}", port)
# ----------------------------------------------------------------------
# 4. WALK-FORWARD: EARLY (<=2021) tune / LATE (>=2022) confirm
# Same single param set for BOTH assets; we just split the equity by date.
# ----------------------------------------------------------------------
print("\n" + "=" * 130)
print("# 4) WALK-FORWARD — split portfolio equity into EARLY (2018-2021) vs LATE (2022-2026)")
print("# One param set, both assets. Both halves must earn for the edge to be regime-robust.")
print("=" * 130)
cfg = dict(kind="TSMOM", params=dict(horizons=(H1, H3, H6), vol_win=30 * 24),
target_vol=0.20, leverage=2.0, long_only=False)
sleeves = {a: run_asset(dfs[a], cfg["kind"], cfg["params"], cfg["target_vol"],
cfg["leverage"], cfg["long_only"]) for a in ASSETS}
a = pd.Series(sleeves["BTC"]["net"], index=sleeves["BTC"]["ts"].values)
b = pd.Series(sleeves["ETH"]["net"], index=sleeves["ETH"]["ts"].values)
j = pd.concat([a.rename("a"), b.rename("b")], axis=1, join="inner").fillna(0.0)
combo = 0.5 * j["a"].values + 0.5 * j["b"].values
idx = pd.to_datetime(j.index)
for lab, mask in (("EARLY 2018-2021", idx.year <= 2021), ("LATE 2022-2026", idx.year >= 2022)):
sub = combo[mask]
eq = np.cumprod(1.0 + np.clip(sub, -0.99, None))
m = metrics(eq, sub, pd.Series(idx[mask]))
print(fmt(m, lab))
print_yearly_row(f"{lab} yearly", m)
# ----------------------------------------------------------------------
# 5. FEE & LEVERAGE SWEEP on the headline portfolio config
# ----------------------------------------------------------------------
print("\n" + "=" * 130)
print("# 5) FEE & LEVERAGE SWEEP — TSMOM 1-3-6m blend portfolio, tvol=20%")
print("=" * 130)
print(" fee sweep (leverage cap 2x):")
for fee in (0.0005, 0.00075, 0.001, 0.0015, 0.002):
sleeves = {a: run_asset(dfs[a], "TSMOM", dict(horizons=(H1, H3, H6), vol_win=30 * 24),
target_vol=0.20, leverage=2.0, long_only=False, fee_side=fee)
for a in ASSETS}
port = portfolio(sleeves["BTC"], sleeves["ETH"])
print(fmt(port, f"fee/side={fee:.5f} (RT={2*fee*100:.2f}%)"))
print(" leverage sweep (fee 0.05%/side):")
for lev in (1.0, 1.5, 2.0, 2.5, 3.0):
sleeves = {a: run_asset(dfs[a], "TSMOM", dict(horizons=(H1, H3, H6), vol_win=30 * 24),
target_vol=0.20, leverage=lev, long_only=False)
for a in ASSETS}
port = portfolio(sleeves["BTC"], sleeves["ETH"])
print(fmt(port, f"leverage cap={lev:.1f}x"))
# ----------------------------------------------------------------------
# 6. HEADLINE ROBUST CONFIG — full per-year table + sleeves + portfolio
# ----------------------------------------------------------------------
print("\n" + "=" * 130)
print("# 6) HEADLINE ROBUST CONFIG: TSMOM 1-3-6m blend, vol-target 20%, leverage cap 2x, LONG-SHORT")
print("=" * 130)
yearly_header()
sleeves = {a: run_asset(dfs[a], "TSMOM", dict(horizons=(H1, H3, H6), vol_win=30 * 24),
target_vol=0.20, leverage=2.0, long_only=False) for a in ASSETS}
for a in ASSETS:
print(fmt(sleeves[a], f"sleeve {a}"))
print_yearly_row(f"sleeve {a} yearly", sleeves[a])
port = portfolio(sleeves["BTC"], sleeves["ETH"])
print(fmt(port, "PORTFOLIO 50/50"))
print_yearly_row("PORTFOLIO yearly", port)
# also long-flat headline (deployable variant — no shorts/funding complexity)
print()
sleeves_lf = {a: run_asset(dfs[a], "TSMOM", dict(horizons=(H1, H3, H6), vol_win=30 * 24),
target_vol=0.20, leverage=2.0, long_only=True) for a in ASSETS}
port_lf = portfolio(sleeves_lf["BTC"], sleeves_lf["ETH"])
print(fmt(port_lf, "PORTFOLIO 50/50 LONG-FLAT"))
print_yearly_row("PORTFOLIO LF yearly", port_lf)
# ----------------------------------------------------------------------
# 7. €/DAY ON 2000 — what leverage gets us toward 50/day, and the DD it costs
# ----------------------------------------------------------------------
print("\n" + "=" * 130)
print("# 7) PATH TO ~50 EUR/day on 2000 — the REAL lever is TARGET-VOL, not the leverage cap.")
print("# At tvol=20%% on 60-80%% crypto vol, positions stay sub-1x: the leverage cap NEVER binds.")
print("# To deploy real leverage you raise target-vol; Sharpe is ~constant, DD scales ~linearly.")
print("# 'avg gross' = mean |position| = leverage actually used. (cap fixed at 3x here)")
print("=" * 130)
print(f" {'target_vol':<12s}{'avgGross':>10s}{'CAGR':>9s}{'Sharpe':>9s}{'maxDD':>8s}"
f"{'€/day(2k,avg)':>16s}{'final/2k':>12s}")
for tvol in (0.20, 0.40, 0.60, 0.80, 1.00):
sleeves = {a: run_asset(dfs[a], "TSMOM", dict(horizons=(H1, H3, H6), vol_win=30 * 24),
target_vol=tvol, leverage=3.0, long_only=False) for a in ASSETS}
port = portfolio(sleeves["BTC"], sleeves["ETH"])
ag = 0.5 * (avg_gross(sleeves["BTC"]["target"]) + avg_gross(sleeves["ETH"]["target"]))
print(f" {tvol*100:>8.0f}% {ag:>9.2f}x{port['cagr']*100:>+8.1f}%{port['sharpe']:>9.2f}"
f"{port['max_dd']*100:>7.1f}%{port['daily_2k']:>+16.2f}{(1+port['total']):>12.1f}x")
# steady-state €/day at current capital under headline CAGR
print("\n Steady-state €/day implied by headline CAGR (NOT path-dependent), at various capital:")
sleeves = {a: run_asset(dfs[a], "TSMOM", dict(horizons=(H1, H3, H6), vol_win=30 * 24),
target_vol=0.20, leverage=2.0, long_only=False) for a in ASSETS}
port = portfolio(sleeves["BTC"], sleeves["ETH"])
g = port["cagr"]
daily_rate = (1 + g) ** (1 / 365.25) - 1
for cap in (2000, 5000, 10000, 50000, 100000):
print(f" capital={cap:>7d} ~€/day = {cap*daily_rate:>+8.2f} (CAGR={g*100:+.1f}%)")
need = 50.0 / daily_rate if daily_rate > 0 else float("inf")
print(f"\n To average ~50 EUR/day at this CAGR you'd need ~{need:,.0f} capital "
f"(at leverage 2x, maxDD~{port['max_dd']*100:.0f}%).")
print("\nDONE. See the report/diary for the honest verdict.")
if __name__ == "__main__":
main()
+526
View File
@@ -0,0 +1,526 @@
"""TRACK E — CROSS-SECTIONAL BTC↔ETH relative-value + ENSEMBLE synthesis.
Two parts, both on certified Deribit-mainnet data (only BTC/ETH), both honest:
PART 1 RELATIVE VALUE (market-neutral-ish spread trading on TWO assets):
* XS relative momentum: go long the stronger asset, short the weaker (dollar-neutral).
* ETH/BTC ratio TREND (z-momentum) and ratio MEAN-REVERSION (z-fade of log-ratio).
* Lead-lag (descriptive): does BTC's last-bar move predict ETH's next bar (and vice versa)?
All positions are decided with data <= close[i] and HELD over the NEXT bar (i->i+1):
realized PnL on bar k uses position set at k-1 -> strict 1-bar shift, NO look-ahead.
Fees are turnover-based: |Δpos| * fee_rt/2 PER LEG (a +1-1 flip = one round trip = fee_rt).
PART 2 ENSEMBLE:
Combine the genuinely-positive residual sleeves into ONE portfolio equity curve:
(S1) BTC low-turnover ML momentum (trackB best honest cell: W16000 H24 thr0.10, 1h)
(S2) Trend-1h, the only cross-asset-robust trend cell from trackA (Donchian N=200 H=12)
(S3) the best relative-value sleeve found in PART 1 (if any net-positive OOS)
Report combined Sharpe / maxDD / CAGR / EUR-per-day-on-2000 AND the sleeve correlation
matrix. A real ensemble edge must be net-positive OOS and LOWER drawdown than its parts.
Run: uv run python scripts/research/trackE_xsec_ensemble.py
uv run python scripts/research/trackE_xsec_ensemble.py --quick (skip slow ML sleeve)
uv run python scripts/research/trackE_xsec_ensemble.py --no-cache (recompute ML proba)
"""
from __future__ import annotations
import argparse
import sys
import time
from pathlib import Path
import numpy as np
import pandas as pd
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
from src.backtest.harness import load, backtest_signals, oos_split
# reuse trackB ML machinery (strict walk-forward, no leakage) and trackA donchian
from scripts.research.trackB_ml import (
build_features, forward_labels, walk_forward_proba, proba_to_entries, mask_entries,
RETRAIN_K,
)
from scripts.research.trackA_trend import sig_donchian
from sklearn.linear_model import LogisticRegression
FEE = 0.001 # 0.10% round-trip baseline (per leg for the pair)
BARS_PER_YEAR_1H = 24 * 365.25
# ===========================================================================
# Generic honest stats on a per-bar RETURN series (returns realized bar (k-1)->k)
# ===========================================================================
def equity_from_returns(rets: np.ndarray) -> np.ndarray:
eq = np.cumprod(1.0 + np.nan_to_num(rets))
return eq
def sharpe(rets: np.ndarray, bpy: float = BARS_PER_YEAR_1H) -> float:
r = rets[np.isfinite(rets)]
if len(r) < 3 or np.std(r) == 0:
return 0.0
return float(np.mean(r) / np.std(r) * np.sqrt(bpy))
def max_dd(equity: np.ndarray) -> float:
peak = np.maximum.accumulate(equity)
dd = (peak - equity) / peak
return float(np.max(dd)) if len(dd) else 0.0
def cagr(equity: np.ndarray, ts: pd.Series) -> float:
if len(equity) < 2:
return 0.0
days = (ts.iloc[-1] - ts.iloc[0]).total_seconds() / 86400
years = days / 365.25 if days > 0 else 1.0
if years <= 0 or equity[-1] <= 0:
return -1.0
return float(equity[-1] ** (1 / years) - 1)
def daily_profit(equity: np.ndarray, ts: pd.Series, capital: float = 2000.0) -> float:
if len(equity) < 2:
return 0.0
days = (ts.iloc[-1] - ts.iloc[0]).total_seconds() / 86400
if days <= 0:
return 0.0
final = capital * equity[-1] / equity[0]
return (final - capital) / days
def yearly_returns(rets: np.ndarray, ts: pd.Series) -> dict:
eq = equity_from_returns(rets)
s = pd.Series(eq, index=pd.DatetimeIndex(ts))
out = {}
for y, g in s.groupby(s.index.year):
if len(g) > 1 and g.iloc[0] > 0:
out[int(y)] = float(g.iloc[-1] / g.iloc[0] - 1)
return out
def stat_block(rets: np.ndarray, ts: pd.Series, bpy: float = BARS_PER_YEAR_1H) -> dict:
eq = equity_from_returns(rets)
return dict(
net=float(eq[-1] - 1.0), sharpe=sharpe(rets, bpy), max_dd=max_dd(eq),
cagr=cagr(eq, ts), eur_day=daily_profit(eq, ts), equity=eq,
turnover=float(np.mean(np.abs(np.diff(np.sign(rets) != 0)))), # placeholder, unused
)
# ===========================================================================
# RELATIVE-VALUE ENGINE — two legs, turnover-based fees, strict 1-bar shift.
# pos arrays are decided at close[i] (data<=i). Realized return on bar k uses pos[k-1].
# ===========================================================================
def pair_returns(cB: np.ndarray, cE: np.ndarray, posB: np.ndarray, posE: np.ndarray,
fee_rt: float = FEE) -> np.ndarray:
"""Per-bar net return series for a two-leg book. rets[k] realized on bar (k-1)->k.
Fee = (|ΔposB| + |ΔposE|) * fee_rt/2 charged when the position is (re)set."""
n = len(cB)
aretB = np.zeros(n); aretE = np.zeros(n)
aretB[1:] = cB[1:] / cB[:-1] - 1.0
aretE[1:] = cE[1:] / cE[:-1] - 1.0
rets = np.zeros(n)
for k in range(1, n):
gross = posB[k - 1] * aretB[k] + posE[k - 1] * aretE[k]
pBp = posB[k - 2] if k >= 2 else 0.0
pEp = posE[k - 2] if k >= 2 else 0.0
turn = abs(posB[k - 1] - pBp) + abs(posE[k - 1] - pEp)
rets[k] = gross - turn * fee_rt / 2.0
return rets
# --- signal builders: return (posB, posE) arrays, leg notional `leg` (gross = 2*leg) ---
def xs_momentum(cB, cE, N, hold, leg=0.5):
"""Cross-sectional momentum: long the asset with higher N-bar return, short the other."""
n = len(cB)
posB = np.zeros(n); posE = np.zeros(n)
curB = curE = 0.0
for i in range(n):
if i >= N and (i % hold == 0):
mB = cB[i] / cB[i - N] - 1.0
mE = cE[i] / cE[i - N] - 1.0
d = 1 if mB > mE else -1 # +1 => BTC stronger -> long BTC short ETH
curB = leg * d; curE = -leg * d
posB[i] = curB; posE[i] = curE
return posB, posE
def ratio_trend(cB, cE, N, hold, leg=0.5):
"""Trend on ETH/BTC ratio: ratio rising over N bars -> long ratio (long ETH, short BTC)."""
ratio = cE / cB
n = len(cB)
posB = np.zeros(n); posE = np.zeros(n)
curB = curE = 0.0
for i in range(n):
if i >= N and (i % hold == 0):
d = 1 if ratio[i] > ratio[i - N] else -1 # +1 => ratio up -> long ratio
curE = leg * d; curB = -leg * d
posB[i] = curB; posE[i] = curE
return posB, posE
def ratio_meanrev(cB, cE, lookback, z_in, z_exit, max_bars, leg=0.5):
"""Mean-reversion (z-fade) on log(ETH/BTC). z>+z_in -> short ratio; z<-z_in -> long ratio.
Exit when |z|<z_exit (reverted to mean) or after max_bars. Stateful, honest at close[i]."""
logr = np.log(cE / cB)
s = pd.Series(logr)
ma = s.rolling(lookback).mean().values
sd = s.rolling(lookback).std(ddof=0).values
z = (logr - ma) / sd
n = len(cB)
posB = np.zeros(n); posE = np.zeros(n)
state = 0 # +1 long ratio, -1 short ratio, 0 flat
bars_in = 0
for i in range(n):
if not np.isfinite(z[i]):
posB[i] = 0.0; posE[i] = 0.0; continue
if state == 0:
if z[i] >= z_in:
state = -1; bars_in = 0 # ratio too high -> short ratio
elif z[i] <= -z_in:
state = 1; bars_in = 0 # ratio too low -> long ratio
else:
bars_in += 1
if abs(z[i]) <= z_exit or bars_in >= max_bars or (state == 1 and z[i] >= z_in) \
or (state == -1 and z[i] <= -z_in):
state = 0
posE[i] = leg * state; posB[i] = -leg * state
return posB, posE
# ===========================================================================
# OOS / fee-sweep helpers for the relative-value sleeves
# ===========================================================================
def rv_eval(cB, cE, ts, build_fn, params, fee_rt=FEE, frac=0.65):
posB, posE = build_fn(cB, cE, **params)
rets = pair_returns(cB, cE, posB, posE, fee_rt=fee_rt)
cut = int(len(cB) * frac)
full = stat_block(rets, ts)
is_ = stat_block(rets[:cut], ts.iloc[:cut])
oos = stat_block(rets[cut:], ts.iloc[cut:])
# turnover: average per-bar leg turnover (both legs)
turn = (np.abs(np.diff(posB, prepend=0)) + np.abs(np.diff(posE, prepend=0)))
tstats = dict(rets=rets, posB=posB, posE=posE,
trades=int((turn > 1e-9).sum()), avg_turn=float(turn.mean()))
return full, is_, oos, tstats
def fmt(s):
return (f"net={s['net']*100:>+8.0f}% Sh={s['sharpe']:>+5.2f} DD={s['max_dd']*100:>4.0f}% "
f"CAGR={s['cagr']*100:>+6.1f}% €/d={s['eur_day']:>+6.2f}")
# ===========================================================================
# PART 1
# ===========================================================================
def part1_relative_value(quick=False):
print("=" * 104)
print("PART 1 — CROSS-SECTIONAL / RELATIVE-VALUE (BTC↔ETH, 1h, market-neutral spread)")
print("=" * 104)
b = load("BTC", "1h"); e = load("ETH", "1h")
m = pd.merge(b[["timestamp", "close"]], e[["timestamp", "close"]],
on="timestamp", suffixes=("_b", "_e")).reset_index(drop=True)
ts = pd.to_datetime(m["timestamp"], unit="ms", utc=True)
cB = m["close_b"].to_numpy(float); cE = m["close_e"].to_numpy(float)
cut = int(len(m) * 0.65)
print(f" common 1h bars: {len(m)} {ts.iloc[0].date()}{ts.iloc[-1].date()} "
f"(OOS starts {ts.iloc[cut].date()})")
rb = np.log(cB[1:] / cB[:-1]); re = np.log(cE[1:] / cE[:-1])
print(f" contemporaneous corr(BTC,ETH 1h logret) = {np.corrcoef(rb, re)[0,1]:.3f} "
f"(very high → the only tradable structure is the SPREAD)")
# ---- LEAD-LAG (descriptive, both directions, IS vs OOS) ----
print("\n -- LEAD-LAG (descriptive: does last-bar move of X predict next bar of Y?) --")
def ll(a_prev, b_next):
a = a_prev[np.isfinite(a_prev) & np.isfinite(b_next)]
bb = b_next[np.isfinite(a_prev) & np.isfinite(b_next)]
return np.corrcoef(a, bb)[0, 1] if len(a) > 30 else np.nan
print(f" corr(rB[i], rE[i+1]) = {ll(rb[:-1], re[1:]):+.4f} "
f"corr(rE[i], rB[i+1]) = {ll(re[:-1], rb[1:]):+.4f}")
print(f" corr(rB[i], rB[i+1]) = {ll(rb[:-1], rb[1:]):+.4f} "
f"corr(rE[i], rE[i+1]) = {ll(re[:-1], re[1:]):+.4f}")
print(" → |lead-lag| ~0.01-0.02: NO exploitable cross-predictive edge. Not pursued as a sleeve.")
results = {}
# ---- A) XS relative momentum grid ----
print("\n -- (A) XS RELATIVE MOMENTUM: long stronger / short weaker (dollar-neutral, gross=1) --")
print(" param FULL | OOS")
Ns = [24, 72, 168, 336] if not quick else [72, 168]
holds = [6, 24, 72] if not quick else [24, 72]
best_xs = None
for N in Ns:
for hold in holds:
full, is_, oos, tstat = rv_eval(cB, cE, ts, xs_momentum, dict(N=N, hold=hold))
ok = oos["net"] > 0 and oos["sharpe"] > 0
print(f" N={N:>3} hold={hold:>2} | {fmt(full)} | OOS {fmt(oos)} "
f"tr={tstat['trades']:>4} {'OK' if ok else ''}")
if oos["net"] > 0 and (best_xs is None or oos["sharpe"] > best_xs[2]["sharpe"]):
best_xs = (dict(N=N, hold=hold), full, oos, tstat, "xs_momentum")
results["xs_momentum"] = best_xs
# ---- B) ETH/BTC ratio TREND grid ----
print("\n -- (B) ETH/BTC RATIO TREND: long ratio when rising over N (long ETH/short BTC) --")
print(" NOTE: with only TWO assets this is ALGEBRAICALLY IDENTICAL to (A) — 'long the")
print(" stronger''trade the ratio trend'. Shown separately only to make that explicit.")
best_rt = None
for N in Ns:
for hold in holds:
full, is_, oos, tstat = rv_eval(cB, cE, ts, ratio_trend, dict(N=N, hold=hold))
ok = oos["net"] > 0 and oos["sharpe"] > 0
print(f" N={N:>3} hold={hold:>2} | {fmt(full)} | OOS {fmt(oos)} "
f"tr={tstat['trades']:>4} {'OK' if ok else ''}")
if oos["net"] > 0 and (best_rt is None or oos["sharpe"] > best_rt[2]["sharpe"]):
best_rt = (dict(N=N, hold=hold), full, oos, tstat, "ratio_trend")
results["ratio_trend"] = best_rt
# ---- C) ETH/BTC ratio MEAN-REVERSION grid ----
print("\n -- (C) ETH/BTC RATIO MEAN-REVERSION: z-fade of log(ETH/BTC) --")
best_mr = None
LBs = [48, 168, 336] if not quick else [168]
zins = [1.5, 2.0, 2.5] if not quick else [2.0]
for lb in LBs:
for zin in zins:
full, is_, oos, tstat = rv_eval(cB, cE, ts, ratio_meanrev,
dict(lookback=lb, z_in=zin, z_exit=0.5, max_bars=72))
ok = oos["net"] > 0 and oos["sharpe"] > 0
print(f" lb={lb:>3} zin={zin} | {fmt(full)} | OOS {fmt(oos)} "
f"tr={tstat['trades']:>4} {'OK' if ok else ''}")
if oos["net"] > 0 and (best_mr is None or oos["sharpe"] > best_mr[2]["sharpe"]):
best_mr = (dict(lookback=lb, z_in=zin, z_exit=0.5, max_bars=72),
full, oos, tstat, "ratio_meanrev")
results["ratio_meanrev"] = best_mr
# ---- choose the single best RV sleeve (positive OOS, highest OOS Sharpe) ----
cands = [v for v in results.values() if v is not None]
cands.sort(key=lambda v: v[2]["sharpe"], reverse=True)
best = cands[0] if cands else None
print("\n -- RELATIVE-VALUE SUMMARY (best per family that is OOS net-positive) --")
for fam in ("xs_momentum", "ratio_trend", "ratio_meanrev"):
v = results[fam]
if v is None:
print(f" {fam:<14}: no OOS net-positive cell.")
else:
params, full, oos, tstat, _ = v
print(f" {fam:<14}: {params} FULL {fmt(full)} | OOS {fmt(oos)}")
if best is None:
print("\n >> NO relative-value sleeve is OOS net-positive. No RV edge to add to the ensemble.")
return None, (cB, cE, ts)
params, full, oos, tstat, fam = best
print(f"\n >> BEST RV sleeve: {fam} {params} (OOS Sharpe {oos['sharpe']:+.2f})")
# ---- per-year + fee sweep + grid-neighbourhood robustness on the winner ----
build_fn = {"xs_momentum": xs_momentum, "ratio_trend": ratio_trend,
"ratio_meanrev": ratio_meanrev}[fam]
fullr, _, _, _ = rv_eval(cB, cE, ts, build_fn, params)
print("\n per-year (full):")
yr = yearly_returns(fullr["rets"] if False else pair_returns(cB, cE,
*build_fn(cB, cE, **params)), ts)
for y in sorted(yr):
print(f" {y}: {yr[y]*100:>+7.1f}%")
print("\n fee sweep (full-sample net, baseline 0.10% RT/leg):")
for f in (0.0, 0.0005, 0.001, 0.0015, 0.002):
fr, _, fo, _ = rv_eval(cB, cE, ts, build_fn, params, fee_rt=f)
print(f" fee={f*1000:.1f}bp/leg → FULL net={fr['net']*100:>+7.0f}% "
f"OOS net={fo['net']*100:>+7.0f}% (Sh {fo['sharpe']:+.2f})")
return best, (cB, cE, ts)
# ===========================================================================
# PART 2 — ENSEMBLE
# ===========================================================================
def lr_factory():
return LogisticRegression(C=1.0, max_iter=300, class_weight="balanced")
def ml_sleeve_btc(cache=True, no_cache=False):
"""BTC low-turnover ML momentum sleeve (trackB best honest cell W16000 H24 thr0.10)."""
W, H, thr = 16000, 24, 0.10
df = load("BTC", "1h")
cpath = Path(__file__).resolve().parent / ".cache_trackE_btc_ml_proba.npy"
proba = None
if cache and not no_cache and cpath.exists():
arr = np.load(cpath)
if len(arr) == len(df):
proba = arr
print(f" [S1 ML] loaded cached proba ({cpath.name})")
if proba is None:
print(f" [S1 ML] walk-forward LogisticRegression W{W} H{H} (slow ~1-2min)...")
t0 = time.time()
X, names, fvalid = build_features(df)
warmup = int(np.argmax(fvalid)) if fvalid.any() else 0
y, _fwd, lvalid = forward_labels(df, H)
proba = walk_forward_proba(X, y, fvalid, lvalid, warmup, W, H, RETRAIN_K, lr_factory)
np.save(cpath, proba)
print(f" [S1 ML] done ({time.time()-t0:.0f}s), cached.")
n = len(df)
entries = proba_to_entries(proba, thr, H, n)
m = backtest_signals(df, entries, fee_rt=FEE, asset="BTC", tf="1h")
return m, df, f"BTC-ML W{W}H{H}thr{thr}"
def trend_sleeve_btc():
"""Trend-1h sleeve: Donchian N=200 H=12 on BTC (the only cross-asset-robust trend cell)."""
df = load("BTC", "1h")
entries = sig_donchian(df, lookback=200, hold=12)
m = backtest_signals(df, entries, fee_rt=FEE, asset="BTC", tf="1h")
return m, df, "BTC-Trend Donchian200/12"
def metrics_to_returns(m):
"""Per-bar return series from a harness Metrics equity, indexed by its timestamps."""
eq = m.equity.astype(float)
ts = m.eq_index
rets = np.zeros(len(eq))
rets[1:] = eq[1:] / np.where(eq[:-1] == 0, np.nan, eq[:-1]) - 1.0
rets = np.nan_to_num(rets)
return pd.Series(rets, index=pd.DatetimeIndex(ts))
def part2_ensemble(rv_best, rv_data, quick=False, no_cache=False):
print("\n" + "=" * 104)
print("PART 2 — ENSEMBLE (combine weakly-correlated residual sleeves into one portfolio)")
print("=" * 104)
sleeves = {} # name -> pd.Series of per-bar returns indexed by ts
# S2 trend (fast, always)
mt, dft, tname = trend_sleeve_btc()
sleeves["S2_trend"] = metrics_to_returns(mt)
print(f" [S2] {tname:<28} net={mt.net_return*100:>+7.0f}% Sh={mt.sharpe:+.2f} "
f"DD={mt.max_dd*100:.0f}% €/d={mt.daily_profit(2000):+.2f}")
# S3 relative value (from PART 1)
if rv_best is not None:
params, full, oos, tstat, fam = rv_best
cB, cE, ts = rv_data
build_fn = {"xs_momentum": xs_momentum, "ratio_trend": ratio_trend,
"ratio_meanrev": ratio_meanrev}[fam]
posB, posE = build_fn(cB, cE, **params)
rv_rets = pair_returns(cB, cE, posB, posE, fee_rt=FEE)
sleeves["S3_relval"] = pd.Series(rv_rets, index=pd.DatetimeIndex(ts))
print(f" [S3] RV {fam} {params} net={full['net']*100:>+7.0f}% "
f"Sh={full['sharpe']:+.2f} DD={full['max_dd']*100:.0f}% €/d={full['eur_day']:+.2f}")
else:
print(" [S3] no relative-value sleeve (none was OOS net-positive in PART 1).")
# S1 ML (slow; skipped in --quick)
if not quick:
m1, df1, mlname = ml_sleeve_btc(no_cache=no_cache)
sleeves["S1_ml"] = metrics_to_returns(m1)
print(f" [S1] {mlname:<28} net={m1.net_return*100:>+7.0f}% Sh={m1.sharpe:+.2f} "
f"DD={m1.max_dd*100:.0f}% €/d={m1.daily_profit(2000):+.2f}")
else:
print(" [S1] ML sleeve SKIPPED (--quick).")
# ---- align all sleeves on a common 1h timeline (BTC clock) ----
master = sleeves["S2_trend"].index
aligned = pd.DataFrame(index=master)
for name, s in sleeves.items():
aligned[name] = s.reindex(master).fillna(0.0)
# the portfolio is only meaningful where the slowest sleeve is live.
# find first bar where each sleeve has produced non-zero activity, take the max.
starts = {}
for name in aligned.columns:
nz = np.nonzero(aligned[name].to_numpy() != 0.0)[0]
starts[name] = nz[0] if len(nz) else len(aligned)
start = max(starts.values())
aligned = aligned.iloc[start:]
ts_a = pd.Series(aligned.index)
print(f"\n Common active window: {aligned.index[0].date()}{aligned.index[-1].date()} "
f"({len(aligned)} bars). Sleeves: {list(aligned.columns)}")
# ---- sleeve correlation matrix (per-bar returns over common window) ----
print("\n SLEEVE CORRELATION MATRIX (per-bar returns, common window):")
corr = aligned.corr()
cols = list(aligned.columns)
print(" " + "".join(f"{c:>10}" for c in cols))
for c in cols:
print(f" {c:>9} " + "".join(f"{corr.loc[c, c2]:>+10.3f}" for c2 in cols))
# ---- per-sleeve stats on the COMMON window (apples-to-apples) ----
print("\n PER-SLEEVE (common window, equal $ scale):")
sl_stats = {}
for c in cols:
st = stat_block(aligned[c].to_numpy(), ts_a)
sl_stats[c] = st
print(f" {c:>9}: {fmt(st)}")
# ---- ensemble: equal-weight (honest, no in-sample tuning) ----
w = 1.0 / len(cols)
ens_eq_w = aligned.to_numpy() @ (np.ones(len(cols)) * w)
ens = stat_block(ens_eq_w, ts_a)
# ---- ensemble: inverse-vol weights (flagged: weights use full-sample vol = mild IS) ----
vols = np.array([np.std(aligned[c].to_numpy()) for c in cols])
iv = (1.0 / np.where(vols == 0, np.nan, vols))
iv = np.nan_to_num(iv); iv = iv / iv.sum()
ens_iv = stat_block(aligned.to_numpy() @ iv, ts_a)
print("\n ENSEMBLE PORTFOLIO (common window):")
best_single = max(sl_stats.values(), key=lambda s: s["sharpe"])
best_single_name = max(sl_stats, key=lambda c: sl_stats[c]["sharpe"])
print(f" best single sleeve : {best_single_name} {fmt(best_single)}")
print(f" EQUAL-WEIGHT (1/N) : {fmt(ens)}")
print(f" inverse-vol (IS wts): {fmt(ens_iv)} [weights use full-sample vol — mild in-sample]")
# ---- OOS check on the ensemble (65/35 of the common window) ----
cut = int(len(ens_eq_w) * 0.65)
ens_is = stat_block(ens_eq_w[:cut], ts_a.iloc[:cut])
ens_oos = stat_block(ens_eq_w[cut:], ts_a.iloc[cut:])
print(f"\n EQUAL-WEIGHT IS : {fmt(ens_is)}")
print(f" EQUAL-WEIGHT OOS : {fmt(ens_oos)} (OOS starts {ts_a.iloc[cut].date()})")
# per-year of the equal-weight ensemble
print("\n Equal-weight ensemble per-year:")
for y, v in sorted(yearly_returns(ens_eq_w, ts_a).items()):
print(f" {y}: {v*100:>+7.1f}%")
# ---- verdict on diversification ----
print("\n DIVERSIFICATION CHECK:")
print(f" ensemble Sharpe {ens['sharpe']:+.2f} vs best single {best_single['sharpe']:+.2f} "
f"({'BEATS' if ens['sharpe'] > best_single['sharpe'] else 'does NOT beat'} best single)")
print(f" ensemble maxDD {ens['max_dd']*100:.0f}% vs best single {best_single['max_dd']*100:.0f}% "
f"({'LOWER' if ens['max_dd'] < best_single['max_dd'] else 'NOT lower'} than best single)")
# RISK-MATCHED: lever the ensemble to the best-single maxDD, compare €/day at equal risk.
# (Sharpe is leverage-invariant; this isolates 'more return per unit of drawdown'.)
if ens["max_dd"] > 0 and best_single["eur_day"] != 0:
lev = best_single["max_dd"] / ens["max_dd"]
rm = stat_block(ens_eq_w * lev, ts_a)
print(f" RISK-MATCHED: lever ensemble {lev:.2f}x to ~{best_single['max_dd']*100:.0f}% DD "
f"→ €/d={rm['eur_day']:+.2f} (DD {rm['max_dd']*100:.0f}%) vs best-single €/d={best_single['eur_day']:+.2f}")
print(f" → at equal drawdown the ensemble earns "
f"{'MORE' if rm['eur_day'] > best_single['eur_day'] else 'LESS'} than the best single sleeve "
f"(ratio {rm['eur_day']/best_single['eur_day']:.2f}); this tracks the Sharpe ratio.")
if ens["eur_day"] > 0:
print(f" ensemble €/day(2k) {ens['eur_day']:+.2f} vs target ~50.00 "
f"→ ~{(50.0/ens['eur_day']):.0f}x short of the goal.")
else:
print(" ensemble €/day(2k) <= 0 → no earning engine.")
return ens, sl_stats, corr
# ===========================================================================
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--quick", action="store_true", help="skip slow ML sleeve + smaller RV grid")
ap.add_argument("--no-cache", action="store_true", help="recompute ML walk-forward proba")
args = ap.parse_args()
t0 = time.time()
rv_best, rv_data = part1_relative_value(quick=args.quick)
part2_ensemble(rv_best, rv_data, quick=args.quick, no_cache=args.no_cache)
print(f"\n(elapsed {time.time()-t0:.0f}s)")
print("\n" + "=" * 104)
print("See docs/diary/2026-06-19-trackE-xsec-ensemble.md for the full honest write-up.")
print("=" * 104)
if __name__ == "__main__":
main()
+365
View File
@@ -0,0 +1,365 @@
"""TRACK F — CALENDAR SEASONALITY on BTC & ETH (hour-of-day, day-of-week, interactions).
Honest test of whether there is a SYSTEMATIC, TRADEABLE calendar edge on the certified
Deribit-mainnet BTC/ETH feeds. Seasonality is the easiest place on earth to overfit
(24 hours x 7 weekdays = 168 buckets => you WILL find "significant" cells by chance), so
every claim here is held to the project's anti-look-ahead, OOS, per-year, both-assets bar.
METHODOLOGY (no shortcuts):
- ret[i] = close[i]/close[i-1]-1 is known at close[i]. A position decided at close[i]
earns ret[i+1]. We NEVER include the bar being traded (or any future bar) in the
statistic that decides the trade.
- DESCRIPTIVE tables (per-hour / per-weekday mean returns) are split IS(65%)/OOS(35%).
They are diagnostics, not trades.
- TRADEABLE rule = ADAPTIVE EXPANDING sign: at close[i] we look up the calendar bucket
of bar i+1 (the clock is known with zero look-ahead) and take the SIGN of that bucket's
mean return computed ONLY on bars <= i (expanding, warmup-gated). Long-flat or
long-short. Fees charged only on |Δposition| (turnover-aware). This lets the data pick
each bucket's sign LIVE — the honest analogue of "trade the seasonal".
- Also an in-sample-optimised discrete rule (enter at hour H, hold W bars, best dir) is
shown ONLY to demonstrate the overfit gap IS->OOS.
- NET fees fee_side baseline 0.0005 (=0.10% RT); swept 0.0005/0.00075/0.001.
- A survivor must be net-positive OOS AND across years AND on BOTH BTC & ETH.
Run: uv run python scripts/research/trackF_seasonality.py
"""
from __future__ import annotations
import sys
from pathlib import Path
import numpy as np
import pandas as pd
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
from src.backtest.harness import load # noqa: E402
ASSETS = ["BTC", "ETH"]
TF = "1h"
FEE_SIDE = 0.0005 # 0.05%/side = 0.10% round-trip
BARS_PER_DAY = 24
BPY = BARS_PER_DAY * 365.25
# ---------------------------------------------------------------------------
# helpers
# ---------------------------------------------------------------------------
def prep(asset: str, tf: str = TF):
df = load(asset, tf)
c = df["close"].values.astype(float)
ret = np.empty(len(c))
ret[0] = 0.0
ret[1:] = c[1:] / c[:-1] - 1.0
dt = pd.to_datetime(df["datetime"])
return dict(
df=df, ret=ret,
hour=dt.dt.hour.values.astype(int),
dow=dt.dt.dayofweek.values.astype(int), # 0=Mon..6=Sun
ts=dt,
)
def metrics_from_pnl(pnl: np.ndarray, ts: pd.Series):
"""pnl[i] = realized per-bar net return of the strategy (already fee-adjusted)."""
eq = np.cumprod(1.0 + np.clip(pnl, -0.99, None))
r = pnl[np.isfinite(pnl)]
sharpe = float(np.mean(r) / np.std(r) * np.sqrt(BPY)) if np.std(r) > 0 else 0.0
peak = np.maximum.accumulate(eq)
maxdd = float(np.max((peak - eq) / peak)) if len(eq) else 0.0
span_days = (ts.iloc[-1] - ts.iloc[0]).total_seconds() / 86400
years = span_days / 365.25 if span_days > 0 else 1.0
total = eq[-1] / eq[0] if len(eq) else 1.0
cagr = total ** (1 / years) - 1 if years > 0 and total > 0 else -1.0
daily_2k = (2000 * total - 2000) / span_days if span_days > 0 else 0.0
return dict(sharpe=sharpe, maxdd=maxdd, cagr=cagr, total=total - 1.0,
daily_2k=daily_2k, eq=eq)
def per_year_pnl(pnl: np.ndarray, ts: pd.Series):
s = pd.Series(pnl, index=ts.values)
out = {}
for y, g in s.groupby(s.index.year):
eq = np.cumprod(1.0 + np.clip(g.values, -0.99, None))
out[int(y)] = float(eq[-1] - 1.0)
return out
# ---------------------------------------------------------------------------
# 1. DESCRIPTIVE seasonality tables (diagnostics, IS vs OOS)
# ---------------------------------------------------------------------------
def descriptive(data, frac=0.65):
n = len(data["ret"])
cut = int(n * frac)
ret, hour, dow = data["ret"], data["hour"], data["dow"]
rows_h, rows_d = {}, {}
for h in range(24):
m_is = ret[:cut][hour[:cut] == h]
m_oos = ret[cut:][hour[cut:] == h]
rows_h[h] = (m_is.mean() * 1e4, m_oos.mean() * 1e4,
np.sign(m_is.mean()) == np.sign(m_oos.mean()))
for d in range(7):
m_is = ret[:cut][dow[:cut] == d]
m_oos = ret[cut:][dow[cut:] == d]
rows_d[d] = (m_is.mean() * 1e4, m_oos.mean() * 1e4,
np.sign(m_is.mean()) == np.sign(m_oos.mean()))
return rows_h, rows_d
# ---------------------------------------------------------------------------
# 2. ADAPTIVE EXPANDING-sign seasonal strategy (the honest tradeable test)
# ---------------------------------------------------------------------------
def adaptive_seasonal(data, bucket="hour", mode="longshort",
warmup=200, fee_side=FEE_SIDE):
"""Position at close[i] = sign of the EXPANDING past mean return of bar (i+1)'s
calendar bucket, using only bars <= i. earns ret[i+1]. Fee on |Δposition|."""
ret = data["ret"]
key = data[bucket]
n = len(ret)
nbuck = int(key.max()) + 1
sums = np.zeros(nbuck)
counts = np.zeros(nbuck)
pos = np.zeros(n)
for i in range(1, n - 1):
b = key[i]
sums[b] += ret[i]
counts[b] += 1
nb = key[i + 1]
if counts[nb] >= warmup:
m = sums[nb] / counts[nb]
if m > 0:
pos[i] = 1.0
else:
pos[i] = -1.0 if mode == "longshort" else 0.0
# pnl[i] earned over bar i+1
pnl = np.zeros(n)
prev = 0.0
for i in range(1, n - 1):
turn = abs(pos[i] - prev)
pnl[i] = pos[i] * ret[i + 1] - fee_side * turn
prev = pos[i]
return pnl, pos
def adaptive_hourxdow(data, mode="longshort", warmup=120, fee_side=FEE_SIDE):
ret, hour, dow = data["ret"], data["hour"], data["dow"]
key = hour * 7 + dow # 168 buckets
n = len(ret)
sums = np.zeros(168)
counts = np.zeros(168)
pos = np.zeros(n)
for i in range(1, n - 1):
b = key[i]
sums[b] += ret[i]
counts[b] += 1
nb = key[i + 1]
if counts[nb] >= warmup:
m = sums[nb] / counts[nb]
if m > 0:
pos[i] = 1.0
else:
pos[i] = -1.0 if mode == "longshort" else 0.0
pnl = np.zeros(n)
prev = 0.0
for i in range(1, n - 1):
turn = abs(pos[i] - prev)
pnl[i] = pos[i] * ret[i + 1] - fee_side * turn
prev = pos[i]
return pnl, pos
# ---------------------------------------------------------------------------
# 3. In-sample-optimised DISCRETE rule (to expose the overfit gap)
# ---------------------------------------------------------------------------
def discrete_hour_rule_scan(data, frac=0.65, fee_side=FEE_SIDE):
"""Scan IS for best (entry_hour, hold_window, direction) by IS Sharpe; report OOS.
A trade: enter at close of bar whose hour==H (decided with data<=close[i]), hold W
bars, exit at close. One trade per day. Fee charged round-trip on each trade.
"""
ret, hour, ts = data["ret"], data["hour"], data["ts"]
n = len(ret)
cut = int(n * frac)
def rule_pnl(H, W, direction, lo, hi):
pnl = np.zeros(n)
i = lo
last_exit = lo - 1
while i < hi:
if hour[i] == H and i > last_exit:
# cumulative return over the next W bars: prod(1+ret[i+1..i+W]) - 1
end = min(i + W, n - 1)
gross = np.prod(1.0 + ret[i + 1:end + 1]) - 1.0
pnl[i] = direction * gross - 2 * fee_side
last_exit = end
i = end
else:
i += 1
return pnl
best = None
n_tested = 0
for H in range(24):
for W in (1, 2, 3, 4, 6, 8, 12, 24):
for direction in (+1, -1):
n_tested += 1
pnl_is = rule_pnl(H, W, direction, 1, cut)
r = pnl_is[pnl_is != 0.0]
if len(r) < 50:
continue
sh = np.mean(r) / np.std(r) * np.sqrt(BPY) if np.std(r) > 0 else 0.0
if best is None or sh > best[0]:
best = (sh, H, W, direction)
sh, H, W, direction = best
pnl_oos = rule_pnl(H, W, direction, cut, n)
r_oos = pnl_oos[pnl_oos != 0.0]
sh_oos = (np.mean(r_oos) / np.std(r_oos) * np.sqrt(BPY)) if (len(r_oos) and np.std(r_oos) > 0) else 0.0
return dict(n_tested=n_tested, H=H, W=W, dir=direction, sh_is=sh,
sh_oos=sh_oos, n_is=int((rule_pnl(H, W, direction, 1, cut) != 0).sum()),
n_oos=len(r_oos), oos_mean_bp=r_oos.mean() * 1e4 if len(r_oos) else 0.0)
# ---------------------------------------------------------------------------
# reporting
# ---------------------------------------------------------------------------
def split_metrics(pnl, ts, frac=0.65):
n = len(pnl)
cut = int(n * frac)
m_is = metrics_from_pnl(pnl[:cut], ts.iloc[:cut])
m_oos = metrics_from_pnl(pnl[cut:], ts.iloc[cut:])
m_all = metrics_from_pnl(pnl, ts)
return m_is, m_oos, m_all
def turnover_per_year(pos, ts):
s = pd.Series(np.abs(np.diff(pos, prepend=0.0)), index=ts.values)
return s.groupby(s.index.year).sum().to_dict()
def main():
print("=" * 100)
print("# TRACK F — CALENDAR SEASONALITY (hour-of-day / day-of-week / hour×weekday)")
print("# certified Deribit-mainnet BTC & ETH, 1h UTC. fee_side=0.0005 (0.10% RT).")
print("# No look-ahead: bucket stats use only bars <= i; position earns ret[i+1].")
print("=" * 100)
data = {a: prep(a) for a in ASSETS}
# --- DESCRIPTIVE ---------------------------------------------------------
print("\n" + "#" * 100)
print("# 1. DESCRIPTIVE per-bucket mean returns (basis points/bar). IS=first 65%, OOS=last 35%.")
print("# 'sign?' = IS and OOS agree on sign. Diagnostics only (NOT trades, no fees).")
print("#" * 100)
for a in ASSETS:
rows_h, rows_d = descriptive(data[a])
print(f"\n ── {a} HOUR-OF-DAY (UTC) mean bp/hr ─────────────────────────────")
print(" hr : IS_bp OOS_bp sign?")
agree_h = 0
for h in range(24):
iv, ov, ag = rows_h[h]
agree_h += int(ag)
flag = " <-- US open" if h in (13, 14) else (" <-- US close" if h in (20, 21) else "")
print(f" {h:>2d} : {iv:>+6.2f} {ov:>+6.2f} {'Y' if ag else '.'}{flag}")
print(f" hour sign-agreement IS/OOS: {agree_h}/24")
print(f"\n ── {a} DAY-OF-WEEK mean bp/bar (0=Mon..6=Sun) ──────────────────")
names = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
agree_d = 0
for d in range(7):
iv, ov, ag = rows_d[d]
agree_d += int(ag)
print(f" {names[d]} : {iv:>+6.3f} {ov:>+6.3f} {'Y' if ag else '.'}")
print(f" weekday sign-agreement IS/OOS: {agree_d}/7")
# --- ADAPTIVE EXPANDING-SIGN (the honest tradeable test) ----------------
print("\n" + "#" * 100)
print("# 2. ADAPTIVE EXPANDING-SIGN seasonal strategies (HONEST tradeable test).")
print("# sign of bucket's PAST-ONLY mean decides position; fee on turnover.")
print("#" * 100)
configs = [
("HOUR long-short", "hour", "longshort", 200),
("HOUR long-flat ", "hour", "longflat", 200),
("DOW long-short", "dow", "longshort", 60),
("DOW long-flat ", "dow", "longflat", 60),
]
for label, bucket, mode, warmup in configs:
print(f"\n ── {label} ────────────────────────────────────────────────────")
for a in ASSETS:
pnl, pos = adaptive_seasonal(data[a], bucket=bucket, mode=mode, warmup=warmup)
ts = data[a]["ts"]
m_is, m_oos, m_all = split_metrics(pnl, ts)
py = per_year_pnl(pnl, ts)
yrs = "".join(f"{py.get(y, float('nan'))*100:>+6.0f}" for y in range(2019, 2027))
print(f" {a}: ALL Sh={m_all['sharpe']:>+5.2f} CAGR={m_all['cagr']*100:>+6.1f}% "
f"DD={m_all['maxdd']*100:>4.1f}% €/d={m_all['daily_2k']:>+5.2f} | "
f"IS Sh={m_is['sharpe']:>+5.2f} OOS Sh={m_oos['sharpe']:>+5.2f}")
print(f" per-year %: {yrs} (2019..2026)")
# buy-and-hold benchmark — the key control: does any 'seasonal' beat just being long?
print(f"\n ── BUY-AND-HOLD benchmark (the control for long-bias) ──")
for a in ASSETS:
ret = data[a]["ret"].copy()
ret[0] = 0.0
m = metrics_from_pnl(ret, data[a]["ts"])
print(f" {a}: Sh={m['sharpe']:>+5.2f} CAGR={m['cagr']*100:>+6.1f}% DD={m['maxdd']*100:>4.1f}% "
f" <- compare to DOW long-flat above (it's nearly identical = no edge, just long)")
# hour x weekday interaction (168 buckets — extreme overfit risk)
print(f"\n ── HOUR×WEEKDAY long-short (168 buckets, warmup 120) — overfit canary ──")
for a in ASSETS:
pnl, pos = adaptive_hourxdow(data[a], mode="longshort", warmup=120)
ts = data[a]["ts"]
m_is, m_oos, m_all = split_metrics(pnl, ts)
print(f" {a}: ALL Sh={m_all['sharpe']:>+5.2f} CAGR={m_all['cagr']*100:>+6.1f}% "
f"DD={m_all['maxdd']*100:>4.1f}% | IS Sh={m_is['sharpe']:>+5.2f} OOS Sh={m_oos['sharpe']:>+5.2f}")
# --- FEE SWEEP on the best adaptive config -------------------------------
print("\n" + "#" * 100)
print("# 3. FEE SWEEP — HOUR long-short adaptive (turnover-aware). Are survivors fee-robust?")
print("#" * 100)
for fee in (0.0, 0.0005, 0.00075, 0.001):
line = f" fee_side={fee:.5f} (RT {fee*2*100:.2f}%): "
for a in ASSETS:
pnl, _ = adaptive_seasonal(data[a], bucket="hour", mode="longshort",
warmup=200, fee_side=fee)
m = metrics_from_pnl(pnl, data[a]["ts"])
line += f"{a} Sh={m['sharpe']:>+5.2f} CAGR={m['cagr']*100:>+6.1f}% "
print(line)
# --- TURNOVER (fees are first-order for hour strategies) -----------------
print("\n" + "#" * 100)
print("# 4. TURNOVER (HOUR long-short adaptive): position flips/year (each flip costs ~fee).")
print("#" * 100)
for a in ASSETS:
_, pos = adaptive_seasonal(data[a], bucket="hour", mode="longshort", warmup=200)
tpy = turnover_per_year(pos, data[a]["ts"])
s = " ".join(f"{y}:{int(v)}" for y, v in sorted(tpy.items()))
print(f" {a} turnover units/yr: {s}")
# --- IN-SAMPLE-OPTIMISED DISCRETE RULE (overfit demonstration) ----------
print("\n" + "#" * 100)
print("# 5. IN-SAMPLE-OPTIMISED discrete rule (enter hour H, hold W, best dir).")
print("# Picked by IS Sharpe, reported OOS. Demonstrates the multiple-testing trap.")
print("#" * 100)
for a in ASSETS:
r = discrete_hour_rule_scan(data[a])
print(f" {a}: tested {r['n_tested']} (H,W,dir) cells -> best IS "
f"H={r['H']:02d} hold={r['W']}h dir={r['dir']:+d} "
f"IS Sh={r['sh_is']:>+5.2f} (n={r['n_is']}) -> OOS Sh={r['sh_oos']:>+5.2f} "
f"(n={r['n_oos']}, mean {r['oos_mean_bp']:>+.1f} bp/trade)")
# --- VERDICT -------------------------------------------------------------
print("\n" + "#" * 100)
print("# MULTIPLE-TESTING CAVEAT")
print("#" * 100)
print("""
Buckets examined: 24 hours + 7 weekdays + 168 hour×weekday = 199 calendar cells PER ASSET,
each tested IS and OOS, plus discrete grid = 24×8×2 = 384 (H,W,dir) cells per asset.
With that many cells, spurious 'significant' buckets are GUARANTEED. The honest filters
applied here: (a) adaptive sign chosen live on PAST data only (no cherry-picking),
(b) must hold OOS, (c) must hold per-year, (d) must hold on BOTH BTC AND ETH.
Read the IS->OOS Sharpe collapse and the per-year sign flips above as the real verdict.
""")
if __name__ == "__main__":
main()
+478
View File
@@ -0,0 +1,478 @@
"""TRACK G — PRIOR-PERIOD LEVEL BREAKOUTS / RANGE on CLEAN BTC/ETH (Deribit mainnet).
HONEST harness only. We test rules defined RELATIVE TO A PRIOR CALENDAR PERIOD:
* prior-DAY high/low breakout (continuation AND fade)
* opening-range breakout (first N UTC hours -> break for rest of day)
* prior-day CLOSE / gap / range-position / prior-day return-sign filter
* prior-WEEK high/low breakout
* time-anchored entries (act at a given UTC hour vs prior-day level), exit EOD/fixed/TP-SL
The single question: on clean BTC/ETH, with a genuinely EXECUTABLE entry (direction and
price decided with data <= close[i], fill at close[i], NEVER entering at the exact level
intrabar), net of realistic Deribit fees, OOS and grid-robust on BOTH assets
do prior-period breakouts CONTINUE (trend) or REVERT (fade)? Is there a deployable edge?
NO LOOK-AHEAD GUARANTEES:
* Prior-period levels are built by aggregating to daily/weekly bars and SHIFTING by one
full period (shift(1) on the closed-period frame). 'Today'/'this-week' is NEVER part of
the level. The prior period is fully closed before any bar of the current period.
* Opening-range levels are used ONLY on bars AFTER the open window has fully closed.
* Direction + price decided at close[i]; fill at close[i] (harness enforces).
Run:
uv run python scripts/research/trackG_prior_levels.py # full
uv run python scripts/research/trackG_prior_levels.py --quick # 1h only, fewer grids
"""
from __future__ import annotations
import argparse
import sys
import time
from itertools import product
from pathlib import Path
import numpy as np
import pandas as pd
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
from src.backtest.harness import load, backtest_signals, oos_split
# ===========================================================================
# Causal helpers
# ===========================================================================
def atr(df: pd.DataFrame, period: int = 14) -> np.ndarray:
h, l, c = df["high"].values, df["low"].values, df["close"].values
pc = np.roll(c, 1)
pc[0] = c[0]
tr = np.maximum(h - l, np.maximum(np.abs(h - pc), np.abs(l - pc)))
return pd.Series(tr).ewm(alpha=1.0 / period, adjust=False).mean().values
def prior_period_levels(df: pd.DataFrame, period: str = "D") -> dict:
"""Return prior-period high/low/close/open/range arrays aligned to each intraday bar.
period='D': prior calendar day (UTC). period='W': prior ISO week (anchored Mon 00:00 UTC).
Uses shift(1) on the CLOSED-period frame: the level for the current period only sees the
fully-closed previous period -> no look-ahead.
"""
dt = df["datetime"]
if period == "D":
key = dt.dt.floor("D")
elif period == "W":
key = dt.dt.floor("D") - pd.to_timedelta(dt.dt.weekday, unit="D")
else:
raise ValueError(period)
key = key.reset_index(drop=True)
agg = pd.DataFrame({
"key": key,
"high": df["high"].values, "low": df["low"].values,
"close": df["close"].values, "open": df["open"].values,
})
g = agg.groupby("key").agg(high=("high", "max"), low=("low", "min"),
close=("close", "last"), open=("open", "first")).sort_index()
gp = g.shift(1) # prior, fully-closed period
km = key.map # map current-period key -> prior-period aggregate
ph = km(gp["high"]).values.astype(float)
pl = km(gp["low"]).values.astype(float)
pc = km(gp["close"]).values.astype(float)
po = km(gp["open"]).values.astype(float)
pret = (gp["close"] / gp["open"] - 1.0) # prior-period return (sign filter)
prv = key.map(pret).values.astype(float)
return {"ph": ph, "pl": pl, "pc": pc, "po": po, "prange": ph - pl, "pret": prv}
def opening_range(df: pd.DataFrame, n_open_hours: int) -> dict:
"""Opening-range high/low for the first n_open_hours of each UTC day, plus a per-bar
flag of whether the open window has CLOSED (hour >= n_open_hours)."""
dt = df["datetime"]
date = dt.dt.floor("D")
hour = dt.dt.hour
date = date.reset_index(drop=True)
in_open = (hour < n_open_hours).values
o = pd.DataFrame({"date": date, "high": df["high"].values, "low": df["low"].values})
o_open = o[in_open]
org = o_open.groupby("date").agg(orh=("high", "max"), orl=("low", "min"))
orh = date.map(org["orh"]).values.astype(float)
orl = date.map(org["orl"]).values.astype(float)
closed = (hour >= n_open_hours).values
return {"orh": orh, "orl": orl, "closed": closed}
def bars_left_in_day(df: pd.DataFrame) -> np.ndarray:
date = df["datetime"].dt.floor("D")
grp = df.groupby(date)
idx_in_day = grp.cumcount().values
size = grp["close"].transform("size").values
return (size - idx_in_day - 1).astype(int)
# ===========================================================================
# Signal generators -> list[dict|None] length len(df). Decisions use data <= close[i].
# ===========================================================================
def sig_prior_break(df, period="D", level="high", side="cont", anchor_hour=None,
exit_mode="eod", max_bars=24, tp_atr=0.0, sl_atr=0.0, atr_p=14,
buffer=0.0):
"""Prior-period level breakout.
level='high': trigger when close[i] > prior_high*(1+buffer)
level='low' : trigger when close[i] < prior_low *(1-buffer)
side='cont' : trade IN the breakout direction (high->long, low->short)
side='fade' : trade AGAINST it (high->short, low->long)
anchor_hour : if set, only evaluate on bars at that UTC hour (time-anchored)
exit_mode : 'eod' (close at end of UTC day), 'bars' (max_bars), TP/SL via *_atr.
"""
lv = prior_period_levels(df, period)
c = df["close"].values
a = atr(df, atr_p) if (tp_atr or sl_atr) else None
bl = bars_left_in_day(df) if exit_mode == "eod" else None
hour = df["datetime"].dt.hour.values
n = len(c)
out = [None] * n
ref = lv["ph"] if level == "high" else lv["pl"]
for i in range(n):
if anchor_hour is not None and hour[i] != anchor_hour:
continue
r = ref[i]
if not np.isfinite(r):
continue
px = c[i]
if level == "high":
if not (px > r * (1.0 + buffer)):
continue
brk_dir = 1
else:
if not (px < r * (1.0 - buffer)):
continue
brk_dir = -1
direction = brk_dir if side == "cont" else -brk_dir
if exit_mode == "eod":
mb = max(int(bl[i]), 1)
else:
mb = max_bars
tp = sl = None
if a is not None and np.isfinite(a[i]):
if tp_atr:
tp = px + direction * tp_atr * a[i]
if sl_atr:
sl = px - direction * sl_atr * a[i]
out[i] = {"dir": direction, "tp": tp, "sl": sl, "max_bars": mb}
return out
def sig_or_break(df, n_open_hours=6, side="cont", exit_mode="eod", max_bars=12,
tp_atr=0.0, sl_atr=0.0, atr_p=14, buffer=0.0):
"""Opening-range breakout: after the first n_open_hours close, trade a break of the
OR high (long if cont) or OR low (short if cont). Only the FIRST break per day fires
(the harness keeps the position busy until exit)."""
orr = opening_range(df, n_open_hours)
c = df["close"].values
a = atr(df, atr_p) if (tp_atr or sl_atr) else None
bl = bars_left_in_day(df) if exit_mode == "eod" else None
n = len(c)
out = [None] * n
orh, orl, closed = orr["orh"], orr["orl"], orr["closed"]
for i in range(n):
if not closed[i] or not np.isfinite(orh[i]):
continue
px = c[i]
if px > orh[i]:
brk = 1
elif px < orl[i]:
brk = -1
else:
continue
direction = brk if side == "cont" else -brk
if exit_mode == "eod":
mb = max(int(bl[i]), 1)
else:
mb = max_bars
tp = sl = None
if a is not None and np.isfinite(a[i]):
if tp_atr:
tp = px + direction * tp_atr * a[i]
if sl_atr:
sl = px - direction * sl_atr * a[i]
out[i] = {"dir": direction, "tp": tp, "sl": sl, "max_bars": mb}
return out
def sig_gap(df, side="cont", anchor_hour=0, thr=0.0, exit_mode="eod", max_bars=24,
ret_filter=0):
"""Gap vs prior-day CLOSE, evaluated at a given UTC hour (default the first bar of the
day). gap = close[i]/prior_close - 1. If gap>thr -> up-gap; gap<-thr -> down-gap.
side='cont' trades in the gap direction; 'fade' against. ret_filter: +1 only when
prior-day return positive, -1 only when negative, 0 no filter."""
lv = prior_period_levels(df, "D")
c = df["close"].values
bl = bars_left_in_day(df) if exit_mode == "eod" else None
hour = df["datetime"].dt.hour.values
pc, pret = lv["pc"], lv["pret"]
n = len(c)
out = [None] * n
for i in range(n):
if hour[i] != anchor_hour or not np.isfinite(pc[i]):
continue
gap = c[i] / pc[i] - 1.0
if gap > thr:
g = 1
elif gap < -thr:
g = -1
else:
continue
if ret_filter and np.isfinite(pret[i]):
if ret_filter > 0 and not (pret[i] > 0):
continue
if ret_filter < 0 and not (pret[i] < 0):
continue
direction = g if side == "cont" else -g
mb = max(int(bl[i]), 1) if exit_mode == "eod" else max_bars
out[i] = {"dir": direction, "tp": None, "sl": None, "max_bars": mb}
return out
# ===========================================================================
# Evaluation
# ===========================================================================
def run_split(df, sigfn, params, fee_rt=0.001, leverage=1.0, frac=0.65):
cut = oos_split(df, frac)
full = backtest_signals(df, sigfn(df, **params), fee_rt=fee_rt, leverage=leverage)
di = df.iloc[:cut].reset_index(drop=True)
do = df.iloc[cut:].reset_index(drop=True)
is_ = backtest_signals(di, sigfn(di, **params), fee_rt=fee_rt, leverage=leverage)
oos = backtest_signals(do, sigfn(do, **params), fee_rt=fee_rt, leverage=leverage)
return full, is_, oos
def hdr(t):
print("\n" + "=" * 100)
print(t)
print("=" * 100)
# ===========================================================================
# Main
# ===========================================================================
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--quick", action="store_true")
args = ap.parse_args()
t0 = time.time()
assets = ["BTC", "ETH"]
tfs = ["1h"] if args.quick else ["1h", "15m"]
data = {}
hdr("DATA")
for a in assets:
for tf in tfs:
df = load(a, tf)
data[(a, tf)] = df
print(f" {a} {tf:>3s}: {len(df):>7d} bars {df['datetime'].iloc[0].date()}"
f"->{df['datetime'].iloc[-1].date()}")
# ---------------------------------------------------------------------
# PASS 1 — PRIOR-DAY BREAKOUT: continuation vs fade, any-bar (first break/day),
# EOD exit. THE core question: do prior-day breakouts continue or revert?
# ---------------------------------------------------------------------
hdr("PASS 1 — PRIOR-DAY HIGH/LOW breakout, any-bar first-break, EOD exit (1h, fee=0.001)\n"
" CONTINUATION vs FADE side-by-side. OOS net must be >0 on BOTH to matter.")
print(f" {'rule':<26s} | "
f"{'BTC IS / OOS (tr, wr, shrp)':<40s} | {'ETH IS / OOS (tr, wr, shrp)':<40s}")
for level in ["high", "low"]:
for side in ["cont", "fade"]:
name = f"PD {level:<4s} {side}"
line = f" {name:<26s} | "
for a in assets:
df = data[(a, "1h")]
_, is_, oos = run_split(df, sig_prior_break,
dict(period="D", level=level, side=side,
exit_mode="eod"))
line += (f"{is_.net_return*100:>+6.0f}/{oos.net_return*100:>+6.0f}% "
f"(t{oos.n_trades:>4d} w{oos.win_rate:>4.1f} s{oos.sharpe:>+4.1f}) | ")
print(line)
# ---------------------------------------------------------------------
# PASS 2 — OPENING-RANGE breakout (continuation vs fade), various open windows.
# ---------------------------------------------------------------------
hdr("PASS 2 — OPENING-RANGE breakout (first N UTC hours), EOD exit (1h, fee=0.001).\n"
" CONTINUATION vs FADE. Survivor = OOS>0 on BOTH assets.")
for nopen in ([6] if args.quick else [3, 6, 8, 12]):
for side in ["cont", "fade"]:
name = f"OR N={nopen:<2d} {side}"
line = f" {name:<26s} | "
for a in assets:
df = data[(a, "1h")]
_, is_, oos = run_split(df, sig_or_break,
dict(n_open_hours=nopen, side=side, exit_mode="eod"))
line += (f"{a} OOS={oos.net_return*100:>+6.0f}% "
f"(t{oos.n_trades:>4d} w{oos.win_rate:>4.1f} s{oos.sharpe:>+4.1f}) | ")
print(line)
# ---------------------------------------------------------------------
# PASS 3 — GAP vs prior close at day open (hour 0), continuation vs fade,
# with optional prior-day return-sign filter.
# ---------------------------------------------------------------------
hdr("PASS 3 — GAP vs prior-day CLOSE at hour 0, EOD exit (1h, fee=0.001).\n"
" continuation vs fade; thr = min |gap|.")
for thr in ([0.0] if args.quick else [0.0, 0.005, 0.01]):
for side in ["cont", "fade"]:
name = f"GAP thr={thr*100:.1f}% {side}"
line = f" {name:<26s} | "
for a in assets:
df = data[(a, "1h")]
_, is_, oos = run_split(df, sig_gap,
dict(side=side, anchor_hour=0, thr=thr, exit_mode="eod"))
line += (f"{a} OOS={oos.net_return*100:>+6.0f}% "
f"(t{oos.n_trades:>4d} w{oos.win_rate:>4.1f} s{oos.sharpe:>+4.1f}) | ")
print(line)
# ---------------------------------------------------------------------
# PASS 4 — PRIOR-WEEK high/low breakout (continuation vs fade), EOD exit.
# ---------------------------------------------------------------------
hdr("PASS 4 — PRIOR-WEEK HIGH/LOW breakout, any-bar first-break, EOD exit (1h, fee=0.001).")
for level in ["high", "low"]:
for side in ["cont", "fade"]:
name = f"PW {level:<4s} {side}"
line = f" {name:<26s} | "
for a in assets:
df = data[(a, "1h")]
_, is_, oos = run_split(df, sig_prior_break,
dict(period="W", level=level, side=side,
exit_mode="eod"))
line += (f"{a} IS={is_.net_return*100:>+6.0f}% OOS={oos.net_return*100:>+6.0f}% "
f"(t{oos.n_trades:>4d} s{oos.sharpe:>+4.1f}) | ")
print(line)
# ---------------------------------------------------------------------
# PASS 5 — TIME-ANCHORED prior-day breakout: sweep the anchor hour to expose
# whether any apparent edge is just a lucky single hour.
# ---------------------------------------------------------------------
hdr("PASS 5 — TIME-ANCHORED PD-high CONTINUATION across UTC anchor hours (1h, EOD exit).\n"
" A real edge is NOT a single lucky hour. (full-sample net per hour.)")
hours = list(range(0, 24, 1 if not args.quick else 3))
for a in assets:
df = data[(a, "1h")]
cells = []
for hh in hours:
full, _, _ = run_split(df, sig_prior_break,
dict(period="D", level="high", side="cont",
anchor_hour=hh, exit_mode="eod"))
cells.append((hh, full.net_return * 100, full.sharpe, full.n_trades))
pos = sum(1 for _, r, _, _ in cells if r > 0)
print(f" {a}: {pos}/{len(cells)} anchor-hours net>0 (full). "
f"best={max(cells, key=lambda x: x[1])[0]}h "
f"({max(c[1] for c in cells):+.0f}%) worst={min(c[1] for c in cells):+.0f}%")
line = " " + " ".join(f"{hh:02d}h:{r:>+5.0f}" for hh, r, _, _ in cells)
print(line)
# ---------------------------------------------------------------------
# PASS 6 — GRID ROBUSTNESS on the best family from PASS 1-4. We grid the
# PD-low CONTINUATION and FADE plus OR breakout, require OOS>0 on BOTH assets.
# ---------------------------------------------------------------------
hdr("PASS 6 — GRID ROBUSTNESS. Cell SURVIVES only if OOS net>0 on BOTH BTC AND ETH.")
def grid(label, fn, base, sweep, tf="1h", fee=0.001):
keys = list(sweep.keys())
rows, surv = [], []
for combo in product(*[sweep[k] for k in keys]):
params = dict(base); params.update(dict(zip(keys, combo)))
res = {}
for a in assets:
_, is_, oos = run_split(data[(a, tf)], fn, params, fee_rt=fee)
res[a] = oos
ok = all(res[a].net_return > 0 for a in assets)
rows.append((params, res, ok))
if ok:
surv.append((params, res))
print(f" [{label}] {len(surv)}/{len(rows)} cells OOS>0 on BOTH assets")
rows.sort(key=lambda r: np.mean([r[1][a].net_return for a in assets]), reverse=True)
for params, res, ok in rows[:5]:
tag = "OK " if ok else " -"
pp = {k: params[k] for k in sweep}
s = f" {tag}{pp} | "
for a in assets:
s += f"{a} OOS={res[a].net_return*100:>+6.0f}% (s{res[a].sharpe:>+4.1f}) "
print(s)
return surv
sweeps = []
sweeps.append(grid("PD-low cont", sig_prior_break,
dict(period="D", level="low", side="cont", exit_mode="eod"),
dict(buffer=[0.0, 0.001, 0.003], anchor_hour=[None])))
sweeps.append(grid("PD-low fade", sig_prior_break,
dict(period="D", level="low", side="fade", exit_mode="eod"),
dict(buffer=[0.0, 0.001, 0.003], anchor_hour=[None])))
sweeps.append(grid("PD-high cont", sig_prior_break,
dict(period="D", level="high", side="cont", exit_mode="eod"),
dict(buffer=[0.0, 0.001, 0.003], anchor_hour=[None])))
sweeps.append(grid("PD-high fade", sig_prior_break,
dict(period="D", level="high", side="fade", exit_mode="eod"),
dict(buffer=[0.0, 0.001, 0.003], anchor_hour=[None])))
if not args.quick:
sweeps.append(grid("OR cont", sig_or_break,
dict(side="cont", exit_mode="eod"),
dict(n_open_hours=[3, 6, 8, 12])))
sweeps.append(grid("OR fade", sig_or_break,
dict(side="fade", exit_mode="eod"),
dict(n_open_hours=[3, 6, 8, 12])))
# ---------------------------------------------------------------------
# PASS 7 — FEE SWEEP + per-year on the single best surviving rule (if any),
# else on the least-bad PD rule, to show fee sensitivity and year stability.
# ---------------------------------------------------------------------
hdr("PASS 7 — FEE SWEEP + PER-YEAR on the best PD rule. fee=0 is GROSS (is the SIGN of\n"
" the edge even right before fees?).")
# pick best rule: scan the 4 PD sides at default, mean OOS over assets
candidates = [
("PD low cont", dict(period="D", level="low", side="cont", exit_mode="eod")),
("PD low fade", dict(period="D", level="low", side="fade", exit_mode="eod")),
("PD high cont", dict(period="D", level="high", side="cont", exit_mode="eod")),
("PD high fade", dict(period="D", level="high", side="fade", exit_mode="eod")),
]
scored = []
for nm, p in candidates:
m = np.mean([run_split(data[(a, "1h")], sig_prior_break, p)[2].net_return for a in assets])
scored.append((m, nm, p))
scored.sort(reverse=True)
best_nm, best_p = scored[0][1], scored[0][2]
print(f" best-by-meanOOS PD rule: {best_nm} (meanOOS={scored[0][0]*100:+.0f}%)")
fees = [0.0, 0.0005, 0.001, 0.0015, 0.002]
for a in assets:
df = data[(a, "1h")]
line = f" {a} fee-sweep (RT%): "
for f in fees:
full, _, oos = run_split(df, sig_prior_break, best_p, fee_rt=f)
line += f"{f*100:.2f}%[full={full.net_return*100:>+5.0f}/OOS={oos.net_return*100:>+5.0f}] "
print(line)
print(" per-year (full sample, fee=0.001):")
for a in assets:
df = data[(a, "1h")]
full, _, _ = run_split(df, sig_prior_break, best_p)
yrs = " ".join(f"{y}:{full.yearly[y]*100:>+5.0f}%" for y in sorted(full.yearly))
print(f" {a}: trades={full.n_trades} Sharpe={full.sharpe:+.2f} "
f"maxDD={full.max_dd*100:.0f}% EUR/d(2k)={full.daily_profit(2000):+.2f}")
print(f" {yrs}")
# ---------------------------------------------------------------------
# VERDICT
# ---------------------------------------------------------------------
hdr("VERDICT")
total_surv = sum(len(s) for s in sweeps)
if total_surv == 0:
print(" ZERO grid cells produced OOS net>0 on BOTH BTC and ETH at baseline fees.")
print(" => No robust prior-period breakout/fade edge on clean BTC/ETH. The continuation-")
print(" vs-fade tables above show which SIDE (if any) is even net-positive in-sample;")
print(" consult PASS 1-5 for direction. Not deployable.")
else:
print(f" {total_surv} grid cell(s) survived OOS>0 on both assets. Inspect PASS 6/7 and")
print(" stress with fee sweep + per-year before trusting. List of survivors:")
for s in sweeps:
for params, res in s:
ms = np.mean([res[a].net_return for a in assets]) * 100
print(f" {params} meanOOS={ms:+.0f}%")
print(f"\n (elapsed {time.time()-t0:.0f}s)")
if __name__ == "__main__":
main()
+602
View File
@@ -0,0 +1,602 @@
"""TRACK H — VOLUME, RANGE & VOLATILITY-REGIME signals on CLEAN BTC/ETH (Deribit mainnet).
The single question: net of realistic Deribit fees, OOS-robust on BOTH BTC & ETH, on >=12h
timeframes (the only honest regime sub-12h is fees + HF-noise overfit + the open-label
look-ahead trap), is there ANY volume / range / volatility-regime signal that is
(a) net-positive OOS on both assets standalone, AND
(b) uncorrelated (|corr| < ~0.3) to the deployed winner TP01, AND/OR
(c) usable as a REGIME FILTER that lifts TP01's Sharpe above ~1.32 or cuts its DD?
HONESTY / NO LOOK-AHEAD:
* Everything runs on the SAME causal per-bar engine used by TP01 (net_returns): we build a
continuous TARGET position decided with data <= close[i], then HOLD it during bar i+1
(pos_held[t] = target[t-1]). Gross = pos_held * simple_return[t]; fee charged on |Δpos|.
This is identical in spirit to the harness `backtest_signals` (decide<=close[i], fill at
close[i]); we cross-check two discrete signals through `backtest_signals` too.
* Volume / range / vol features for bar i use ONLY bars <= i (rolling, prior-window, shift).
* 12h / 1d frames are resampled from the certified 1h feed via resample_tf (label='left',
closed='left') and consumed index-based with the +1 bar hold -> the open-label is never
leaked (verified in trackD_lookahead_audit.py: Sharpe is label-invariant under this hold).
Run:
uv run python scripts/research/trackH_volume_vol.py # full (12h + 1d)
uv run python scripts/research/trackH_volume_vol.py --quick # 12h only, fewer grids
"""
from __future__ import annotations
import argparse
import sys
from pathlib import Path
import numpy as np
import pandas as pd
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
from src.backtest.harness import load, backtest_signals
from src.strategies.trend_portfolio import TrendPortfolio, CANONICAL, resample_tf
ASSETS = ["BTC", "ETH"]
FEE_SIDE = 0.0005 # 0.05%/side = 0.10% RT (Deribit taker)
OOS_FRAC = 0.65
TF_BPD = {"12h": 2, "1d": 1}
# ===========================================================================
# Causal feature helpers (all use data <= i)
# ===========================================================================
def simple_returns(c: np.ndarray) -> np.ndarray:
r = np.zeros(len(c))
r[1:] = c[1:] / c[:-1] - 1.0
return r
def realized_vol(r: np.ndarray, win: int, bpy: float) -> np.ndarray:
return pd.Series(r).rolling(win, min_periods=max(2, win // 2)).std().values * np.sqrt(bpy)
def roll_max_prior(x: np.ndarray, win: int) -> np.ndarray:
"""Max over the PRIOR `win` bars (excludes current bar i)."""
return pd.Series(x).shift(1).rolling(win, min_periods=win).max().values
def roll_min_prior(x: np.ndarray, win: int) -> np.ndarray:
return pd.Series(x).shift(1).rolling(win, min_periods=win).min().values
def roll_mean_prior(x: np.ndarray, win: int) -> np.ndarray:
return pd.Series(x).shift(1).rolling(win, min_periods=win).mean().values
def vol_zscore(vol: np.ndarray, win: int) -> np.ndarray:
"""z-score of current volume vs PRIOR `win` bars (uses <= i)."""
s = pd.Series(vol)
m = s.shift(1).rolling(win, min_periods=win).mean()
sd = s.shift(1).rolling(win, min_periods=win).std()
return ((s - m) / sd).values
def atr(df: pd.DataFrame, period: int) -> np.ndarray:
h, l, c = df["high"].values, df["low"].values, df["close"].values
pc = np.roll(c, 1)
pc[0] = c[0]
tr = np.maximum(h - l, np.maximum(np.abs(h - pc), np.abs(l - pc)))
return pd.Series(tr).ewm(alpha=1.0 / period, adjust=False).mean().values
# ===========================================================================
# Per-bar net-returns engine (causal, fee on turnover) — identical to TP01.net_returns
# ===========================================================================
def net_from_target(target: np.ndarray, r: np.ndarray, fee_side: float):
"""target[i] decided with data <= close[i] -> HELD during bar i+1."""
target = np.nan_to_num(target, nan=0.0)
pos = np.zeros(len(target))
pos[1:] = target[:-1]
gross = pos * r
turn = np.abs(np.diff(pos, prepend=0.0))
net = gross - fee_side * turn
net[0] = 0.0
net = np.clip(net, -0.99, None)
return net, pos, turn
def metrics(net: np.ndarray, idx: pd.DatetimeIndex, turn: np.ndarray, bpy: float) -> dict:
rr = net[np.isfinite(net)]
sharpe = float(np.mean(rr) / np.std(rr) * np.sqrt(bpy)) if np.std(rr) > 0 else 0.0
equity = np.cumprod(1.0 + np.clip(net, -0.99, None))
peak = np.maximum.accumulate(equity)
dd = float(np.max((peak - equity) / peak)) if len(equity) else 0.0
span_days = (idx[-1] - idx[0]).total_seconds() / 86400
years = span_days / 365.25 if span_days > 0 else 1.0
total = equity[-1] / equity[0] if len(equity) else 1.0
cagr = total ** (1 / years) - 1 if years > 0 and total > 0 else -1.0
ann_turn = float(np.sum(turn)) / years if years > 0 else 0.0
return dict(sharpe=sharpe, max_dd=dd, cagr=cagr, total=total - 1,
ann_turnover=ann_turn, equity=equity, years=years)
def per_year(net: np.ndarray, idx: pd.DatetimeIndex) -> dict:
eq = pd.Series(np.cumprod(1.0 + np.clip(net, -0.99, None)), index=idx)
out = {}
for y, g in eq.groupby(eq.index.year):
if len(g) > 1 and g.iloc[0] > 0:
out[int(y)] = float(g.iloc[-1] / g.iloc[0] - 1)
return out
# ===========================================================================
# SIGNALS — each returns a continuous TARGET array (frac of equity, +/-), causal.
# ===========================================================================
def sig_vt_long(df, bpd, target_vol=0.20, vol_win_days=30, lev=2.0, **_):
"""Volatility-managed LONG: always long, sized to a vol target (no trend at all).
Tests Moreira-Muir 'volatility-managed' alpha vs plain buy-and-hold."""
c = df["close"].values.astype(float)
r = simple_returns(c)
bpy = bpd * 365.25
vol = realized_vol(r, vol_win_days * bpd, bpy)
tgt = np.where((vol > 0) & np.isfinite(vol), target_vol / vol, 0.0)
return np.clip(tgt, 0, lev)
def sig_vol_breakout(df, bpd, don=20, zwin=20, zk=1.0, long_short=False, **_):
"""Volume-confirmed Donchian breakout (continuation). Long when close > prior-`don`-bar high
AND volume z-score > zk; stay long until close < prior-`don`-bar low (then flat/short)."""
c = df["close"].values.astype(float)
h = df["high"].values.astype(float)
l = df["low"].values.astype(float)
vol = df["volume"].values.astype(float)
hi = roll_max_prior(h, don)
lo = roll_min_prior(l, don)
z = vol_zscore(vol, zwin)
up = (c > hi) & (z > zk)
dn = (c < lo) & (z > zk)
state = np.zeros(len(c))
s = 0.0
for i in range(len(c)):
if up[i]:
s = 1.0
elif dn[i]:
s = -1.0 if long_short else 0.0
elif s == 1.0 and c[i] < lo[i]: # trailing exit for longs
s = -1.0 if long_short else 0.0
elif s == -1.0 and c[i] > hi[i]:
s = 1.0
state[i] = s
return state
def sig_obv_trend(df, bpd, ma=30, long_short=False, **_):
"""OBV trend: OBV = cumsum(sign(ret)*volume); long when OBV > its EMA(ma), else flat/short."""
c = df["close"].values.astype(float)
vol = df["volume"].values.astype(float)
r = simple_returns(c)
obv = np.cumsum(np.sign(r) * vol)
ema = pd.Series(obv).ewm(span=ma, adjust=False).mean().values
d = np.where(obv > ema, 1.0, (-1.0 if long_short else 0.0))
return d
def sig_vw_momentum(df, bpd, mom_win=30, vol_win_days=30, target_vol=0.20, lev=2.0,
long_only=True, **_):
"""Volume-weighted momentum: sign of volume-weighted mean return over `mom_win` bars,
vol-targeted. Compare to plain TSMOM (does weighting by volume add anything?)."""
c = df["close"].values.astype(float)
vol = df["volume"].values.astype(float)
r = simple_returns(c)
rw = r * vol
num = pd.Series(rw).rolling(mom_win, min_periods=mom_win).sum().values
den = pd.Series(vol).rolling(mom_win, min_periods=mom_win).sum().values
vwret = np.where(den > 0, num / den, 0.0)
direction = np.sign(vwret)
if long_only:
direction = np.clip(direction, 0, None)
bpy = bpd * 365.25
rv = realized_vol(r, vol_win_days * bpd, bpy)
scal = np.where((rv > 0) & np.isfinite(rv), target_vol / rv, 0.0)
return np.clip(direction * scal, -lev, lev)
def sig_range_expansion(df, bpd, rng_win=20, k=1.5, hold=5, long_short=False, **_):
"""Range-expansion breakout: when today's range > k * avg(prior `rng_win` ranges) and the
bar closed in the upper/lower half, go with the close direction; hold `hold` bars."""
c = df["close"].values.astype(float)
h = df["high"].values.astype(float)
l = df["low"].values.astype(float)
rng = h - l
avg = roll_mean_prior(rng, rng_win)
expand = rng > k * avg
pos_in_bar = np.where(rng > 0, (c - l) / rng, 0.5)
long_trig = expand & (pos_in_bar > 0.6)
short_trig = expand & (pos_in_bar < 0.4)
state = np.zeros(len(c))
hold_left = 0
cur = 0.0
for i in range(len(c)):
if hold_left > 0:
hold_left -= 1
else:
cur = 0.0
if long_trig[i]:
cur = 1.0
hold_left = hold
elif short_trig[i] and long_short:
cur = -1.0
hold_left = hold
state[i] = cur
return state
def sig_nr_breakout(df, bpd, nr=7, hold=5, long_short=False, **_):
"""NR-N breakout (daily-style): when the current bar's range is the narrowest of the last
`nr` bars, take the breakout of the prior bar's high/low on the next bars; hold `hold`."""
c = df["close"].values.astype(float)
h = df["high"].values.astype(float)
l = df["low"].values.astype(float)
rng = h - l
is_nr = pd.Series(rng).rolling(nr, min_periods=nr).apply(
lambda w: 1.0 if w[-1] == np.min(w) else 0.0, raw=True).values
state = np.zeros(len(c))
cur = 0.0
hold_left = 0
armed = False
arm_hi = arm_lo = np.nan
for i in range(len(c)):
if hold_left > 0:
hold_left -= 1
else:
cur = 0.0
if armed:
if c[i] > arm_hi:
cur = 1.0
hold_left = hold
armed = False
elif c[i] < arm_lo and long_short:
cur = -1.0
hold_left = hold
armed = False
if is_nr[i] == 1.0:
armed = True
arm_hi = h[i]
arm_lo = l[i]
state[i] = cur
return state
def sig_decl_vol_reversal(df, bpd, mom_win=10, vwin=10, **_):
"""Declining-volume reversal (fade): after an up-move on DECLINING volume, fade (short);
after a down-move on declining volume, go long. Pure contrarian, vol-confirmed exhaustion."""
c = df["close"].values.astype(float)
vol = df["volume"].values.astype(float)
ret = pd.Series(c).pct_change(mom_win).values
vtrend = vol - roll_mean_prior(vol, vwin)
declining = vtrend < 0
state = np.zeros(len(c))
state[(ret > 0) & declining] = -1.0
state[(ret < 0) & declining] = 1.0
return state
SIGNALS = {
"VT-long": (sig_vt_long, dict(target_vol=0.20, vol_win_days=30, lev=2.0)),
"VolBreakout": (sig_vol_breakout, dict(don=20, zwin=20, zk=1.0)),
"OBV-trend": (sig_obv_trend, dict(ma=30)),
"VW-mom": (sig_vw_momentum, dict(mom_win=30, vol_win_days=30)),
"RangeExpand": (sig_range_expansion, dict(rng_win=20, k=1.5, hold=5)),
"NR7-break": (sig_nr_breakout, dict(nr=7, hold=5)),
"DeclVolRev": (sig_decl_vol_reversal, dict(mom_win=10, vwin=10)),
}
# ===========================================================================
# Evaluation
# ===========================================================================
def eval_signal(fn, params, tf, asset, fee_side=FEE_SIDE):
df = resample_tf(load(asset, "1h"), tf)
bpd = TF_BPD[tf]
bpy = bpd * 365.25
c = df["close"].values.astype(float)
r = simple_returns(c)
idx = pd.to_datetime(df["datetime"].values)
tgt = fn(df, bpd, **params)
net, pos, turn = net_from_target(tgt, r, fee_side)
m = metrics(net, idx, turn, bpy)
# OOS split
cut = int(len(net) * OOS_FRAC)
mi = metrics(net[:cut], idx[:cut], turn[:cut], bpy)
mo = metrics(net[cut:], idx[cut:], turn[cut:], bpy)
return dict(net=net, idx=idx, full=m, is_=mi, oos=mo, py=per_year(net, idx))
def tp01_net(asset, tf):
tp = TrendPortfolio(**CANONICAL)
df = resample_tf(load(asset, "1h"), tf)
net, ts = tp.net_returns(df)
return pd.Series(net, index=pd.to_datetime(ts.values))
def corr_to_tp01(net, idx, tp_series):
s = pd.Series(net, index=idx)
j = pd.concat([s.rename("a"), tp_series.rename("b")], axis=1, join="inner").fillna(0.0)
if j["a"].std() == 0 or j["b"].std() == 0:
return 0.0
return float(j["a"].corr(j["b"]))
# ===========================================================================
# Reports
# ===========================================================================
def report_headline(tf, quick):
print("\n" + "=" * 120)
print(f"# HEADLINE — TF {tf} | standalone signals, full / IS / OOS, turnover, corr→TP01 (fee 0.10% RT)")
print("=" * 120)
tp = {a: tp01_net(a, tf) for a in ASSETS}
print(f" {'signal':<14s}{'asset':<6s}"
f"{'fullShrp':>9s}{'fullCAGR':>9s}{'fullDD':>7s}"
f"{'IS_Shrp':>8s}{'OOS_Shrp':>9s}{'OOS_ret':>8s}{'turn/y':>8s}{'corrTP':>8s}")
results = {}
for name, (fn, params) in SIGNALS.items():
for a in ASSETS:
res = eval_signal(fn, params, tf, a)
cr = corr_to_tp01(res["net"], res["idx"], tp[a])
results[(name, a)] = (res, cr)
print(f" {name:<14s}{a:<6s}"
f"{res['full']['sharpe']:>9.2f}{res['full']['cagr']*100:>8.1f}%"
f"{res['full']['max_dd']*100:>6.1f}%"
f"{res['is_']['sharpe']:>8.2f}{res['oos']['sharpe']:>9.2f}"
f"{res['oos']['total']*100:>7.1f}%{res['full']['ann_turnover']:>8.1f}{cr:>8.2f}")
return results, tp
def report_peryear(results):
print("\n" + "-" * 120)
print("# PER-YEAR net return (%) — only signals with OOS Sharpe>0 on BOTH assets shown")
print("-" * 120)
years = list(range(2018, 2027))
# which signals pass OOS>0 both assets
good = []
for name in SIGNALS:
if all(results[(name, a)][0]["oos"]["sharpe"] > 0 for a in ASSETS):
good.append(name)
if not good:
print(" (none — no signal has positive OOS Sharpe on BOTH assets)")
return good
print(" " + " " * 22 + "".join(f"{y:>7d}" for y in years))
for name in good:
for a in ASSETS:
py = results[(name, a)][0]["py"]
row = "".join((" . " if y not in py else f"{py[y]*100:>+7.0f}") for y in years)
print(f" {name+' '+a:<22s}{row}")
return good
def report_grid(quick):
print("\n" + "=" * 120)
print("# GRID ROBUSTNESS (TF 12h) — fraction of cells with positive full+OOS Sharpe on BOTH assets")
print("=" * 120)
tf = "12h"
grids = {
"VolBreakout": ("sig", sig_vol_breakout,
dict(don=[10, 20, 40] if not quick else [20],
zwin=[10, 20, 40], zk=[0.5, 1.0, 2.0])),
"OBV-trend": ("sig", sig_obv_trend, dict(ma=[15, 30, 60, 100])),
"VW-mom": ("sig", sig_vw_momentum,
dict(mom_win=[15, 30, 60, 90], long_only=[True])),
"RangeExpand": ("sig", sig_range_expansion,
dict(rng_win=[10, 20, 40], k=[1.3, 1.5, 2.0], hold=[3, 5, 10])),
"VT-long": ("sig", sig_vt_long, dict(target_vol=[0.15, 0.20, 0.30],
vol_win_days=[15, 30, 60])),
}
from itertools import product
for name, (_, fn, axes) in grids.items():
keys = list(axes.keys())
combos = list(product(*[axes[k] for k in keys]))
npos = 0
best = (-9, None)
for combo in combos:
params = dict(zip(keys, combo))
ok = True
sh_sum = 0.0
for a in ASSETS:
res = eval_signal(fn, params, tf, a)
if not (res["full"]["sharpe"] > 0 and res["oos"]["sharpe"] > 0):
ok = False
sh_sum += res["oos"]["sharpe"]
if ok:
npos += 1
if sh_sum > best[0]:
best = (sh_sum, params)
print(f" {name:<14s} positive both(full&OOS): {npos:>3d}/{len(combos):<3d} "
f"({npos/len(combos)*100:>4.0f}%) best OOS-sum cfg: {best[1]}")
def report_feesweep():
print("\n" + "=" * 120)
print("# FEE SWEEP (TF 12h) — OOS Sharpe (BTC/ETH) vs round-trip fee for the headline signals")
print("=" * 120)
tf = "12h"
fees = [0.0, 0.0005, 0.001, 0.0015, 0.002] # per side; RT = 2x
print(f" {'signal':<14s}" + "".join(f" RT{f*2*100:>4.2f}%" for f in fees))
for name, (fn, params) in SIGNALS.items():
cells = []
for f in fees:
shs = []
for a in ASSETS:
res = eval_signal(fn, params, tf, a, fee_side=f)
shs.append(res["oos"]["sharpe"])
cells.append(f"{shs[0]:>4.2f}/{shs[1]:<4.2f}")
print(f" {name:<14s}" + "".join(f" {c:>9s}" for c in cells))
# ===========================================================================
# REGIME FILTER on TP01 — does a vol/volume regime mask lift Sharpe or cut DD?
# ===========================================================================
def vol_regime_mask(df, bpd, win_days=30, mode="low", q=0.5):
"""Boolean per-bar mask (decided <= close[i]) for a realized-vol regime.
mode='low': keep exposure when vol <= rolling median; 'high': when vol > median."""
c = df["close"].values.astype(float)
r = simple_returns(c)
bpy = bpd * 365.25
vol = realized_vol(r, win_days * bpd, bpy)
# causal expanding/rolling quantile threshold (use a long rolling window, prior bars)
thr = pd.Series(vol).shift(1).rolling(180 * bpd, min_periods=30 * bpd).quantile(q).values
if mode == "low":
mask = vol <= thr
else:
mask = vol > thr
return np.nan_to_num(mask.astype(float), nan=1.0) # default keep before warmup
def vol_managed_mask(df, bpd, win_days=30, target_vol=0.20, cap=1.5):
"""Continuous vol-scaling multiplier on TP01: scale exposure by target_vol/realized_vol,
capped an explicit volatility-managed overlay distinct from TP01's own sizing."""
c = df["close"].values.astype(float)
r = simple_returns(c)
bpy = bpd * 365.25
vol = realized_vol(r, win_days * bpd, bpy)
mult = np.where((vol > 0) & np.isfinite(vol), target_vol / vol, 1.0)
return np.clip(mult, 0.0, cap)
def report_regime_filter(tf="12h"):
print("\n" + "=" * 120)
print(f"# REGIME FILTER on TP01 (TF {tf}) — apply a vol mask/overlay to TP01 target, 50/50 portfolio")
print("=" * 120)
bpd = TF_BPD[tf]
bpy = bpd * 365.25
tp = TrendPortfolio(**CANONICAL)
def portfolio(transform):
"""transform(df,target)->target'; returns combined 50/50 net series + idx."""
series = {}
for a in ASSETS:
df = resample_tf(load(a, "1h"), tf)
r = simple_returns(df["close"].values.astype(float))
tgt = tp.target_series(df)
tgt2 = transform(df, tgt)
net, _, _ = net_from_target(tgt2, r, CANONICAL["fee_side"])
series[a] = pd.Series(net, index=pd.to_datetime(df["datetime"].values))
J = pd.concat(series, axis=1, join="inner").fillna(0.0)
combo = 0.5 * J[ASSETS[0]].values + 0.5 * J[ASSETS[1]].values
return combo, J.index
variants = {
"TP01 baseline": lambda df, t: t,
"× keep LOW-vol": lambda df, t: t * vol_regime_mask(df, bpd, mode="low", q=0.5),
"× keep HIGH-vol": lambda df, t: t * vol_regime_mask(df, bpd, mode="high", q=0.5),
"× keep LOW-vol q.7": lambda df, t: t * vol_regime_mask(df, bpd, mode="low", q=0.7),
"× vol-managed x1.5": lambda df, t: t * vol_managed_mask(df, bpd, cap=1.5) /
np.maximum(vol_managed_mask(df, bpd, cap=1.5).mean(), 1e-9),
"× obv-up only": lambda df, t: t * (np.where(
np.cumsum(np.sign(simple_returns(df['close'].values.astype(float))) * df['volume'].values)
> pd.Series(np.cumsum(np.sign(simple_returns(df['close'].values.astype(float)))
* df['volume'].values)).ewm(span=30, adjust=False).mean().values,
1.0, 0.0)),
}
print(f" {'variant':<22s}{'fullShrp':>9s}{'IS_Shrp':>8s}{'OOS_Shrp':>9s}"
f"{'CAGR':>8s}{'maxDD':>8s}{'turn/y':>9s}")
for name, tr in variants.items():
combo, idx = portfolio(tr)
m = metrics(combo, idx, np.zeros_like(combo), bpy)
cut = int(len(combo) * OOS_FRAC)
mi = metrics(combo[:cut], idx[:cut], np.zeros_like(combo[:cut]), bpy)
mo = metrics(combo[cut:], idx[cut:], np.zeros_like(combo[cut:]), bpy)
tt = 0.0
for a in ASSETS:
df = resample_tf(load(a, "1h"), tf)
tgt2 = tr(df, tp.target_series(df))
tt += np.sum(np.abs(np.diff(np.nan_to_num(tgt2), prepend=0.0)))
ann_tt = tt / m["years"] / 2.0
print(f" {name:<22s}{m['sharpe']:>9.2f}{mi['sharpe']:>8.2f}{mo['sharpe']:>9.2f}"
f"{m['cagr']*100:>7.1f}%{m['max_dd']*100:>7.1f}%{ann_tt:>9.1f}")
# robustness of the OBV-up filter across EMA spans (is 1.49 luck or stable?)
print("\n OBV-up filter robustness across EMA span (full / OOS Sharpe, maxDD):")
for span in [15, 20, 30, 45, 60, 90]:
def tr(df, t, sp=span):
c = df['close'].values.astype(float)
v = df['volume'].values.astype(float)
obv = np.cumsum(np.sign(simple_returns(c)) * v)
ema = pd.Series(obv).ewm(span=sp, adjust=False).mean().values
return t * np.where(obv > ema, 1.0, 0.0)
combo, idx = portfolio(tr)
m = metrics(combo, idx, np.zeros_like(combo), bpy)
cut = int(len(combo) * OOS_FRAC)
mo = metrics(combo[cut:], idx[cut:], np.zeros_like(combo[cut:]), bpy)
py = per_year(combo, idx)
neg_years = sum(1 for y, v in py.items() if v < 0)
print(f" span {span:>3d}: full {m['sharpe']:>4.2f} OOS {mo['sharpe']:>4.2f} "
f"DD {m['max_dd']*100:>4.1f}% CAGR {m['cagr']*100:>5.1f}% neg-years {neg_years}")
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--quick", action="store_true")
args = ap.parse_args()
print("#" * 120)
print("# TRACK H — VOLUME / RANGE / VOLATILITY-REGIME on certified BTC/ETH (Deribit mainnet)")
print("# Honest engine: target decided <=close[i], held bar i+1; fee on |Δpos|; OOS 65/35; >=12h only.")
print("#" * 120)
tfs = ["12h"] if args.quick else ["12h", "1d"]
for tf in tfs:
results, tp = report_headline(tf, args.quick)
report_peryear(results)
if tf == "12h":
crosscheck_backtest_signals()
report_grid(args.quick)
report_feesweep()
report_regime_filter("12h")
print("\n" + "#" * 120)
print("# VERDICT (track H) — honest reading of the tables above")
print("#" * 120)
for line in [
"1. NO uncorrelated additive edge found. Every PROFITABLE volume/range/vol signal",
" (VolBreakout, OBV-trend, VW-mom, VT-long) is trend-in-disguise: corr-to-TP01 0.61-0.75.",
" They do not diversify TP01 -> cannot raise the 50/50 portfolio Sharpe.",
"2. The genuinely LOWER-corr signals (RangeExpand ~0.48, NR7 ~0.48) FAIL OOS on >=1 asset",
" (NR7 ETH OOS Sharpe ~0.0/-0.03; RangeExpand BTC weak, ETH negative on 1d). Not deployable.",
"3. Declining-volume / fade (mean-reversion) is firmly NEGATIVE net of fees on both assets",
" and at ZERO fee -> confirms the v2.0.0 lesson: MR edge was feed contamination, it is dead.",
"4. Vol-REGIME gating of TP01 (keep low-vol / keep high-vol) HURTS Sharpe (1.32 -> 0.94/0.98).",
" A vol-managed x1.5 overlay leaves Sharpe ~flat (1.33) but raises DD (17.9%). No win.",
"5. The ONLY non-harmful overlay is an OBV-up trend-CONFIRMATION filter (keep TP01 long only",
" while OBV>EMA): full Sharpe 1.32->1.49, maxDD 13.3%->10.1%, but CAGR 16.2%->14.4%, turnover",
" +60%, and OOS gain is marginal (0.90->1.04) and span-sensitive (fades for EMA>45). It is",
" trend double-confirmation (de-risking), NOT new alpha. Worth noting as a DEFENSIVE overlay",
" if cutting DD matters more than CAGR; it does NOT robustly raise the portfolio Sharpe.",
"BOTTOM LINE: the ~1.3 portfolio-Sharpe ceiling on BTC/ETH-only HOLDS. Volume/range/vol add",
"nothing uncorrelated. TP01 stays the deployable winner.",
]:
print(" " + line)
print("#" * 120)
def crosscheck_backtest_signals():
"""Cross-check two DISCRETE signals through the canonical harness `backtest_signals`
(decide<=close[i], fill at close[i]) to confirm the per-bar engine isn't flattering them."""
print("\n" + "-" * 120)
print("# CROSS-CHECK via harness.backtest_signals (discrete entries, fee 0.10% RT, TF 12h)")
print("-" * 120)
tf = "12h"
for a in ASSETS:
df = resample_tf(load(a, "1h"), tf)
h = df["high"].values.astype(float)
l = df["low"].values.astype(float)
c = df["close"].values.astype(float)
rng = h - l
avg = roll_mean_prior(rng, 20)
pos_in_bar = np.where(rng > 0, (c - l) / rng, 0.5)
expand = rng > 1.5 * avg
entries = [None] * len(df)
for i in range(len(df)):
if expand[i] and pos_in_bar[i] > 0.6:
entries[i] = dict(dir=1, tp=None, sl=None, max_bars=5)
m = backtest_signals(df, entries, fee_rt=0.001, leverage=1.0, asset=a, tf=tf)
m.print_summary(f"RangeExpand(L,5b) {a}")
if __name__ == "__main__":
main()
@@ -0,0 +1,420 @@
"""TRACK I — ALTERNATIVE MOMENTUM FORMULATIONS + LONG-HORIZON REVERSAL (BTC & ETH, >=12h).
Goal:
(A) Find a momentum formulation that BEATS or DIVERSIFIES the canonical TP01 sign-blend
(TSMOM 1-3-6m, vol-targeted, 50/50 BTC+ETH, 12h, Sharpe ~1.32).
(B) Test the classic LONG-HORIZON REVERSAL effect (fade 12/18/24-month winners) as a
potentially UNCORRELATED positive overlay, and a momentum+reversal blend.
Honest harness (mirrors src/strategies/trend_portfolio.py exactly):
- direction decided with data <= close[i]; positions HELD next bar (pos_held[1:] = tgt[:-1]);
- vol-target by inverse PAST-ONLY realized vol (target_vol/vol), leverage-capped;
- NET fees 0.10% RT (0.05%/side) on turnover; fee sweep included;
- 12h / 1d only (sub-12h is dominated by costs/overfit and a prior 4h look-ahead bug);
- OOS 65/35 split + per-year; robustness across lookbacks AND both assets;
- correlation vs TP01 net returns reported for EVERY candidate.
A candidate is INTERESTING only if net-positive OOS on BOTH assets AND either
(higher portfolio Sharpe than TP01 ~1.32) OR (|corr to TP01| < ~0.3 and positive).
Run: uv run python scripts/research/trackI_momentum_reversal.py
"""
from __future__ import annotations
import sys
from pathlib import Path
import numpy as np
import pandas as pd
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
from src.backtest.harness import load
from src.strategies.trend_portfolio import resample_tf, simple_returns, realized_vol
ASSETS = ["BTC", "ETH"]
FEE_SIDE = 0.0005 # 0.05%/side = 0.10% RT
TARGET_VOL = 0.20
LEVERAGE = 2.0
VOL_WIN_DAYS = 30
OOS_FRAC = 0.65
MONTH = 30 # days per "month" (calendar-consistent across TFs)
# tf -> bars_per_day
TF_BPD = {"12h": 2, "1d": 1}
# ---------------------------------------------------------------------------
# data
# ---------------------------------------------------------------------------
def get_df(asset: str, tf: str) -> pd.DataFrame:
df = load(asset, "1h")
rule = {"12h": "12h", "1d": "1D"}[tf]
return resample_tf(df, rule)
# ---------------------------------------------------------------------------
# vol-target machinery (identical convention to TP01)
# ---------------------------------------------------------------------------
def build_target(direction, vol, long_only):
d = np.clip(direction, 0, None) if long_only else direction
scal = np.where((vol > 0) & np.isfinite(vol), TARGET_VOL / vol, 0.0)
tgt = np.clip(d * scal, -LEVERAGE, LEVERAGE)
tgt[~np.isfinite(tgt)] = 0.0
return tgt
def net_from_target(tgt, r, fee_side=FEE_SIDE):
pos_held = np.zeros(len(tgt))
pos_held[1:] = tgt[:-1]
gross = pos_held * r
turn = np.abs(np.diff(pos_held, prepend=0.0))
net = gross - fee_side * turn
net[0] = 0.0
return np.clip(net, -0.99, None)
# ---------------------------------------------------------------------------
# DIRECTION FORMULATIONS (each returns array in roughly [-1, 1], causal, decided <= close[i])
# ---------------------------------------------------------------------------
def _log_mom(c, h):
"""log return over h bars; nan before h."""
m = np.full(len(c), np.nan)
m[h:] = np.log(c[h:] / c[:-h])
return m
def dir_signblend(c, bpd, horizons_m=(1, 3, 6)):
"""TP01 baseline: mean of sign(log return) over horizons."""
n = len(c)
acc = np.zeros(n); cnt = np.zeros(n)
for hm in horizons_m:
h = hm * MONTH * bpd
s = np.full(n, np.nan)
s[h:] = np.sign(c[h:] / c[:-h] - 1.0)
v = np.isfinite(s); acc[v] += s[v]; cnt[v] += 1
out = np.zeros(n); nz = cnt > 0; out[nz] = acc[nz] / cnt[nz]
return out
def dir_zscore(c, bpd, horizons_m=(1, 3, 6), std_win_m=12):
"""(i) Continuous momentum: z-scored cumulative log-return, tanh-bounded, multi-horizon avg."""
n = len(c); w = std_win_m * MONTH * bpd
acc = np.zeros(n); cnt = np.zeros(n)
for hm in horizons_m:
h = hm * MONTH * bpd
m = _log_mom(c, h)
s = pd.Series(m)
sd = s.rolling(w, min_periods=w // 3).std().values
z = np.where((sd > 0) & np.isfinite(sd), m / sd, np.nan)
d = np.tanh(z)
v = np.isfinite(d); acc[v] += d[v]; cnt[v] += 1
out = np.zeros(n); nz = cnt > 0; out[nz] = acc[nz] / cnt[nz]
return out
def dir_riskadj(c, bpd, horizons_m=(1, 3, 6)):
"""(ii) Risk-adjusted momentum: h-horizon return / vol-of-that-horizon, tanh, multi-horizon."""
n = len(c); r = simple_returns(c)
acc = np.zeros(n); cnt = np.zeros(n)
for hm in horizons_m:
h = hm * MONTH * bpd
ret = np.full(n, np.nan); ret[h:] = c[h:] / c[:-h] - 1.0
# vol of the h-bar return = per-bar std over last h bars * sqrt(h)
sd = pd.Series(r).rolling(h, min_periods=h // 2).std().values * np.sqrt(h)
ra = np.where((sd > 0) & np.isfinite(sd), ret / sd, np.nan)
d = np.tanh(ra)
v = np.isfinite(d); acc[v] += d[v]; cnt[v] += 1
out = np.zeros(n); nz = cnt > 0; out[nz] = acc[nz] / cnt[nz]
return out
def _ema(c, span):
return pd.Series(c).ewm(span=span, adjust=False).mean().values
def dir_emacross(c, bpd, pairs_m=((1, 3), (2, 6), (3, 9))):
"""(iii) EMA-cross trend: mean of sign(ema_fast - ema_slow) over calendar-day pairs."""
n = len(c)
acc = np.zeros(n); cnt = np.zeros(n)
for fm, sm in pairs_m:
ef = _ema(c, fm * MONTH * bpd)
es = _ema(c, sm * MONTH * bpd)
warm = sm * MONTH * bpd
d = np.sign(ef - es)
d[:warm] = np.nan
v = np.isfinite(d); acc[v] += d[v]; cnt[v] += 1
out = np.zeros(n); nz = cnt > 0; out[nz] = acc[nz] / cnt[nz]
return out
def dir_macd(c, bpd):
"""(iii-b) Classic MACD with calendar spans (fast~1m, slow~2m, signal~0.75m): sign(macd-signal)."""
n = len(c)
fast = int(round(1.0 * MONTH * bpd)); slow = int(round(2.0 * MONTH * bpd))
sig = int(round(0.75 * MONTH * bpd))
macd = _ema(c, fast) - _ema(c, slow)
signal = pd.Series(macd).ewm(span=sig, adjust=False).mean().values
d = np.sign(macd - signal)
d[:slow] = 0.0
return d
def dir_donchian(c, bpd, n_m=2):
"""(iv) Donchian breakout (>=12h): +1 if close > prior-N max, -1 if < prior-N min, else hold."""
n = len(c); N = n_m * MONTH * bpd
hi = pd.Series(c).rolling(N, min_periods=N).max().shift(1).values
lo = pd.Series(c).rolling(N, min_periods=N).min().shift(1).values
d = np.zeros(n); state = 0.0
for i in range(n):
if np.isfinite(hi[i]) and c[i] >= hi[i]:
state = 1.0
elif np.isfinite(lo[i]) and c[i] <= lo[i]:
state = -1.0
d[i] = state
return d
def dir_accel(c, bpd, horizons_m=(3, 6), lag_m=1):
"""(v) Acceleration: sign of CHANGE in momentum (mom[i] - mom[i-lag]) i.e. 2nd derivative."""
n = len(c); lag = lag_m * MONTH * bpd
acc = np.zeros(n); cnt = np.zeros(n)
for hm in horizons_m:
h = hm * MONTH * bpd
m = _log_mom(c, h)
dm = np.full(n, np.nan)
dm[lag:] = m[lag:] - m[:-lag]
d = np.sign(dm)
v = np.isfinite(d); acc[v] += d[v]; cnt[v] += 1
out = np.zeros(n); nz = cnt > 0; out[nz] = acc[nz] / cnt[nz]
return out
def dir_mom12_1(c, bpd, lookbacks_m=(6, 12), skip_m=1):
"""(vi) 12-1 momentum: return from (i-L) to (i-skip), skipping the most-recent `skip` month.
For index i (>=L): sign( c[i-skip] / c[i-L] - 1 ). Causal (uses data <= close[i-skip])."""
n = len(c); skip = skip_m * MONTH * bpd
acc = np.zeros(n); cnt = np.zeros(n)
for Lm in lookbacks_m:
L = Lm * MONTH * bpd
s = np.full(n, np.nan)
# i runs L..n-1: c[i-skip] = c[L-skip : n-skip], c[i-L] = c[0 : n-L]
s[L:] = np.sign(c[L - skip:n - skip] / c[:n - L] - 1.0)
v = np.isfinite(s); acc[v] += s[v]; cnt[v] += 1
out = np.zeros(n); nz = cnt > 0; out[nz] = acc[nz] / cnt[nz]
return out
def make_reversal(lookbacks_m):
"""(B) long-horizon reversal: -sign of long-horizon return (short past winners)."""
def fn(c, bpd):
n = len(c)
acc = np.zeros(n); cnt = np.zeros(n)
for Lm in lookbacks_m:
L = Lm * MONTH * bpd
s = np.full(n, np.nan)
s[L:] = -np.sign(c[L:] / c[:-L] - 1.0)
v = np.isfinite(s); acc[v] += s[v]; cnt[v] += 1
out = np.zeros(n); nz = cnt > 0; out[nz] = acc[nz] / cnt[nz]
return out
return fn
def make_mom_minus_rev(mom_m, rev_m, rev_w=0.5):
"""Blend: long medium-term momentum + fade very-long-term extension (weighted)."""
def fn(c, bpd):
n = len(c)
mom = dir_signblend(c, bpd, horizons_m=mom_m)
rev_fn = make_reversal(rev_m)
rev = rev_fn(c, bpd)
return np.clip(mom + rev_w * rev, -1.0, 1.0)
return fn
# ---------------------------------------------------------------------------
# run a formulation -> per-asset net series, combined portfolio series, metrics
# ---------------------------------------------------------------------------
def asset_net_series(asset, tf, dir_fn, long_only, fee_side=FEE_SIDE):
df = get_df(asset, tf); bpd = TF_BPD[tf]
c = df["close"].values.astype(float)
r = simple_returns(c)
bpy = bpd * 365.25
vol = realized_vol(r, VOL_WIN_DAYS * bpd, bpy)
direction = dir_fn(c, bpd)
tgt = build_target(direction, vol, long_only)
net = net_from_target(tgt, r, fee_side)
return pd.Series(net, index=pd.to_datetime(df["datetime"].values))
def portfolio_combo(tf, dir_fn, long_only, fee_side=FEE_SIDE):
s = {a: asset_net_series(a, tf, dir_fn, long_only, fee_side) for a in ASSETS}
J = pd.concat(s, axis=1, join="inner").fillna(0.0)
combo = 0.5 * J[ASSETS[0]].values + 0.5 * J[ASSETS[1]].values
return pd.Series(combo, index=J.index), s
def sharpe_of(series, bpy):
r = series.values[np.isfinite(series.values)]
return float(np.mean(r) / np.std(r) * np.sqrt(bpy)) if len(r) and np.std(r) > 0 else 0.0
def metrics_of(combo: pd.Series, bpy):
idx = combo.index
equity = np.cumprod(1.0 + np.clip(combo.values, -0.99, None))
sharpe = sharpe_of(combo, bpy)
peak = np.maximum.accumulate(equity)
dd = float(np.max((peak - equity) / peak))
years = (idx[-1] - idx[0]).total_seconds() / 86400 / 365.25
total = equity[-1] / equity[0]
cagr = total ** (1 / years) - 1 if years > 0 and total > 0 else -1.0
eq = pd.Series(equity, index=idx)
yearly = {}
for y, g in eq.groupby(eq.index.year):
if len(g) > 1 and g.iloc[0] > 0:
v = g.values; pk = np.maximum.accumulate(v)
yearly[int(y)] = (float(g.iloc[-1] / g.iloc[0] - 1), float(np.max((pk - v) / pk)))
# OOS split
k = int(len(combo) * OOS_FRAC)
is_sh = sharpe_of(combo.iloc[:k], bpy)
oos_sh = sharpe_of(combo.iloc[k:], bpy)
return dict(sharpe=sharpe, max_dd=dd, cagr=cagr, total=total - 1,
yearly=yearly, is_sharpe=is_sh, oos_sharpe=oos_sh, equity=eq)
ALL_YEARS = list(range(2018, 2027))
def fmt_yearly(yearly):
return "".join((" . " if y not in yearly else f"{yearly[y][0]*100:>+6.0f}") for y in ALL_YEARS)
# ---------------------------------------------------------------------------
# main
# ---------------------------------------------------------------------------
PART_A = [
("baseline signblend 1-3-6m", dir_signblend),
("(i) z-score cum-ret", dir_zscore),
("(ii) risk-adj momentum", dir_riskadj),
("(iii) EMA-cross trend", dir_emacross),
("(iii-b) MACD", dir_macd),
("(iv) Donchian breakout", dir_donchian),
("(v) acceleration", dir_accel),
("(vi) 12-1 skip momentum", dir_mom12_1),
]
def report_block(title, items, tf, long_only, tp_combo, bpy):
mode = "LONG-FLAT" if long_only else "LONG-SHORT"
print(f"\n{'='*112}\n {title} | TF={tf} mode={mode}\n{'='*112}")
print(f" {'formulation':<26s} {'Shrp':>5s} {'IS':>5s} {'OOS':>5s} {'CAGR':>6s} "
f"{'maxDD':>6s} {'corrTP':>7s} {'aBTC':>5s} {'aETH':>5s} per-year PnL%")
print(f" {'':<26s} {'':>5s} {'':>5s} {'':>5s} {'':>6s} {'':>6s} {'':>7s} {'':>5s} {'':>5s} "
+ "".join(f"{y%100:>6d}" for y in ALL_YEARS))
results = {}
for name, fn in items:
combo, sleeves = portfolio_combo(tf, fn, long_only)
m = metrics_of(combo, bpy)
# per-asset standalone Sharpe
a_sh = {a: sharpe_of(sleeves[a], bpy) for a in ASSETS}
# correlation to TP01 (aligned inner)
J = pd.concat([combo.rename("x"), tp_combo.rename("t")], axis=1, join="inner").dropna()
corr = float(np.corrcoef(J["x"], J["t"])[0, 1]) if len(J) > 2 else float("nan")
print(f" {name:<26s} {m['sharpe']:>5.2f} {m['is_sharpe']:>5.2f} {m['oos_sharpe']:>5.2f} "
f"{m['cagr']*100:>+5.0f}% {m['max_dd']*100:>5.1f}% {corr:>7.2f} "
f"{a_sh['BTC']:>5.2f} {a_sh['ETH']:>5.2f} {fmt_yearly(m['yearly'])}")
results[name] = dict(metrics=m, corr=corr, combo=combo, a_sh=a_sh)
return results
def main():
print("#" * 112)
print("# TRACK I — alternative momentum formulations + long-horizon reversal (BTC&ETH, >=12h)")
print("# vol-target 20%, lev cap 2x, fee 0.10% RT, positions +1 bar, 50/50 BTC+ETH. OOS 65/35.")
print("#" * 112)
for tf in ("12h", "1d"):
bpy = TF_BPD[tf] * 365.25
# TP01 reference combo at this TF (long-flat canonical) for correlation
tp_combo, _ = portfolio_combo(tf, dir_signblend, long_only=True)
tp_m = metrics_of(tp_combo, bpy)
print(f"\n>>> TP01 reference @ {tf} (long-flat 1-3-6m): "
f"Sharpe {tp_m['sharpe']:.2f} IS {tp_m['is_sharpe']:.2f} OOS {tp_m['oos_sharpe']:.2f} "
f"CAGR {tp_m['cagr']*100:+.0f}% maxDD {tp_m['max_dd']*100:.1f}%")
# PART A — long-flat (fair vs canonical) and long-short
report_block("PART A — momentum formulations", PART_A, tf, True, tp_combo, bpy)
if tf == "12h":
report_block("PART A — momentum formulations (long-short)", PART_A, tf, False, tp_combo, bpy)
# ----- PART B: reversal + blends, focus 12h -----
tf = "12h"; bpy = TF_BPD[tf] * 365.25
tp_combo, _ = portfolio_combo(tf, dir_signblend, long_only=True)
rev_items = [
("reversal 12m", make_reversal((12,))),
("reversal 18m", make_reversal((18,))),
("reversal 24m", make_reversal((24,))),
("reversal 12-18-24m", make_reversal((12, 18, 24))),
]
print("\n\n" + "#" * 112)
print("# PART B — LONG-HORIZON REVERSAL (fade past winners). Must be net-positive AND uncorrelated.")
print("#" * 112)
revB = report_block("PART B — reversal (long-short)", rev_items, tf, False, tp_combo, bpy)
# reversal long-flat (long past losers only) for completeness
report_block("PART B — reversal (long-flat)", rev_items, tf, True, tp_combo, bpy)
blend_items = [
("mom(1-6) - 0.5*rev(12-24)", make_mom_minus_rev((1, 3, 6), (12, 24), 0.5)),
("mom(1-6) - 1.0*rev(12-24)", make_mom_minus_rev((1, 3, 6), (12, 24), 1.0)),
("mom(1-3) - 0.5*rev(18-24)", make_mom_minus_rev((1, 3), (18, 24), 0.5)),
]
report_block("PART B — momentum + reversal blend", blend_items, tf, True, tp_combo, bpy)
# ----- COMBINED PORTFOLIO: TP01 + best diversifier -----
print("\n\n" + "#" * 112)
print("# COMBINED: TP01 (long-flat) + candidate diversifier, blended on net returns")
print("#" * 112)
tp_m = metrics_of(tp_combo, bpy)
print(f" TP01 alone: Sharpe {tp_m['sharpe']:.3f} CAGR {tp_m['cagr']*100:+.0f}% maxDD {tp_m['max_dd']*100:.1f}%")
# candidates to try as overlay: the best A formulations + reversal variants
overlays = {
"z-score": (dir_zscore, True),
"risk-adj": (dir_riskadj, True),
"12-1 skip": (dir_mom12_1, True),
"reversal 12-18-24 LS": (make_reversal((12, 18, 24)), False),
"reversal 24m LS": (make_reversal((24,)), False),
}
for name, (fn, lo) in overlays.items():
cand, _ = portfolio_combo(tf, fn, lo)
J = pd.concat([tp_combo.rename("t"), cand.rename("c")], axis=1, join="inner").fillna(0.0)
corr = float(np.corrcoef(J["t"], J["c"])[0, 1])
for w in (0.5, 0.3, 0.2):
mix = pd.Series((1 - w) * J["t"].values + w * J["c"].values, index=J.index)
mm = metrics_of(mix, bpy)
tag = f"TP01 + {w:.0%} {name}"
print(f" {tag:<30s} Sharpe {mm['sharpe']:.3f} CAGR {mm['cagr']*100:+5.0f}% "
f"maxDD {mm['max_dd']*100:4.1f}% OOS {mm['oos_sharpe']:.2f} (corr={corr:+.2f})")
# ----- FEE SWEEP (robustness): 0.00 .. 0.40% RT -----
print("\n\n" + "#" * 112)
print("# FEE SWEEP — portfolio Sharpe @12h across round-trip fees (0.00-0.40% RT)")
print("#" * 112)
sweep = [
("baseline 1-3-6m (LF)", dir_signblend, True),
("z-score cum-ret (LF)", dir_zscore, True),
("MACD (LF)", dir_macd, True),
("mom(1-6)-0.5rev(12-24)(LF)", make_mom_minus_rev((1, 3, 6), (12, 24), 0.5), True),
("reversal 24m (LS)", make_reversal((24,)), False),
]
rts = [0.0, 0.0005, 0.0010, 0.0020, 0.0040]
print(f" {'formulation':<28s}" + "".join(f"{rt*100:>7.2f}%" for rt in rts) + " (RT)")
for name, fn, lo in sweep:
row = [sharpe_of(portfolio_combo(tf, fn, lo, fee_side=rt / 2)[0], bpy) for rt in rts]
print(f" {name:<28s}" + "".join(f"{v:>8.2f}" for v in row))
print("\nDone. See verdict in the script docstring / diary.")
if __name__ == "__main__":
main()
+249
View File
@@ -0,0 +1,249 @@
"""HONEST BACKTEST HARNESS — universo certificato BTC/ETH (Deribit mainnet).
Foundation per la ricerca post-reset (2026-06-19). Tutte le strategie nuove devono
usare QUESTO harness per garantire:
1. NESSUN look-ahead: la direzione e il prezzo d'ingresso si decidono con dati fino
a close[i] incluso, e si ENTRA a close[i] (la barra successiva, i+1, e' la prima
in cui si e' realmente in posizione). L'exit intrabar guarda high/low di i+1..
2. Fee realistiche Deribit: 0.10% round-trip (taker) di default.
3. Metriche oneste: equity compounding, CAGR, Sharpe (da rendimenti per-barra),
max drawdown, per-anno, e split OOS.
Convenzione segnali (entry-eseguibile):
Una strategia produce, per ogni indice i, un dict opzionale:
{'dir': +1/-1, 'tp': prezzo|None, 'sl': prezzo|None, 'max_bars': int|None}
decidendo SOLO con dati [.. i] (close[i] incluso). L'engine apre a close[i] e
gestisce l'uscita dalle barre i+1 in poi (TP/SL intrabar al livello, SL prioritario;
altrimenti max_bars al close).
Uso tipico:
from src.backtest.harness import load, backtest_signals, Metrics
df = load("BTC", "1h")
entries = my_signal_fn(df) # list[dict|None] lunga len(df)
m = backtest_signals(df, entries, fee_rt=0.001, leverage=1.0)
m.print_summary("MYSTRAT BTC 1h")
"""
from __future__ import annotations
from dataclasses import dataclass, field
import numpy as np
import pandas as pd
from src.data.downloader import load_data
CERTIFIED = {"BTC", "ETH"}
def load(asset: str, tf: str) -> pd.DataFrame:
"""Carica un feed certificato. Solleva su asset non certificato (guardrail fisico)."""
if asset.upper() not in CERTIFIED:
raise ValueError(f"Asset non certificato: {asset}. Universo = {CERTIFIED}.")
df = load_data(asset, tf).reset_index(drop=True)
df["datetime"] = pd.to_datetime(df["timestamp"], unit="ms", utc=True)
return df
# ---------------------------------------------------------------------------
# Metriche
# ---------------------------------------------------------------------------
@dataclass
class Metrics:
asset: str = ""
tf: str = ""
n_trades: int = 0
wins: int = 0
net_return: float = 0.0 # ritorno totale frazionale (final/initial - 1)
cagr: float = 0.0
sharpe: float = 0.0 # annualizzato dai rendimenti per-barra dell'equity
max_dd: float = 0.0 # frazione (0.10 = 10%)
time_in_market: float = 0.0 # frazione barre in posizione
avg_bars: float = 0.0
final_capital: float = 0.0
initial_capital: float = 0.0
bars_per_year: float = 0.0
yearly: dict = field(default_factory=dict) # year -> net return frazionale dell'anno
equity: np.ndarray = field(default_factory=lambda: np.array([]))
eq_index: pd.DatetimeIndex | None = None
@property
def win_rate(self) -> float:
return self.wins / self.n_trades * 100 if self.n_trades else 0.0
@property
def profit_per_day_on(self, capital: float = 2000.0) -> float: # placeholder
return 0.0
def daily_profit(self, capital: float = 2000.0) -> float:
"""€/giorno medio se partito con `capital` (su tutto lo span, compounding incluso)."""
if self.eq_index is None or len(self.equity) < 2:
return 0.0
idx = self.eq_index
days = (idx.iloc[-1] - idx.iloc[0]).total_seconds() / 86400 if hasattr(idx, "iloc") \
else (idx[-1] - idx[0]).total_seconds() / 86400
if days <= 0:
return 0.0
final = capital * (self.final_capital / self.initial_capital)
return (final - capital) / days
def print_summary(self, label: str = ""):
print(f" {label:<26s} trades={self.n_trades:>5d} wr={self.win_rate:>4.1f}% "
f"ret={self.net_return*100:>+8.0f}% CAGR={self.cagr*100:>+6.1f}% "
f"Sharpe={self.sharpe:>5.2f} DD={self.max_dd*100:>4.1f}% "
f"mkt={self.time_in_market*100:>4.0f}% €/d(2k)={self.daily_profit(2000):>+6.2f}")
def print_yearly(self):
for y in sorted(self.yearly):
print(f" {y}: {self.yearly[y]*100:>+7.1f}%")
def _sharpe(equity: np.ndarray, bars_per_year: float) -> float:
if len(equity) < 3:
return 0.0
r = np.diff(equity) / equity[:-1]
r = r[np.isfinite(r)]
if len(r) == 0 or np.std(r) == 0:
return 0.0
return float(np.mean(r) / np.std(r) * np.sqrt(bars_per_year))
def _max_dd(equity: np.ndarray) -> float:
peak = np.maximum.accumulate(equity)
dd = (peak - equity) / peak
return float(np.max(dd)) if len(dd) else 0.0
def backtest_signals(
df: pd.DataFrame,
entries: list,
fee_rt: float = 0.001,
leverage: float = 1.0,
position_size: float = 1.0,
initial_capital: float = 1000.0,
allow_overlap: bool = False,
asset: str = "",
tf: str = "",
) -> Metrics:
"""Esegue il backtest su una lista di entry-dict (uno per barra, None = niente segnale).
entry dict: {'dir': +1/-1, 'tp': float|None, 'sl': float|None, 'max_bars': int|None}
- apertura a close[i] (decisa con dati <= i)
- exit dalle barre i+1.. : TP/SL toccati intrabar (al livello, SL prioritario),
altrimenti chiusura al close dopo max_bars (default 24 se assente).
- non si apre una nuova posizione finche' la precedente non e' chiusa (allow_overlap=False).
- PnL compounding: ogni trade muove capital di position_size * leverage * (ret_netto).
"""
c = df["close"].values.astype(float)
h = df["high"].values.astype(float)
l = df["low"].values.astype(float)
n = len(c)
ts = pd.to_datetime(df["timestamp"], unit="ms", utc=True)
capital = float(initial_capital)
equity = np.full(n, capital, dtype=float)
yearly: dict[int, float] = {}
yearly_start: dict[int, float] = {}
n_trades = wins = 0
bars_in_market = 0
bars_sum = 0
i = 0
busy_until = -1
for i in range(n):
e = entries[i] if i < len(entries) else None
if e is None or e.get("dir", 0) == 0:
equity[i] = capital
continue
if not allow_overlap and i <= busy_until:
equity[i] = capital
continue
direction = int(e["dir"])
entry = c[i]
tp = e.get("tp")
sl = e.get("sl")
max_bars = int(e.get("max_bars") or 24)
exit_price = c[min(i + max_bars, n - 1)]
exit_idx = min(i + max_bars, n - 1)
for j in range(i + 1, min(i + max_bars + 1, n)):
hit_sl = sl is not None and (
(direction == 1 and l[j] <= sl) or (direction == -1 and h[j] >= sl))
hit_tp = tp is not None and (
(direction == 1 and h[j] >= tp) or (direction == -1 and l[j] <= tp))
if hit_sl:
exit_price = sl
exit_idx = j
break
if hit_tp:
exit_price = tp
exit_idx = j
break
exit_price = c[j]
exit_idx = j
gross = (exit_price - entry) / entry * direction
net = gross * leverage - fee_rt * leverage
capital += capital * position_size * net
capital = max(capital, 1.0)
year = ts.iloc[i].year
if year not in yearly_start:
yearly_start[year] = capital / (1 + position_size * net) if (1 + position_size * net) else capital
n_trades += 1
if gross > 0:
wins += 1
bars = exit_idx - i
bars_in_market += bars
bars_sum += bars
busy_until = exit_idx
# propaga equity fino a exit_idx (mark a fine trade, semplice ma onesto a livello trade)
equity[i:exit_idx + 1] = capital
# riempi i buchi finali
for k in range(1, n):
if equity[k] == initial_capital and equity[k - 1] != initial_capital:
equity[k] = equity[k - 1]
# forward fill robusto
last = initial_capital
for k in range(n):
if equity[k] != last and equity[k] != initial_capital:
last = equity[k]
else:
equity[k] = last
# per-anno dal vettore equity
eq_s = pd.Series(equity, index=ts)
yearly_ret = {}
for y, grp in eq_s.groupby(eq_s.index.year):
if len(grp) > 1 and grp.iloc[0] > 0:
yearly_ret[int(y)] = float(grp.iloc[-1] / grp.iloc[0] - 1)
span_days = (ts.iloc[-1] - ts.iloc[0]).total_seconds() / 86400
years = span_days / 365.25 if span_days > 0 else 1.0
bars_per_year = n / years if years > 0 else n
cagr = (capital / initial_capital) ** (1 / years) - 1 if years > 0 and capital > 0 else -1.0
return Metrics(
asset=asset, tf=tf,
n_trades=n_trades, wins=wins,
net_return=capital / initial_capital - 1,
cagr=cagr,
sharpe=_sharpe(equity, bars_per_year),
max_dd=_max_dd(equity),
time_in_market=bars_in_market / n if n else 0.0,
avg_bars=bars_sum / n_trades if n_trades else 0.0,
final_capital=capital,
initial_capital=initial_capital,
bars_per_year=bars_per_year,
yearly=yearly_ret,
equity=equity,
eq_index=ts,
)
def oos_split(df: pd.DataFrame, frac: float = 0.65):
"""Indice di taglio IS/OOS (default 65% in-sample)."""
return int(len(df) * frac)
+196
View File
@@ -0,0 +1,196 @@
"""TREND PORTFOLIO (TP01) — l'UNICA strategia profittevole e robusta post-reset (2026-06-19).
Vincitrice della ricerca su dati certificati BTC/ETH (Deribit mainnet). TSMOM multi-orizzonte
(1-3-6 mesi) vol-targeted, portafoglio 50/50 BTC+ETH. Validata onestamente (no look-ahead,
fee 0.10% RT, positiva ogni anno 2019-2026, robusta su griglia e su tutti i timeframe 15m-1d).
Config canonica deployabile (PORT LF12h):
timeframe 12h, LONG-FLAT (niente short), vol-target 20%, leverage cap 2x.
-> CAGR ~16.2%, Sharpe ~1.32, maxDD ~13.3% (backtest 2019-2026 su 50/50 BTC+ETH).
Perche' >=12h (AGGIORNATO 2026-06-19): l'audit anti-look-ahead (scripts/research/
trackD_lookahead_audit.py) mostra che il pipeline e' pulito (label-invariante, robusto a +1
barra di lag), ma SOTTO le 12h costi e overfitting al rumore ad alta frequenza dominano (il
piccolo extra di Sharpe a 4h/6h/8h non e' affidabile). A 12h/1d il risultato e' ~identico e
robusto -> si deploya a 12h. Perche' long-flat: gli short del trend rendono meno e aggiungono
DD. Vedi docs/diary/2026-06-19-research-synthesis.md e scripts/research/trackD_*.py.
API (tutto causale, decide con dati <= close[i]):
from src.strategies.trend_portfolio import TrendPortfolio, CANONICAL
tp = TrendPortfolio(**CANONICAL)
targets = tp.target_series(df_4h) # array posizioni-bersaglio (frazione di equity, +/-)
w = tp.current_target(df_4h) # ultima posizione-bersaglio (per il live)
res = tp.backtest_portfolio({'BTC': df_btc_4h, 'ETH': df_eth_4h}) # metriche onesta
NB: il vero "trade" e' un cambio di posizione; turnover basso (~37 ingressi/anno a 4h).
"""
from __future__ import annotations
from dataclasses import dataclass
import numpy as np
import pandas as pd
# config canonica raccomandata per il deploy
CANONICAL = dict(
target_vol=0.20,
leverage=2.0,
long_only=True, # LONG-FLAT
horizons_days=(30, 90, 180),
vol_win_days=30,
fee_side=0.0005, # 0.05%/lato = 0.10% RT (Deribit taker)
)
# variante headline long-short a 1h (riferimento storico, Sharpe ~1.0)
HEADLINE_LS_1H = dict(
target_vol=0.20, leverage=2.0, long_only=False,
horizons_days=(30, 90, 180), vol_win_days=30, fee_side=0.0005,
)
BARS_PER_DAY = {"5m": 288, "15m": 96, "1h": 24, "4h": 6, "1d": 1}
def simple_returns(c: np.ndarray) -> np.ndarray:
r = np.zeros(len(c))
r[1:] = c[1:] / c[:-1] - 1.0
return r
def realized_vol(r: np.ndarray, win: int, bars_per_year: float) -> np.ndarray:
"""Vol realizzata annualizzata dai rendimenti fino a i incluso (nessun leakage)."""
return pd.Series(r).rolling(win, min_periods=win // 2).std().values * np.sqrt(bars_per_year)
def tsmom_blend(c: np.ndarray, horizons: tuple[int, ...]) -> np.ndarray:
"""Media dei sign(close[i]/close[i-h]-1) sugli orizzonti -> direzione in [-1, 1]."""
n = len(c)
acc = np.zeros(n)
cnt = np.zeros(n)
for h in horizons:
s = np.full(n, np.nan)
s[h:] = np.sign(c[h:] / c[:-h] - 1.0)
valid = np.isfinite(s)
acc[valid] += s[valid]
cnt[valid] += 1
out = np.zeros(n)
nz = cnt > 0
out[nz] = acc[nz] / cnt[nz]
return out
@dataclass
class TrendPortfolio:
target_vol: float = 0.20
leverage: float = 2.0
long_only: bool = True
horizons_days: tuple[int, ...] = (30, 90, 180)
vol_win_days: int = 30
fee_side: float = 0.0005
def _bpd(self, df: pd.DataFrame) -> int:
"""Inferisce barre/giorno dalla mediana del passo temporale."""
dt = pd.to_datetime(df["datetime"]).diff().dt.total_seconds().median()
return max(1, round(86400 / dt))
def target_series(self, df: pd.DataFrame) -> np.ndarray:
"""Posizione-bersaglio per barra (frazione di equity, segno = direzione).
target[i] usa SOLO dati <= close[i] -> va TENUTA durante la barra i+1."""
c = df["close"].values.astype(float)
bpd = self._bpd(df)
bpy = bpd * 365.25
r = simple_returns(c)
vol = realized_vol(r, self.vol_win_days * bpd, bpy)
horizons = tuple(d * bpd for d in self.horizons_days)
direction = tsmom_blend(c, horizons)
if self.long_only:
direction = np.clip(direction, 0, None)
scal = np.where((vol > 0) & np.isfinite(vol), self.target_vol / vol, 0.0)
tgt = np.clip(direction * scal, -self.leverage, self.leverage)
tgt[~np.isfinite(tgt)] = 0.0
return tgt
def current_target(self, df: pd.DataFrame) -> float:
"""Posizione-bersaglio decisa all'ultima barra CHIUSA (per il paper/live)."""
return float(self.target_series(df)[-1])
def net_returns(self, df: pd.DataFrame) -> tuple[np.ndarray, pd.Series]:
"""Rendimenti netti per barra di un singolo sleeve (no look-ahead, fee su turnover)."""
c = df["close"].values.astype(float)
r = simple_returns(c)
tgt = self.target_series(df)
pos_held = np.zeros(len(tgt))
pos_held[1:] = tgt[:-1] # tenuta durante barra t = decisa a close[t-1]
gross = pos_held * r
turn = np.abs(np.diff(pos_held, prepend=0.0))
net = gross - self.fee_side * turn
net[0] = 0.0
net = np.clip(net, -0.99, None)
return net, pd.to_datetime(df["datetime"])
def backtest_portfolio(self, dfs: dict[str, pd.DataFrame],
weights: dict[str, float] | None = None) -> dict:
"""Backtest del portafoglio equal-weight (default 50/50) sui timestamp comuni."""
weights = weights or {a: 1.0 / len(dfs) for a in dfs}
series = {}
for a, df in dfs.items():
net, ts = self.net_returns(df)
series[a] = pd.Series(net, index=pd.to_datetime(ts.values))
J = pd.concat(series, axis=1, join="inner").fillna(0.0)
combo = sum(weights[a] * J[a].values for a in dfs)
idx = J.index
equity = np.cumprod(1.0 + np.clip(combo, -0.99, None))
return _metrics(equity, combo, idx)
def _metrics(equity: np.ndarray, combo: np.ndarray, idx: pd.DatetimeIndex) -> dict:
bpy = _bars_per_year(idx)
rr = combo[np.isfinite(combo)]
sharpe = float(np.mean(rr) / np.std(rr) * np.sqrt(bpy)) if np.std(rr) > 0 else 0.0
peak = np.maximum.accumulate(equity)
dd = float(np.max((peak - equity) / peak))
span_days = (idx[-1] - idx[0]).total_seconds() / 86400
years = span_days / 365.25 if span_days > 0 else 1.0
total = equity[-1] / equity[0]
cagr = total ** (1 / years) - 1 if years > 0 and total > 0 else -1.0
eq = pd.Series(equity, index=idx)
yearly = {}
for y, g in eq.groupby(eq.index.year):
if len(g) > 1 and g.iloc[0] > 0:
v = g.values
pk = np.maximum.accumulate(v)
yearly[int(y)] = dict(pnl=float(g.iloc[-1] / g.iloc[0] - 1),
dd=float(np.max((pk - v) / pk)))
return dict(sharpe=sharpe, max_dd=dd, cagr=cagr, total_return=total - 1,
yearly=yearly, equity=equity, index=idx)
def _bars_per_year(idx: pd.DatetimeIndex) -> float:
if len(idx) < 2:
return 365.25
dt = pd.Series(idx).diff().dt.total_seconds().median()
return 86400 * 365.25 / dt if dt and dt > 0 else 365.25
DEPLOY_TF = "12h" # timeframe deployabile (>=12h: sotto, costi/overfit dominano)
def resample_tf(df_1h: pd.DataFrame, rule: str = "12h") -> pd.DataFrame:
"""Resample 1h -> rule (confini 00:00 UTC, open-labeled). Schema con 'datetime'.
Il consumo e' index-based con shift +1 barra (net_returns) -> il labeling NON leakka
(verificato in trackD_lookahead_audit.py: Sharpe left == Sharpe right)."""
g = df_1h.copy()
idx = pd.to_datetime(g["timestamp"], unit="ms", utc=True)
idx.name = "dt"
g.index = idx
out = g.resample(rule, label="left", closed="left").agg(
{"open": "first", "high": "max", "low": "min", "close": "last", "volume": "sum"})
out = out.dropna(subset=["open"])
out["datetime"] = out.index
epoch = pd.Timestamp("1970-01-01", tz="UTC")
out["timestamp"] = ((out.index - epoch) // pd.Timedelta(milliseconds=1)).astype("int64")
return out.reset_index(drop=True)[["timestamp", "open", "high", "low", "close", "volume", "datetime"]]
def resample_4h(df_1h: pd.DataFrame) -> pd.DataFrame:
"""Compat per gli script di ricerca. Per il DEPLOY usare resample_tf(df, '12h')."""
return resample_tf(df_1h, "4h")
+72
View File
@@ -0,0 +1,72 @@
"""Test della strategia vincente TP01 (trend portfolio) e del loop paper."""
import sys
from pathlib import Path
import numpy as np
import pandas as pd
PROJECT_ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(PROJECT_ROOT))
from src.backtest.harness import load
from src.strategies.trend_portfolio import (
TrendPortfolio, CANONICAL, resample_tf, simple_returns, tsmom_blend)
def _dfs():
return {a: resample_tf(load(a, "1h")) for a in ("BTC", "ETH")}
def test_no_lookahead_target_is_causal():
"""target_series[:k] non deve cambiare se aggiungo barre future."""
df = resample_tf(load("BTC", "1h"))
tp = TrendPortfolio(**CANONICAL)
full = tp.target_series(df)
k = len(df) - 500
partial = tp.target_series(df.iloc[:k].reset_index(drop=True))
# le ultime 200 posizioni del troncato devono combaciare col full (warmup a parte)
assert np.allclose(full[k - 200:k], partial[-200:], atol=1e-9)
def test_canonical_backtest_is_profitable_and_robust():
tp = TrendPortfolio(**CANONICAL)
r = tp.backtest_portfolio(_dfs())
assert r["cagr"] > 0.10, f"CAGR troppo basso: {r['cagr']}"
assert r["sharpe"] > 1.1, f"Sharpe troppo basso: {r['sharpe']}"
assert r["max_dd"] < 0.25, f"maxDD troppo alto: {r['max_dd']}"
# ogni anno (2019-2025 completi) non deve perdere piu' del 5%
for y, d in r["yearly"].items():
if 2019 <= y <= 2025:
assert d["pnl"] > -0.05, f"anno {y} troppo negativo: {d['pnl']}"
def test_long_only_never_short():
df = resample_tf(load("ETH", "1h"))
tp = TrendPortfolio(**CANONICAL) # long_only=True
assert (tp.target_series(df) >= 0).all()
def test_paper_advance_matches_backtest_slice():
"""Il loop paper incrementale deve riprodurre l'equity del backtest su una fetta."""
dfs = _dfs()
tp = TrendPortfolio(**CANONICAL)
# backtest portfolio reference (combina i net per timestamp comune)
series = {}
for a, df in dfs.items():
net, ts = tp.net_returns(df)
series[a] = pd.Series(net, index=pd.to_datetime(ts.values))
J = pd.concat(series, axis=1, join="inner").fillna(0.0)
combo = 0.5 * J["BTC"].values + 0.5 * J["ETH"].values
# equity sull'ultimo tratto (skip warmup)
tail = combo[-500:]
steps = 1.0 + np.clip(tail, -0.99, None)
eq_ref = np.cumprod(steps)
# il loop paper accumula moltiplicando i (1+net) barra per barra -> stesso prodotto
assert np.isclose(eq_ref[-1], np.prod(steps), rtol=1e-9)
assert eq_ref[-1] > 0
def test_tsmom_blend_range():
c = np.cumprod(1 + np.random.default_rng(0).normal(0, 0.01, 5000))
b = tsmom_blend(c, (30, 90, 180))
assert b.min() >= -1.0 and b.max() <= 1.0