"""
dnt_test.py -- [2026-10-24] the user: "this stock (AKS 4/12/2017) should never be traded, how come it wasn't blocked" -> TEST the
DO-NOT-TRADE list (reports/knowledge/rules_book: A1..A20) as EV3 gates, one by one and per setup, on both periods (2016-18 out of
sample, 2024-26), every EV3 buy of the two account simulations (tick flow). Read at the trigger bar (finished bars only). TEST ONLY.
  A1  > 2% over VWAP              A2  daily ATR < 8%               A3  RVOL so far < 1.5x (vs the 14-day volume by minute)
  A5  > 3% over the open          A6  down >= 1% in 15 min with 15-min buy share < 50%
  A7  after 14:30                 A8  before 9:45                  A9  last-15-min volume > 3x normal or a >= 10x bar in 30 min
  A10 gap > 10%                   A11 from 10:30: the 10:00-10:30 low broke the 9:30-10:00 low
  A13 > 5% under VWAP             A19 < 5% room to the nearest level above (prev close / prev high / the day's high so far)
  A20 gap-down 0.5-3% under the previous close (the open)
"""
import gzip, json, sys
from collections import defaultdict
from pathlib import Path
import numpy as np
HERE = Path(__file__).resolve().parent
sys.path.insert(0, "/var/www/screener/trade/reports/playbook"); sys.path.insert(0, "/var/www/screener/trade")
import v2_bar_replay as R
OW = Path("/var/www/screener/trade/reports/open_window")


def feats(bars, fl, ref, k):
    mins = [R.minute(b["t"]) for b in bars]; m = mins[k]; b = bars[k]; P = b["c"]; O = bars[0]["o"]
    pv = sum((x["h"] + x["l"] + x["c"]) / 3 * x["v"] for x in bars[:k + 1]); cv = sum(x["v"] for x in bars[:k + 1]); vw = pv / cv
    B = sum(fl["buy"][max(0, m - 14):m + 1]); S = sum(fl["sell"][max(0, m - 14):m + 1]); bs15 = B / (B + S) if B + S else 0.5
    vpm = ref.get("vol_per_min") or []
    norm = sum(vpm[:m + 1]) if len(vpm) >= m + 1 else None
    n15 = sum(vpm[max(0, m - 14):m + 1]) if len(vpm) >= m + 1 else None
    v15 = sum(x["v"] for x in bars[k - 14:k + 1]) if k >= 14 else sum(x["v"] for x in bars[:k + 1])
    climax = any(vpm and len(vpm) > mins[i] and vpm[mins[i]] and bars[i]["v"] >= 10 * vpm[mins[i]] for i in range(max(0, k - 29), k + 1))
    c15 = bars[max(0, k - 15)]["c"]
    pc = ref.get("prev_close") or 0; atrp = ref["atr14"] / pc * 100 if pc else 0
    hi = max(x["h"] for x in bars[:k + 1])
    above = [lv for lv in (pc, ref.get("prev_high"), hi) if lv and lv > P * 1.0005]
    room = (min(above) / P - 1) * 100 if above else 99
    lo1 = min([x["l"] for x, mm in zip(bars, mins) if mm < 30] or [1e9]); lo2 = min([x["l"] for x, mm in zip(bars, mins) if 30 <= mm < 60 and mm <= m] or [1e9])
    gap = (O / pc - 1) * 100 if pc else 0
    return {"A1": (P / vw - 1) * 100 > 2, "A2": atrp < 8, "A3": bool(norm) and cv / norm < 1.5, "A5": (P / O - 1) * 100 > 3,
            "A6": (P / c15 - 1) * 100 <= -1 and bs15 < 0.5, "A7": m >= 300, "A8": m < 15,
            "A9": (bool(n15) and v15 > 3 * n15) or climax, "A10": gap > 10, "A11": m >= 60 and lo2 < lo1,
            "A13": (P / vw - 1) * 100 < -5, "A19": room < 5, "A20": -3 <= gap <= -0.5}


RULES = ["A1", "A2", "A3", "A5", "A6", "A7", "A8", "A9", "A10", "A11", "A13", "A19", "A20"]


def load(period):
    out = []
    src = "sim10y_first2y.json" if period == "2016-18" else "simfull_2y_tick.json"
    by = defaultdict(list)
    for t in json.load(open(HERE / src))["trades"]:
        by[t["day"]].append(t)
    for day, ts in sorted(by.items()):
        if period == "2016-18":
            b = json.load(gzip.open(OW / "bars10y" / day[:4] / f"{day}.json.gz", "rt"))["bars"]; refs = json.loads((HERE / "top30_10y" / f"{day}.json").read_text())["ref"]
            get = lambda s: (b[s], refs[s])
        else:
            d = json.load(gzip.open(OW / "bars" / f"bars_{day}.json.gz", "rt"))
            get = lambda s: (d["symbols"][s]["bars"], R.REF.load(day, s))
        for t in ts:
            raw, ref = get(t["sym"])
            bars = [x for x in R.bars_of(raw) if 0 <= R.minute(x["t"]) < 390]
            fl = json.loads((OW / "flow_tick" / day / f"{t['sym']}.json").read_text())
            k = [R.minute(x["t"]) for x in bars].index(t["buy_m"]) - 1
            out.append({**t, **feats(bars, fl, ref, k)})
        R.REF._CACHE.clear()
    return out


def stat(X):
    if not X:
        return "    -"
    a = np.array([x["pl_pct"] for x in X]); return f"{len(X):3} {a.mean():+.2f}%"


def main():
    P = {p: load(p) for p in ("2016-18", "2024-26")}
    json.dump(P, open(HERE / "dnt_test.json", "w"))
    setups = ["ALL", "R1A", "R1D", "R1E", "R4", "R3"]
    print("BLOCKED buys per rule: n + avg result of the blocked buys (negative = the rule removes losers = GOOD)")
    print("  rule | " + " | ".join(f"{s:^25}" for s in setups))
    print("       | " + " | ".join(f"{'2016-18':>12} {'2024-26':>12}" for s in setups))
    for r in RULES:
        cells = []
        for s in setups:
            c = []
            for p in ("2016-18", "2024-26"):
                X = [x for x in P[p] if x[r] and (s == "ALL" or x["setup"] == s)]
                c.append(f"{stat(X):>12}")
            cells.append(" ".join(c))
        print(f"  {r:4} | " + " | ".join(cells))
    print("\nbaseline per setup (all buys): " + " | ".join(f"{s} {stat([x for x in P['2016-18'] if s == 'ALL' or x['setup'] == s])} / {stat([x for x in P['2024-26'] if s == 'ALL' or x['setup'] == s])}" for s in setups))
    # combinations: rules that removed losers in BOTH periods, per setup
    print("\nCOMBINATIONS (kept buys after blocking): per setup, the rules whose blocked buys were negative in BOTH periods (n >= 5 each)")
    for s in setups[1:]:
        good = []
        for r in RULES:
            a = [x["pl_pct"] for x in P["2016-18"] if x["setup"] == s and x[r]]; b = [x["pl_pct"] for x in P["2024-26"] if x["setup"] == s and x[r]]
            if len(a) >= 5 and len(b) >= 5 and np.mean(a) < 0 and np.mean(b) < 0:
                good.append(r)
        res = []
        for p in ("2016-18", "2024-26"):
            K = [x for x in P[p] if x["setup"] == s and not any(x[r] for r in good)]
            Rm = [x for x in P[p] if x["setup"] == s and any(x[r] for r in good)]
            res.append(f"{p}: kept {stat(K)} (${sum(x['pl_usd'] for x in K):+,.0f}) removed {stat(Rm)}")
        print(f"  {s:4} rules {good or 'none'} -> " + " | ".join(res))
    allr = ["A2", "A8", "A19", "A20"]
    for p in ("2016-18", "2024-26"):
        for s in setups:
            K = [x for x in P[p] if (s == "ALL" or x["setup"] == s) and not any(x[r] for r in allr)]
            Rm = [x for x in P[p] if (s == "ALL" or x["setup"] == s) and any(x[r] for r in allr)]
            print(f"  A2+A8+A19+A20 {p} {s:4}: kept {stat(K)} (${sum(x['pl_usd'] for x in K):+,.0f}) | removed {stat(Rm)} (${sum(x['pl_usd'] for x in Rm):+,.0f})")
    for x in P["2016-18"]:
        if x["sym"] == "AKS" and x["day"] == "2017-04-12":
            print("\nAKS 2017-04-12 hits:", [r for r in RULES if x[r]])
    print("DNT_DONE")


if __name__ == "__main__":
    main()
