"""up3g_2y.py -- [2026-10-27] the user's green-bar rule on the 2-YEAR library (2024-10..2026-10, old-score top 30, quote flow; no queue
history): dip >= 0.5%, then 3 GREEN 1-min bars each closing higher; checks >= yesterday's close, volume, buy share 3 min >= 55%.
Variants: no new buys after 13:00; TREND DAY = list breadth (mean move from the open of the other listed stocks at the signal minute) >= +0.5%.
Buy next open, stop low of dip bar + 3 bars x0.998 clamped 0.5-4%, C3 exit, re-entry after exit. Year 1 = 2024-10..2025-09, year 2 after.
Per day processed and dropped (1 GB server). -> up3g_2y.json (trades of every variant) + printed table"""
import gzip, json, sys
from pathlib import Path
import numpy as np
HERE = Path(__file__).resolve().parent
sys.path.insert(0, str(HERE)); sys.path.insert(0, "/var/www/screener/trade/reports/playbook"); sys.path.insert(0, "/var/www/screener/trade")
import v2_bar_replay as R
from queue_test import Day
from sim_2y import sim
from up3_test import path
V = {"signal (dip + 3 green + >= prev close)": dict(need=["PC"]), "+ volume + buyers (rule, no queue)": dict(need=["PC", "VOL", "BUY"]),
     "rule, no buys after 13:00": dict(need=["PC", "VOL", "BUY"], tmax=210), "rule + trend day (breadth >= +0.5%)": dict(need=["PC", "VOL", "BUY", "TREND"]),
     "rule + trend day + no buys after 13:00": dict(need=["PC", "VOL", "BUY", "TREND"], tmax=210),
     "signal + trend day + before 13:00 (no vol/buyers)": dict(need=["PC", "TREND"], tmax=210)}
def scan(D, br, need, tmax=375):
    b, M, pc = D.b, D.m, D.ref["prev_close"]; fb, fs = D.fl["buy"], D.fl["sell"]; out = []; free = 0
    for i in range(9, len(b) - 1):
        t = M[i]
        if t > tmax or i < free or M[i] - M[i - 4] != 4: continue
        if not (b[i - 3]["c"] < b[i - 4]["c"] and all(b[k]["c"] > b[k - 1]["c"] and b[k]["c"] > b[k]["o"] for k in (i - 2, i - 1, i))): continue
        hi = max(x["c"] for x in b[max(0, i - 18):i - 2])
        if (1 - b[i - 3]["c"] / hi) * 100 < 0.5: continue
        bb = sum(fb[t - 2:t + 1]); ss = sum(fs[t - 2:t + 1])
        chk = {"PC": b[i]["c"] >= pc, "VOL": np.mean([x["v"] for x in b[i - 2:i + 1]]) >= 0.9 * np.mean([x["v"] for x in b[i - 7:i - 2]]),
               "BUY": bb + ss > 0 and bb / (bb + ss) >= 0.55, "TREND": br[t] is not None and br[t] >= 0.5}
        if not all(chk[k] for k in need): continue
        bi = i + 1; P = b[bi]["o"]; stop = min(max(min(x["l"] for x in b[i - 3:i + 1]) * 0.998, P * 0.96), P * 0.995)
        pl, j, why = sim(D, bi, P, stop, 2.0, "C3")
        out.append(dict(t=t, pl=round(pl, 3), why=why, **{k: round(v, 3) for k, v in path(b, bi, P).items()})); free = j + 1
    return out
OUT = {k: [] for k in V}; n_days = 0
for p in sorted(R.OW.glob("bars/bars_20*.json.gz")):
    day = p.name[5:15]
    if not ("2024-10-01" <= day <= "2026-10-02"): continue
    d = json.load(gzip.open(p, "rt")); Ds = {}
    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("atr14") or not ref.get("prev_close"): continue
        b = [x for x in R.bars_of(d["symbols"][s]["bars"]) if 0 <= R.minute(x["t"]) < 390]
        if len(b) < 200: continue
        Ds[s] = Day(b, [R.minute(x["t"]) for x in b], json.loads(fp.read_text()), ref)
    del d
    cl = {}
    for s, D in Ds.items():          # close per minute (carried forward) for breadth
        a = np.full(390, np.nan); O = D.b[0]["o"]
        for x, m in zip(D.b, D.m): a[m] = (x["c"] / O - 1) * 100
        for m in range(1, 390):
            if np.isnan(a[m]): a[m] = a[m - 1]
        cl[s] = a
    for s, D in Ds.items():
        others = np.array([cl[z] for z in cl if z != s])
        br = [None if not len(others) else float(np.nanmean(others[:, m])) for m in range(390)]
        for k, v in V.items():
            for x in scan(D, br, **v): OUT[k].append(dict(day=day, sym=s, **x))
    n_days += 1
    if n_days % 25 == 0: print(day, n_days, flush=True)
json.dump(OUT, open(HERE / "up3g_2y.json", "w"))
Y1 = lambda T: [x for x in T if x["day"] < "2025-10-01"]; Y2 = lambda T: [x for x in T if x["day"] >= "2025-10-01"]
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} | +1% first {np.mean([z['first'] == 1 for z in Z]):3.0%} -1% first {np.mean([z['first'] == -1 for z in Z]):3.0%} | 30m {a('r30'):+.2f}% "
            f"| C3 {a('pl'):+.3f}%/trade win {np.mean([z['pl'] > 0 for z in Z]):3.0%} sum {sum(z['pl'] for z in Z):+7.1f}%")
print(f"\n{n_days} days; C3 per trade after 0.2% cost; year 1 = 2024-10..2025-09, year 2 = 2025-10..2026-10")
for k, T in OUT.items():
    print(f"{k}\n   year 1 {f(Y1(T))}\n   year 2 {f(Y2(T))}")
T = OUT["+ volume + buyers (rule, no queue)"]
print("\nrule by signal time:")
for a_, b_, lab in ((0, 30, "9:30-10:00"), (30, 90, "10:00-11:00"), (90, 210, "11:00-13:00"), (210, 376, "13:00-15:45")):
    Z = [x for x in T if a_ <= x["t"] < b_]; print(f"  {lab:12} year 1 {f(Y1(Z))}\n  {'':12} year 2 {f(Y2(Z))}")
print("UP3G2Y_DONE")
