"""
group_confirm_study.py -- [2026-10-09] The user's design (step 1 = data):
  * every minute 9:45..14:30 each scanner-like stock is in a GROUP = shape style so far x volatility tier
    (style: none / straight / dip, then up / deep V / base break / slow grind / choppy / late move -- up2_styles rules
     at the first +2% touch; tier: daily ATR < 6% / 6-10% / >= 10%). The group can change until confirmation.
  * CONFIRMATION options (all causal: bar m is closed, the signal price is its close):
      O1 new high      close above the day high so far, the old high >= 10 min old (a base)
      O2 VWAP reclaim  2 closes above VWAP after >= 6 closes below it
      O3 V turn        a V low (drop >= 2% from the 30-bar high, lowest of 3 bars each side -- the 3 bars AFTER it
                       must be closed, no look-ahead), then the first close up (> previous close and > the V close)
                       within 8 bars, low unbroken
      O4 9:45 break    first close above the 9:30-9:45 high
      O5 smooth trend  15-bar R2 >= 0.7, rising, 0..+2% over VWAP (like v2's steady-rise rule)
    at most one fire per option per 10 minutes, 10 per day.
  * AFTER a fire: watch until the stock makes its high and FADES (a close more than max(1%, 2x the normal 1-min
    range) under the high since the signal) or 14:30 -> P/L at the fade (GROSS), max gain, high time, to the close.
Output: group_confirm_rows.csv.gz (one row per fire).    python3 group_confirm_study.py [START] [END]
"""
import csv
import gzip
import json
import sys
from pathlib import Path

import numpy as np

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

OUT = HERE / "group_confirm_rows.csv.gz"
FIRST, LAST = 15, 300          # 9:45 .. 14:30


def tier(atrp):
    return "ATR<6" if atrp == atrp and atrp < 6 else ("ATR>=10" if atrp == atrp and atrp >= 10 else "ATR6-10")


def style_series(O, H, L, C):
    """style known at each minute m (bars 0..m closed): 'none' until the first +2% touch, then that touch's style."""
    st = np.array(["none"] * 390, dtype=object)
    hit = np.where(H >= O * 1.02)[0]
    if len(hit):
        k = int(hit[0]); lb = (L[:k + 1].min() / O - 1) * 100
        p2 = np.abs(np.diff(C[:k + 1])).sum() if k else 0.0
        eff = (C[k] - O) / p2 if p2 > 0 else 1.0
        bs = (H[k - 35:k - 5].max() - L[k - 35:k - 5].min()) / O * 100 if k >= 35 else 99
        s = ("deep V" if lb <= -3 else "dip, then up" if lb <= -1 else "straight" if k < 15 else
             "late move" if k >= 270 else "base break" if bs <= 1.0 else "slow grind" if k >= 60 and eff >= 0.25 else "choppy")
        st[k:] = s
    return st


def fires(O, H, L, C, V):
    """{option: [minutes]} -- causal."""
    n = 390
    cv = np.cumsum(V); vw = np.cumsum((H + L + C) / 3 * V) / np.where(cv > 0, cv, np.nan)
    vw = np.where(np.isnan(vw), C, vw)
    out = {k: [] for k in ("O1 new high", "O2 VWAP reclaim", "O3 V turn", "O4 9:45 break", "O5 smooth trend")}
    hi_prev = np.maximum.accumulate(H)
    # O1
    for m in range(FIRST, LAST):
        if C[m] > hi_prev[m - 1]:
            j = int(np.argmax(H[:m]))
            if m - j >= 10:
                out["O1 new high"].append(m)
    # O2
    above = C > vw
    for m in range(FIRST, LAST):
        if above[m] and above[m - 1] and m >= 8 and not above[m - 7:m - 1].any():
            out["O2 VWAP reclaim"].append(m)
    # O3 (V low j confirmed at j+3; entry m in j+3..j+8)
    for j in range(max(3, FIRST - 8), LAST - 3):
        lo = L[j]
        if lo > L[j - 3:j + 4].min() or (H[max(0, j - 30):j].max() / lo - 1) * 100 < 2.0:
            continue
        for m in range(j + 3, min(j + 9, LAST)):
            if L[j + 1:m + 1].min() < lo:
                break
            if m >= FIRST and C[m] > C[m - 1] and C[m] > C[j]:
                out["O3 V turn"].append(m); break
    # O4
    orh = H[:15].max()
    br = np.where(C[FIRST:LAST] > orh)[0]
    if len(br):
        out["O4 9:45 break"].append(FIRST + int(br[0]))
    # O5 rolling R2 of 15 closes
    x = np.arange(15, dtype=float); xm = x.mean(); sxx = ((x - xm) ** 2).sum()
    for m in range(FIRST, LAST):
        y = C[m - 14:m + 1]; ym = y.mean()
        sxy = ((x - xm) * (y - ym)).sum(); syy = ((y - ym) ** 2).sum()
        if sxy <= 0 or syy <= 0:
            continue
        r2 = sxy * sxy / (sxx * syy)
        d = (C[m] / vw[m] - 1) * 100
        if r2 >= 0.7 and 0 <= d <= 2:
            out["O5 smooth trend"].append(m)
    # thin: one fire per 10 minutes, max 10
    for k, ms in out.items():
        keep, last = [], -99
        for m in ms:
            if m - last >= 10:
                keep.append(m); last = m
        out[k] = keep[:10]
    return out


def after(H, L, C, m):
    P = C[m]
    r1 = np.mean(H[max(0, m - 14):m + 1] - L[max(0, m - 14):m + 1]) / P * 100
    f = max(1.0, 2 * r1)
    seg_c = C[m + 1:LAST + 1]; seg_h = H[m + 1:LAST + 1]
    if not len(seg_c):
        return None
    run_hi = np.maximum.accumulate(np.maximum(seg_h, P))
    fade = np.where(seg_c < run_hi * (1 - f / 100))[0]
    e = int(fade[0]) if len(fade) else len(seg_c) - 1
    px = seg_c[e]
    hi_i = int(np.argmax(seg_h[:e + 1]))
    return {"pl": (px / P - 1) * 100, "max_gain": (max(seg_h[:e + 1].max(), P) / P - 1) * 100, "hi_m": m + 1 + hi_i,
            "exit_m": m + 1 + e, "fade_pct": f, "to_close": (C[389] / P - 1) * 100,
            "day_max": (H[m + 1:386].max() / P - 1) * 100}


def main(start="2021-10-08", end="2026-12-31"):
    files = [f for f in sorted((HERE / "bars10y").glob("*/*.json.gz")) if start <= f.name[:10] <= end]
    n = 0
    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"))
            pmf = HERE / "pm_history" / f"{day}.json.gz"
            pm = {r["s"]: r for r in json.load(gzip.open(pmf, "rt"))["rows"]} if pmf.exists() else {}
            for s in v3_top30(day):
                if s not in d["bars"] or s not in pm:
                    continue
                a = arrays(d["bars"][s])
                if a is None:
                    continue
                Op, H, L, C, V = a
                O = float(Op[0]); r = pm[s]; pc = r.get("prev_close")
                atrp = r["atr"] / pc * 100 if r.get("atr") and pc else np.nan
                if atrp == atrp and atrp > 25:
                    atrp = np.nan
                tr = tier(atrp); st = style_series(O, H, L, C)
                for opt, ms in fires(O, H, L, C, V).items():
                    for m in ms:
                        x = after(H, L, C, m)
                        if x is None:
                            continue
                        row = {"day": day, "sym": s, "opt": opt, "group": f"{st[m]} | {tr}", "style": st[m], "tier": tr,
                               "m": m, "price": round(float(C[m]), 4), "ret_open": round((C[m] / O - 1) * 100, 3),
                               **{k: (round(float(v), 3) if isinstance(v, (float, np.floating)) else v) for k, v in x.items()}}
                        if w is None:
                            w = csv.DictWriter(fh, fieldnames=list(row)); w.writeheader()
                        w.writerow(row); n += 1
            del d
            print(day, n, flush=True)
    print("CONFIRM_BUILD_DONE", n)


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