Files
MBT/scripts/run_feature_split.py
T
FXDavidandClaude Sonnet 4.6 3490441649 Audit fixes: public repo readiness + MCP improvements
- Add MIT LICENSE
- Anonymize broker path in config.example.yaml; add slippage_points + risk_per_trade
- Fix encoding ansi→cp1252 in signals.py (cross-platform safe)
- Fix install.py silently ignoring pip errors
- Cap get_ohlcv at 2000 bars; add ping tool; clarify HTML report UX in descriptions
- Escape symbol/timeframe in HTML report to prevent malformed output
- Move run_*.py to scripts/; move example CSVs to examples/
- Update .gitignore to block accidental CSV commits from root
- README: Python 3.9+ requirement, Chart.js internet note, new sections for examples/ and scripts/

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-08 12:08:23 -07:00

51 lines
2.0 KiB
Python

"""
Validate the momentum-inversion finding: does the pattern hold in BOTH the
train half and the locked test half? A real edge shows in both; noise doesn't.
"""
import csv
from datetime import datetime
from core.connection import signal_file_path
from core.backtest import run_backtest
rep = run_backtest(sequential=False)
outcome_by_time = {t.time.strftime("%Y-%m-%d %H:%M"): t.outcome for t in rep.trades}
COLS = ["time","direction","entry","sl","tp","atr_pips","regime",
"adx","mom_fast","mom_med","mom_slow","vol_ratio","vpt_dir","near_level"]
rows = []
with open(signal_file_path(), encoding="ansi") as f:
for r in csv.reader(f):
if not r or len(r) < 12 or not r[0][:4].isdigit(): continue
d = dict(zip(COLS, r))
try:
dt = datetime.strptime(d["time"], "%Y.%m.%d %H:%M")
except ValueError:
continue
oc = outcome_by_time.get(dt.strftime("%Y-%m-%d %H:%M"))
if oc not in ("WIN","LOSS"): continue
rows.append({"dt": dt, "win": 1 if oc=="WIN" else 0,
"mom_med": abs(float(d["mom_med"])),
"mom_slow": abs(float(d["mom_slow"])),
"vol_ratio": float(d["vol_ratio"])})
rows.sort(key=lambda r: r["dt"])
split = int(len(rows)*0.7)
train, test = rows[:split], rows[split:]
def terciles(data, feat):
vals = sorted(r[feat] for r in data)
c1, c2 = vals[len(vals)//3], vals[2*len(vals)//3]
g = [[],[],[]]
for r in data:
idx = 0 if r[feat]<=c1 else (1 if r[feat]<=c2 else 2)
g[idx].append(r["win"])
return [(sum(x)/len(x)*100 if x else 0, len(x)) for x in g]
for feat in ["mom_med","mom_slow","vol_ratio"]:
tr = terciles(train, feat)
te = terciles(test, feat)
print(f"=== {feat}: win rate by tercile (low / mid / high) ===")
print(f" TRAIN: {tr[0][0]:5.1f}% / {tr[1][0]:5.1f}% / {tr[2][0]:5.1f}% (n={tr[0][1]},{tr[1][1]},{tr[2][1]})")
print(f" TEST : {te[0][0]:5.1f}% / {te[1][0]:5.1f}% / {te[2][0]:5.1f}% (n={te[0][1]},{te[1][1]},{te[2][1]})")
print()