"""dip2.py collect A..B OUT | analyze OUT.. -- [2026-10-28] the user: v2 buys that fall to -2% first -- what does the board show AT THAT MOMENT
(the first bar whose low touches -2%), and does it separate the ones that later come back to +2% (before 15:55) from the ones that don't?
The board keeps running after the buy (finished bars only)."""
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))
if sys.argv[1] == "collect":
    import v2_bar_replay as R
    import stock_board as SB
    from opening_patterns import BIAS_UP, BIAS_DOWN
    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")); pp = R.OW / f"pm_history/{day}.json.gz"
        pmh = {r["s"]: r.get("pm_high") for r in json.load(gzip.open(pp, "rt"))["rows"]} if pp.exists() else {}
        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: continue
            fl = json.loads(fp.read_text()); M = [R.minute(x["t"]) for x in bars]
            B = SB.Board(ref["prev_close"], ref["atr14"], bars[0]["o"], pm_high=pmh.get(s), vpm=ref.get("vol_per_min"), rule="v2", vwap2="rising")
            watch = []; recent = []
            for i, x in enumerate(bars[:-1]):
                t = M[i]
                if t > 385: break
                r = B.update(x, t, fl["buy"][t], fl["sell"][t])
                recent = [(tt, p_) for tt, p_ in recent if t - tt < 5] + [(t, p_) for p_ in r["new"]]
                for w in watch:
                    if w["done"]: continue
                    if not w["hit_dn"]:
                        if x["h"] >= w["P"] * 1.02: w["done"] = True; continue          # +2% came first -> not a loser
                        if x["l"] <= w["P"] * 0.98:
                            w["hit_dn"] = True; vw = B.vw_hist
                            lowday = min(B.R.L)
                            w["snap"] = dict(min_to_dn=t - w["t"], t_dn=t, vwap_line_up=len(vw) >= 4 and vw[-1] >= vw[-4] * 1.0002 and vw[-1] >= vw[-2],
                                vwap_3m=round((vw[-1] / vw[-4] - 1) * 100, 3) if len(vw) >= 4 else 0, vs_vwap=r["vs_vwap"], s3=r["s3"], s15=r["s15"],
                                s3_gt_15=r["s3"] > r["s15"], s3_up=r["s3_up"], s15_up=r["s15_up"], pos=r["pos"], below_sup=round((x["c"] / r["sup"] - 1) * 100, 3),
                                vs_daylow=round((x["c"] / lowday - 1) * 100, 3), vacc_tod=r["vacc_tod"],
                                bull5=any(p_ in BIAS_UP for _, p_ in recent), bear5=any(p_ in BIAS_DOWN for _, p_ in recent), close_vs_P=round((x["c"] / w["P"] - 1) * 100, 3))
                            w["low"] = x["l"]
                    else:
                        w["low"] = min(w["low"], x["l"])
                        if x["h"] >= w["P"] * 1.02 and t <= 385:
                            w["back"] = True; w["back_min"] = t - w["snap"]["t_dn"]; w["done"] = True
                if r["buy"] and t <= 135:
                    watch.append(dict(t=t, P=bars[i + 1]["o"], hit_dn=False, done=False, back=False, back_min=None, low=None))
            for w in watch:
                if w["hit_dn"]:
                    out.write(json.dumps(dict(day=day, sym=s, t=w["t"], back=w["back"], back_min=w["back_min"], low_after=round((w["low"] / w["P"] - 1) * 100, 3),
                                              close=round((bars[-1]["c"] / w["P"] - 1) * 100, 3), **w["snap"])) + "\n")
        del d; R.REF._CACHE.clear()
    out.close(); print("DIP2_DONE")
else:
    X = [json.loads(l) for f in sys.argv[2:] for l in open(f)]
    H1 = lambda Z: [z for z in Z if z["day"] < "2026-07-28"]; H2 = lambda Z: [z for z in Z if z["day"] >= "2026-07-28"]
    rate = lambda Z: f"{np.mean([z['back'] for z in Z]):4.0%} of {len(Z):4}" if Z else "  -        "
    print(f"{len(X)} v2 buys that fell to -2% FIRST (6 months). Share that later CAME BACK to +2% before 15:55, at the moment of -2%:")
    print(f"   ALL                              first 3m {rate(H1(X))} | last 3m {rate(H2(X))}\n")
    tests = [("VWAP line still rising", lambda z: z["vwap_line_up"]), ("VWAP line NOT rising", lambda z: not z["vwap_line_up"]),
             ("price still above VWAP", lambda z: z["vs_vwap"] > 0), ("price below VWAP", lambda z: z["vs_vwap"] <= 0),
             ("3m buy share > 15m", lambda z: z["s3_gt_15"]), ("3m buy share <= 15m", lambda z: not z["s3_gt_15"]),
             ("15m buy share >= 55%", lambda z: z["s15"] >= 0.55), ("15m buy share < 45%", lambda z: z["s15"] < 0.45),
             ("3m buy share >= 60% (buyers at the dip)", lambda z: z["s3"] >= 0.6), ("3m buy share < 40% (sellers at the dip)", lambda z: z["s3"] < 0.4),
             ("still above board support", lambda z: z["below_sup"] > 0), ("below board support", lambda z: z["below_sup"] <= 0),
             ("near the day's low (< 1% above)", lambda z: z["vs_daylow"] < 1), ("well above the day's low (>= 3%)", lambda z: z["vs_daylow"] >= 3),
             ("time-of-day volume >= 1.5 (heavy dip)", lambda z: (z["vacc_tod"] or 0) >= 1.5), ("time-of-day volume < 0.8 (quiet dip)", lambda z: (z["vacc_tod"] or 0) < 0.8),
             ("bullish pattern in the last 5 min", lambda z: z["bull5"]), ("bearish pattern in the last 5 min", lambda z: z["bear5"]),
             ("fast drop (-2% within 5 min of the buy)", lambda z: z["min_to_dn"] <= 5), ("slow drop (-2% after 30+ min)", lambda z: z["min_to_dn"] >= 30),
             ("the -2% comes before 10:30", lambda z: z["t_dn"] < 60), ("the -2% comes after 12:00", lambda z: z["t_dn"] >= 150),
             ("bar closed back above -1.5% (wick only)", lambda z: z["close_vs_P"] > -1.5), ("bar closed below -2.5% (strong down bar)", lambda z: z["close_vs_P"] < -2.5)]
    for lab, f in tests:
        print(f"   {lab:42} first 3m {rate([z for z in H1(X) if f(z)])} | last 3m {rate([z for z in H2(X) if f(z)])}")
    print("\ncombinations:")
    combos = [("VWAP rising + price above VWAP + 3m>15m", lambda z: z["vwap_line_up"] and z["vs_vwap"] > 0 and z["s3_gt_15"]),
              ("price above VWAP + above support", lambda z: z["vs_vwap"] > 0 and z["below_sup"] > 0),
              ("below VWAP + below support + bearish pattern", lambda z: z["vs_vwap"] <= 0 and z["below_sup"] <= 0 and z["bear5"]),
              ("wick only + buyers >= 60%", lambda z: z["close_vs_P"] > -1.5 and z["s3"] >= 0.6)]
    for lab, f in combos:
        print(f"   {lab:42} first 3m {rate([z for z in H1(X) if f(z)])} | last 3m {rate([z for z in H2(X) if f(z)])}")
    print("DIP2_ANALYSIS_DONE")
