"""
day_pages.py -- [2026-10-11] The user: a web page per PAST DAY listing every stock the scanners selected; click a stock
-> its chart and a table with every indicator value PER MINUTE, for as long as the bot could see it.
Source = what the bots really saw: data/decisions/<day>.jsonl (one record per symbol per 5-s poll: state, failing
rules, all entry metrics) -> the LAST record of each minute. Bars for the chart: Alpaca 1-min bars. Trades: both bots.
Output (served by Apache): reports/days/index.html, reports/days/<day>/index.html, reports/days/<day>/<SYM>.html
    python3 day_pages.py [DAY ...]      default: today          python3 day_pages.py --all   (every day with decisions)
cron: 16:40 weekdays (today + the previous day). [2026-11-03] charts in the Rule Lab book standard (stock_page)
"""
import glob
import gzip
import html
import json
import os
import sys
from collections import defaultdict
from datetime import date, datetime, timezone
from pathlib import Path
from zoneinfo import ZoneInfo

TRADE = Path("/var/www/screener/trade"); STAR = Path("/var/www/screener/premarket")
BOTS = {"trade": TRADE, "star": STAR}
OUT = TRADE / "reports" / "days"
ET = ZoneInfo("America/New_York")
sys.path.insert(0, str(TRADE)); sys.path.insert(0, str(TRADE / "reports" / "playbook"))
OW = TRADE / "reports" / "open_window"
LWC = "https://unpkg.com/lightweight-charts@4.1.3/dist/lightweight-charts.standalone.production.js"

# (column title, how to read it from a decision record)
COLS = [
    ("state", lambda r: r.get("state")),
    ("vs open %", lambda m: m.get("ext_open_pct")),
    ("vs VWAP %", lambda m: m.get("vwap_pct")),
    ("RVOL so far", lambda m: m.get("rvol_sofar")),
    ("vol 15m x", lambda m: m.get("vol15_x")),
    ("buy share 15m", lambda m: m.get("buy_share15")),
    ("buy vol 15m", lambda m: m.get("buy_vol15")),
    ("sell vol 15m", lambda m: m.get("sell_vol15")),
    ("smooth R2", lambda m: m.get("r2_15")),
    ("straight", lambda m: m.get("eff15")),
    ("accel 5", lambda m: m.get("acc5")),
    ("move x", lambda m: m.get("move_x")),
    ("spread %", lambda m: m.get("spread_pct")),
    ("shares/min 15", lambda m: m.get("shares_per_min_15")),
    ("room day high %", lambda m: m.get("room_day_high_pct")),
    ("to resistance %", lambda m: (m.get("sr14") or {}).get("dist_to_resistance_pct")),
    ("range pos", lambda m: m.get("range_pos")),
    ("playbook", lambda m: (m.get("playbook") or {}).get("group_name") or (m.get("playbook") or {}).get("action")),
]


def minute_of(ts):
    t = datetime.fromisoformat(ts)
    if t.tzinfo is None:
        t = t.replace(tzinfo=timezone.utc)
    t = t.astimezone(ET)
    return (t.hour - 9) * 60 + t.minute - 30


def hm(m):
    return f"{9 + (30 + m) // 60}:{(30 + m) % 60:02d}"


def read_decisions(root, day):
    """{sym: {minute: last entry record}}, {sym: {minute: last exit record}} -- streamed (files are ~200 MB)."""
    ent, ext = defaultdict(dict), defaultdict(dict)
    p = root / "data" / "decisions" / f"{day}.jsonl"
    if not p.exists() and p.with_suffix(".jsonl.gz").exists():          # [2026-11-02] old logs are gzipped
        p = p.with_suffix(".jsonl.gz")
    if not p.exists():
        return ent, ext
    import gzip as _gz
    with (_gz.open(p, "rt") if str(p).endswith(".gz") else open(p)) as fh:
        for line in fh:
            try:
                r = json.loads(line)
                m = minute_of(r["timestamp"])
            except Exception:
                continue
            if not 0 <= m < 390:
                continue
            (ent if r.get("kind") == "entry" else ext)[r["symbol"]][m] = r
    return ent, ext


def scanned(root, day):
    first = {}
    for f in sorted(glob.glob(str(root / "data" / "candidates" / f"{day}_scanner*.json"))):
        b = os.path.basename(f)
        t = b.split("_scanner_")[1][:4] if "_scanner_" in b else "0930"
        try:
            for c in json.load(open(f)):
                first.setdefault(c["symbol"], f"{t[:2]}:{t[2:]}")
        except Exception:
            pass
    return first


def trades(root, day):
    p = root / "data" / "trades" / f"{day}_trades.jsonl"
    return [json.loads(l) for l in open(p)] if p.exists() else []


def fmt(v):
    if v is None:
        return ""
    if isinstance(v, float):
        return f"{v:,.0f}" if abs(v) >= 1000 else f"{v:.3f}".rstrip("0").rstrip(".") if abs(v) < 10 else f"{v:.2f}"
    return str(v)


CSS = """<style>
:root{--bg:#ffffff;--fg:#1d2733;--mute:#5b6675;--line:#d5dbe3;--head:#f1f4f8;--buy:#2e7d32;--sell:#c62828;--hl:#fff7d6}
@media (prefers-color-scheme: dark){:root:not([data-theme="light"]){--bg:#11161d;--fg:#e3e8ef;--mute:#98a3b3;--line:#2b3440;--head:#1a212b;--buy:#66bb6a;--sell:#ef5350;--hl:#3a3418}}
:root[data-theme="dark"]{--bg:#11161d;--fg:#e3e8ef;--mute:#98a3b3;--line:#2b3440;--head:#1a212b;--buy:#66bb6a;--sell:#ef5350;--hl:#3a3418}
body{font-family:system-ui,Arial,sans-serif;margin:16px;background:var(--bg);color:var(--fg)}
a{color:inherit} h1{font-size:20px;margin:4px 0 8px} .mute{color:var(--mute);font-size:13px}
table{border-collapse:collapse;font-size:12px;margin:8px 0} .wrap{overflow-x:auto;max-width:100%}
th,td{border:1px solid var(--line);padding:3px 6px;text-align:right;white-space:nowrap} th{background:var(--head);position:sticky;top:0}
td:first-child,th:first-child{text-align:left} tr.buy td{background:var(--hl)} .pos{color:var(--buy)} .neg{color:var(--sell)}
#chart{width:100%;height:740px;margin:8px 0} .tabs button{margin-right:6px;padding:4px 10px}
</style>"""


def _ema(vals, n):
    a = 2 / (n + 1); e = vals[0] if vals else 0; out = []
    for v in vals:
        e = a * v + (1 - a) * e; out.append(e)
    return out


def _hm(m):
    return f"{9 + (30 + m) // 60}:{(30 + m) % 60:02d}"


def stock_page(day, sym, bars, bot_rows, bot_exits, bot_trades, first_seen, ref=None, pmh=None, flow=None):
    """[2026-11-03] chart in the Rule Lab book standard (lab_engine.draw_trade): candles, EMA9 blue / EMA20 orange / VWAP purple / EMA50 brown, MACD 12/26/9 panel,
    open / prev close / 5-min opening high / premarket high lines, each trade's STOP line, BUY / SELL labels with time, price, % and
    reason, volume vs normal volume, buy share 3m / 15m. Readings at the buy (signal bar = the finished bar before the buy) in the table."""
    T = [int(b["t"].timestamp()) for b in bars]; M = [b["m"] for b in bars]; C = [b["c"] for b in bars]
    data = [{"time": t, "open": b["o"], "high": b["h"], "low": b["l"], "close": b["c"], "v": b["v"]} for t, b in zip(T, bars)]
    pv = cv = 0.0; VW = []
    for b in bars:
        pv += (b["h"] + b["l"] + b["c"]) / 3 * b["v"]; cv += b["v"]; VW.append(pv / cv if cv else b["c"])
    EF, ES, E50 = _ema(C, 9), _ema(C, 20), _ema(C, 50)
    MF, MS = _ema(C, 12), _ema(C, 26); MACD = [a - b for a, b in zip(MF, MS)]; SIG = _ema(MACD, 9)    # MACD 12/26/9
    HIST = [a - b for a, b in zip(MACD, SIG)]
    line = lambda vals: [{"time": t, "value": round(v, 4)} for t, v in zip(T, vals) if v is not None]
    vpm = (ref or {}).get("vol_per_min") or []
    normal = [vpm[m] if m < len(vpm) else None for m in M] if vpm else []
    # buy share: the library's per-minute flow (top-30 stocks, after the 16:50 nightly) else the bot's own 15-min reading
    s3, s15, share_src = [None] * len(M), [None] * len(M), ""
    if flow and flow.get("buy"):
        fb, fs = flow["buy"], flow["sell"]
        def sh(m, k):
            a = sum(fb[max(0, m - k + 1):m + 1]); z = sum(fs[max(0, m - k + 1):m + 1])
            return a / (a + z) if a + z > 0 else None
        s3 = [sh(m, 3) for m in M]; s15 = [sh(m, 15) for m in M]; share_src = "buy share from the full trade flow"
    else:
        rows = bot_rows.get("trade") or bot_rows.get("star") or {}
        if rows:
            s15 = [((rows.get(m) or {}).get("metrics") or {}).get("buy_share15") for m in M]
            share_src = "buy share 15m = the bot's own reading while it watched the stock (no 3m: stock not in the flow library)"
    by_m = {m: k for k, m in enumerate(M)}
    O = bars[0]["o"] if bars else None
    levels = []
    if O: levels.append((O, "#888888", 3, "open"))
    pc = (ref or {}).get("prev_close")
    if pc: levels.append((pc, "#b07a00", 1, "prev close"))
    oh = max((b["h"] for b in bars if b["m"] < 5), default=None)
    if oh: levels.append((oh, "#c0392b", 2, "5-min opening high"))
    if pmh: levels.append((pmh, "#16a085", 2, "premarket high"))
    lo, hi = min(b["l"] for b in bars), max(b["h"] for b in bars)
    levels = [{"price": round(p, 4), "color": c, "style": st, "title": t} for p, c, st, t in levels if lo * 0.9 <= p <= hi * 1.1]
    markers, stops, tl = [], [], []
    tmin = lambda iso: datetime.fromisoformat(iso).astimezone(ET)
    for bot, TT in bot_trades.items():
        for t in TT:
            if not t.get("entry_time"):
                continue
            eb = tmin(t["entry_time"]); bm = (eb.hour - 9) * 60 + eb.minute - 30
            xb = tmin(t["exit_time"]) if t.get("exit_time") else None; xm = (xb.hour - 9) * 60 + xb.minute - 30 if xb else None
            P, X = t.get("entry_price"), t.get("exit_price"); pl = t.get("pl_pct")
            if pl is None and P and X: pl = (X / P - 1) * 100
            why = (t.get("exit_reason") or "").split(":")[0][:28]
            tag = "Trade" if bot == "trade" else "Star"
            if bm in by_m:
                markers.append({"time": T[by_m[bm]], "position": "belowBar", "color": "#1f6feb", "shape": "arrowUp",
                                "text": f"{tag} BUY {_hm(bm)} {P:.2f}" if P else f"{tag} BUY"})
            if xm is not None and xm in by_m:
                markers.append({"time": T[by_m[xm]], "position": "aboveBar", "color": "#d64545", "shape": "arrowDown",
                                "text": f"SELL {_hm(xm)} {X:.2f} ({pl:+.2f}%) {why}" if X and pl is not None else "SELL"})
            st = t.get("stop_price")
            if st and bm in by_m:
                e = by_m.get(xm, len(T) - 1) if xm is not None else len(T) - 1
                if e <= by_m[bm]: e = min(by_m[bm] + 1, len(T) - 1)
                if e > by_m[bm]:
                    stops.append({"data": [{"time": T[by_m[bm]], "value": st}, {"time": T[e], "value": st}],
                                  "title": f"stop {st:.2f} ({(st / P - 1) * 100:+.1f}%)" if P else "stop"})
            # readings at the buy = the finished bar before the buy minute (as in the Rule Lab)
            k = by_m.get(bm - 1, by_m.get(bm)); rd = ""
            if k is not None:
                vx = bars[k]["v"] / normal[k] if normal and normal[k] else None
                after = [b["h"] for b in bars if bm <= b["m"] <= bm + 60]
                r60 = (max(after) / P - 1) * 100 if after and P else None
                parts = [f"close vs VWAP {(C[k] / VW[k] - 1) * 100:+.2f}%", f"EMA9 vs VWAP {(EF[k] / VW[k] - 1) * 100:+.2f}%",
                         "EMA9 rising" if k and EF[k] >= EF[k - 1] else "EMA9 falling",
                         f"close vs EMA50 {(C[k] / E50[k] - 1) * 100:+.2f}%",
                         f"MACD {'above' if MACD[k] >= SIG[k] else 'below'} signal ({'rising' if k and HIST[k] >= HIST[k - 1] else 'falling'})",
                         f"vol {vx:.2f}x normal" if vx is not None else "",
                         f"buy 3m {s3[k] * 100:.0f}%" if s3[k] is not None else "", f"15m {s15[k] * 100:.0f}%" if s15[k] is not None else "",
                         f"above open {(C[k] / O - 1) * 100:+.2f}%", f"max rise 60m {r60:+.1f}%" if r60 is not None else ""]
                rd = " | ".join(p for p in parts if p)
            et = lambda d: d.strftime("%H:%M") if d else ""
            tl.append(f"<tr><td>{tag}</td><td>{fmt(P)}</td><td>{et(eb)}</td><td>{fmt(st)}</td><td>{fmt(X)}</td><td>{et(xb)}</td>"
                      f"<td class='{'pos' if (pl or 0) > 0 else 'neg'}'>{f'{pl:+.2f}%' if pl is not None else ''}</td>"
                      f"<td class='{'pos' if (t.get('pl_dollars') or 0) > 0 else 'neg'}'>{fmt(t.get('pl_dollars'))}</td>"
                      f"<td style='text-align:left'>{html.escape((t.get('exit_reason') or '')[:90])}</td>"
                      f"<td style='text-align:left'>{html.escape(rd)}</td></tr>")
    for bot, t in first_seen.items():
        h, mi = int(t[:2]), int(t[3:]); m = (h - 9) * 60 + mi - 30
        k = by_m.get(m) if m >= 0 else 0
        if k is not None:
            markers.append({"time": T[k], "position": "aboveBar", "color": "#6a1b9a", "shape": "circle",
                            "text": f"{'Trade' if bot == 'trade' else 'Star'} list"})
    markers.sort(key=lambda x: x["time"])
    tables = []
    for bot, rows in bot_rows.items():
        if not rows:
            continue
        head = "<tr><th>time</th><th>open</th><th>high</th><th>low</th><th>close</th><th>volume</th>" + "".join(
            f"<th>{html.escape(c)}</th>" for c, _ in COLS) + "<th>exit check</th><th>failing rules</th></tr>"
        byb = {b["m"]: b for b in bars}
        body = []
        for m in sorted(rows):
            r = rows[m]; mt = r.get("metrics") or {}; b = byb.get(m)
            cells = [hm(m)] + ([fmt(b["o"]), fmt(b["h"]), fmt(b["l"]), fmt(b["c"]), fmt(b["v"])] if b else [""] * 5)
            for c, f in COLS:
                v = f(r) if c == "state" else f(mt)
                cells.append(fmt(v))
            xr = bot_exits.get(bot, {}).get(m)
            cells.append(f"{xr.get('state')}" if xr else "")
            reasons = "; ".join(r.get("reasons_against") or [])
            cls = ' class="buy"' if r.get("state") == "BUY" else ""
            body.append(f"<tr{cls}>" + "".join(f"<td>{html.escape(x)}</td>" for x in cells)
                        + f"<td style='text-align:left'>{html.escape(reasons[:220])}</td></tr>")
        tables.append(f"<div class='bot' id='t_{bot}'><h2>{'Trade bot' if bot == 'trade' else 'Star bot'}: what it saw, minute by minute "
                      f"({hm(min(rows))} to {hm(max(rows))})</h2><div class='wrap'><table>{head}{''.join(body)}</table></div></div>")
    trade_tbl = ("<h2>Trades</h2><div class='wrap'><table><tr><th>bot</th><th>buy</th><th>at</th><th>stop</th><th>sell</th><th>at</th><th>P/L %</th>"
                 "<th>P/L $</th><th>exit reason</th><th>at the buy (finished bar before the buy)</th></tr>"
                 + "".join(tl) + "</table></div>") if tl else "<p class='mute'>No trades in this stock.</p>"
    tabs = "".join('<button onclick="show(\'%s\')">%s</button>' % (b, "Trade bot" if b == "trade" else "Star bot")
                   for b in bot_rows if bot_rows[b])
    atrp = (ref["atr14"] / pc * 100) if ref and ref.get("atr14") and pc else None
    head = " | ".join(x for x in [f"gap {(O / pc - 1) * 100:+.1f}%" if pc and O else "", f"ATR {atrp:.1f}%" if atrp else "",
                                  f"day high vs open {(hi / O - 1) * 100:+.1f}%", f"close vs open {(C[-1] / O - 1) * 100:+.1f}%"] if x)
    sw = lambda c, t, dash=False: (f"<span class='sw'><i style='border-top:2px {'dashed' if dash else 'solid'} {c}'></i>{t}</span>")
    legend = "".join([sw("#1f6feb", "EMA9"), sw("#e07b00", "EMA20"), sw("#7b3fbf", "VWAP"), sw("#8d6e63", "EMA50"), sw("#888", "open", True), sw("#b07a00", "prev close", True),
                      sw("#c0392b", "5-min opening high", True), sw("#16a085", "premarket high", True), sw("#d64545", "stop", True),
                      sw("#555", "normal volume"), sw("#1f6feb", "buy share 3m"), sw("#e07b00", "buy share 15m"), sw("#1f6feb", "MACD"), sw("#e07b00", "MACD signal")])
    return f"""<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
<title>{sym} {day}</title>{CSS}<style>.sw{{display:inline-block;margin:0 12px 4px 0;font-size:12px;color:var(--mute)}}
.sw i{{display:inline-block;width:18px;margin-right:4px;vertical-align:middle}} .pn{{font-size:11px;color:var(--mute);margin:0}}</style>
<script src="{LWC}"></script></head><body>
<p class="mute"><a href="index.html">&larr; {day} list</a> &middot; <a href="../index.html">all days</a></p>
<h1>{sym} &middot; {day}</h1>
<p class="mute">{html.escape(head)}</p>
<div>{legend}</div>
<div id="chart"></div>
<p class="pn">Top: 1-min candles. Middle: volume (bars) vs normal volume for that minute (grey line). Then: buy share (0 to 1, grey line = 0.5). Bottom: MACD 12/26/9 (blue MACD, orange signal, bars = difference).
Blue arrow = BUY, red arrow = SELL (time, price, result, reason), purple dot = when each bot's scanner added the stock. New York time.
{html.escape(share_src)}</p>
{trade_tbl}
<div class="tabs">{tabs}</div>
{''.join(tables)}
<script>
const data={json.dumps(data)}, mk={json.dumps(markers)}, LV={json.dumps(levels)}, ST={json.dumps(stops)};
const ef={json.dumps(line(EF))}, es={json.dumps(line(ES))}, vw={json.dumps(line(VW))}, e50={json.dumps(line(E50))};
const md={json.dumps(line(MACD))}, sg={json.dumps(line(SIG))}, hs={json.dumps([{'time': t, 'value': round(v, 5), 'color': 'rgba(42,157,75,.6)' if v >= 0 else 'rgba(214,69,69,.6)'} for t, v in zip(T, HIST)])};
const nv={json.dumps(line(normal) if normal else [])}, s3={json.dumps(line(s3))}, s15={json.dumps(line(s15))};
const dark=matchMedia('(prefers-color-scheme: dark)').matches;
const el=document.getElementById('chart');
const tf=t=>new Date(t*1000).toLocaleTimeString('en-US',{{timeZone:'America/New_York',hour:'2-digit',minute:'2-digit',hour12:false}});
const ch=LightweightCharts.createChart(el,{{height:740,layout:{{background:{{color:dark?'#11161d':'#ffffff'}},textColor:dark?'#e3e8ef':'#1d2733'}},
 grid:{{vertLines:{{color:dark?'#2b3440':'#eef1f5'}},horzLines:{{color:dark?'#2b3440':'#eef1f5'}}}},
 rightPriceScale:{{scaleMargins:{{top:0.03,bottom:0.47}}}},
 timeScale:{{timeVisible:true,secondsVisible:false,tickMarkFormatter:tf}}, localization:{{timeFormatter:tf}}}});
const cs=ch.addCandlestickSeries({{upColor:'#2a9d4b',downColor:'#d64545',borderVisible:false,wickUpColor:'#2a9d4b',wickDownColor:'#d64545'}});
cs.setData(data); cs.setMarkers(mk);
LV.forEach(l=>cs.createPriceLine({{price:l.price,color:l.color,lineWidth:1,lineStyle:l.style,axisLabelVisible:true,title:l.title}}));
const L=(c,w,o)=>ch.addLineSeries(Object.assign({{color:c,lineWidth:w,priceLineVisible:false,lastValueVisible:false,crosshairMarkerVisible:false}},o||{{}}));
L('#1f6feb',1).setData(ef); L('#e07b00',1).setData(es); L('#7b3fbf',2).setData(vw); L('#8d6e63',1).setData(e50);
ST.forEach(s=>{{const x=L('#d64545',2,{{lineStyle:2,title:s.title}}); x.setData(s.data);}});
const vs=ch.addHistogramSeries({{priceFormat:{{type:'volume'}},priceScaleId:'v',priceLineVisible:false,lastValueVisible:false}});
ch.priceScale('v').applyOptions({{scaleMargins:{{top:0.555,bottom:0.31}}}});
vs.setData(data.map(d=>({{time:d.time,value:d.v,color:d.close>=d.open?'rgba(42,157,75,.55)':'rgba(214,69,69,.55)'}})));
if(nv.length) L('#555555',1,{{priceScaleId:'v'}}).setData(nv);
const sa=L('#1f6feb',1,{{priceScaleId:'s'}}); sa.setData(s3);
const sb=L('#e07b00',1,{{priceScaleId:'s'}}); sb.setData(s15);
sb.createPriceLine({{price:0.5,color:'#999999',lineWidth:1,lineStyle:2,axisLabelVisible:false}});
ch.priceScale('s').applyOptions({{scaleMargins:{{top:0.71,bottom:0.17}}}});
const mh=ch.addHistogramSeries({{priceScaleId:'m',priceLineVisible:false,lastValueVisible:false}}); mh.setData(hs);
L('#1f6feb',1,{{priceScaleId:'m'}}).setData(md); L('#e07b00',1,{{priceScaleId:'m'}}).setData(sg);
ch.priceScale('m').applyOptions({{scaleMargins:{{top:0.86,bottom:0.01}}}});
ch.timeScale().fitContent(); new ResizeObserver(()=>ch.applyOptions({{width:el.clientWidth}})).observe(el);
function show(b){{document.querySelectorAll('.bot').forEach(d=>d.style.display=d.id==='t_'+b?'block':'none')}}
show('{next((b for b in bot_rows if bot_rows[b]), 'trade')}');
</script></body></html>"""


def build_day(day, client):
    import analyze_trades as A
    first = {b: scanned(root, day) for b, root in BOTS.items()}
    syms = sorted(set(first["trade"]) | set(first["star"]))
    if not syms:
        print(day, "no scanner lists"); return
    dec = {b: read_decisions(root, day) for b, root in BOTS.items()}
    tr = {b: trades(root, day) for b, root in BOTS.items()}
    d = OUT / day; d.mkdir(parents=True, exist_ok=True)
    import reference as REF                                               # [2026-11-03] prev close, ATR, normal volume
    pp = OW / "pm_history" / f"{day}.json.gz"                             # premarket high
    pmh = {r["s"]: r.get("pm_high") for r in json.load(gzip.open(pp, "rt"))["rows"]} if pp.exists() else {}
    rows = []
    for s in syms:
        try:
            bars = A.load_bars(client, s, day, None)
        except Exception:
            bars = []
        if not bars:
            continue
        bt = {b: [t for t in tr[b] if t["symbol"] == s] for b in BOTS}
        try:
            ref = REF.load(day, s)
        except Exception:
            ref = None
        fp = OW / "flow" / day / f"{s}.json"
        flow = json.loads(fp.read_text()) if fp.exists() else None
        try:
            page = stock_page(day, s, bars, {b: dec[b][0].get(s, {}) for b in BOTS}, {b: dec[b][1].get(s, {}) for b in BOTS},
                              {b: v for b, v in bt.items() if v}, {b: first[b][s] for b in BOTS if s in first[b]},
                              ref=ref, pmh=pmh.get(s), flow=flow)
        except Exception as e:                                            # one bad stock must not stop the day
            print(day, s, "page failed:", repr(e), flush=True); continue
        (d / f"{s}.html").write_text(page)
        O = bars[0]["o"]; H = max(b["h"] for b in bars); C = bars[-1]["c"]
        pl = sum(t.get("pl_dollars") or 0 for v in bt.values() for t in v)
        n = sum(len(v) for v in bt.values())
        buys = {b: sum(1 for r in dec[b][0].get(s, {}).values() if r.get("state") == "BUY") for b in BOTS}
        rows.append((s, first["trade"].get(s, ""), first["star"].get(s, ""), (H / O - 1) * 100, (C / O - 1) * 100, n, pl, buys))
    body = "".join(
        f"<tr><td><a href='{s}.html'>{s}</a></td><td>{ft}</td><td>{fs}</td><td class='{'pos' if hi >= 3 else ''}'>{hi:+.1f}%</td>"
        f"<td class='{'pos' if cl > 0 else 'neg'}'>{cl:+.1f}%</td><td>{bu['trade']}/{bu['star']}</td><td>{n or ''}</td>"
        f"<td class='{'pos' if pl > 0 else 'neg' if pl < 0 else ''}'>{f'{pl:+.2f}' if n else ''}</td></tr>"
        for s, ft, fs, hi, cl, n, pl, bu in sorted(rows, key=lambda r: -r[3]))
    (d / "index.html").write_text(f"""<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
<title>Scanner stocks {day}</title>{CSS}</head><body><p class="mute"><a href="../index.html">&larr; all days</a></p>
<h1>Scanner stocks &middot; {day}</h1><p class="mute">{len(rows)} stocks selected by either bot's scanner (9:30 list + rescans), sorted by the
day's high over the open. Click a stock for its chart and the bot's indicators minute by minute.</p>
<div class="wrap"><table><tr><th>stock</th><th>trade bot list from</th><th>Star bot list from</th><th>day high vs open</th><th>close vs open</th>
<th>v2 BUY polls (trade/Star)</th><th>trades</th><th>P/L $</th></tr>{body}</table></div></body></html>""")
    print(day, len(rows), "stocks", flush=True)


def build_index():
    days = sorted((p.name for p in OUT.iterdir() if p.is_dir() and (p / "index.html").exists()), reverse=True)
    (OUT / "index.html").write_text(f"""<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
<title>Past days</title>{CSS}</head><body><h1>Past days</h1><p class="mute">Every stock the scanners selected, with chart and the bot's
indicators per minute.</p><ul>{''.join(f"<li><a href='{d}/index.html'>{d}</a></li>" for d in days)}</ul></body></html>""")


def main(args):
    from alpaca_client import get_client
    c = get_client()
    if "--all" in args:
        days = sorted({os.path.basename(f)[:10] for f in glob.glob(str(TRADE / "data" / "decisions" / "2026-*.jsonl*"))})
    elif args:
        days = args
    else:
        # [2026-11-03] today + the previous day: the previous day's charts get the full buy/sell flow, which the 16:50 nightly
        # adds to the library after this 16:40 run
        have = sorted({os.path.basename(f)[:10] for f in glob.glob(str(TRADE / "data" / "decisions" / "2026-*.jsonl*"))})
        today = date.today().isoformat()
        days = [d for d in have if d < today][-1:] + [today]
    for d in days:
        build_day(d, c)
    build_index()


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