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