"""
up2_styles_report.py -- [2026-10-08] Step 2 of 2 (see up2_styles_build.py): the report.
  A  how many stock-days reach +2% / +3% over the open (all stocks / scanner-like top 30), per year
  B  STYLES at the moment of the hit: how common, what happened after (+5%, back to the open, close), and the
     trade (buy at the level; sell at the close, or +5% target vs the open as the stop) at the real 0.2% cost
  C  style x gap        D  time of the hit
  E  catching it early: among the stocks that hit the level, which features KNOWN AT THE HIT separate the runners
     (went on to >= +5% over the open) from the rest -- AUC per year, plus the best feature's quintiles
  F  before the open: which stocks reach +2% at all (gap, ATR, scanner pick) -- AUC
    python3 up2_styles_report.py [ROWS.csv.gz] [LEVEL]       default up2_styles_rows.csv.gz, both levels
"""
import csv
import gzip
import sys
from collections import defaultdict
from pathlib import Path

import numpy as np

HERE = Path(__file__).resolve().parent
COST = 0.2
STYLES = ["straight", "dip, then up", "deep V", "base break", "slow grind", "late move", "choppy"]
NUM = ["gap", "atrp", "day_max", "close_pct"]
LNUM = ["m", "low_before", "hi_before", "eff", "base", "rvol", "acc5", "vwap_pct", "atr_x", "max_after", "tr_close",
        "tr_5_open", "mae"]


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 load(path):
    col = defaultdict(list)
    with gzip.open(path, "rt") as fh:
        for r in csv.DictReader(fh):
            col["day"].append(r["day"]); col["scan"].append(r["scan"] == "1")
            for k in NUM:
                col[k].append(float(r[k]) if r[k] != "" else np.nan)
            for L in (2, 3):
                col[f"l{L}_style"].append(r[f"l{L}_style"]); col[f"l{L}_first"].append(r[f"l{L}_first"])
                for k in LNUM:
                    v = r[f"l{L}_{k}"]; col[f"l{L}_{k}"].append(float(v) if v != "" else np.nan)
    out = {k: np.array(v) for k, v in col.items()}
    out["year"] = np.array([d[:4] for d in out["day"]])
    return out


def line(D, k, L, lab):
    n = k.sum()
    if n == 0:
        return f"  {lab:22} {0:7}"
    g = lambda c: D[f"l{L}_{c}"][k]
    run = (g("max_after") >= 5).mean(); back = (D[f"l{L}_first"][k] == "open").mean()
    tc, t5 = g("tr_close") - COST, g("tr_5_open") - COST
    yrs = sorted(set(D["year"][k]))
    pos = sum(np.nanmean((D[f"l{L}_tr_5_open"] - COST)[k & (D["year"] == y)]) > 0 for y in yrs)
    return (f"  {lab:22} {n:7} | +5% after {run:4.0%} | back to open first {back:4.0%} | max after (median) "
            f"{np.nanmedian(g('max_after')):+5.2f}% | close {np.nanmean(D['close_pct'][k]):+5.2f}% | trade: to close "
            f"{np.nanmean(tc):+.3f}%, +5%/open {np.nanmean(t5):+.3f}% (years > 0: {pos}/{len(yrs)})")


def report(D, L):
    hit = ~np.isnan(D[f"l{L}_m"])
    yrs = sorted(set(D["year"]))
    print(f"\n{'=' * 30} LEVEL +{L}% over the open {'=' * 30}")
    print("A  share of stock-days that reach it (all / scanner-like):")
    for y in yrs:
        k = D["year"] == y
        print(f"  {y}: {k.sum():6} stock-days, {hit[k].mean():.0%} reach +{L}%  | scanner-like {(k & D['scan']).sum():5}, "
              f"{hit[k & D['scan']].mean():.0%}")
    for grp, gk in (("ALL STOCKS", hit), ("SCANNER-LIKE (top 30 by v3)", hit & D["scan"])):
        print(f"\nB  styles -- {grp}  (trade costs {COST}% round trip)")
        print(line(D, gk, L, "all that hit"))
        for st in STYLES:
            print(line(D, gk & (D[f"l{L}_style"] == st), L, st))
    gapc = np.where(D["gap"] >= 3, "gap up", np.where(D["gap"] <= -3, "gap down", "flat"))
    print("\nC  style x gap (all stocks): trades | +5% after | trade +5%/open per trade")
    print(f"  {'':16}" + "".join(f"{g:>36}" for g in ("gap down", "flat", "gap up")))
    for st in STYLES:
        cells = []
        for g in ("gap down", "flat", "gap up"):
            k = hit & (D[f"l{L}_style"] == st) & (gapc == g)
            cells.append(f"{k.sum():8} | {(D[f'l{L}_max_after'][k] >= 5).mean() if k.sum() else 0:4.0%} | "
                         f"{np.nanmean(D[f'l{L}_tr_5_open'][k] - COST) if k.sum() else 0:+.3f}%")
        print(f"  {st:16}" + "".join(f"{c:>36}" for c in cells))
    print("\nD  time of the hit (all stocks)")
    m = D[f"l{L}_m"]
    for lo, hi, lab in ((0, 15, "9:30-9:45"), (15, 30, "9:45-10:00"), (30, 60, "10:00-10:30"), (60, 120, "10:30-11:30"),
                        (120, 210, "11:30-13:00"), (210, 270, "13:00-14:00"), (270, 300, "14:00-14:30"), (300, 390, "14:30-16:00")):
        print(line(D, hit & (m >= lo) & (m < hi), L, lab))
    print(f"\nE  catching it early: among stocks that hit +{L}%, AUC for 'goes on to +5%' (0.5 = no info; < 0.5 = lower is better)")
    y5 = D[f"l{L}_max_after"] >= 5
    feats = {"gap": D["gap"], "gap size": np.abs(D["gap"]), "daily ATR %": D["atrp"], "scanner pick": D["scan"].astype(float),
             "minute of the hit": m, "low before (vs open)": D[f"l{L}_low_before"], "earlier high": D[f"l{L}_hi_before"],
             "path efficiency": D[f"l{L}_eff"], "base range": D[f"l{L}_base"], "volume so far / ADV": D[f"l{L}_rvol"],
             "volume last 5 / avg": D[f"l{L}_acc5"], "vs VWAP": D[f"l{L}_vwap_pct"], "level in ATRs": D[f"l{L}_atr_x"]}
    print(f"  {'feature':24}" + "".join(f"{y:>7}" for y in yrs) + "    all")
    best = None
    for name, x in feats.items():
        a = [auc(x[hit & (D['year'] == y)], y5[hit & (D['year'] == y)]) for y in yrs]
        al = auc(x[hit], y5[hit])
        print(f"  {name:24}" + "".join(f"{v:7.2f}" for v in a) + f"  {al:5.2f}")
        if name != "scanner pick" and (best is None or abs(al - 0.5) > abs(best[1] - 0.5)):
            best = (name, al)
    for name in dict.fromkeys([best[0], "daily ATR %", "volume so far / ADV", "vs VWAP"]):
        x = feats[name][hit]; ok = ~np.isnan(x)
        qs = np.nanquantile(x, [0.2, 0.4, 0.6, 0.8])
        print(f"\n  quintiles of '{name}' (cut points {', '.join(f'{q:.2f}' for q in qs)}):")
        b = np.digitize(x, qs)
        for q in range(5):
            k = np.zeros(len(D["day"]), bool); idx = np.where(hit)[0][ok & (b == q)]; k[idx] = True
            print(line(D, k, L, f"Q{q + 1}"))
    print(f"\nF  before the open: AUC for 'reaches +{L}%' (all stock-days)")
    for name, x in (("gap", D["gap"]), ("gap size", np.abs(D["gap"])), ("daily ATR %", D["atrp"]), ("scanner pick", D["scan"].astype(float))):
        print(f"  {name:16}" + "".join(f"{auc(x[D['year'] == y], hit[D['year'] == y]):7.2f}" for y in yrs) + f"  {auc(x, hit):5.2f}")


def main(path="up2_styles_rows.csv.gz", lv=None):
    D = load(HERE / path)
    days = sorted(set(D["day"]))
    print(f"{len(D['day'])} stock-days, {len(days)} days {days[0]}..{days[-1]}, scanner-like {D['scan'].sum()}")
    for L in ((int(lv),) if lv else (2, 3)):
        report(D, L)


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