"""p42_test.py collect A..B OUT | analyze OUT.. | day DAY SYM.. -- [2026-10-29] the user's ENTRY #42: only 9:30-10:00, a BIG DIP, buy AS SOON AS the V
turns. Dip = from the day's high so far down to the low, >= k x daily ATR. V turn = the first bar after the low that closes above the previous
bar's high while the low holds (the low is at least 1 bar old). Variants: A = V turn; B = V turn + 3-min buy share > 15-min; C = V turn + the price
has recovered >= 25% of the dip. The low and the signal both before 10:00; buy at the next open; one buy per stock per day per variant.
Measured from the buy: max rise 15/60 min, max drop 30 min, higher after 15 min, +2% vs -2% first, distance above the V low (the natural stop).
Random baseline: every 5th minute 9:35-10:00 on the same stock-days."""
import gzip, json, sys
from pathlib import Path
import numpy as np
TR = Path("/var/www/screener/trade"); sys.path.insert(0, str(TR / "reports/playbook")); sys.path.insert(0, str(TR))
def meas(b, bi, P):
    f2 = 0
    for z in b[bi:bi + 120]:
        if z["l"] <= P * 0.98: f2 = -1; break
        if z["h"] >= P * 1.02: f2 = 1; break
    r = lambda n: round((max(z["h"] for z in b[bi:bi + n]) / P - 1) * 100, 3)
    return dict(r15=r(15), r60=r(60), d30=round((min(z["l"] for z in b[bi:bi + 30]) / P - 1) * 100, 3), c15=round((b[min(bi + 14, len(b) - 1)]["c"] / P - 1) * 100, 3), f2=f2)
def signals(bars, M, fl, atr_d, ks=(0.3, 0.5)):
    """-> list of (variant, k, i_signal, low, dip_pct)"""
    fb, fs = fl["buy"], fl["sell"]
    sh = lambda t, n: sum(fb[max(0, t - n + 1):t + 1]) / max(sum(fb[max(0, t - n + 1):t + 1]) + sum(fs[max(0, t - n + 1):t + 1]), 1)
    out = []; cv = pv = 0; VW = []
    for x in bars[:40]: cv += x["v"]; pv += (x["h"] + x["l"] + x["c"]) / 3 * x["v"]; VW.append(pv / max(cv, 1))
    vr = lambda i: i >= 3 and VW[i] >= VW[i - 3] * 1.0002 and VW[i] >= VW[i - 1]      # VWAP rising (board vwap2 definition)
    vr1 = lambda i: i >= 1 and VW[i] > VW[i - 1]                                        # VWAP higher than 1 minute ago
    for k in ks:
        done = set()
        for i in range(2, len(bars) - 1):
            if M[i] > 30: break
            hi_i = max(range(0, i), key=lambda j: bars[j]["h"]); hi = bars[hi_i]["h"]
            lo_i = min(range(hi_i + 1, i), key=lambda j: bars[j]["l"]) if hi_i + 1 < i else None     # the low must come from a LATER bar than the high (10/29 fix: EROC same-bar dip)
            if lo_i is None or lo_i >= i: continue
            lo = bars[lo_i]["l"]
            if hi - lo < k * atr_d or bars[i]["l"] < lo: continue                       # big dip, low still holding on this bar
            if not bars[i]["c"] > bars[i - 1]["h"]: continue                               # V turn: close above the previous bar's high
            dip = (hi / lo - 1) * 100
            dlow = lo <= min(bars[j]["l"] for j in range(0, lo_i + 1)); bopen = lo < bars[0]["o"]        # V low = day low so far | V low under the 9:30 open (10/29, EROC)
            for v, ok in (("A", True), ("B", sh(M[i], 3) > sh(M[i], 15)), ("C", bars[i]["c"] >= lo + 0.25 * (hi - lo)), ("V", vr(i)), ("V1", vr1(i)), ("AD", dlow), ("AO", bopen), ("VD", vr(i) and dlow), ("VO", vr(i) and bopen)):
                if ok and v not in done:
                    done.add(v); out.append((v, k, i, lo, dip))
    return out
mode = sys.argv[1]
if mode == "collect":
    import v2_bar_replay as R
    a, b_ = sys.argv[2].split(".."); out = open(sys.argv[3], "w")
    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("atr14"): continue
            bars = [x for x in R.bars_of(d["symbols"][s]["bars"]) if 0 <= R.minute(x["t"]) < 390]
            if len(bars) < 150 or R.minute(bars[0]["t"]) != 0: continue
            M = [R.minute(x["t"]) for x in bars]; fl = json.loads(fp.read_text())
            for v, k, i, lo, dip in signals(bars, M, fl, ref["atr14"]):
                P = bars[i + 1]["o"]
                out.write(json.dumps(dict(kind=f"{v}{k}", day=day, sym=s, t=M[i], dip=round(dip, 2), above_low=round((P / lo - 1) * 100, 3), **meas(bars, i + 1, P))) + "\n")
            for i in range(len(bars) - 1):
                if 5 <= M[i] <= 30 and M[i] % 5 == 0:
                    out.write(json.dumps(dict(kind="RANDOM", day=day, sym=s, t=M[i], **meas(bars, i + 1, bars[i + 1]["o"]))) + "\n")
        del d; R.REF._CACHE.clear()
    out.close(); print("P42_DONE")
elif mode == "day":
    import v2_bar_replay as R
    day = sys.argv[2]; d = json.load(gzip.open(R.OW / "bars" / f"bars_{day}.json.gz", "rt"))
    for s in sys.argv[3:]:
        ref = R.REF.load(day, s); fl = json.loads((R.OW / "flow" / day / f"{s}.json").read_text())
        bars = [x for x in R.bars_of(d["symbols"][s]["bars"]) if 0 <= R.minute(x["t"]) < 390]; M = [R.minute(x["t"]) for x in bars]
        sig = signals(bars, M, fl, ref["atr14"])
        print(f"{s} {day} | daily ATR {ref['atr14'] / ref['prev_close'] * 100:.1f}%")
        for v, k, i, lo, dip in sig:
            P = bars[i + 1]["o"]; m = meas(bars, i + 1, P); hm = lambda t: f"{9 + (30 + t) // 60}:{(30 + t) % 60:02d}"
            print(f"   {v} dip>={k}ATR: dip {dip:.1f}% to {lo:.2f} -> V turn {hm(M[i])}, buy {hm(M[i + 1])} @ {P:.2f} ({(P / lo - 1) * 100:+.1f}% above the low) | max rise 15m {m['r15']:+.2f}% 60m {m['r60']:+.2f}% | max drop 30m {m['d30']:+.2f}% | 15m later {m['c15']:+.2f}% | {'+2% first' if m['f2'] == 1 else '-2% first' if m['f2'] == -1 else 'neither'}")
        if not sig: print("   no #42 signal")
elif mode == "analyze":
    X = [json.loads(l) for f in sys.argv[2:] for l in open(f)]
    H = lambda Z, h: [z for z in Z if (z["day"] < "2026-07-28") == (h == 1)]
    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):5} | max rise 15m {a('r15'):+.2f}% 60m {a('r60'):+.2f}% | max drop 30m {a('d30'):+.2f}% | higher after 15m {np.mean([z['c15'] > 0 for z in Z]):3.0%} "
                f"| +2% first {np.mean([z['f2'] == 1 for z in Z]):3.0%} / -2% first {np.mean([z['f2'] == -1 for z in Z]):3.0%}")
    print(f"#42 (6 months; first 3m < 2026-07-28 <= last 3m)\n")
    lab = {"RANDOM": "RANDOM minute 9:35-10:00", "A0.3": "A V turn, dip >= 0.3 ATR", "B0.3": "B V turn + 3m>15m, dip >= 0.3 ATR", "C0.3": "C V turn + 25% recovered, dip >= 0.3 ATR",
           "A0.5": "A V turn, dip >= 0.5 ATR", "B0.5": "B V turn + 3m>15m, dip >= 0.5 ATR", "C0.5": "C V turn + 25% recovered, dip >= 0.5 ATR",
           "V0.3": "V V turn + VWAP rising (3m +0.02%), dip >= 0.3 ATR", "V0.5": "V V turn + VWAP rising (3m +0.02%), dip >= 0.5 ATR",
           "V10.3": "V1 V turn + VWAP up vs 1 min ago, dip >= 0.3 ATR", "V10.5": "V1 V turn + VWAP up vs 1 min ago, dip >= 0.5 ATR",
           "AD0.5": "AD V turn, V low = day low, dip >= 0.5 ATR", "AO0.5": "AO V turn, V low under the open, dip >= 0.5 ATR",
           "VD0.5": "VD V turn + VWAP rising + V low = day low, dip >= 0.5 ATR", "VO0.5": "VO V turn + VWAP rising + V low under the open, dip >= 0.5 ATR"}
    for k, l in lab.items():
        Z = [x for x in X if x["kind"] == k]
        extra = f"   (buy median {np.median([z['above_low'] for z in Z]):+.2f}% above the V low, dip median {np.median([z['dip'] for z in Z]):.1f}%)" if Z and k != "RANDOM" else ""
        print(f"{l}{extra}\n   first 3m {f(H(Z, 1))}\n   last 3m  {f(H(Z, 2))}")
    print("P42_ANALYSIS_DONE")
