"""p42h_ema.py collect A..B OUT | analyze OUT.. -- [2026-10-29] user: "can EMA9 with EMA20 have a faster response than VWAP?" For every deep dip
(>= 0.5u from the day high to a low on a later bar, dip in by 10:00) find the FIRST time after the V low (low still holding; a new low resets)
of: X9 = EMA9 crosses above EMA20 | P9 = close > EMA9 and EMA9 rising | VWF = VWAP not falling (board 'flat') | VWC = close above VWAP.
Events up to 10:30. EMAs on 1-min closes from 9:30 (seeded with the first close). Report minutes after the low, % above the low, and the outcome
of a buy at the next open (+2% vs -2% first, rise, stop under the V low with a +2% target after 0.2% cost)."""
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))
exec(open(TR / "reports/ev3/p42e_test.py").read().split('if sys.argv[1] != "summary":')[0].split('"""', 2)[2])     # meas, play
if sys.argv[1] == "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"]:
            ref = R.REF.load(day, s)
            if s not in d["symbols"] 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) < 150 or R.minute(bars[0]["t"]) != 0: continue
            M = [R.minute(x["t"]) for x in bars]; u = max(ref["atr14"], bars[0]["o"] * 0.005)
            e9 = e20 = bars[0]["c"]; E9, E20, VW = [], [], []; cv = pv = 0
            for x in bars[:70]:
                e9 += (x["c"] - e9) * 2 / 10; e20 += (x["c"] - e20) * 2 / 21; E9.append(e9); E20.append(e20)
                cv += x["v"]; pv += (x["h"] + x["l"] + x["c"]) / 3 * x["v"]; VW.append(pv / max(cv, 1))
            hi_i = 0; lo_i = None; dip_on = False; got = {}
            for i in range(1, min(len(bars) - 1, 70)):
                if M[i] > 60: break
                x = bars[i]
                if not dip_on:
                    if x["h"] > bars[hi_i]["h"]: hi_i = i; lo_i = None; continue
                    if lo_i is None or x["l"] < bars[lo_i]["l"]: lo_i = i
                    if bars[hi_i]["h"] - bars[lo_i]["l"] >= 0.5 * u and M[i] <= 30: dip_on = True
                    continue
                if x["l"] < bars[lo_i]["l"]: lo_i = i; continue             # new low: the dip goes on, events must come after the low
                if i <= lo_i: continue
                ev = {"X9": E9[i] > E20[i] and E9[i - 1] <= E20[i - 1], "P9": x["c"] > E9[i] and E9[i] > E9[i - 1],
                      "VWF": i >= 3 and VW[i] >= VW[i - 3] * 0.9998 and VW[i] >= VW[i - 1], "VWC": x["c"] > VW[i]}
                for k, ok in ev.items():
                    if ok and k not in got:
                        got[k] = 1; P = bars[i + 1]["o"]; lo = bars[lo_i]["l"]; st = lo * 0.998
                        row = dict(kind=k, day=day, sym=s, t=M[i], after_low=M[i] - M[lo_i], above_low=round((P / lo - 1) * 100, 3), **meas(bars, i + 1, P))
                        if st < P: row["s1t2"] = round(play(bars, i + 1, P, st, 2), 3)
                        out.write(json.dumps(row) + "\n")
                if len(got) == 4: break
        del d; R.REF._CACHE.clear(); out.flush()
    out.close(); print("P42H_DONE")
else:
    X = [json.loads(l) for f in sys.argv[2:] for l in open(f)]
    Y = lambda Z, h: [z for z in Z if (z["day"] < "2025-10-01") == (h == 1)]
    def f(Z):
        a = lambda k: np.mean([z[k] for z in Z])
        e = (np.mean([z["f2"] == 1 for z in Z]) - np.mean([z["f2"] == -1 for z in Z])) * 100
        pl = np.mean([z["s1t2"] for z in Z if "s1t2" in z])
        return (f"n {len(Z):5} | {np.median([z['after_low'] for z in Z]):4.0f} min after the low, {np.median([z['above_low'] for z in Z]):+5.2f}% above it | "
                f"rise 60m {a('r60'):+.2f}% drop 30m {a('d30'):+.2f}% | +2% first {np.mean([z['f2'] == 1 for z in Z]):3.0%} / -2% {np.mean([z['f2'] == -1 for z in Z]):3.0%} (edge {e:+3.0f}) | V-low stop +2%: {pl:+.3f}%")
    print("#42 deep dip (>= 0.5 ATR by 10:00) -> FIRST event after the V low (low holding), events up to 10:30; buy next open. year 1 < 2025-10-01 <= year 2 (medians for min/% above)\n")
    for k, lab in (("P9", "close above EMA9 + EMA9 rising"), ("X9", "EMA9 crosses above EMA20"), ("VWF", "VWAP stops falling"), ("VWC", "close above VWAP")):
        Z = [x for x in X if x["kind"] == k]
        print(f"{lab}\n   year 1 {f(Y(Z, 1))}\n   year 2 {f(Y(Z, 2))}")
    print("P42H_ANALYSIS_DONE")
