"""r11_rise.py collect A..B OUT | analyze OUT -- [2026-10-27] the user: R11 opening-turn filters judged by the RISE after the signal (not the
exit), on the last 6 months. collect: every signal of the current r11_setup (defaults, 15-min cooldown) + a random minute every 10 min on the
same stock-days; each measured from the buy price (next open): max rise / max drop within 30 / 60 min, +2% vs -2% first and +1% vs -1% first
(within 120 min), reached +3% / fell -3% within 60 min; filter readings: minute, % above the day's low, range position, vs open, vs prev close.
analyze: walk-forward -- pick the best filter combination on the first 3 months by EDGE = share(+2% first) - share(-2% first) (>= 30 signals),
test it unchanged on the last 3 months; plus a monthly roll (3 months pick -> next month test)."""
import gzip, json, sys, itertools
from pathlib import Path
import numpy as np
HERE = Path(__file__).resolve().parent
sys.path.insert(0, str(HERE)); sys.path.insert(0, "/var/www/screener/trade/reports/playbook"); sys.path.insert(0, "/var/www/screener/trade")
def meas(b, bi, P, lo, hi, m, c, o0, pc):
    w30, w60 = b[bi:bi + 30], b[bi:bi + 60]
    def first(x):
        for z in b[bi:bi + 120]:
            if z["l"] <= P * (1 - x / 100): return -1
            if z["h"] >= P * (1 + x / 100): return 1
        return 0
    r60 = (max(z["h"] for z in w60) / P - 1) * 100; d60 = (min(z["l"] for z in w60) / P - 1) * 100
    return dict(m=m, r30=round((max(z["h"] for z in w30) / P - 1) * 100, 3), r60=round(r60, 3), d30=round((min(z["l"] for z in w30) / P - 1) * 100, 3),
                d60=round(d60, 3), f2=first(2), f1=first(1), up3=r60 >= 3, dn3=d60 <= -3,
                above_low=round((c / lo - 1) * 100, 3), pos=round((c - lo) / (hi - lo), 3) if hi > lo else 0.5,
                vs_open=round((c / o0 - 1) * 100, 3), vs_pc=round((c / pc - 1) * 100, 3))
if sys.argv[1] == "collect":
    import v2_bar_replay as R
    import r11_setup
    a, b_ = sys.argv[2].split(".."); out = open(sys.argv[3], "w"); nd = 0
    for p in sorted(R.OW.glob("bars/bars_20*.json.gz")):
        day = p.name[5:15]
        if not (a <= day <= b_): continue
        d = json.load(gzip.open(p, "rt"))
        for s in d["top30_current"]:
            fp = R.OW / "flow" / day / f"{s}.json"; ref = R.REF.load(day, s)
            if s not in d["symbols"] or not fp.exists() or not ref or not ref.get("prev_close") or not ref.get("atr14"): continue
            bars = [x for x in R.bars_of(d["symbols"][s]["bars"]) if 0 <= R.minute(x["t"]) < 390]
            if len(bars) < 60: continue
            fl = json.loads(fp.read_text()); M = [R.minute(x["t"]) for x in bars]; last = -99; lo = 1e9; hi = 0
            for i in range(len(bars) - 1):
                m = M[i]; lo = min(lo, bars[i]["l"]); hi = max(hi, bars[i]["h"])
                if m >= 345: break
                c = bars[i]["c"]; P = bars[i + 1]["o"]
                if m % 10 == 0 and m >= 5:
                    out.write(json.dumps(dict(kind="random", day=day, sym=s, **meas(bars, i + 1, P, lo, hi, m, c, bars[0]["o"], ref["prev_close"]))) + "\n")
                if m - last < 15: continue
                ok, info = r11_setup.check(bars[:i + 1], fl, ref, m, None)
                if ok:
                    last = m; out.write(json.dumps(dict(kind="r11", day=day, sym=s, **meas(bars, i + 1, P, lo, hi, m, c, bars[0]["o"], ref["prev_close"]))) + "\n")
        del d; R.REF._CACHE.clear(); nd += 1; out.flush(); print(day, nd, flush=True)
    out.close(); print("R11RISE_COLLECT_DONE")
else:
    X = [json.loads(l) for l in open(sys.argv[2])]; S = [x for x in X if x["kind"] == "r11"]; RND = [x for x in X if x["kind"] == "random"]
    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)"
    edge = lambda Z: np.mean([z["f2"] == 1 for z in Z]) - np.mean([z["f2"] == -1 for z in Z])
    def f(Z):
        if not Z: return "n     0"
        a = lambda k: np.mean([z[k] for z in Z])
        return (f"n {len(Z):6} | rise 60m {a('r60'):+.2f}% drop 60m {a('d60'):+.2f}% (rise/drop {a('r60') / max(-a('d60'), 1e-9):.2f}) | +2% first {np.mean([z['f2'] == 1 for z in Z]):3.0%} "
                f"-2% first {np.mean([z['f2'] == -1 for z in Z]):3.0%} (edge {edge(Z):+.0%}) | +1%/-1% first {np.mean([z['f1'] == 1 for z in Z]):3.0%}/{np.mean([z['f1'] == -1 for z in Z]):3.0%} "
                f"| reached +3% {np.mean([z['up3'] for z in Z]):3.0%} fell -3% {np.mean([z['dn3'] for z in Z]):3.0%}")
    months = sorted({x["day"][:7] for x in X}); days = sorted({x["day"] for x in X}); cut = days[len(days) // 2]
    print(f"{len(S)} R11 signals, {len(RND)} random minutes, {days[0]} .. {days[-1]} ({len(days)} days); first half < {cut} <= second half\n")
    for lab, A_ in (("RANDOM minute", RND), ("ALL R11 (no filter)", S)):
        print(f"{lab}\n   first 3 months {f([x for x in A_ if x['day'] < cut])}\n   last 3 months  {f([x for x in A_ if x['day'] >= cut])}")
    TR = [x for x in S if x["day"] < cut]; TE = [x for x in S if x["day"] >= cut]
    res = sorted([(edge(Z), c, len(Z)) for c in COMBOS for Z in [[x for x in TR if keep(x, c)]] if len(Z) >= 30], key=lambda r: -r[0])
    print(f"\nWALK-FORWARD: best 5 combinations on the FIRST 3 months (by edge = +2% first - -2% first), then the LAST 3 months unchanged:")
    for e, c, n in res[:5]:
        print(f"   {name(c)[:62]:62}\n      pick months {f([x for x in TR if keep(x, c)])}\n      TEST months {f([x for x in TE if keep(x, c)])}")
    print("\nMONTHLY ROLL (3 months pick -> next month test):")
    oos, oosr = [], []
    for k in range(3, len(months)):
        tr = [x for x in S if x["day"][:7] in months[k - 3:k]]; te = [x for x in S if x["day"][:7] == months[k]]
        best = max(((edge(Z), c) for c in COMBOS for Z in [[x for x in tr if keep(x, c)]] if len(Z) >= 30), key=lambda r: r[0])
        Z = [x for x in te if keep(x, best[1])]; oos += Z; oosr += [x for x in RND if x["day"][:7] == months[k]]
        print(f"   pick {months[k - 3]}..{months[k - 1]}: {name(best[1])[:55]:55} (edge {best[0]:+.0%}) -> test {months[k]}: {f(Z)}")
    print(f"   ALL test months, chosen filters: {f(oos)}\n   same months, random minute:      {f(oosr)}")
    print("\nthe FMC rules, both halves (first 3 months | last 3 months):")
    for c in [(60, None, None, False, False), (None, 3, None, False, False), (None, None, None, True, False), (60, 3, None, True, False), (30, 3, 0.5, False, True)]:
        print(f"   {name(c)[:52]:52}\n      first {f([x for x in TR if keep(x, c)])}\n      last  {f([x for x in TE if keep(x, c)])}")
    print("R11RISE_DONE")
