"""
bs_jump_study.py -- [2026-10-21] the user: "check that all the stocks, when they get an unusual buy share, rise >= 1%".
2 years, daily top-30 lists, per-minute trade-by-trade buy/sell flow. EVERY minute 9:45-15:00 of every stock:
  bs3 = buy share of the last 3 full bars, bs3p = the 3 bars before; vol3x = last-3 volume vs the stock's normal for
  those minutes (reference vol_per_min)
  EVENTS (10-min cooldown per stock and definition):
    JUMPxx   bs3 - bs3p >= 0.20 / 0.30 / 0.40 (a sudden swing to buyers)
    HIGHxx   bs3 >= 0.75 / 0.85 (a very one-sided 3 minutes)
    UNUSUAL  bs3 >= the stock's mean bs3 so far today + 2 standard deviations (unusual for THIS stock today)
  each split by volume (vol3x < 1 / 1-2 / >= 2) and by price over the 3 bars (down / up)
  OUTCOME from the NEXT bar's open (where a bot could buy): touches +1% within 30 / 60 min, +1% before -1% (60 min),
  best / worst in 30 min, return at +30 min. BASELINE = every 5th minute of the same stocks.
    nice -n 19 python3 bs_jump_study.py   -> bs_jump_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"
REFD = Path("/var/www/screener/trade/data/reference")
sys.path.insert(0, str(OW))
from up2_styles_build import arrays  # noqa: E402

SPLIT = "2025-10-01"


def outcome(O, H, L, C, m):
    P = O[m + 1]
    if P <= 0:
        return None
    e30, e60 = min(m + 31, 389), min(m + 61, 389)
    hi30 = H[m + 1:e30 + 1].max() / P - 1; lo30 = L[m + 1:e30 + 1].min() / P - 1
    hi60 = H[m + 1:e60 + 1].max() / P - 1
    first = 0
    for j in range(m + 1, e60 + 1):
        if L[j] <= P * 0.99:
            first = -1; break
        if H[j] >= P * 1.01:
            first = 1; break
    return (hi30 * 100, lo30 * 100, hi60 * 100, first, (C[e30] / P - 1) * 100)


def main():
    ev = defaultdict(list); base = []
    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)
            rp = REFD / day / f"{s}.json"
            vpm = np.array(json.loads(rp.read_text())["vol_per_min"], float) if rp.exists() else None
            b3 = np.convolve(bu, np.ones(3))[:390]; s3 = np.convolve(se, np.ones(3))[:390]   # sum of bars m-2..m
            tot = b3 + s3
            bs3 = np.where(tot > 0, b3 / np.maximum(tot, 1e-9), np.nan)
            v3 = np.convolve(V, np.ones(3))[:390]
            n3 = np.convolve(vpm, np.ones(3))[:390] if vpm is not None and len(vpm) >= 390 else None
            last = {}
            hist = []
            y1 = day < SPLIT
            for m in range(5, 331):
                if bs3[m] == bs3[m]:
                    hist.append(bs3[m])
                if m < 15:
                    continue
                if m % 5 == 0:
                    o = outcome(O, H, L, C, m)
                    if o:
                        base.append((y1,) + o)
                if bs3[m] != bs3[m] or bs3[m - 3] != bs3[m - 3] or tot[m] <= 0:
                    continue
                jump = bs3[m] - bs3[m - 3]
                vx = v3[m] / n3[m] if n3 is not None and n3[m] > 0 else np.nan
                up = C[m] > O[m - 2]
                mu, sd = np.mean(hist[:-1]), np.std(hist[:-1])
                hits = []
                for k, t in (("JUMP20", 0.20), ("JUMP30", 0.30), ("JUMP40", 0.40)):
                    if jump >= t:
                        hits.append(k)
                for k, t in (("HIGH75", 0.75), ("HIGH85", 0.85)):
                    if bs3[m] >= t:
                        hits.append(k)
                if len(hist) >= 20 and sd > 0 and bs3[m] >= mu + 2 * sd:
                    hits.append("UNUSUAL")
                if not hits:
                    continue
                o = None
                for k in hits:
                    if m - last.get(k, -99) < 10:
                        continue
                    last[k] = m
                    o = o or outcome(O, H, L, C, m)
                    if o:
                        ev[k].append((y1,) + o + (vx, up, m))
        del d
        print(day, {k: len(v) for k, v in ev.items()}, flush=True)

    def line(lab, X):
        if not X:
            print(f"   {lab:44} 0"); return
        A = np.array([x[:6] for x in X], float)
        y1 = A[:, 0] == 1
        f = A[:, 4]
        r = lambda sel: f"{np.mean(A[sel, 3] >= 1):4.0%}" if sel.any() else "  - "
        print(f"   {lab:44} {len(A):6} | +1% within 30m {np.mean(A[:, 1] >= 1):4.0%}  60m {r(np.ones(len(A), bool))} "
              f"(y1 {r(y1)} y2 {r(~y1)}) | +1% before -1% {np.mean(f == 1):4.0%} / -1% first {np.mean(f == -1):4.0%} | "
              f"30m best {np.median(A[:, 1]):+.2f}% worst {np.median(A[:, 2]):+.2f}% ret {A[:, 5].mean():+.3f}%")
    print(f"\n2 years, daily top-30 lists, 9:45-15:00; outcomes from the NEXT bar's open\n")
    line("BASELINE: every 5th minute", base)
    names = {"JUMP20": "buy share jumps >= 20 pts (3 bars vs 3 before)", "JUMP30": "buy share jumps >= 30 pts",
             "JUMP40": "buy share jumps >= 40 pts", "HIGH75": "buy share last 3 bars >= 75%", "HIGH85": "buy share last 3 bars >= 85%",
             "UNUSUAL": "unusual for the stock today (mean + 2 SD)"}
    for k in names:
        X = ev[k]
        print()
        line(names[k], X)
        line("   volume < 1x normal", [x for x in X if x[6] == x[6] and x[6] < 1])
        line("   volume 1-2x normal", [x for x in X if x[6] == x[6] and 1 <= x[6] < 2])
        line("   volume >= 2x normal", [x for x in X if x[6] == x[6] and x[6] >= 2])
        line("   price down over the 3 bars", [x for x in X if not x[7]])
        line("   price up over the 3 bars", [x for x in X if x[7]])
        line("   9:45-10:30", [x for x in X if x[8] < 60])
        line("   10:30-15:00", [x for x in X if x[8] >= 60])
    print("BSJUMP_DONE")


if __name__ == "__main__":
    main()
