Files
momentum devandClaude Fable 5 485cec0372 feat: wide_stop_veto_v1 — skip entries with stops >12% below entry (shadow)
Year-replay stop-width buckets: <8% +0.072R, 8-12% +0.120R, 12-16%
-0.054R, >16% -0.092R — the wide cohort holds every recent dollar
disaster (BIYA 16.7%, FCUV day-2 17.5%), where the exit ladder's blind
spot (BE/trail only arm on the way up) rides the full catastrophe
distance. New stopwidth_gate intervention (fail-open without levels) +
registered spec, same forward bar as the pm-high gate; the two cohorts
overlap so the adjudicator attributes which does the work. In-sample
counts for nothing.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-03 21:06:37 -05:00

77 lines
3.0 KiB
Python

"""iv_pmhigh_gate: keep when RTH reclaimed the premarket high, zero otherwise."""
from datetime import date
import pandas as pd
from research.experiments.replay import Fire, iv_pmhigh_gate
class _Con:
def __init__(self, pm_high):
self._pm = pm_high
def execute(self, sql, params=None):
self._rows = [(self._pm,)] if self._pm is not None else []
return self
def fetchall(self):
return self._rows
def _bars(rows):
"""rows: (high, is_regular_hours)"""
return pd.DataFrame([{"timestamp": i, "high": h, "low": h - 0.2, "open": h - 0.1,
"close": h - 0.05, "is_regular_hours": reg}
for i, (h, reg) in enumerate(rows)])
def _fire():
return Fire(fire_id="o1", symbol="TST", session_date=date(2026, 7, 31), fill_ts=None,
entry_price=10.0, stop_price=9.0, exit_price=11.0, baseline_r=1.0,
trigger_price=10.0)
def test_kept_when_rth_broke_pm_high():
bars = _bars([(9.5, False), (10.6, True), (10.2, True)]) # RTH high 10.6 >= 10.5
v, d = iv_pmhigh_gate(_Con(10.5), _fire(), bars, 2, 1.0)
assert v == 1.0 and d["broke"] is True
def test_zeroed_when_pm_high_never_reclaimed():
# premarket touched 10.5 (definitionally its own high) but RTH never did
bars = _bars([(10.5, False), (10.1, True), (10.2, True)])
v, d = iv_pmhigh_gate(_Con(10.5), _fire(), bars, 2, 1.0)
assert v == 0.0 and d["broke"] is False
def test_fail_open_without_pm_row():
bars = _bars([(10.6, True)])
v, d = iv_pmhigh_gate(_Con(None), _fire(), bars, 0, 1.0)
assert v == 1.0 and d["broke"] is None and d["reason"] == "no_pm_row"
def test_bars_after_entry_do_not_count():
# pm high broken only AFTER the entry bar (index 1) -> gated at entry time
bars = _bars([(9.5, False), (10.1, True), (10.9, True)])
v, d = iv_pmhigh_gate(_Con(10.5), _fire(), bars, 1, 1.0)
assert v == 0.0 and d["broke"] is False
def test_stopwidth_gate_zeroes_wide_and_keeps_tight():
from research.experiments.replay import iv_stopwidth_gate
P = {"max_stop_pct": 0.12}
wide = Fire(fire_id="o1", symbol="W", session_date=date(2026, 8, 3), fill_ts=None,
entry_price=17.60, stop_price=14.43, exit_price=None, baseline_r=-0.7,
trigger_price=17.35) # 18% stop -> gated
v, d = iv_stopwidth_gate(wide, -0.7, P)
assert v == 0.0 and d["gated"] is True
tight = Fire(fire_id="o2", symbol="T", session_date=date(2026, 8, 3), fill_ts=None,
entry_price=10.0, stop_price=9.2, exit_price=None, baseline_r=0.5,
trigger_price=10.0) # 8% stop -> kept
v, d = iv_stopwidth_gate(tight, 0.5, P)
assert v == 0.5 and d["gated"] is False
nolvl = Fire(fire_id="o3", symbol="N", session_date=date(2026, 8, 3), fill_ts=None,
entry_price=None, stop_price=None, exit_price=None, baseline_r=0.3,
trigger_price=None)
v, d = iv_stopwidth_gate(nolvl, 0.3, P)
assert v == 0.3 and d["reason"] == "no_levels" # fail-open