"""p42e_test.py A..B OUT | summary OUT.. -- [2026-10-29] user: "we keep buying at the wrong position". #42 HIGHER-LOW entry (position, not strength):
deep dip >= 0.5u from the day high to the V low L1 (later bar) -> first bounce to BH >= L1 + 25% of the dip -> pullback that gives back >= 40% of
the bounce with its low L2 > L1 (higher low) -> buy the first bar after L2 that closes above the previous bar's high while the close is still in the
LOWER 60% of the bounce (L1..BH). Same stocks as #41. Variants: window 9:45 | 10:00; + 3m share >= 55%; + liquidity (>= 2000 sh/min on the bars so
far, no empty minutes). Stops: under L2 (x0.998) and under L1."""
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))
COST = 0.2
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 play(b, bi, P, stop, tgt):
    for j in range(bi, len(b)):
        x = b[j]
        if x["l"] <= stop: return (min(stop, x["o"]) if j > bi else stop) / P * 100 - 100 - COST
        if tgt and x["h"] >= P * (1 + tgt / 100): return tgt - COST
    return b[-1]["c"] / P * 100 - 100 - COST
def hl_signals(bars, M, u, tmax):
    """-> list of (i, L1, L2, BH, dip%): every higher-low turn up to tmax (first one per stock is used)"""
    out = []
    for i in range(4, len(bars) - 1):
        if M[i] > tmax: break
        hi_i = max(range(0, i), key=lambda k: bars[k]["h"])
        if hi_i + 3 >= i: continue
        l1_i = min(range(hi_i + 1, i), key=lambda k: bars[k]["l"]); L1 = bars[l1_i]["l"]
        if bars[hi_i]["h"] - L1 < 0.5 * u or l1_i + 2 >= i: continue
        bh_i = max(range(l1_i + 1, i), key=lambda k: bars[k]["h"]); BH = bars[bh_i]["h"]
        if BH - L1 < 0.25 * (bars[hi_i]["h"] - L1) or bh_i + 1 >= i: continue
        l2_i = min(range(bh_i + 1, i), key=lambda k: bars[k]["l"]); L2 = bars[l2_i]["l"]
        if not (L2 > L1 and BH - L2 >= 0.4 * (BH - L1)): continue
        x = bars[i]
        if x["l"] < L2 or not x["c"] > bars[i - 1]["h"]: continue
        if (x["c"] - L1) / (BH - L1) > 0.6: continue
        out.append((i, L1, L2, BH, (bars[hi_i]["h"] / L1 - 1) * 100))
    return out
if sys.argv[1] != "summary":
    import v2_bar_replay as R
    a, b_ = sys.argv[1].split(".."); out = open(sys.argv[2], "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"]
            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)
            u = max(ref["atr14"], bars[0]["o"] * 0.005)
            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))
            vnf = lambda i: i >= 3 and VW[i] >= VW[i - 3] * 0.9998 and VW[i] >= VW[i - 1]      # VWAP not sloping down (board vwap2 "flat")
            def liq(i):
                nb = min(i + 1, 15); V = [x["v"] for x in bars[i + 1 - nb:i + 1]]
                return nb >= 3 and sum(V) / nb >= 2000 and M[i] - M[i + 1 - nb] <= nb - 1
            for tmax, tag in ((15, "9:45"), (30, "10:00")):
                done = set()
                for i, L1, L2, BH, dip in hl_signals(bars, M, u, tmax):
                    s3 = sh(M[i], 3)
                    vf = vnf(i)
                    for k, ok in (("HL", True), ("HL55", s3 >= 0.55), ("HLliq", liq(i)), ("HL55liq", s3 >= 0.55 and liq(i)),
                                  ("HL+VW", vf), ("HL55+VW", vf and s3 >= 0.55), ("HLliq+VW", vf and liq(i)), ("HL55liq+VW", vf and s3 >= 0.55 and liq(i))):
                        key = f"{k} {tag}"
                        if not ok or key in done: continue
                        done.add(key); P = bars[i + 1]["o"]
                        ex = {}
                        for sn, st in (("s2", L2 * 0.998), ("s1", L1 * 0.998)):
                            if st < P:
                                for tg in (2, 3): ex[f"{sn}t{tg}"] = round(play(bars, i + 1, P, st, tg), 3)
                                ex[f"{sn}hold"] = round(play(bars, i + 1, P, st, None), 3)
                        out.write(json.dumps(dict(kind=key, day=day, sym=s, t=M[i], dip=round(dip, 2), above_l1=round((P / L1 - 1) * 100, 3), above_l2=round((P / L2 - 1) * 100, 3), **meas(bars, i + 1, P), **ex)) + "\n")
            for i in range(len(bars) - 1):
                if 5 <= M[i] <= 30 and M[i] % 3 == 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.flush()
    out.close(); print("P42F_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"] < "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%}")
    def g(Z, k):
        v = np.sort([z[k] for z in Z if k in z])
        return f"{v.mean():+.3f}% (w/o top5 {v[:-5].mean():+.3f})" if len(v) > 10 else "-"
    print("#42 HIGHER-LOW entry, 6 months: first 3m 2026-04-28..07-27 | last 3m 07-28..10-27; from the next open\n")
    Z = [x for x in X if x["kind"] == "RANDOM"]; print(f"RANDOM 9:35-10:00\n   first 3m {f(Y(Z, 1))}\n   last 3m  {f(Y(Z, 2))}")
    for tag in ("9:45", "10:00"):
        for k in ("HL", "HL55", "HLliq", "HL55liq", "HL+VW", "HL55+VW", "HLliq+VW", "HL55liq+VW"):
            Z = [x for x in X if x["kind"] == f"{k} {tag}"]
            if not Z: continue
            print(f"{k} by {tag}   (buy median {np.median([z['above_l1'] for z in Z]):+.2f}% above the V low, {np.median([z['above_l2'] for z in Z]):+.2f}% above the higher low)\n   first 3m {f(Y(Z, 1))}\n   last 3m  {f(Y(Z, 2))}")
            for e, lab in (("s2t2", "stop under HIGHER low, +2%"), ("s2t3", "stop under HIGHER low, +3%"), ("s1t2", "stop under V low, +2%"), ("s1t3", "stop under V low, +3%")):
                print(f"      {lab:28} after cost: first 3m {g(Y(Z, 1), e)} | last 3m {g(Y(Z, 2), e)}")
    print("P42F_SUMMARY_DONE")
