"""
v_pressure_study.py -- [2026-10-10] The user: at the V, does BUYING PRESSURE at the turn separate the Vs that
REVERSE from the ones that KEEP GOING DOWN? 2 years (Oct 2024 - Oct 2026), the 9:30 top-30 lists that have
trade-by-trade buy/sell flow per minute (reports/open_window/bars + flow, tick rule).
  V          deepv_outcomes.vs_of (local low, 3 bars each side, >= 1% drop from the 30-bar high, 9:45-15:15);
             here only drops >= 2%. Judged at the CONFIRMATION = the close 3 bars after the low (bars up to it).
  pressure   bs3 / bs5   buy share of the last 3 / 5 minutes (buy / (buy + sell))
             bs_pre      buy share of the 15 minutes BEFORE the low;  shift = bs3 - bs_pre
             sell_dry    sell volume low..confirm per minute / sell per minute in the 10 min before the low (< 1 = dried up)
             vol_low     total volume low..confirm per minute / the 10 min before
             net15       (buy - sell) / (buy + sell) over the last 15 minutes
  labels     KEPT DOWN (< 1% bounce from the low, then broke it) vs REVERSED >= 3% (from the low)
  trade      buy at the confirmation close, stop = V low - 0.1%, target 2R, 60 min limit; at the real 0.2% cost
Output: v_pressure_rows.csv.gz + report (AUC per feature per year-half, quintiles, simple filters train/test).
    python3 v_pressure_study.py          python3 v_pressure_study.py --report
"""
import csv
import gzip
import json
import sys
from pathlib import Path

import numpy as np

HERE = Path(__file__).resolve().parent
OW = HERE.parent / "open_window"
sys.path.insert(0, str(HERE)); sys.path.insert(0, str(OW))
from deepv_outcomes import vs_of  # noqa: E402
from up2_styles_build import arrays  # noqa: E402

OUT = HERE / "v_pressure_rows.csv.gz"
COST, SPLIT = 0.2, "2025-10-01"
sh = lambda b, s: b / (b + s) if b + s > 0 else np.nan


def trade(a, k, stop):
    H, L, C, Op = a[1], a[2], a[3], a[0]
    e = C[k]; R = e - stop
    if R <= 0:
        return None
    for j in range(k + 1, min(k + 61, 386)):
        if L[j] <= stop:
            return (min(stop, Op[j]) / e - 1) * 100
        if H[j] >= e + 2 * R:
            return (max(e + 2 * R, Op[j]) / e - 1) * 100
    return (C[min(k + 60, 385)] / e - 1) * 100


def build():
    files = sorted(OW.glob("bars/bars_20*.json.gz"))
    n = 0
    with gzip.open(OUT, "wt", newline="") as fh:
        w = None
        for p in files:
            day = p.name[5:15]
            d = json.load(gzip.open(p, "rt"))
            for s in d["top30_current"]:
                fp = OW / "flow" / day / f"{s}.json"
                if s not in d["symbols"] or not fp.exists():
                    continue
                a = arrays(d["symbols"][s]["bars"])
                if a is None:
                    continue
                fl = json.loads(fp.read_text()); B, S = np.array(fl["buy"], float), np.array(fl["sell"], float)
                V = a[4]
                cv = np.cumsum(V); vw = np.cumsum((a[1] + a[2] + a[3]) / 3 * V) / np.where(cv > 0, cv, np.nan)
                for v in vs_of(a):
                    if v["drop"] < 2:
                        continue
                    j = v["m"]; k = j + 3
                    pre = slice(j - 10, j)
                    row = {"day": day, "sym": s, "m": j, "n": v["n"], "drop": round(v["drop"], 2), "vs_open": round(v["vs_open"], 2),
                           "vwap": round((a[3, k] / vw[k] - 1) * 100, 2) if vw[k] == vw[k] else "",
                           "bs3": sh(B[k - 2:k + 1].sum(), S[k - 2:k + 1].sum()), "bs5": sh(B[k - 4:k + 1].sum(), S[k - 4:k + 1].sum()),
                           "bs_pre": sh(B[j - 14:j + 1].sum(), S[j - 14:j + 1].sum()),
                           "sell_dry": (S[j:k + 1].mean() / S[pre].mean()) if S[pre].mean() > 0 else np.nan,
                           "vol_low": (V[j:k + 1].mean() / V[pre].mean()) if V[pre].mean() > 0 else np.nan,
                           "net15": (B[k - 14:k + 1].sum() - S[k - 14:k + 1].sum()) / max(B[k - 14:k + 1].sum() + S[k - 14:k + 1].sum(), 1),
                           "bounce": round(v["bounce"], 3), "cls": v["cls"], "up_conf": round(v["up_conf"], 3), "held": v["held"]}
                    row["shift"] = row["bs3"] - row["bs_pre"] if row["bs3"] == row["bs3"] and row["bs_pre"] == row["bs_pre"] else np.nan
                    t = trade(a, k, a[2, j] * 0.999)
                    row["trade"] = "" if t is None else round(t, 3)
                    row = {x: (round(y, 4) if isinstance(y, (float, np.floating)) else y) for x, y in row.items()}
                    if w is None:
                        w = csv.DictWriter(fh, fieldnames=list(row)); w.writeheader()
                    w.writerow(row); n += 1
            del d
            print(day, n, flush=True)
    report()


def auc(x, y):
    k = ~np.isnan(x); x, y = x[k], y[k]
    if y.sum() == 0 or (~y).sum() == 0:
        return np.nan
    r = np.argsort(np.argsort(x)) + 1.0
    return (r[y].sum() - y.sum() * (y.sum() + 1) / 2) / (y.sum() * (~y).sum())


def report():
    cols = {}
    with gzip.open(OUT, "rt") as fh:
        rd = csv.DictReader(fh)
        for r in rd:
            for k, v in r.items():
                cols.setdefault(k, []).append(v)
    D = {k: np.array(v) for k, v in cols.items()}
    F = ["bs3", "bs5", "bs_pre", "shift", "sell_dry", "vol_low", "net15", "drop", "vs_open", "vwap", "m", "n"]
    for k in F + ["bounce", "up_conf", "trade"]:
        D[k] = np.array([float(x) if x not in ("", "nan") else np.nan for x in D[k]])
    tr = D["day"] < SPLIT
    rev, kept = D["bounce"] >= 3, D["cls"] == "kept down"
    pl = D["trade"] - COST
    print(f"{len(D['day'])} Vs with a drop >= 2% ({len(set(D['day']))} days); year 1 < {SPLIT} <= year 2")
    print(f"  REVERSED >= 3%: {rev.mean():.0%}   KEPT DOWN: {kept.mean():.0%}   trade (buy at confirm, stop under the low, 2R, 60 min): "
          f"{np.nanmean(pl):+.3f}%/trade at {COST}% cost, year 1 {np.nanmean(pl[tr]):+.3f}% / year 2 {np.nanmean(pl[~tr]):+.3f}%")
    print("\nAUC: reversed >= 3% vs kept down (0.5 = no info; > 0.5 = higher value -> more reversals)   | AUC for a winning trade")
    k2 = rev | kept
    for f in F:
        a1 = auc(D[f][k2 & tr], rev[k2 & tr]); a2 = auc(D[f][k2 & ~tr], rev[k2 & ~tr])
        t1 = auc(D[f][tr], pl[tr] > 0); t2 = auc(D[f][~tr], pl[~tr] > 0)
        print(f"  {f:9} year 1 {a1:.2f}  year 2 {a2:.2f}   |  trade win: year 1 {t1:.2f}  year 2 {t2:.2f}")
    for f in ("bs3", "shift", "sell_dry", "net15"):
        x = D[f]; ok = ~np.isnan(x)
        qs = np.nanquantile(x[tr], [0.2, 0.4, 0.6, 0.8])           # cut points from YEAR 1 only
        b = np.digitize(x, qs)
        print(f"\n  quintiles of {f} (cuts from year 1: {', '.join(f'{q:.2f}' for q in qs)})")
        for q in range(5):
            for lab, sel in (("y1", tr), ("y2", ~tr)):
                k = ok & (b == q) & sel
                print(f"    Q{q + 1} {lab}: {k.sum():6} Vs | reversed>=3% {rev[k].mean():4.0%} kept down {kept[k].mean():4.0%} | "
                      f"trade {np.nanmean(pl[k]):+.3f}% win {np.nanmean(pl[k] > 0):.0%}")
    print("\nSIMPLE FILTERS (trade at the real cost; year 1 / year 2)")
    filt = {
        "drop >= 3%": D["drop"] >= 3,
        "drop >= 3% + buyers took over (bs3 >= 0.6)": (D["drop"] >= 3) & (D["bs3"] >= 0.6),
        "drop >= 3% + selling dried up (sell_dry < 0.7)": (D["drop"] >= 3) & (D["sell_dry"] < 0.7),
        "drop >= 3% + pressure shift >= +0.15": (D["drop"] >= 3) & (D["shift"] >= 0.15),
        "drop >= 3% + bs3 >= 0.6 + sell_dry < 0.7": (D["drop"] >= 3) & (D["bs3"] >= 0.6) & (D["sell_dry"] < 0.7),
        "1st/2nd V + drop >= 3% + bs3 >= 0.6": (D["n"] <= 2) & (D["drop"] >= 3) & (D["bs3"] >= 0.6),
        "drop >= 5% + bs3 >= 0.6": (D["drop"] >= 5) & (D["bs3"] >= 0.6),
    }
    for lab, k in filt.items():
        print(f"  {lab:48} " + " | ".join(f"{nm}: {np.sum(k & s):5} Vs, rev {rev[k & s].mean():.0%}, kept {kept[k & s].mean():.0%}, "
                                         f"trade {np.nanmean(pl[k & s]):+.3f}%" for nm, s in (("y1", tr), ("y2", ~tr))))


if __name__ == "__main__":
    report() if "--report" in sys.argv else build()
