3909646028
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>
57 lines
2.4 KiB
Python
57 lines
2.4 KiB
Python
import numpy as np
|
|
import pandas as pd
|
|
from src.live.rotation_worker import RotationWorker
|
|
|
|
|
|
def _df(n=200, slope=1.0):
|
|
c = np.linspace(100, 100 + slope * n, n)
|
|
ts = (pd.date_range("2023-01-01", periods=n, freq="1D", tz="UTC").astype("int64") // 10**6)
|
|
return pd.DataFrame({"timestamp": ts, "open": c, "high": c, "low": c, "close": c, "volume": 1.0})
|
|
|
|
|
|
def test_rotation_picks_top_momentum_when_risk_on(tmp_path):
|
|
w = RotationWorker(universe=["BTC", "AAA", "BBB"], top_k=2, gross=0.45, data_dir=tmp_path)
|
|
data = {"BTC": _df(slope=1.0), "AAA": _df(slope=3.0), "BBB": _df(slope=0.1)}
|
|
w.tick(data)
|
|
assert w.weights["AAA"] > 0
|
|
assert abs(sum(w.weights.values()) - 0.45) < 1e-9
|
|
|
|
|
|
def test_rotation_flat_when_risk_off(tmp_path):
|
|
# BTC in downtrend -> risk_off -> nessuna posizione
|
|
w = RotationWorker(universe=["BTC", "AAA"], top_k=1, gross=0.45, data_dir=tmp_path)
|
|
data = {"BTC": _df(slope=-1.0), "AAA": _df(slope=3.0)}
|
|
w.tick(data)
|
|
assert sum(w.weights.values()) == 0.0
|
|
|
|
|
|
def test_rotation_persists_and_resumes(tmp_path):
|
|
w = RotationWorker(universe=["BTC", "AAA"], top_k=1, gross=0.45, data_dir=tmp_path)
|
|
w.tick({"BTC": _df(slope=1.0), "AAA": _df(slope=3.0)})
|
|
w2 = RotationWorker(universe=["BTC", "AAA"], top_k=1, gross=0.45, data_dir=tmp_path)
|
|
assert w2.weights == w.weights
|
|
|
|
|
|
def test_rotation_warns_once_on_short_panel(tmp_path, monkeypatch):
|
|
# worker GIA' operativo + panel troncato -> WARN (una sola notifica per episodio)
|
|
calls = []
|
|
monkeypatch.setattr("src.live.telegram_notifier.notify_event",
|
|
lambda e, d=None: calls.append(e))
|
|
w = RotationWorker(universe=["BTC", "AAA"], data_dir=tmp_path)
|
|
w.last_bar_ts = 123 # era gia' operativo
|
|
short = {"BTC": _df(n=20), "AAA": _df(n=20)}
|
|
w.tick(short)
|
|
w.tick(short) # dedup: niente seconda notifica
|
|
assert calls == ["PANEL_SHORT"]
|
|
w.tick({"BTC": _df(n=200), "AAA": _df(n=200)}) # recovery resetta il dedup
|
|
assert w._panel_warned is False
|
|
|
|
|
|
def test_rotation_no_warn_on_cold_start(tmp_path, monkeypatch):
|
|
calls = []
|
|
monkeypatch.setattr("src.live.telegram_notifier.notify_event",
|
|
lambda e, d=None: calls.append(e))
|
|
w = RotationWorker(universe=["BTC", "AAA"], data_dir=tmp_path) # last_bar_ts=0
|
|
w.tick({"BTC": _df(n=20), "AAA": _df(n=20)})
|
|
assert calls == []
|