"""p42g_vol.py collect A..B OUT | analyze OUT.. -- [2026-10-29] user: "we are still buying at the wrong spot" -> volume BALANCE in shares at the
#42 buy: net buying since the V low vs the net selling of the flush (high -> V low), gross buy since low / sell in the flush, volume since the
low / flush volume, time-of-day volume since the low. Signals: HL = higher-low turn by 10:00 (no share filter); A = plain V turn, dip >= 0.5u,
by 10:00 (low on a later bar than the high). Quintiles cut on year 1 (< 2025-10-01), edge = +2% first - -2% first, checked in both years."""
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)); sys.path.insert(0, str(TR / "reports/ev3"))
exec(open(TR / "reports/ev3/p42e_test.py").read().split('if sys.argv[1] != "summary":')[0].split('"""', 2)[2])     # meas, play, hl_signals
def a_turns(bars, M, u, tmax):
    for i in range(3, len(bars) - 1):
        if M[i] > tmax: break
        hi_i = max(range(0, i), key=lambda k: bars[k]["h"])
        if hi_i + 1 >= i: continue
        lo_i = min(range(hi_i + 1, i), key=lambda k: bars[k]["l"]); lo = bars[lo_i]["l"]
        if bars[hi_i]["h"] - lo >= 0.5 * u and bars[i]["l"] >= lo and bars[i]["c"] > bars[i - 1]["h"]:
            return [(i, hi_i, lo_i, lo)]
    return []
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"]:
            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) < 150 or R.minute(bars[0]["t"]) != 0: continue
            M = [R.minute(x["t"]) for x in bars]; fl = json.loads(fp.read_text()); fb, fs = fl["buy"], fl["sell"]; vpm = ref.get("vol_per_min") or [0] * 390
            u = max(ref["atr14"], bars[0]["o"] * 0.005)
            sig = []
            hl = hl_signals(bars, M, u, 30)
            if hl:
                i, L1, L2, BH, dip = hl[0]
                hi_i = max(range(0, i), key=lambda k: bars[k]["h"]); lo_i = min(range(hi_i + 1, i), key=lambda k: bars[k]["l"])
                sig.append(("HL", i, hi_i, lo_i, L1))
            for i, hi_i, lo_i, lo in a_turns(bars, M, u, 30): sig.append(("A", i, hi_i, lo_i, lo))
            for kind, i, hi_i, lo_i, lo in sig:
                f0, f1 = M[hi_i], M[lo_i]; r0, r1 = M[lo_i] + 1, M[i]
                fB, fS = sum(fb[f0:f1 + 1]), sum(fs[f0:f1 + 1]); rB, rS = sum(fb[r0:r1 + 1]), sum(fs[r0:r1 + 1])
                fV = sum(x["v"] for x in bars[hi_i:lo_i + 1]); rV = sum(x["v"] for x in bars[lo_i + 1:i + 1])
                norm = sum(vpm[m] for m in range(r0, r1 + 1))
                P = bars[i + 1]["o"]; st = lo * 0.998
                row = dict(kind=kind, day=day, sym=s, t=M[i], net_ratio=round((rB - rS) / (fS - fB), 3) if fS > fB else None,
                           gross=round(rB / fS, 3) if fS else None, vol_ratio=round(rV / fV, 3) if fV else None, tod=round(rV / norm, 3) if norm else None,
                           flush_share=round(fB / (fB + fS), 3) if fB + fS else None, since_share=round(rB / (rB + rS), 3) if rB + rS else None,
                           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")
        del d; R.REF._CACHE.clear(); out.flush()
    out.close(); print("P42G_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)]
    e = lambda Z: (np.mean([z["f2"] == 1 for z in Z]) - np.mean([z["f2"] == -1 for z in Z])) * 100 if Z else float("nan")
    pl = lambda Z: np.mean([z["s1t2"] for z in Z if "s1t2" in z]) if any("s1t2" in z for z in Z) else float("nan")
    print("#42 volume balance at the buy, 2 years (year 1 < 2025-10-01 <= year 2). EDGE = (+2% first) - (-2% first) in points; P/L = stop under V low, +2% target, after 0.2% cost\n")
    for kind, lab in (("HL", "HIGHER-LOW turn by 10:00"), ("A", "plain V turn, dip >= 0.5 ATR, by 10:00")):
        Z = [x for x in X if x["kind"] == kind]
        print(f"=== {lab}: n {len(Y(Z, 1))} / {len(Y(Z, 2))} | edge {e(Y(Z, 1)):+.0f} / {e(Y(Z, 2)):+.0f} | P/L {pl(Y(Z, 1)):+.3f} / {pl(Y(Z, 2)):+.3f}")
        for k, desc in (("net_ratio", "NET buying since low / NET selling in the flush"), ("gross", "buy volume since low / sell volume in the flush"),
                        ("vol_ratio", "volume since low / flush volume"), ("tod", "volume since low vs normal for those minutes"),
                        ("flush_share", "buy share during the flush"), ("since_share", "buy share since the low")):
            Zk = [z for z in Z if z.get(k) is not None]
            q = np.quantile([z[k] for z in Y(Zk, 1)], [0.2, 0.4, 0.6, 0.8])
            cells = []
            for gi in range(5):
                g = [z for z in Zk if int(np.searchsorted(q, z[k])) == gi]
                cells.append(f"{e(Y(g, 1)):+4.0f}/{e(Y(g, 2)):+4.0f} pl {pl(Y(g, 1)):+.2f}/{pl(Y(g, 2)):+.2f} (n{len(Y(g, 1))}/{len(Y(g, 2))})")
            print(f"  {k:11} {desc}\n     quintile cuts {np.round(q, 2)}  (low -> high)\n     " + "\n     ".join(cells))
        Zk = [z for z in Z if z.get("net_ratio") is not None]
        print("  fixed NET ratio thresholds:")
        for th in (0.1, 0.3, 0.5, 1.0):
            g = [z for z in Zk if z["net_ratio"] >= th]
            print(f"     net >= {th:3}: edge {e(Y(g, 1)):+4.0f}/{e(Y(g, 2)):+4.0f} | P/L {pl(Y(g, 1)):+.3f}/{pl(Y(g, 2)):+.3f} | n {len(Y(g, 1))}/{len(Y(g, 2))}")
        print()
    print("P42G_ANALYSIS_DONE")
