"""
flush_winners_study.py -- [2026-10-21] the user: "why didn't the bot buy the big winners after the flush situation?"
2 years, daily top-30 lists. A FLUSH = a down bar with sellers > buyers and >= 3x the average volume of the 15 bars
after it; it LEAVES the 15-min window 15 minutes later (minute L = the S1 moment). Outcome from the open after L:
  BIG WINNER = the high reaches >= +3% within 60 min (peak = the highest high in those 60 min)
For every big winner the stock-day is replayed with the REAL entry_v2 (v2_bar_replay.run_stock, every signal) and
every v2 decision from L to the peak is recorded: did the bot buy? if not, which v2 checks said no, minute by minute.
    nice -n 19 python3 flush_winners_study.py   -> flush_winners_study.log
"""
import gzip
import json
import sys
from collections import Counter, defaultdict
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"); sys.path.insert(0, str(HERE.parent / "open_window"))
import v2_bar_replay as R  # noqa: E402
from up2_styles_build import arrays  # noqa: E402

SPLIT = "2025-10-01"
CTX = {"log": None}
_orig = R.entry_v2.evaluate


def cat(r):
    r = r.lower()
    for k, lab in (("rvol", "RVOL too low"), ("last-15-min spike", "volume spike > 5x"), ("today's high", "too close to day high"),
                   ("resistance", "too close to resistance"), ("range >", "high in the day's range"), ("buy share", "buy share < 55%"),
                   ("net selling", "falling on net selling"), ("smoothness", "smoothness < 0.7"), ("straightness", "straightness < 0.5"),
                   ("over the open", "too far over the open"), ("atrs over yesterday", "too far over prev close"),
                   ("normal move", "move too big"), ("b1", "big bar at the high"), ("spread", "spread too wide"),
                   ("vwap", "VWAP not 0..+2%"), ("acceleration", "acceleration spike"), ("liquidity", "too thin"),
                   ("bars", "too early (min bars)"), ("confirm", "confirming")):
        if k in r:
            return lab
    return r[:40]


def wrapped(sym, view, ref, f15, pers, as_of=None, cfg=None, quote=None):
    d = _orig(sym, view, ref, f15, pers, as_of=as_of, cfg=cfg, quote=quote)
    if CTX["log"] is not None:
        CTX["log"][R.minute(as_of) - 1] = (bool(d.should_enter), [cat(x) for x in (d.reasons_against or [])
                                                                     if not x.startswith("warning")])
    return d


R.entry_v2.evaluate = wrapped


def events(O, H, L, C, V, bu, se):
    out = []
    last = -99
    for f in range(15, 300):
        Lm = f + 15
        if Lm + 2 >= 389 or f - last < 10:
            continue
        avg = V[f + 1:f + 16].mean()
        if not (C[f] < O[f] and se[f] > bu[f] and V[f] >= 3 * max(avg, 1)):
            continue
        last = f
        P0 = O[Lm + 1]
        e = min(Lm + 61, 389)
        hi = H[Lm + 1:e + 1]
        pk = Lm + 1 + int(np.argmax(hi))
        out.append({"f": f, "L": Lm, "max": (hi.max() / P0 - 1) * 100, "peak": pk,
                    "min_before_peak": (L[Lm + 1:pk + 1].min() / P0 - 1) * 100})
    return out


def main():
    cfg = R.get_config(); ecfg = {**(cfg.get("entry_v2") or {}), "confirm_checks": 1}; xcfg = {**(cfg.get("exit_v2") or {}), "enabled": True}
    allev, big = [], []
    for p in sorted(R.OW.glob("bars/bars_20*.json.gz")):
        day = p.name[5:15]
        if day > "2026-10-02":
            continue
        d = json.load(gzip.open(p, "rt")); bench = R.bench_pct(day)
        for s in d["top30_current"]:
            fp = R.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())
            ev = events(O, H, L, C, V, np.array(fl["buy"], float), np.array(fl["sell"], float))
            for e in ev:
                e.update(day=day, sym=s)
            allev += ev
            bw = [e for e in ev if e["max"] >= 3]
            if not bw:
                continue
            ref = R.REF.load(day, s)
            if not ref:
                continue
            CTX["log"] = {}
            trades = R.run_stock(day, s, d["symbols"][s]["bars"], fl, ref, bench, ecfg, xcfg)
            lg = CTX["log"]; CTX["log"] = None
            for e in bw:
                mins = [lg.get(m) for m in range(e["L"], e["peak"])]
                bought = [t for t in trades if e["L"] < t["buy_m"] <= e["peak"]]
                held = [t for t in trades if t["buy_m"] <= e["L"] < t["sell_m"]]
                e.update(bought=bool(bought), pl=(bought[0]["pl"] if bought else None), in_pos=bool(held),
                         n_eval=sum(1 for x in mins if x), passed=sum(1 for x in mins if x and x[0]),
                         reasons=Counter(r for x in mins if x and not x[0] for r in x[1]),
                         only=Counter(x[1][0] for x in mins if x and not x[0] and len(x[1]) == 1),
                         first=(next((x[1] for x in mins if x and not x[0]), None)))
                big.append(e)
        R.REF._CACHE.clear(); del d
        print(day, len(allev), "flushes,", len(big), "big winners", flush=True)

    n = len(allev); nd = len({e["day"] for e in allev})
    mx = np.array([e["max"] for e in allev])
    print(f"\n{n} sell flushes leaving the window on {nd} days ({n / nd:.1f}/day), 9:45-15:00, daily top-30 lists")
    print(f"   after the flush left: reached +1% in 60 min {np.mean(mx >= 1):.0%}, +2% {np.mean(mx >= 2):.0%}, +3% (BIG WINNER) {np.mean(mx >= 3):.0%}")
    B = big
    print(f"\nBIG WINNERS (+3% within 60 min): {len(B)}  (median best {np.median([e['max'] for e in B]):+.1f}%, "
          f"median dip before the peak {np.median([e['min_before_peak'] for e in B]):+.1f}%, median {np.median([e['peak'] - e['L'] for e in B]):.0f} min to the peak)")
    yb = [e for e in B if e["bought"]]
    print(f"   the bot BOUGHT between the flush leaving and the peak: {len(yb)} ({len(yb) / len(B):.0%})"
          + (f", those trades {np.mean([e['pl'] for e in yb]):+.2f}%/trade after cost, {np.mean([e['pl'] > 0 for e in yb]):.0%} wins" if yb else ""))
    ip = [e for e in B if e["in_pos"] and not e["bought"]]
    print(f"   already holding the stock (bought before the flush left): {len(ip)}")
    nb = [e for e in B if not e["bought"] and not e["in_pos"]]
    print(f"   NOT bought: {len(nb)} ({len(nb) / len(B):.0%}); v2 said BUY at some minute but no trade (re-entry rule / after 15:15 / stop >= price): "
          f"{sum(1 for e in nb if e['passed'])}")
    tot = Counter(); only = Counter(); first = Counter(); per_ev = Counter()
    for e in nb:
        tot.update(e["reasons"]); only.update(e["only"])
        for r in set(e["reasons"]):
            per_ev[r] += 1
        if e["first"]:
            first.update(e["first"])
    print("\n   WHY v2 SAID NO (the not-bought big winners, every minute from the flush leaving to the peak)")
    print(f"   {'check':28} {'winners it blocked at least once':>32} {'minutes it said no':>19} {'minutes it was the ONLY no':>27}")
    nm = sum(tot.values())
    for r, k in per_ev.most_common():
        print(f"   {r:28} {k:24} ({k / len(nb):4.0%}) {tot[r]:12} {only[r]:20}")
    print(f"\n   minutes where only ONE check was missing (one rule away from a buy): {sum(only.values())} of "
          f"{sum(e['n_eval'] - e['passed'] for e in nb)} 'no' minutes")
    print("\n   by year:")
    for y, lab in ((True, "year 1"), (False, "year 2")):
        X = [e for e in B if (e["day"] < SPLIT) == y]
        if X:
            print(f"      {lab}: {len(X)} big winners, bought {np.mean([e['bought'] for e in X]):.0%}")
    print("\n   10 biggest not-bought winners:")
    for e in sorted(nb, key=lambda e: -e["max"])[:10]:
        L_ = e["L"]; top = ", ".join(f"{r} {k}" for r, k in e["reasons"].most_common(3))
        print(f"      {e['day']} {e['sym']:6} flush {9 + (30 + e['f']) // 60}:{(30 + e['f']) % 60:02d} left {9 + (30 + L_) // 60}:{(30 + L_) % 60:02d} "
              f"-> +{e['max']:.1f}% in {e['peak'] - L_} min | no-minutes: {top}")
    json.dump([{k: (dict(v) if isinstance(v, Counter) else v) for k, v in e.items()} for e in B], open(HERE / "flush_winners.json", "w"), default=str)
    print("FLUSHWIN_DONE")


if __name__ == "__main__":
    main()
