"""ev1_analysis.py -- [2026-11-04] EV1 study, step 2 (after ev1_features.py): the user (11/04): rename the 10-year entry to EV1;
(A) what the +2%-first buys have in common -> the EV1 group; (B) group the -2%-first buys, 10 sample charts each; (C) what makes 2020,
2022-23 and 2025-26 different -> regime groups, 10 sample charts each; results in a PDF. Entry quality only (exits later).
Judged by what comes first after the buy: +2% (W) or -2% (L). Two periods: P1 = 2016-11..2021-10, P2 = 2021-11..now (the user's halves).
Plain numpy (no sklearn on the server): AUC, k-means (standardized, clipped), a small rule search for the EV1 group.
Charts = lab_engine.draw_trade (the Rule Lab / Rule Lab book standard). Output: reports/ev1/ev1_study.pdf + ev1_study.json."""
import json, random, sys
from collections import defaultdict
from pathlib import Path
import numpy as np
HERE = Path(__file__).resolve().parent; LAB = Path("/var/www/screener/trade/reports/lab")
sys.path.insert(0, str(LAB)); sys.path.insert(0, "/var/www/screener/trade/reports/playbook"); sys.path.insert(0, "/var/www/screener/trade")
JOB = "20261104-193157-ea088a"
import os
TEST = bool(os.environ.get("EV1_TEST"))                       # dry run: synthetic rows, no charts, test PDF
ROWS = json.load(open(HERE / ("ev1_rows_synth.json" if TEST else "ev1_rows.json")))
# [11/04 fix] signals_day counted the WHOLE day's EV1 signals = look-ahead. Now: EV1 signals SO FAR that day (this one included,
# same-minute signals count: they close on the same bar)
_bd = defaultdict(list)
for r in ROWS: _bd[r["day"]].append(r["t_min"])
for r in ROWS: r["signals_day"] = sum(1 for x in _bd[r["day"]] if x <= r["t_min"])
SPLIT = "2021-11-01"; GOOD = {2020, 2022, 2023, 2025, 2026}
FEAT = {   # feature: plain words (high / low)
    "t_min": ("late signal (minutes after 9:30)", "early signal"), "gap": ("bigger gap up", "smaller gap"), "atr": ("higher daily ATR %", "lower daily ATR %"),
    "price": ("higher price", "lower price"), "ext_open": ("further above the open", "closer to the open"),
    "c_vs_vwap": ("further above VWAP", "close to VWAP"), "ema_vs_vwap": ("EMA9 further above VWAP", "EMA9 close to VWAP"),
    "ema9_20": ("EMA9 far above EMA20", "EMA9 just above EMA20"), "ema9_slope3": ("steep EMA9 (3 bars)", "flat EMA9"),
    "vwap_3m": ("VWAP rising fast (3 min)", "VWAP rising slowly (3 min)"), "vwap_30m": ("VWAP up a lot in 30 min", "VWAP flat/down over 30 min"),
    "range_atr": ("wide day range so far (x ATR)", "narrow day range so far"), "run_from_low_atr": ("big run from the day low (x ATR)", "little run from the low"),
    "below_high": ("at/near the day high", "well below the day high"), "low_min": ("day low made late", "day low made early"),
    "daypos": ("top of the day range", "lower in the day range"), "open5_atr": ("wide first 5 minutes", "quiet first 5 minutes"),
    "open5_dir": ("first 5 min up", "first 5 min down"), "ret5": ("up a lot in 5 min", "little 5-min move"), "ret15": ("up a lot in 15 min", "little 15-min move"),
    "up_bars": ("many green closes in a row", "few green closes in a row"), "bar_atr": ("big signal bar (x ATR)", "small signal bar"),
    "bar_close_pos": ("signal bar closes at its high", "signal bar closes low in its range"), "upper_wick": ("long upper wick", "no upper wick"),
    "vol_x": ("volume far above normal (3 bars)", "volume near normal"), "rvol": ("high RVOL so far", "low RVOL so far"),
    "vol_trend": ("volume rising (5 vs 10 bars)", "volume fading"), "s3": ("3-min buy share high", "3-min buy share low"),
    "s15": ("15-min buy share high", "15-min buy share low"), "s_open": ("buy share since open high", "buy share since open low"),
    "room": ("more room to resistance", "less room to resistance"), "dip_atr": ("deeper dip before", "shallow dip before"),
    "chg_60m": ("up a lot in 60 min", "little 60-min move"), "long_ema_10m": ("EMA50 rising fast", "EMA50 flat"), "liq": ("more liquid", "less liquid"),
    "vs_pmh": ("above the premarket high", "below the premarket high"), "prev_close_gap": ("far above yesterday's close", "near yesterday's close"),
    "top30_up": ("most of the day's top 30 are up", "most of the day's top 30 are down"), "top30_gap_med": ("big gaps in the day's top 30", "small gaps in the top 30"),
    "signals_day": ("many EV1 signals so far that day", "few EV1 signals so far that day"), "spy_prev": ("SPY up yesterday", "SPY down yesterday"),
    "spy_trend20": ("SPY above its 20-day mean", "SPY below its 20-day mean"), "spy_rng20": ("volatile market (20-day SPY range)", "calm market"),
    "spy_gap": ("SPY gapped up", "SPY gapped down"), "spy_open": ("SPY up since the open", "SPY down since the open"),
    "spy_vwap": ("SPY above its VWAP", "SPY below its VWAP"), "spy_5m": ("SPY rising (5 min)", "SPY falling (5 min)"),
    "iwm_open": ("small caps (IWM) up since the open", "small caps (IWM) down since the open"), "iwm_vwap": ("IWM above its VWAP", "IWM below its VWAP"),
    "iwm_5m": ("IWM rising (5 min)", "IWM falling (5 min)"),
    # [11/04 user] the last 15 minutes before the signal
    "w_range_atr": ("15 min before: wide range (x ATR)", "15 min before: tight range"), "w_straight": ("15 min before: straight climb", "15 min before: choppy path"),
    "w_r2": ("15 min before: smooth trend line", "15 min before: ragged"), "w_green": ("15 min before: mostly green bars", "15 min before: mostly red bars"),
    "w_pullback": ("15 min before: no real pullback", "15 min before: deep pullback inside"), "w_since_low": ("15 min before: low made early in the window", "15 min before: low made just before the signal"),
    "w_since_high": ("15 min before: high made earlier (signal below it)", "15 min before: signal at the window high"),
    "w_below_vwap": ("15 min before: many closes below VWAP", "15 min before: stayed above VWAP"), "w_vwap_cross": ("15 min before: was at/below VWAP (reclaim)", "15 min before: never touched VWAP"),
    "w_ema_cross": ("15 min before: crossed EMA9 often (chop)", "15 min before: stayed on one side of EMA9"),
    "w_vwap_slope": ("15 min before: VWAP rising fast", "15 min before: VWAP flat"), "w_ema_slope": ("15 min before: EMA9 rising fast", "15 min before: EMA9 flat"),
    "w_maxbar_atr": ("15 min before: a big bar (spike)", "15 min before: no big bar"), "w_vol_spike": ("15 min before: a volume spike", "15 min before: even volume"),
    "w_share_trend": ("15 min before: buy share rising", "15 min before: buy share falling"), "w_share_min": ("15 min before: buying never dropped", "15 min before: a selling minute (low 3-min share)"),
    "w_sell_flush": ("15 min before: a big sell minute (flush)", "15 min before: no sell flush"), "w_new_high": ("15 min before: made a new day high", "15 min before: no new day high"),
    "w_near_vwap": ("15 min before: stayed well above VWAP", "15 min before: came close to VWAP"),
}
F = [f for f in FEAT if sum(r.get(f) is not None for r in ROWS) > 0.8 * len(ROWS)]
DAYF = ["spy_trend20", "spy_rng20", "spy_prev", "spy_gap", "top30_up", "top30_gap_med", "signals_day", "spy_open", "iwm_open", "iwm_vwap"]
DAYF = [f for f in DAYF if f in F]
WL = [r for r in ROWS if r["f2"] in (1, -1)]
per = lambda r: "P1" if r["day"] < SPLIT else "P2"
val = lambda r, f: r.get(f) if r.get(f) is not None else np.nan


def auc(pos, neg):
    pos = np.asarray([x for x in pos if x == x]); neg = np.asarray([x for x in neg if x == x])
    if len(pos) < 5 or len(neg) < 5: return np.nan
    allv = np.concatenate([pos, neg]); rk = allv.argsort().argsort() + 1.0
    # average ranks for ties
    order = np.argsort(allv); sv = allv[order]; r_ = np.empty(len(allv)); i = 0
    while i < len(sv):
        j = i
        while j + 1 < len(sv) and sv[j + 1] == sv[i]: j += 1
        r_[order[i:j + 1]] = (i + j) / 2 + 1; i = j + 1
    return (r_[:len(pos)].sum() - len(pos) * (len(pos) + 1) / 2) / (len(pos) * len(neg))


def edge(R):
    n = len(R)
    if not n: return dict(n=0, w=0, l=0, e=0)
    w = sum(r["f2"] == 1 for r in R) / n; l = sum(r["f2"] == -1 for r in R) / n
    return dict(n=n, w=w, l=l, e=w - l)


# ---------------- A: what the +2%-first buys have in common
featrows = []
for f in F:
    a1 = auc([val(r, f) for r in WL if per(r) == "P1" and r["f2"] == 1], [val(r, f) for r in WL if per(r) == "P1" and r["f2"] == -1])
    a2 = auc([val(r, f) for r in WL if per(r) == "P2" and r["f2"] == 1], [val(r, f) for r in WL if per(r) == "P2" and r["f2"] == -1])
    mw = np.nanmedian([val(r, f) for r in WL if r["f2"] == 1]); ml = np.nanmedian([val(r, f) for r in WL if r["f2"] == -1])
    cons = (a1 >= 0.55 and a2 >= 0.55) or (a1 <= 0.45 and a2 <= 0.45)
    featrows.append(dict(f=f, a1=a1, a2=a2, mw=mw, ml=ml, cons=cons, strength=min(abs(a1 - 0.5), abs(a2 - 0.5)) if a1 == a1 and a2 == a2 else 0))
featrows.sort(key=lambda x: -x["strength"])

# rule search: up to 3 conditions, thresholds at the 20..80% quantiles; objective = the WORSE period's edge, support >= 15% in both periods
def cond_ok(r, c):
    f, d, th = c; v = r.get(f)
    return v is not None and (v >= th if d == ">=" else v <= th)
cands = []
for fr in featrows[:18]:
    f = fr["f"]; vs = np.array([val(r, f) for r in WL]); vs = vs[vs == vs]
    for q in (20, 30, 40, 50, 60, 70, 80):
        th = float(np.percentile(vs, q)); cands += [(f, ">=", th), (f, "<=", th)]
base1, base2 = edge([r for r in WL if per(r) == "P1"]), edge([r for r in WL if per(r) == "P2"])
def score(conds):
    R = [r for r in ROWS if all(cond_ok(r, c) for c in conds)]
    e1, e2 = edge([r for r in R if per(r) == "P1"]), edge([r for r in R if per(r) == "P2"])
    n1 = sum(1 for r in ROWS if per(r) == "P1"); n2 = len(ROWS) - n1
    if e1["n"] < 0.15 * n1 or e2["n"] < 0.15 * n2: return -9, e1, e2
    return min(e1["e"], e2["e"]), e1, e2
best = []
for step in range(3):
    bs, bc = (score(best)[0] if best else -9), None
    for c in cands:
        if any(c[0] == x[0] for x in best): continue
        s_ = score(best + [c])[0]
        if s_ > bs + 0.01: bs, bc = s_, c
    if bc is None: break
    best.append(bc)
_, G1, G2 = score(best)
inG = lambda r: all(cond_ok(r, c) for c in best)

# ---------------- B: the -2%-first buys, grouped (k-means on the strongest features)
def kmeans(X, k, seed=7, it=60, tries=8):
    rng = np.random.default_rng(seed); bestc, besti, bestj = None, None, 1e18
    for _ in range(tries):
        c = X[rng.choice(len(X), k, replace=False)]
        for _ in range(it):
            d = ((X[:, None, :] - c[None]) ** 2).sum(-1); lab = d.argmin(1)
            nc = np.array([X[lab == j].mean(0) if (lab == j).any() else c[j] for j in range(k)])
            if np.allclose(nc, c): break
            c = nc
        j_ = ((X - c[lab]) ** 2).sum()
        if j_ < bestj: bestj, bestc, besti = j_, c, lab
    return bestc, besti
KF = [x["f"] for x in featrows[:12]]
allv = np.array([[val(r, f) for f in KF] for r in ROWS], float)
mu, sd = np.nanmedian(allv, 0), np.nanstd(allv, 0) + 1e-9
Z = lambda R, fs=KF, m=mu, s=sd: np.clip(np.nan_to_num((np.array([[val(r, f) for f in fs] for r in R], float) - m) / s), -3, 3)
LOS = [r for r in WL if r["f2"] == -1]; WIN = [r for r in WL if r["f2"] == 1]
KL = 5
cL, labL = kmeans(Z(LOS), KL)
dW = ((Z(WIN)[:, None, :] - cL[None]) ** 2).sum(-1).argmin(1)          # winners assigned to the nearest loser group
def describe(c, fs):
    idx = np.argsort(-np.abs(c))[:4]
    return "; ".join(FEAT[fs[i]][0 if c[i] > 0 else 1] + f" ({'+' if c[i] > 0 else '-'}{abs(c[i]):.1f} sd)" for i in idx if abs(c[i]) >= 0.3)
lgroups = []
for j in range(KL):
    Lj = [r for r, l in zip(LOS, labL) if l == j]; Wj = [r for r, l in zip(WIN, dW) if l == j]
    lgroups.append(dict(id=f"L{j + 1}", n_l=len(Lj), n_w=len(Wj), desc=describe(cL[j], KF), rows=Lj,
                        w_share=len(Wj) / max(len(Wj) + len(Lj), 1),
                        med={f: float(np.nanmedian([val(r, f) for r in Lj])) for f in KF}, medw={f: float(np.nanmedian([val(r, f) for r in WIN])) for f in KF}))
lgroups.sort(key=lambda g: -g["n_l"])
for k, g in enumerate(lgroups): g["id"] = f"L{k + 1}"

# ---------------- C: what makes the good years different -> regime groups (day-level / market features)
yr = defaultdict(list)
for r in ROWS: yr[r["year"]].append(r)
yrows = []
for y in sorted(yr):
    e = edge(yr[y]); yrows.append(dict(y=y, good=y in GOOD, **e, **{f: float(np.nanmedian([val(r, f) for r in yr[y]])) for f in DAYF + ["atr", "gap", "t_min"]}))
reg = []
for f in DAYF + ["atr", "gap", "t_min"]:
    g = [val(r, f) for r in ROWS if r["year"] in GOOD]; b = [val(r, f) for r in ROWS if r["year"] not in GOOD]
    reg.append(dict(f=f, good=float(np.nanmedian(g)), bad=float(np.nanmedian(b)), auc=auc(g, b)))
reg.sort(key=lambda x: -abs(x["auc"] - 0.5))
dmu, dsd = np.nanmedian(np.array([[val(r, f) for f in DAYF] for r in ROWS], float), 0), np.nanstd(np.array([[val(r, f) for f in DAYF] for r in ROWS], float), 0) + 1e-9
ZD = lambda R: Z(R, DAYF, dmu, dsd)
KR = 4
cR, labR = kmeans(ZD(ROWS), KR, seed=11)
rgroups = []
for j in range(KR):
    Rj = [r for r, l in zip(ROWS, labR) if l == j]
    rgroups.append(dict(id=f"M{j + 1}", desc=describe(cR[j], DAYF), rows=Rj, all=edge(Rj), p1=edge([r for r in Rj if per(r) == "P1"]),
                        p2=edge([r for r in Rj if per(r) == "P2"]), good_share=sum(r["year"] in GOOD for r in Rj) / max(len(Rj), 1),
                        years={y: sum(r["year"] == y for r in Rj) for y in sorted(yr)}))
rgroups.sort(key=lambda g: -g["all"]["e"])
for k, g in enumerate(rgroups): g["id"] = f"M{k + 1}"

# ---------------- charts (Rule Lab standard)
import lab_engine as LE
cfg = LE.merge(LE.DEFAULTS, json.loads((LAB / "jobs" / JOB / "rules.json").read_text()))
TR = {}
for l in open(LAB / "jobs" / JOB / "trades.jsonl"):
    t = json.loads(l); TR[t["n"]] = t
CH = HERE / "charts"; CH.mkdir(exist_ok=True)
def charts(rows, tag, k=10, seed=1):
    if TEST: return []
    pick = random.Random(seed).sample(rows, min(k, len(rows))); out = []
    for r in sorted(pick, key=lambda x: x["day"]):
        z = TR[r["n"]]; fn = CH / f"{tag}_{z['sym']}_{z['day']}.png"
        if not fn.exists():
            try:
                LE.draw_trade(cfg, z, fn, [z])
            except Exception as e:
                print("chart failed", tag, z["sym"], z["day"], e, flush=True); continue
        out.append((fn, r))
    return out
CHG = {"EV1 group (winners inside)": charts([r for r in ROWS if inG(r) and r["f2"] == 1], "G_win"),
       "EV1 group (losers inside)": charts([r for r in ROWS if inG(r) and r["f2"] == -1], "G_los", k=4)}
for g in lgroups: CHG[g["id"]] = charts(g["rows"], g["id"])
for g in rgroups: CHG[g["id"]] = charts(g["rows"], g["id"])
print("charts done", flush=True)

# ---------------- PDF
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, Image, PageBreak, KeepTogether
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib import colors
from reportlab.lib.units import inch
ss = getSampleStyleSheet(); H1 = ss["Title"]; H2 = ParagraphStyle("h2", parent=ss["Heading2"], textColor=colors.HexColor("#1f4e79"))
H3 = ParagraphStyle("h3", parent=ss["Heading3"], textColor=colors.HexColor("#1f4e79")); N = ParagraphStyle("n", parent=ss["BodyText"], fontSize=9, leading=12)
S = ParagraphStyle("s", parent=N, fontSize=7.5, leading=9.5)
pc = lambda x: f"{x * 100:.0f}%"
def tbl(data, w=None, fs=7.5):
    t = Table([[Paragraph(str(c), S) for c in row] for row in data], colWidths=w, repeatRows=1)
    t.setStyle(TableStyle([("GRID", (0, 0), (-1, -1), 0.3, colors.grey), ("BACKGROUND", (0, 0), (-1, 0), colors.HexColor("#dde7f3")), ("VALIGN", (0, 0), (-1, -1), "TOP")]))
    return t
def eline(e): return f"n {e['n']} · +2% first {pc(e['w'])} · -2% first {pc(e['l'])} · edge {e['e'] * 100:+.0f} pts"
ALL1, ALL2 = edge([r for r in ROWS if per(r) == "P1"]), edge([r for r in ROWS if per(r) == "P2"])
st = [Paragraph("EV1 entry study", H1),
      Paragraph(f"Made 2026-11-04 from the 10-year Rule Lab run <b>EV1</b> (job {JOB}: EMA9 &gt; EMA20 both rising, EMA50 rising, VWAP rising, close above VWAP, "
                "3-min buy share rising and above the 15-min (&gt;= 55%), volume &gt;= 1.5x normal, close &gt; 3 bars ago by 0.5%, new 1-bar high, room &gt;= 1%, "
                "ATR 6-10%, gap -5..+5%, above yesterday's close, 9:35-10:30). Only the ENTRY is judged here: what came first after the buy, +2% or -2% "
                "(within 2 hours). Every reading is taken at the signal bar (finished bars only). Exits come later.", N),
      Paragraph(f"<b>All EV1 buys:</b> 2016-11..2021-10: {eline(ALL1)}<br/>2021-11..now: {eline(ALL2)}<br/>"
                "Random minutes on the same stocks reach +2% first ~37-38% and -2% first ~39%, so 'edge' = +2%-first minus -2%-first in points; random ~ -1..-2.", N),
      Paragraph("<b>Caution:</b> groups and thresholds below were found on these same 10 years (in-sample). They are leads to test in the Rule Lab "
                "(both halves + year by year), not proven rules.", N)]
# A
st += [Paragraph("A. What the +2%-first buys have in common", H2),
       Paragraph("AUC = chance that a random +2%-first buy has a higher value than a random -2%-first buy (0.5 = no difference). "
                 "'Both halves' = the difference points the same way in 2016-21 and 2021-26 by at least 0.05.", N)]
st.append(Paragraph("<b>At the signal bar:</b>", N))
d = [["reading", "+2% first (median)", "-2% first (median)", "AUC 2016-21", "AUC 2021-26", "both halves?"]]
for x in [y for y in featrows if not y["f"].startswith("w_")][:22]:
    d.append([FEAT[x["f"]][0 if x["mw"] >= x["ml"] else 1] + f" <font size=6>({x['f']})</font>", f"{x['mw']:.2f}", f"{x['ml']:.2f}", f"{x['a1']:.2f}", f"{x['a2']:.2f}", "YES" if x["cons"] else ""])
st.append(tbl(d, [2.6 * inch, 1 * inch, 1 * inch, 0.8 * inch, 0.8 * inch, 0.7 * inch]))
st += [Spacer(1, 6), Paragraph("<b>The last 15 minutes before the buy</b> (the path into the signal):", N)]
d = [["reading", "+2% first (median)", "-2% first (median)", "AUC 2016-21", "AUC 2021-26", "both halves?"]]
for x in [y for y in featrows if y["f"].startswith("w_")]:
    d.append([FEAT[x["f"]][0 if x["mw"] >= x["ml"] else 1] + f" <font size=6>({x['f']})</font>", f"{x['mw']:.2f}", f"{x['ml']:.2f}", f"{x['a1']:.2f}", f"{x['a2']:.2f}", "YES" if x["cons"] else ""])
st.append(tbl(d, [2.6 * inch, 1 * inch, 1 * inch, 0.8 * inch, 0.8 * inch, 0.7 * inch]))
gtxt = " AND ".join(f"{FEAT[c[0]][0 if c[1] == '>=' else 1]} ({c[0]} {c[1]} {c[2]:.2f})" for c in best) or "none found"
st += [Spacer(1, 6), Paragraph("<b>The EV1 group</b> (best simple combination; scored on the WORSE of the two halves, at least 15% of the buys in each):", N),
       Paragraph(gtxt, N), Paragraph(f"Inside the group: 2016-21 {eline(G1)}<br/>2021-26 {eline(G2)}", N)]
d = [["year", "all EV1 buys", "inside the EV1 group", "outside"]]
for y in sorted(yr):
    a = edge(yr[y]); i_ = edge([r for r in yr[y] if inG(r)]); o_ = edge([r for r in yr[y] if not inG(r)])
    d.append([y, f"n {a['n']} edge {a['e'] * 100:+.0f}", f"n {i_['n']} edge {i_['e'] * 100:+.0f}" if i_["n"] else "-", f"n {o_['n']} edge {o_['e'] * 100:+.0f}" if o_["n"] else "-"])
st.append(tbl(d, [0.7 * inch, 1.9 * inch, 1.9 * inch, 1.9 * inch]))
# B
st += [PageBreak(), Paragraph("B. The -2%-first buys, grouped", H2),
       Paragraph(f"The {len(LOS)} -2%-first buys split into {KL} groups by their readings at the buy (k-means on the 12 strongest readings). "
                 "'Winners that look the same' = +2%-first buys closest to that group: a high share means the group can't be avoided "
                 "by these readings alone and needs a different entry trigger.", N)]
d = [["group", "-2% first buys", "winners that look the same", "what it looks like (vs all EV1 buys)"]]
for g in lgroups: d.append([g["id"], g["n_l"], f"{g['n_w']} ({pc(g['w_share'])} of the look-alikes are winners)", g["desc"]])
st.append(tbl(d, [0.6 * inch, 0.9 * inch, 1.6 * inch, 4.1 * inch]))
# C
st += [PageBreak(), Paragraph("C. What makes 2020, 2022-23 and 2025-26 different", H2)]
d = [["year", "good?", "buys", "edge"] + [f for f in DAYF[:7]]]
for y in yrows: d.append([y["y"], "GOOD" if y["good"] else "", y["n"], f"{y['e'] * 100:+.0f}"] + [f"{y[f]:.2f}" for f in DAYF[:7]])
st.append(tbl(d, None, 6.5))
st.append(Spacer(1, 6)); d = [["reading", "good years (median)", "other years (median)", "AUC good vs other"]]
for x in reg[:12]: d.append([FEAT[x["f"]][0 if x["good"] >= x["bad"] else 1] + f" <font size=6>({x['f']})</font>", f"{x['good']:.2f}", f"{x['bad']:.2f}", f"{x['auc']:.2f}"])
st.append(tbl(d, [3 * inch, 1.2 * inch, 1.2 * inch, 1.2 * inch]))
st += [Spacer(1, 6), Paragraph(f"<b>Market / day groups</b> (k-means on the day-level readings: {', '.join(DAYF)}), best first:", N)]
d = [["group", "buys", "edge all", "edge 2016-21", "edge 2021-26", "share from good years", "what the day looks like"]]
for g in rgroups:
    d.append([g["id"], g["all"]["n"], f"{g['all']['e'] * 100:+.0f}", f"{g['p1']['e'] * 100:+.0f} (n {g['p1']['n']})", f"{g['p2']['e'] * 100:+.0f} (n {g['p2']['n']})", pc(g["good_share"]), g["desc"]])
st.append(tbl(d, [0.5 * inch, 0.45 * inch, 0.55 * inch, 0.85 * inch, 0.85 * inch, 0.75 * inch, 3.2 * inch]))
# charts
def chart_section(title, key, note=""):
    out = [PageBreak(), Paragraph(title, H2)] + ([Paragraph(note, N)] if note else [])
    for fn, r in CHG.get(key, []):
        cap = (f"{r['sym']} {r['day']} · signal {LE.m2hm(r['t_min'])} · {'+2% FIRST' if r['f2'] == 1 else '-2% FIRST' if r['f2'] == -1 else 'neither'} · "
               f"max rise 60m {r['r60']:+.1f}% · max drop 30m {r['d30']:+.1f}% · vs VWAP {r['c_vs_vwap']:+.2f}% · run from low {r['run_from_low_atr']:.1f} ATR · "
               f"SPY since open {r.get('spy_open', 0):+.2f}%")
        out.append(KeepTogether([Image(str(fn), width=7.3 * inch, height=4.65 * inch), Paragraph(cap, S), Spacer(1, 4)]))
    return out
st += chart_section("Charts: the EV1 group, +2%-first buys", "EV1 group (winners inside)", gtxt)
st += chart_section("Charts: the EV1 group, -2%-first buys (what still fails inside it)", "EV1 group (losers inside)")
for g in lgroups: st += chart_section(f"Charts: loser group {g['id']} ({g['n_l']} buys)", g["id"], g["desc"])
for g in rgroups: st += chart_section(f"Charts: market/day group {g['id']} (edge {g['all']['e'] * 100:+.0f} pts, {g['all']['n']} buys)", g["id"], g["desc"])
SimpleDocTemplate(str(HERE / ("ev1_study_test.pdf" if TEST else "ev1_study.pdf")), pagesize=letter, leftMargin=0.5 * inch, rightMargin=0.5 * inch, topMargin=0.5 * inch,
                  bottomMargin=0.5 * inch, title="EV1 entry study").build(st)
json.dump(dict(features=featrows, group=dict(conds=best, p1=G1, p2=G2), loser_groups=[{k: v for k, v in g.items() if k != "rows"} for g in lgroups],
               regimes=[{k: v for k, v in g.items() if k != "rows"} for g in rgroups], years=yrows, regime_features=reg),
          open(HERE / "ev1_study.json", "w"), default=float, indent=1)
print("PDF_DONE")
