14522262e6
Reset del progetto su fondamenta verificate dopo la scoperta che l'intera libreria "validata OOS" era artefatto di feed contaminato (print fantasma del feed Cerbero TESTNET + storico Binance/USDT). - Storico ricostruito da Deribit MAINNET (ccxt pubblico, tokenless) e CERTIFICATO (certify_feed.py): BTC/ETH puliti su TUTTA la storia (mediana 2-6 bps vs Coinbase USD), integrita' OHLC + coerenza resample (maxΔ 0.00) + cross-venue OK. Alt esclusi (illiquidi/divergenti: LTC/DOGE 50-82% barre flat; XRP/BNB non certificabili). - Verdetto sul feed pulito: FADE / PAIRS / XS01 / TSM01 morti (ogni portafoglio Sharpe -2.3..-3.0, DD ~40%); solo SH01 e frammenti HONEST con segnale residuo, da ri-validare in isolamento. - Cleanup "restart pulito": strategie, stack live (src/live, src/portfolio, runner/executor, yml, docker), ~100 script ricerca/gate, waste/games/ portfolios, dati non certificati + cache e 60+ diari -> archiviati in Old/ (preservati, non cancellati). Diario consolidato in un unico documento. - Skeleton ricerca tenuto: Strategy ABC + indicatori + src/fractal + src/backtest/engine + load_data; tool dati certificati (rebuild_history, certify_feed, audit_feed, multi_source_check). - Universo dati ATTIVO: solo BTC/ETH (5m/15m/1h); guardrail fisico (load_data su alt -> FileNotFoundError). Esecuzione DISABILITATA, conto flat. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
75 lines
2.8 KiB
Python
75 lines
2.8 KiB
Python
"""EXIT-04 — breakeven (protezione).
|
|
|
|
Quando il profitto su CLOSE supera k*ATR(entry) (ATR fissato all'entrata,
|
|
atr14[i]), sposta lo SL a BREAKEVEN = entry +/- buffer. Il buffer e' a favore
|
|
(0.2% = 2*fee_rt*entry) cosi' l'uscita a BE non e' in perdita per le fee. Il TP
|
|
fisso tp0 RESTA invariato. horizon = max_bars (invariato).
|
|
|
|
Una volta armato il breakeven, lo SL non torna mai indietro (cliquet): protegge
|
|
il profitto gia' maturato senza allentare il rischio.
|
|
|
|
Stop attivo nel bar j (solo dati <= j-1, anti-look-ahead):
|
|
- trigger: si arma quando close[j-1] e' a favore di >= k*atr14[i] dall'entrata
|
|
(atr14[i] = ATR all'entrata, costante; close[j-1] = ultimo close noto).
|
|
- prima dell'arm: sl = sl0 (protezione iniziale invariata).
|
|
- dopo l'arm:
|
|
long : be = entry + buffer; sl = max(sl0, be)
|
|
short: be = entry - buffer; sl = min(sl0, be)
|
|
NB max/min con sl0 fa si' che lo SL non venga mai ALLENTATO: se sl0 fosse
|
|
gia' oltre il BE (raro), resta sl0. Tipicamente sl0 e' sotto entry (long) e
|
|
il BE lo alza -> stop piu' protettivo.
|
|
|
|
GRID: k in {0.5, 1.0, 1.5} x buffer_frac in {0.0, 0.002} (6 celle).
|
|
buffer = buffer_frac * entry. buffer_frac 0.002 = 0.2% = 2*fee_rt a favore.
|
|
"""
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
from exit_lab import ExitPolicy, evaluate # noqa: E402
|
|
|
|
|
|
class Breakeven(ExitPolicy):
|
|
name = "breakeven"
|
|
|
|
def __init__(self, ctx, i, d, entry, tp0, sl0, mb, **params):
|
|
super().__init__(ctx, i, d, entry, tp0, sl0, mb, **params)
|
|
self.k = float(params.get("k", 1.0))
|
|
self.buffer_frac = float(params.get("buffer_frac", 0.002))
|
|
self.close = ctx["close"]
|
|
self.atr = ctx["atr14"]
|
|
self.atr_entry = self.atr[i] # ATR all'entrata, costante
|
|
self.buffer = self.buffer_frac * entry
|
|
self.armed = False
|
|
|
|
def levels(self, j: int):
|
|
# arma il breakeven usando SOLO close[j-1] (ultimo close noto) e atr14[i]
|
|
if not self.armed and self.atr_entry is not None and self.atr_entry == self.atr_entry:
|
|
cprev = self.close[j - 1]
|
|
profit = (cprev - self.entry) * self.d # >0 = a favore
|
|
if profit >= self.k * self.atr_entry:
|
|
self.armed = True
|
|
if not self.armed:
|
|
return self.tp0, self.sl0, 1.0
|
|
if self.d == 1:
|
|
be = self.entry + self.buffer
|
|
sl = max(self.sl0, be) # non allenta mai
|
|
else:
|
|
be = self.entry - self.buffer
|
|
sl = min(self.sl0, be)
|
|
return self.tp0, sl, 1.0
|
|
|
|
|
|
GRID = [
|
|
{"k": 0.5, "buffer_frac": 0.0},
|
|
{"k": 0.5, "buffer_frac": 0.002},
|
|
{"k": 1.0, "buffer_frac": 0.0},
|
|
{"k": 1.0, "buffer_frac": 0.002},
|
|
{"k": 1.5, "buffer_frac": 0.0},
|
|
{"k": 1.5, "buffer_frac": 0.002},
|
|
]
|
|
|
|
|
|
if __name__ == "__main__":
|
|
evaluate(Breakeven, GRID)
|