ad65a0b344
23 famiglie esplorate (harness condiviso exit_lab, train/OOS embargo nov-2023, tutto lo storico 1h 2018-2026) + 10 verifiche avversariali + test PORT06. 'Cavalcare il prezzo' non esiste (4a conferma: oltre il TP=media non c'e' coda). Scoperta: lo SL intrabar fisso e' il distruttore di valore n.1 delle fade (stop da wick = falsi negativi). Forma robusta: SL solo su CHIUSURA oltre sl0±0.5·ATR14 — PORT06 FULL Sharpe 6.47→7.84 DD 4.10→2.60, OOS 8.82→10.06. Collaterali: bias gap-through dell'engine sugli stop stretti; ramo -2% del worker morto con sl=0. Diario: docs/diary/2026-06-04-exit-lab.md Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
72 lines
2.7 KiB
Python
72 lines
2.7 KiB
Python
"""EXIT-21 — vol_rescale (livelli che RESPIRANO con la volatilita').
|
|
|
|
I TP/SL fissi della base nascono come multipli dell'ATR all'entrata:
|
|
m_tp = |tp0 - entry| / atr14[i] (quanti ATR dista il TP)
|
|
m_sl = |sl0 - entry| / atr14[i] (quanti ATR dista lo SL)
|
|
Invece di congelare la DISTANZA in prezzo (tp0, sl0), congeliamo il MULTIPLO e
|
|
lasciamo che la distanza respiri con l'ATR corrente:
|
|
tp(j) = entry + d * m_tp * atr14[j-1]
|
|
sl(j) = entry - d * m_sl * atr14[j-1]
|
|
Cosi' se la vol si comprime mentre la fade torna alla media, il TP si AVVICINA
|
|
(prende profitto prima, coerente col fatto che il movimento residuo si esaurisce);
|
|
se la vol esplode, lo SL si ALLARGA (meno stop-out su spike), e viceversa.
|
|
|
|
Anti-look-ahead: m_tp/m_sl usano atr14[i] (noto al close del bar d'entrata, dove
|
|
il worker fissa i livelli); levels(j) usa SOLO atr14[j-1]. Se atr14[j-1] e' NaN
|
|
(warmup), si ricade sui livelli fissi base (tp0/sl0).
|
|
|
|
Varianti (mode):
|
|
- tp_only : TP respira, SL resta fisso a sl0
|
|
- sl_only : SL respira, TP resta fisso a tp0
|
|
- both : entrambi respirano
|
|
|
|
GRID: mode in {tp_only, sl_only, both} (3 celle).
|
|
"""
|
|
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 VolRescale(ExitPolicy):
|
|
name = "vol_rescale"
|
|
|
|
def __init__(self, ctx, i, d, entry, tp0, sl0, mb, **params):
|
|
super().__init__(ctx, i, d, entry, tp0, sl0, mb, **params)
|
|
self.mode = params.get("mode", "both")
|
|
self.atr = ctx["atr14"]
|
|
# ATR all'entrata: noto al close[i] (il worker fissa i livelli qui).
|
|
a_i = self.atr[i]
|
|
if a_i is None or a_i != a_i or a_i <= 0:
|
|
# nessun ATR valido all'entrata -> impossibile derivare i multipli:
|
|
# la policy degenera nei livelli fissi base.
|
|
self.valid = False
|
|
self.m_tp = self.m_sl = 0.0
|
|
else:
|
|
self.valid = True
|
|
self.m_tp = abs(tp0 - entry) / a_i
|
|
self.m_sl = abs(sl0 - entry) / a_i
|
|
|
|
def levels(self, j: int):
|
|
if not self.valid:
|
|
return self.tp0, self.sl0, 1.0
|
|
a = self.atr[j - 1] # solo dati <= j-1
|
|
if a is None or a != a: # NaN -> ricadi sui fissi base
|
|
return self.tp0, self.sl0, 1.0
|
|
d = self.d
|
|
tp = self.tp0
|
|
sl = self.sl0
|
|
if self.mode in ("tp_only", "both"):
|
|
tp = self.entry + d * self.m_tp * a
|
|
if self.mode in ("sl_only", "both"):
|
|
sl = self.entry - d * self.m_sl * a
|
|
return tp, sl, 1.0
|
|
|
|
|
|
GRID = [{"mode": "tp_only"}, {"mode": "sl_only"}, {"mode": "both"}]
|
|
|
|
|
|
if __name__ == "__main__":
|
|
evaluate(VolRescale, GRID)
|