"""p27_study.py collect A..B OUT | analyze OUT.. -- [2026-10-28] the user: study the most frequent bullish pattern, #27 "Drops -> higher low
-> breaks resistance". Every #27 confirmation (opening_patterns, 9:35-11:30) on the library with its readings: the pattern (drop size,
higher-low margin, breakout margin, legs, minutes the pattern took), the context (time, gap type, daily ATR, vs VWAP / open / prev close,
range position, 3-min and 15-min buy share, volume acceleration, RVOL so far) and the outcome from the next open (max rise 15/60 min,
max drop 30 min, higher after 15 min, +2% vs -2% first). analyze: every reading in 5 groups (cuts from year 1) -> year 1 / year 2."""
import gzip, json, sys
from pathlib import Path
import numpy as np
HERE = Path(__file__).resolve().parent; 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)
if sys.argv[1] == "collect":
    import v2_bar_replay as R
    from opening_patterns import Recognizer
    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"]:
            ref = R.REF.load(day, s); fp = R.OW / "flow" / day / f"{s}.json"
            if s not in d["symbols"] or not ref or not ref.get("prev_close") or not ref.get("atr14") or not fp.exists(): continue
            bars = [x for x in R.bars_of(d["symbols"][s]["bars"]) if 0 <= R.minute(x["t"]) < 390]
            if len(bars) < 150: continue
            fl = json.loads(fp.read_text()); fb, fs = fl["buy"], fl["sell"]
            Rz = Recognizer(ref["prev_close"], ref["atr14"], bars[0]["o"]); cum = 0.0
            for i, x in enumerate(bars[:-1]):
                t = R.minute(x["t"]); cum += x["v"]
                if t > 120: break
                new = Rz.update(x, t)
                if 27 not in new: continue
                lows = [q for q in Rz.piv if q[0] == "L"]; highs = [q for q in Rz.piv if q[0] == "H"]
                l1, l2, h1 = lows[-2], lows[-1], highs[-1]; u = Rz.u; c = x["c"]
                prior_high = max(q[1] for q in highs[:-1]) if len(highs) > 1 else Rz.o
                sh = lambda 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)
                V = Rz.V; prev = V[-18:-3] if len(V) >= 18 else V[:-3]
                hi, lo = max(Rz.H), min(Rz.L)
                row = dict(day=day, sym=s, t=t, ctx=Rz.ctx, gap=round(Rz.gap, 2), atr=round(ref["atr14"] / ref["prev_close"] * 100, 2),
                           drop_u=round((prior_high - l1[1]) / u, 3), hl_margin_u=round((l2[1] - l1[1]) / u, 3), brk_u=round((c - h1[1]) / u, 3),
                           took=t - l1[2], n_piv=len(Rz.piv), vs_vwap=round((c / Rz.vwap - 1) * 100, 3), vs_open=round((c / Rz.o - 1) * 100, 3),
                           vs_pc=round((c / Rz.pc - 1) * 100, 3), pos=round((c - lo) / (hi - lo), 3) if hi > lo else 0.5,
                           s3=round(sh(3), 3), s15=round(sh(15), 3), vacc=round((sum(V[-3:]) / 3) / (sum(prev) / len(prev)), 3) if len(V) >= 6 and sum(prev) else None,
                           rvol=round(cum / max(ref["cum_vol"][t], 1), 3), above_hi=round((c / hi - 1) * 100, 3), **meas(bars, i + 1, bars[i + 1]["o"]))
                out.write(json.dumps(row) + "\n")
        del d; R.REF._CACHE.clear(); nd += 1; out.flush(); print(day, nd, flush=True)
    out.close(); print("P27_DONE")
else:
    X = [json.loads(l) for f in sys.argv[2:] for l in open(f)]
    Y1 = [x for x in X if x["day"] < "2025-10-01"]; Y2 = [x for x in X if x["day"] >= "2025-10-01"]
    def st(Z):
        if not Z: return "  -  "
        return (f"n {len(Z):4} up15 {np.mean([z['c15'] > 0 for z in Z]):3.0%} edge {np.mean([z['f2'] == 1 for z in Z]) - np.mean([z['f2'] == -1 for z in Z]):+4.0%} "
                f"rise60 {np.mean([z['r60'] for z in Z]):+.2f} drop30 {np.mean([z['d30'] for z in Z]):+.2f}")
    print(f"#27 'Drops -> higher low -> breaks resistance': {len(X)} confirmations ({len(Y1)} year 1, {len(Y2)} year 2)")
    print("up15 = higher 15 min later; edge = (+2% first) - (-2% first); rise60 = max rise within 60 min %; drop30 = max drop within 30 min %\n")
    print(f"ALL #27            y1 {st(Y1)} | y2 {st(Y2)}")
    for c in ("GU", "GD", "FLAT"):
        print(f"   context {c:5}    y1 {st([x for x in Y1 if x['ctx'] == c])} | y2 {st([x for x in Y2 if x['ctx'] == c])}")
    F = ["t", "gap", "atr", "drop_u", "hl_margin_u", "brk_u", "took", "n_piv", "vs_vwap", "vs_open", "vs_pc", "pos", "s3", "s15", "vacc", "rvol", "above_hi"]
    print("\neach reading in 5 groups (cuts from year 1), y1 | y2;  <== = this group beats ALL #27 in BOTH years on up15 AND edge")
    b1u = np.mean([z['c15'] > 0 for z in Y1]); b2u = np.mean([z['c15'] > 0 for z in Y2])
    b1e = np.mean([z['f2'] == 1 for z in Y1]) - np.mean([z['f2'] == -1 for z in Y1]); b2e = np.mean([z['f2'] == 1 for z in Y2]) - np.mean([z['f2'] == -1 for z in Y2])
    for k in F:
        v1 = np.array([x[k] for x in Y1 if x.get(k) is not None], float)
        q = np.quantile(v1, [0.2, 0.4, 0.6, 0.8]); print(f" {k:12} cuts {np.round(q, 2)}")
        for gi in range(5):
            g = lambda x: x.get(k) is not None and int(np.searchsorted(q, x[k])) == gi
            a1 = [x for x in Y1 if g(x)]; a2 = [x for x in Y2 if g(x)]
            if not a1 or not a2: continue
            u1 = np.mean([z['c15'] > 0 for z in a1]); u2 = np.mean([z['c15'] > 0 for z in a2])
            e1 = np.mean([z['f2'] == 1 for z in a1]) - np.mean([z['f2'] == -1 for z in a1]); e2 = np.mean([z['f2'] == 1 for z in a2]) - np.mean([z['f2'] == -1 for z in a2])
            flag = "  <==" if u1 > b1u + 0.02 and u2 > b2u + 0.02 and e1 > b1e + 0.03 and e2 > b2e + 0.03 else ""
            print(f"    group {gi + 1}: y1 {st(a1)} | y2 {st(a2)}{flag}")
    print("P27_ANALYSIS_DONE")
