"""
up2_styles_build.py -- [2026-10-08] The user: "find all styles of stocks that went 2-3% above the open price", 5 years
first (2021-10 .. now; the other 5 later if it is interesting). Step 1 of 2: one row per stock-day -> CSV.
Universe: bars10y (every $5-15 stock with >= 500k shares that day). Scanner-like = the day's top 30 by the v3 score
(pm_history, same formula as score_weights_10y.py).
For each level L = +2% / +3% over the official open: the first minute the high reaches it, the STYLE of the path up
to that minute (only what was known then), the features at that minute, and what happened after.
  STYLE (first match):  deep V      the low before the hit was <= -3% under the open
                        dip, then up  the low before the hit was <= -1% under the open
                        straight      hit in the first 15 min (no 1% dip)
                        late move     hit at 14:00 or later
                        base break    the 30 bars ending 5 bars before the hit stayed in a <= 1% range
                        slow grind    hit after >= 60 min with a straight path (efficiency >= 0.25)
                        choppy        the rest
  GAP: up >= +3%, down <= -3%, else flat.
  AFTER: max gain after the hit (vs the open), +5% reached, back to the open, which came first, close vs open;
         trade = buy at the level (or the bar's open if it gapped through), sell at the close / or +5% target vs the
         open-price stop (both in one bar = stop). Stored GROSS (no cost).
    python3 up2_styles_build.py [START] [END]     default 2021-10-08 .. 2026-12-31
"""
import csv
import gzip
import json
import sys
from collections import defaultdict
from datetime import datetime, timedelta, timezone
from pathlib import Path
from zoneinfo import ZoneInfo

import numpy as np

HERE = Path(__file__).resolve().parent
ET = ZoneInfo("America/New_York")
OUT = HERE / (sys.argv[3] if len(sys.argv) > 3 else "up2_styles_rows.csv.gz")
cl = lambda x, lo=0.0, hi=1.0: min(max(x, lo), hi)


def v3_top30(day):
    f = HERE / "pm_history" / f"{day}.json.gz"
    if not f.exists():
        return set()
    sc = []
    for r in json.load(gzip.open(f, "rt"))["rows"]:
        g = lambda k: r.get(k) if r.get(k) is not None else np.nan
        pc, atr, pmv, adv, gap, dist = g("prev_close"), g("atr"), g("pm_vol") or 0, g("adv20"), g("gap"), r.get("dist")
        atrp = atr / pc * 100 if pc and atr == atr else 0.0
        atrp = 0.0 if atrp != atrp or atrp > 25 else atrp
        gap = 0.0 if gap != gap else gap
        s = (35 * cl(atrp / 10) + 25 * cl(abs(gap) / 10) + 20 * cl(pmv / 1e5)
             + 10 * (cl(pmv / adv / 0.05) if adv and adv == adv else 0) + 10 * (1 if dist is None else cl(dist / 8)))
        sc.append((s, r["s"]))
    return {s for _, s in sorted(sc, reverse=True)[:30]}


def arrays(bars):
    a = np.full((5, 390), np.nan)
    for t, o, h, l, c, v in bars:
        tt = datetime.fromtimestamp(t, timezone.utc).astimezone(ET); m = (tt.hour - 9) * 60 + tt.minute - 30
        if 0 <= m < 390:
            a[:, m] = (o, h, l, c, v)
    if np.isnan(a[0, 0]) or np.isnan(a[3]).sum() > 120:
        return None
    for m in range(1, 390):
        if np.isnan(a[3, m]):
            a[0:4, m] = a[3, m - 1]; a[4, m] = 0
    return a


def level(a, O, pct, adv, atrp):
    Op, H, L, C, V = a
    lv = O * (1 + pct / 100)
    hit = np.where(H[:386] >= lv)[0]
    if not len(hit):
        return None
    k = int(hit[0])
    low_b = float(L[:k + 1].min() / O - 1) * 100 if k else 0.0
    path = np.abs(np.diff(C[:k + 1])).sum() if k else 0.0
    eff = (C[k] - O) / path if path > 0 else 1.0
    base = (H[k - 35:k - 5].max() - L[k - 35:k - 5].min()) / O * 100 if k >= 35 else np.nan
    if low_b <= -3: st = "deep V"
    elif low_b <= -1: st = "dip, then up"
    elif k < 15: st = "straight"
    elif k >= 270: st = "late move"
    elif base == base and base <= 1.0: st = "base break"
    elif k >= 60 and eff >= 0.25: st = "slow grind"
    else: st = "choppy"
    cv = V[:k + 1].sum()
    pv = ((H[:k + 1] + L[:k + 1] + C[:k + 1]) / 3 * V[:k + 1]).sum()
    vwap = pv / cv if cv else C[k]
    v5 = V[max(0, k - 4):k + 1].mean(); vall = V[:k + 1].mean()
    hi_b = H[:k].max() / O * 100 - 100 if k else 0.0          # high before the hit bar (a retest of an earlier high?)
    e = max(lv, Op[k])
    # after the hit
    Ha, La = H[k + 1:390], L[k + 1:390]
    mx = float(max(H[k], Ha.max() if len(Ha) else H[k]) / O - 1) * 100
    t5 = np.where(Ha >= O * 1.05)[0]; t0 = np.where(La <= O)[0]
    f5 = t5[0] if len(t5) else 999; f0 = t0[0] if len(t0) else 999
    first = "+5%" if f5 < f0 else ("open" if f0 < 999 else "neither")
    if f5 == f0 and f5 < 999: first = "open"
    if first == "+5%": tr = (O * 1.05 / e - 1) * 100
    elif first == "open": tr = (O / e - 1) * 100
    else: tr = (C[389] / e - 1) * 100
    return {"m": k, "style": st, "low_before": round(low_b, 2), "hi_before": round(hi_b, 2), "eff": round(eff, 3),
            "base": round(base, 2) if base == base else "", "rvol": round(cv / adv, 4) if adv else "",
            "acc5": round(v5 / vall, 2) if vall else "", "vwap_pct": round((C[k] / vwap - 1) * 100, 2),
            "atr_x": round(pct / atrp, 2) if atrp else "",
            "max_after": round(mx, 2), "first": first, "close_pct": round(float(C[389] / O - 1) * 100, 2),
            "tr_close": round(float(C[389] / e - 1) * 100, 3), "tr_5_open": round(float(tr), 3),
            "mae": round(float((La[:max(1, min(f5, 390))].min() if len(La) else e) / e - 1) * 100, 2)}


def main(start="2021-10-08", end="2026-12-31"):
    files = sorted((HERE / "bars10y").glob("*/*.json.gz"))
    hist = defaultdict(list)       # sym -> (h, l, c, v) daily, last 21 (only days the stock was in the universe)
    warm_from = (datetime.fromisoformat(start) - timedelta(days=60)).date().isoformat()
    cols = None
    n = 0
    with gzip.open(OUT, "wt", newline="") as fh:
        w = None
        for f in files:
            day = f.name[:10]
            if day > end:
                break
            if day < warm_from:
                continue
            warm = day < start                     # the 60 days before START only build the ATR / volume history
            d = json.load(gzip.open(f, "rt"))
            daily = d.get("daily", {})
            if not warm:
                top = v3_top30(day)
                for s, bars in d["bars"].items():
                    if s not in daily:
                        continue
                    O, _, _, _, vol, pc = daily[s][:6]
                    hs = hist[s]
                    adv = np.mean([x[3] for x in hs[-20:]]) if len(hs) >= 5 else None
                    atrp = None
                    if len(hs) >= 6:
                        tr = [max(b[0] - b[1], abs(b[0] - p[2]), abs(b[1] - p[2])) for p, b in zip(hs[-15:], hs[-14:])]
                        atrp = np.mean(tr) / hs[-1][2] * 100
                    a = arrays(bars)
                    if a is None:
                        continue
                    O = float(a[0, 0])
                    row = {"day": day, "sym": s, "scan": int(s in top), "gap": round((O / pc - 1) * 100, 2) if pc else "",
                           "atrp": round(atrp, 2) if atrp else "", "day_max": round(float(a[1].max() / O - 1) * 100, 2),
                           "close_pct": round(float(a[3, 389] / O - 1) * 100, 2)}
                    for pct in (2, 3):
                        r = level(a, O, pct, adv, atrp)
                        for kk in ("m", "style", "low_before", "hi_before", "eff", "base", "rvol", "acc5", "vwap_pct",
                                   "atr_x", "max_after", "first", "tr_close", "tr_5_open", "mae"):
                            row[f"l{pct}_{kk}"] = "" if r is None else r[kk]
                    if w is None:
                        w = csv.DictWriter(fh, fieldnames=list(row)); w.writeheader()
                    w.writerow(row); n += 1
            for s, x in daily.items():
                hist[s].append((x[1], x[2], x[3], x[4])); hist[s] = hist[s][-21:]
            del d
            if not warm:
                print(day, n, flush=True)
    print("UP2_BUILD_DONE", n)


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