"""r11_wf.py -- [2026-10-27] the user: WALK-FORWARD for R11 filters. Train = 3 months -> pick the best combination (highest total P/L,
>= MIN_N trades) of: last signal minute, max % above the day's low, max position in the day's range, below the open, below the prev close.
Test = the FOLLOWING 3 months with that combination unchanged; roll forward 3 months. Signals from r11_wf_collect.py (each played alone,
C3 exit, 0.2% cost). Out-of-sample = only the test windows."""
import json, itertools
from collections import Counter
import numpy as np
S = [json.loads(l) for f in ("r11wf_y1.jsonl", "r11wf_y2.jsonl") for l in open(f)]
MIN_N = 15
T_MAX = [None, 30, 60, 90, 120, 180]; LOW = [None, 2, 3, 5]; POS = [None, 0.4, 0.5, 0.6]; BO = [False, True]; BPC = [False, True]
COMBOS = list(itertools.product(T_MAX, LOW, POS, BO, BPC))
def keep(x, c):
    t, lo, po, bo, bpc = c
    return (t is None or x["m"] <= t) and (lo is None or x["above_low"] <= lo) and (po is None or x["pos"] <= po) and (not bo or x["vs_open"] <= 0) and (not bpc or x["vs_pc"] <= 0)
def name(c):
    t, lo, po, bo, bpc = c; p = []
    if t is not None: p.append(f"by {9 + (30 + t) // 60}:{(30 + t) % 60:02d}")
    if lo is not None: p.append(f"<= {lo}% above low")
    if po is not None: p.append(f"range pos <= {po}")
    if bo: p.append("below open")
    if bpc: p.append("below prev close")
    return ", ".join(p) or "no filter (all R11)"
st = lambda Z: (len(Z), np.mean([z["pl"] for z in Z]) if Z else 0, sum(z["pl"] for z in Z))
month = lambda d: d[:7]
months = sorted({month(x["day"]) for x in S}); print(f"{len(S)} R11 signals, {months[0]} .. {months[-1]} ({len(months)} months)")
n, a, s = st(S); print(f"ALL R11, no filter: {n} trades, {a:+.3f}%/trade, total {s:+.1f}%\n")
print(f"{'train':17} | {'chosen combination':58} | {'train n  avg  total':22} | {'TEST (next 3 months) n  avg  total':36} | {'test, no filter':22}")
oos, oos_base, picks = [], [], Counter()
for k in range(0, len(months) - 5, 3):
    tr_m, te_m = months[k:k + 3], months[k + 3:k + 6]
    TR = [x for x in S if month(x["day"]) in tr_m]; TE = [x for x in S if month(x["day"]) in te_m]
    best = None
    for c in COMBOS:
        Z = [x for x in TR if keep(x, c)]
        if len(Z) < MIN_N: continue
        r = st(Z)
        if best is None or (r[2], r[1]) > (best[1][2], best[1][1]): best = (c, r)
    c, r = best; Zt = [x for x in TE if keep(x, c)]; rt = st(Zt); rb = st(TE); oos += Zt; oos_base += TE; picks[name(c)] += 1
    print(f"{tr_m[0]}..{tr_m[-1]} | {name(c)[:58]:58} | {r[0]:4} {r[1]:+.2f}% {r[2]:+7.1f}% | {te_m[0]}..{te_m[-1]}: {rt[0]:4} {rt[1]:+.3f}% {rt[2]:+7.1f}% | {rb[0]:5} {rb[1]:+.3f}% {rb[2]:+7.1f}%")
o = st(oos); ob = st(oos_base)
print(f"\nOUT-OF-SAMPLE (all test windows): chosen filters {o[0]} trades, {o[1]:+.3f}%/trade, total {o[2]:+.1f}%  |  no filter {ob[0]} trades, {ob[1]:+.3f}%/trade, total {ob[2]:+.1f}%")
print("how often each combination was chosen:", dict(picks))
print("\nfixed combinations over the WHOLE 2 years (in-sample view, for reference), year 1 | year 2:")
Y1 = [x for x in S if x["day"] < "2025-10-01"]; Y2 = [x for x in S if x["day"] >= "2025-10-01"]
res = []
for c in COMBOS:
    z1 = [x for x in Y1 if keep(x, c)]; z2 = [x for x in Y2 if keep(x, c)]
    if len(z1) >= 30 and len(z2) >= 30: res.append((min(st(z1)[1], st(z2)[1]), c, st(z1), st(z2)))
for w, c, r1, r2 in sorted(res, key=lambda x: -x[0])[:8]:
    print(f"   {name(c)[:60]:60} | y1 {r1[0]:4} {r1[1]:+.3f}% | y2 {r2[0]:4} {r2[1]:+.3f}%")
for c in [(None, None, None, False, False), (60, None, None, False, False), (None, 3, None, False, False), (None, None, None, True, False), (60, 3, None, True, False)]:
    z1 = [x for x in Y1 if keep(x, c)]; z2 = [x for x in Y2 if keep(x, c)]
    print(f"   [rule from FMC] {name(c)[:44]:44} | y1 {st(z1)[0]:4} {st(z1)[1]:+.3f}% | y2 {st(z2)[0]:4} {st(z2)[1]:+.3f}%")
print("R11WF_ANALYSIS_DONE")
