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>
42 lines
1.5 KiB
Python
42 lines
1.5 KiB
Python
"""PairsWorker: entry/exit valutati SOLO su barre COMPLETE (lezione EXIT-16)."""
|
|
import time
|
|
|
|
import numpy as np
|
|
import pandas as pd
|
|
|
|
from src.live.pairs_worker import PairsWorker
|
|
|
|
|
|
def _pair_dfs(n=80, jump=0.05, forming=True):
|
|
"""Log-ratio = rumore alternato ±0.001 (z ~ ±1, nessun segnale) tranne l'ULTIMA
|
|
barra, dove un salto `jump` del ratio porta z >> z_in (e dr <= jump_max).
|
|
Con forming=True la serie termina ADESSO -> l'ultima riga e' in formazione."""
|
|
r = 0.001 * np.array([(-1) ** i for i in range(n)], dtype=float)
|
|
r[-1] = jump
|
|
ca = 100.0 * np.exp(r)
|
|
cb = np.full(n, 100.0)
|
|
if forming:
|
|
now_ms = int(time.time() * 1000)
|
|
ts = now_ms - 3_600_000 * np.arange(n - 1, -1, -1)
|
|
else:
|
|
ts = (pd.date_range("2024-01-01", periods=n, freq="1h", tz="UTC").astype("int64") // 10**6)
|
|
dfa = pd.DataFrame({"timestamp": ts, "close": ca})
|
|
dfb = pd.DataFrame({"timestamp": ts, "close": cb})
|
|
return dfa, dfb
|
|
|
|
|
|
def test_pairs_opens_on_completed_extreme_bar(tmp_path):
|
|
# controllo: la stessa barra estrema, se COMPLETA, apre (z >= z_in, short ratio)
|
|
w = PairsWorker("AAA", "BBB", "1h", data_dir=tmp_path)
|
|
dfa, dfb = _pair_dfs(forming=False)
|
|
w.tick(dfa, dfb)
|
|
assert w.in_position and w.direction == -1
|
|
|
|
|
|
def test_pairs_ignores_forming_bar(tmp_path):
|
|
# la stessa barra estrema IN FORMAZIONE va ignorata: nessun ingresso
|
|
w = PairsWorker("AAA", "BBB", "1h", data_dir=tmp_path)
|
|
dfa, dfb = _pair_dfs(forming=True)
|
|
w.tick(dfa, dfb)
|
|
assert not w.in_position
|