"""
deepv_outcomes.py -- [2026-10-10] The user: for the Vs, how many REVERSED and how much they went up (not
necessarily positive at the close), and how many KEPT GOING DOWN. Every V of the day, not only the deepest:
  V        a bar whose low is the lowest of the 3 bars on each side, after a >= 1% drop from the 30-bar high,
           9:45-15:15 (confirmed when the 3 bars after it are closed = the earliest a bot could know it)
  outcome  the highest high after the V until its low breaks (or 15:55):
             bounce from the low      -> KEPT DOWN (< 1% then broke the low) / 1-2% / 2-3% / 3-5% / >= 5%
             gain from the confirmation close (3 bars after the low) -> what a bot buying then could get at best
           held the low to the close or not; the close vs the V low and vs the confirmation price
  split    drop into the V (1-2 / 2-3 / 3-5 / 5+ %), the V's number in the day (1st, 2nd, 3rd+), scanner-like
Universe: all $5-15 stocks >= 500k shares (bars10y) and the v3 top 30. HINDSIGHT outcome counts (no trading rule).
    python3 deepv_outcomes.py [START] [END]     default 2024-10-10 .. 2026-10-09   (-> deepv_outcomes.csv.gz / .log)
"""
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(OW))
from up2_styles_build import v3_top30, arrays  # noqa: E402

OUT = HERE / "deepv_outcomes.csv.gz"
CLASSES = ["kept down", "1-2%", "2-3%", "3-5%", ">=5%"]


def vs_of(a):
    O, H, L, C = a[0], a[1], a[2], a[3]
    out = []
    n_v = 0
    for j in range(15, 346):
        lo = L[j]
        if lo > L[j - 3:j + 4].min() or (H[max(0, j - 30):j].max() / lo - 1) * 100 < 1.0:
            continue
        if j > 0 and L[j - 1] == lo:          # flat bottom: count once
            continue
        n_v += 1
        brk = np.where(L[j + 1:386] < lo)[0]
        end = j + 1 + int(brk[0]) if len(brk) else 386          # first bar that breaks the low (or 15:55)
        seg = H[j + 1:end]
        bounce = (seg.max() / lo - 1) * 100 if len(seg) else 0.0
        cp = C[j + 3]
        seg2 = H[j + 4:end]
        up_conf = (seg2.max() / cp - 1) * 100 if len(seg2) else 0.0
        cls = "kept down" if bounce < 1 else "1-2%" if bounce < 2 else "2-3%" if bounce < 3 else "3-5%" if bounce < 5 else ">=5%"
        out.append({"m": j, "n": n_v, "drop": (H[max(0, j - 30):j].max() / lo - 1) * 100, "vs_open": (lo / O[0] - 1) * 100,
                    "bounce": bounce, "cls": cls, "up_conf": up_conf, "held": int(not len(brk)),
                    "broke_after": (end - j) if len(brk) else -1, "close_vs_low": (C[385] / lo - 1) * 100,
                    "close_vs_conf": (C[385] / cp - 1) * 100})
    return out


def main(start="2024-10-10", end="2026-10-09"):
    files = [f for f in sorted((OW / "bars10y").glob("*/*.json.gz")) if start <= f.name[:10] <= end]
    with gzip.open(OUT, "wt", newline="") as fh:
        w = None
        for f in files:
            day = f.name[:10]
            d = json.load(gzip.open(f, "rt")); top = v3_top30(day); k = 0
            for s, bars in d["bars"].items():
                a = arrays(bars)
                if a is None:
                    continue
                for v in vs_of(a):
                    row = {"day": day, "sym": s, "v3": int(s in top),
                           **{x: (round(y, 3) if isinstance(y, (float, np.floating)) else y) for x, y in v.items()}}
                    if w is None:
                        w = csv.DictWriter(fh, fieldnames=list(row)); w.writeheader()
                    w.writerow(row); k += 1
            del d
            print(day, k, flush=True)
    report()


def report():
    cols = {k: [] for k in ("day", "v3", "m", "n", "drop", "bounce", "cls", "up_conf", "held", "close_vs_low", "close_vs_conf")}
    with gzip.open(OUT, "rt") as fh:
        for r in csv.DictReader(fh):
            for k in cols:
                cols[k].append(r[k])
    D = {k: np.array(v) for k, v in cols.items()}
    for k in ("m", "n", "drop", "bounce", "up_conf", "close_vs_low", "close_vs_conf"):
        D[k] = D[k].astype(float)
    D["v3"] = D["v3"] == "1"; D["held"] = D["held"] == "1"
    ndays = len(set(D["day"]))
    print(f"\n{len(D['day'])} Vs, {ndays} days. HINDSIGHT outcomes: bounce = highest high after the V low until the low"
          f" breaks; 'from confirm' = from the close 3 bars after the low (the earliest a bot can know the V)")
    for lab, base in (("SCANNER-LIKE (v3 top 30)", D["v3"]), ("ALL $5-15 STOCKS", np.ones(len(D["day"]), bool))):
        print(f"\n===== {lab}: {base.sum() / ndays:.1f} Vs per day =====")
        print(f"  {'':22} {'Vs/day':>7} | " + " | ".join(f"{c:>9}" for c in CLASSES) + " | held to close | median up from confirm | "
              f"close > confirm")
        groups = [("all Vs", base)]
        for lo, hi in ((1, 2), (2, 3), (3, 5), (5, 99)):
            groups.append((f"drop {lo}-{hi if hi < 99 else '+'}%", base & (D["drop"] >= lo) & (D["drop"] < hi)))
        for nth, sel in (("1st V of the day", D["n"] == 1), ("2nd V", D["n"] == 2), ("3rd+ V", D["n"] >= 3)):
            groups.append((nth, base & sel))
        for tl, a, b in (("V 9:45-11:00", 15, 90), ("V 11:00-13:00", 90, 210), ("V 13:00-15:15", 210, 346)):
            groups.append((tl, base & (D["m"] >= a) & (D["m"] < b)))
        for g, k in groups:
            n = k.sum()
            if not n:
                continue
            print(f"  {g:22} {n / ndays:7.1f} | " + " | ".join(f"{np.mean(D['cls'][k] == c):9.0%}" for c in CLASSES)
                  + f" | {np.mean(D['held'][k]):13.0%} | {np.median(D['up_conf'][k]):+21.2f}% | {np.mean(D['close_vs_conf'][k] > 0):14.0%}")
        k = base & (D["drop"] >= 3)
        print(f"  drop >= 3%: per day {k.sum() / ndays:.1f} -> reversed >= 3% {np.sum(k & (D['bounce'] >= 3)) / ndays:.1f}/day, "
              f"kept down {np.sum(k & (D['cls'] == 'kept down')) / ndays:.1f}/day")


if __name__ == "__main__":
    report() if "--report" in sys.argv else main(*sys.argv[1:3])
