fix(live): parita' worker multi-asset — TR01 fee x leva, forming-bar su TR01/Pairs, WARN panel corto

Punti 2-4 dell'improvement-sweep 2026-06-06 (fix di parita', nessun cambio di strategia):

- TR01 (basket_trend_worker): fee per flip = POS * LEV * fee_rt/2 come il reference
  honest_improve2._tr_basket_daily:150 (mancava il fattore leva -> fee sotto-caricate 2x,
  bias ottimistico che gonfiava total_capital al ribilancio).
- TR01: crossover EMA e booking del return valutati SOLO su barre 4h COMPLETE
  (la riga -1 e' la candela in corso finche' non e' trascorsa la sua durata) — lezione
  EXIT-16; evidenza live: flip SOL 0->1->0 in 59min nella stessa finestra 4h.
- PairsWorker: entry ED exit valutati sul close di barra COMPLETA, come il backtest
  pairs_research (close settled). Stesso pattern bar_ms di strategy_worker.tick.
- TSM01/ROT02: il silent return su panel inner-join troncato sotto il lookback ora
  emette WARN log + Telegram PANEL_SHORT (helper _warn_panel_short condiviso), gated
  su "era gia' operativo" (no falsi positivi al cold-start), una notifica per episodio.

Test: 72/72 verdi (7 nuovi: forming-bar TR01/pairs + controllo positivo, fee con leva,
PANEL_SHORT warn/dedup/cold-start). Replay parity: validate_worker_pairs ESATTO
(ETH/BTC e BTC/LTC == backtest); validate_honest_workers invariato (TR01 -44% vs ref
+42% IDENTICO pre/post fix: e' la divergenza di convenzione capitale-unico vs
media-equity gia' documentata, da rivisitare a parte).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Adriano Dal Pastro
2026-06-07 08:23:19 +00:00
parent 9ce43031ee
commit 3909646028
9 changed files with 164 additions and 5 deletions
+29
View File
@@ -28,3 +28,32 @@ def test_basket_persists_and_resumes(tmp_path):
w.tick({"AAA": _ramp_df(slope=1.0)})
w2 = BasketTrendWorker(universe=["AAA"], tf="4h", capital=1000.0, data_dir=tmp_path)
assert w2.positions["AAA"] == 1.0 # stato ripreso da status.json
def _ramp_df_now(n=300, slope=1.0, forming_close=None):
"""Serie 4h che termina ADESSO: l'ultima riga e' la candela IN FORMAZIONE."""
import time
c = np.linspace(100, 100 + slope * n, n)
if forming_close is not None:
c = c.copy()
c[-1] = forming_close
bar = 4 * 3_600_000
now_ms = int(time.time() * 1000)
ts = now_ms - bar * np.arange(n - 1, -1, -1)
return pd.DataFrame({"timestamp": ts, "open": c, "high": c, "low": c, "close": c, "volume": 1.0})
def test_basket_ignores_forming_bar(tmp_path):
# downtrend su barre COMPLETE (target flat); la candela in formazione e' un
# spike estremo che flipperebbe EMA20>EMA100 se contata -> va ignorata.
w = BasketTrendWorker(universe=["AAA"], tf="4h", capital=1000.0, data_dir=tmp_path)
w.tick({"AAA": _ramp_df_now(slope=-0.3, forming_close=1e6)})
assert w.positions["AAA"] == 0.0
def test_basket_fee_includes_leverage(tmp_path):
# flip 0->1: fee = cap * POS * LEV * fee_rt/2 (reference honest_improve2:150)
w = BasketTrendWorker(universe=["AAA"], tf="4h", capital=1000.0, data_dir=tmp_path)
w.tick({"AAA": _ramp_df(slope=1.0)})
expected = 1000.0 - 1000.0 * w.position_size * w.leverage * (w.fee_rt / 2)
assert np.isclose(w.capital, expected)