"""
s1_study.py -- [2026-10-21] DEEP STUDY of situation S1 (window flip), the user's request after HPK 10/20.
2 years, daily top-30 lists with per-minute trade-by-trade buy/sell flow. EVERY minute of every stock (not only bot buys):
  FLIP at minute m: 15-min buy share up >= 20 pts, or 15-bar straightness up >= 0.3, vs 1-2 min before, AND a bar at
        the window edge (m-16..m-14) had >= 3x the window's average volume (the bar that just left / is leaving)
  type: SELL-FLUSH flip = that big bar was DOWN with sellers > buyers (HPK) / BUY-SURGE flip = up bar / other
  HPK-like at the flip: price flat (|5-bar change| < 0.5%), volume NOT increasing (last 5 min <= 1.2x the 5 before),
        near VWAP (within +-1%), under the stock's open
  outcome from the flip close: best / worst in 30 and 60 min, +1% before -1% (60 min), return at +30 / +60 min and at
        the close. Baseline = every minute of the same stocks (10:00-15:00). Also the bot's real S1 buys
        (situations_test.jsonl BASE, tagged S1).
    python3 s1_study.py     -> s1_study.log
"""
import gzip
import json
import sys
from collections import defaultdict
from pathlib import Path

import numpy as np

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

SPLIT = "2025-10-01"


def after(H, L, C, m):
    P = C[m]; out = {}
    for n in (30, 60):
        e = min(m + n, 385)
        out[f"max{n}"] = (H[m + 1:e + 1].max() / P - 1) * 100
        out[f"min{n}"] = (L[m + 1:e + 1].min() / P - 1) * 100
        out[f"ret{n}"] = (C[e] / P - 1) * 100
    first = 0
    for j in range(m + 1, min(m + 61, 386)):
        up, dn = H[j] >= P * 1.01, L[j] <= P * 0.99
        if dn:
            first = -1; break
        if up:
            first = 1; break
    out["first"] = first; out["close"] = (C[385] / P - 1) * 100
    return out


def main():
    rows = []; base = defaultdict(list)
    for p in sorted(OW.glob("bars/bars_20*.json.gz")):
        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
            O, H, L, C, V = a
            fl = json.loads(fp.read_text()); bu, se = np.array(fl["buy"], float), np.array(fl["sell"], float)
            cv = np.cumsum(V); vw = np.cumsum((H + L + C) / 3 * V) / np.where(cv > 0, cv, np.nan)
            bs = np.full(390, np.nan); eff = np.full(390, np.nan)
            for m in range(14, 390):
                b_, s_ = bu[m - 14:m + 1].sum(), se[m - 14:m + 1].sum()
                bs[m] = b_ / (b_ + s_) if b_ + s_ > 0 else np.nan
                path = np.abs(np.diff(C[m - 14:m + 1])).sum()
                eff[m] = (C[m] - C[m - 14]) / path if path > 0 else 0.0
            last_flip = -99
            for m in range(30, 330):
                if m % 7 == 0:
                    base[day < SPLIT].append(after(H, L, C, m))
                dbs = np.nanmax([bs[m] - bs[m - 1], bs[m] - bs[m - 2]])
                deff = max(eff[m] - eff[m - 1], eff[m] - eff[m - 2])
                if not (dbs >= 0.20 or deff >= 0.30) or m - last_flip < 10:
                    continue
                avgv = V[m - 14:m + 1].mean()
                edge = list(range(m - 16, m - 13))
                big = max(edge, key=lambda i: V[i])
                if V[big] < 3 * max(avgv, 1):
                    continue
                last_flip = m
                down = C[big] < O[big] and se[big] > bu[big]
                upb = C[big] > O[big] and bu[big] > se[big]
                ch5 = (C[m] / C[m - 5] - 1) * 100
                v5, v5p = V[m - 4:m + 1].sum(), V[m - 9:m - 4].sum()
                vwd = (C[m] / vw[m] - 1) * 100 if vw[m] == vw[m] else 0
                hpk = abs(ch5) < 0.5 and v5 <= 1.2 * max(v5p, 1) and abs(vwd) <= 1.0 and C[m] < O[0]
                rows.append({"day": day, "sym": s, "m": m, "type": "sell-flush" if down else "buy-surge" if upb else "other",
                             "hpk": bool(hpk and down), "flat": abs(ch5) < 0.5, "vol_up": v5 > 1.2 * max(v5p, 1),
                             "slope_up": ch5 >= 0.3, "vwap": vwd, "dbs": dbs, "deff": deff, **after(H, L, C, m)})
        del d
        print(day, len(rows), flush=True)
    nd = len({r["day"] for r in rows})
    def line(lab, X):
        if not X:
            print(f"  {lab:52} 0"); return
        g = lambda k: np.array([x[k] for x in X])
        y1 = np.array([x["day"] < SPLIT for x in X])
        f = g("first")
        print(f"  {lab:52} {len(X):6} ({len(X) / nd:5.2f}/day) | +1% first {np.mean(f == 1):4.0%} -1% first {np.mean(f == -1):4.0%} | "
              f"30 min: best {np.median(g('max30')):+.2f}% worst {np.median(g('min30')):+.2f}% ret {g('ret30').mean():+.2f}% | "
              f"60 min ret {g('ret60').mean():+.2f}% (y1 {g('ret60')[y1].mean() if y1.any() else 0:+.2f} / y2 {g('ret60')[~y1].mean() if (~y1).any() else 0:+.2f}) | close {g('close').mean():+.2f}%")
    allb = base[True] + base[False]
    print(f"\n{len(rows)} window flips on {nd} days (daily top-30 lists, 10:00-15:00)\n")
    print("  BASELINE: every 7th minute of the same stocks")
    g = lambda X, k: np.array([x[k] for x in X])
    f = g(allb, "first")
    print(f"  {'all minutes':52} {len(allb):6}        | +1% first {np.mean(f == 1):4.0%} -1% first {np.mean(f == -1):4.0%} | "
          f"30 min ret {g(allb, 'ret30').mean():+.2f}% | 60 min ret {g(allb, 'ret60').mean():+.2f}% | close {g(allb, 'close').mean():+.2f}%\n")
    line("ALL window flips", rows)
    for t in ("sell-flush", "buy-surge", "other"):
        line(f"  {t} flips", [r for r in rows if r["type"] == t])
    sf = [r for r in rows if r["type"] == "sell-flush"]
    print("\n  SELL-FLUSH flips (the HPK type) split by how price and volume behaved at the flip:")
    line("  HPK-like: flat, volume NOT up, near VWAP, under open", [r for r in sf if r["hpk"]])
    line("  NOT HPK-like", [r for r in sf if not r["hpk"]])
    line("  price flat (|5-bar| < 0.5%)", [r for r in sf if r["flat"]])
    line("  price sloping up (>= +0.3% over 5 bars)", [r for r in sf if r["slope_up"]])
    line("  volume NOT increasing", [r for r in sf if not r["vol_up"]])
    line("  volume increasing (> 1.2x the 5 before)", [r for r in sf if r["vol_up"]])
    line("  slope up AND volume increasing (the R1 rule passes)", [r for r in sf if r["slope_up"] and r["vol_up"]])
    line("  under VWAP", [r for r in sf if r["vwap"] < 0])
    line("  over VWAP", [r for r in sf if r["vwap"] >= 0])
    st = HERE / "situations_test.jsonl"
    if st.exists():
        B = [json.loads(l) for l in open(st)]
        B = [t for t in B if t["run"] == "BASE"]
        idx = {(r["day"], r["sym"], r["m"]): r for r in rows}
        s1 = [t for t in B if "S1" in t["tags"]]
        p = np.array([t["pl_gross"] - 0.2 for t in s1])
        print(f"\n  The bot's real v2 buys tagged S1: {len(s1)} trades, {p.mean():+.3f}%/trade (0.2% cost)")
        match = []
        for t in s1:
            r = next((idx.get((t["day"], t["sym"], t["buy_m"] - k)) for k in (1, 2, 3) if idx.get((t["day"], t["sym"], t["buy_m"] - k))), None)
            if r:
                match.append((t, r))
        for lab, sel in (("on a SELL-FLUSH flip", lambda r: r["type"] == "sell-flush"), ("on an HPK-like flip", lambda r: r["hpk"]),
                         ("on other flips", lambda r: r["type"] != "sell-flush")):
            Z = np.array([t["pl_gross"] - 0.2 for t, r in match if sel(r)])
            print(f"    {lab:32} {len(Z):4} trades {Z.mean() if len(Z) else 0:+.3f}%/trade")
    print("S1_DONE")


if __name__ == "__main__":
    main()
