"""
sim_2y.py -- [2026-10-24] the user: "run a simulation on the two years with this new configuration". Account-level replay of the
trade bot as configured for 10/26 (config.json): EV3 buys R1A R1B R1D R1E R4 R3 (ATR 6-10%), N2+N3 no-trade gate (R1D exempt),
stop under the setup support (x0.998, 0.5-4%), C3 exit (GB2 + X1 control) + 15:55, and the bot's account rules: 5 slots, one
position per stock, risk 1% of equity / (buy - stop) capped at 19% of equity, re-entry after a loss only above that entry + 1c,
no new buys after 15:15. Buys = the triggers the live code was checked against (52/53 identical): notrade.json (kept) +
queue_test.json R1D u0.3x3.0a45 / R1E n8d10x1.5; R3's ATR filter on the exact (unrounded) ATR. Buy at the next open, 0.2% cost.
Same minute: the scanner's order (top-30 rank), one setup per stock per minute (setup order R1A R1B R1D R1E R4 R3).
BASELINE = the original EV3 plan (10/23 morning): R1A R1B R4 R3, no gate, give-back armed at +1% (GB1).
Start $100,000, compounding. Limit-order fills / slippage beyond the 0.2% are not modelled.
    nice -n 19 python3 sim_2y.py -> sim_2y.log, sim_2y.json, sim_2y.pdf
"""
import gzip, json, math, sys
from collections import defaultdict
from pathlib import Path
import numpy as np
HERE = Path(__file__).resolve().parent
sys.path.insert(0, str(HERE)); sys.path.insert(0, "/var/www/screener/trade/reports/playbook"); sys.path.insert(0, "/var/www/screener/trade")
import v2_bar_replay as R
import r1b_setup, r1d_setup, r1e_setup, exit_x1
from exit_pepg import MODS
from queue_test import Day, c3
MODS = {**MODS, "R1B": r1b_setup, "R1D": r1d_setup, "R1E": r1e_setup}
PARAMS = {"R1D": {"undercut_pct": 0.3, "heavy_x": 3.0, "absorb_bs": 0.45}, "R1E": {"bleed_n": 8, "div_up": 0.10, "bleed_x": 1.5}}
ORDER = ["R1A", "R1B", "R1D", "R1E", "R4", "R3"]
COST = 0.2; START = 100000.0; SPLIT = "2025-10-01"; LAST_BUY_M = 344


def sim(D, bi, P, stop, arm, mode):
    """-> (pl %, exit index, why). mode C3 = GB2 + X1 control; GB = plain give-back armed at `arm`"""
    bars, mins, fl = D.b, D.m, D.fl
    peak = P; x1 = False
    for j in range(bi, len(bars)):
        b = bars[j]
        if b["l"] <= stop:
            return (min(stop, b["o"]) if j > bi else stop) / P * 100 - 100 - COST, j, "stop"
        if not x1 and j > bi:
            gain = peak - P
            if gain >= P * arm / 100:
                x = min(peak - 0.25 * gain, bars[j - 1]["c"] * 0.9999)
                if b["l"] <= x:
                    if mode == "C3" and c3(D, bi, j - 1, P, peak):
                        x1 = True
                    else:
                        return min(x, b["o"]) / P * 100 - 100 - COST, j, "give-back"
        peak = max(peak, b["h"])
        if mins[j] >= 385:
            return b["c"] / P * 100 - 100 - COST, j, "15:55"
        if x1 and j + 1 < len(bars):
            sell, why = exit_x1.check(bars[:j + 1], fl, P, bi)
            if sell:
                return bars[j + 1]["o"] / P * 100 - 100 - COST, j + 1, why.split(":")[0]
    return bars[-1]["c"] / P * 100 - 100 - COST, len(bars) - 1, "end"


def main():
    new = defaultdict(list); base = defaultdict(list)
    for r in json.load(open(HERE / "notrade.json")):
        base[r["day"]].append((r["sym"], r["setup"], r["m"]))
        if not (r["f"]["N2"] or r["f"]["N3"]):
            new[r["day"]].append((r["sym"], r["setup"], r["m"]))
    for r in json.load(open(HERE / "queue_test.json"))["B"]:
        if (r["setup"], r["var"]) in (("R1D", "u0.3x3.0a45"), ("R1E", "n8d10x1.5")):
            new[r["day"]].append((r["sym"], r["setup"], r["m"]))
    trades = {"NEW": [], "BASE": []}; skipped = {"NEW": defaultdict(int), "BASE": defaultdict(int)}
    eq = {"NEW": START, "BASE": START}; daily = {"NEW": [], "BASE": []}
    days = sorted(set(new) | set(base))
    for p in sorted(R.OW.glob("bars/bars_20*.json.gz")):
        day = p.name[5:15]
        if day < "2024-10-01" or day > "2026-10-02":
            continue
        if day not in new and day not in base:
            for k in daily:
                daily[k].append((day, eq[k]))
            continue
        d = json.load(gzip.open(p, "rt")); rank = {s: i for i, s in enumerate(d["top30_current"])}; cache = {}
        def load(s):
            if s not in cache:
                fp = R.OW / "flow" / day / f"{s}.json"; ref = R.REF.load(day, s)
                bars = [b for b in R.bars_of(d["symbols"][s]["bars"]) if 0 <= R.minute(b["t"]) < 390]
                cache[s] = Day(bars, [R.minute(b["t"]) for b in bars], json.loads(fp.read_text()), ref)
            return cache[s]
        for sysname, trig, mode, arm in (("NEW", new, "C3", 2.0), ("BASE", base, "GB", 1.0)):
            cand = {}
            for s, st, m in trig.get(day, []):
                D = load(s)
                if st == "R3":
                    a = D.ref["atr14"] / D.ref["prev_close"] * 100
                    if not (6 <= a < 10):
                        continue
                k = (s, m)
                if k not in cand or ORDER.index(st) < ORDER.index(cand[k]):
                    cand[k] = st
            ev = sorted(((m + 1, rank.get(s, 99), s, st, m) for (s, m), st in cand.items()))
            open_pos = []; last = {}; realized = 0.0; e0 = eq[sysname]
            for bm, _, s, st, m in ev:
                if bm > LAST_BUY_M:
                    skipped[sysname]["after 15:15"] += 1; continue
                for op in list(open_pos):                       # close positions that exited by now
                    if op["exit_m"] <= bm:
                        open_pos.remove(op); realized += op["pl_usd"]; last[op["sym"]] = op
                if any(op["sym"] == s for op in open_pos):
                    skipped[sysname]["stock already open"] += 1; continue
                if len(open_pos) >= 5:
                    skipped[sysname]["no free slot"] += 1; continue
                D = load(s); ti = D.m.index(m); bi = ti + 1
                if bi >= len(D.b):
                    continue
                P = D.b[bi]["o"]
                if s in last and last[s]["pl_usd"] <= 0 and not P > last[s]["entry"] + 0.01:
                    skipped[sysname]["re-entry after a loss"] += 1; continue
                ok, info = MODS[st].check(D.b[:ti + 1], D.fl, D.ref, m, PARAMS.get(st))
                if not ok:
                    skipped[sysname]["setup not confirmed"] += 1; continue
                sup = (info or {}).get("support") or P * 0.99
                stop = min(max(sup * 0.998, P * 0.96), P * 0.995)
                equity = e0 + realized
                qty = min(math.floor(equity * 0.01 / (P - stop)), math.floor(equity * 0.19 / P))
                if qty < 1:
                    continue
                pl, j, why = sim(D, bi, P, stop, arm, mode)
                t = {"day": day, "sym": s, "setup": st, "buy_m": D.m[bi], "exit_m": D.m[j], "entry": P, "qty": qty,
                     "notional": round(qty * P, 2), "pl_pct": round(pl, 3), "pl_usd": round(qty * P * pl / 100, 2), "why": why}
                open_pos.append(t); trades[sysname].append(t)
            for op in open_pos:
                realized += op["pl_usd"]
            eq[sysname] = e0 + realized
        for k in daily:
            daily[k].append((day, eq[k]))
        R.REF._CACHE.clear(); del d, cache
        print(day, round(eq["NEW"]), round(eq["BASE"]), flush=True)
    json.dump({"trades": trades, "daily": daily, "skipped": {k: dict(v) for k, v in skipped.items()}}, open(HERE / "sim_2y.json", "w"))
    report()


def report():
    Q = json.load(open(HERE / "sim_2y.json"))
    for k, lab in (("NEW", "NEW CONFIG (10/26)"), ("BASE", "BASELINE (original EV3: no gate, GB1)")):
        T = Q["trades"][k]; dl = Q["daily"][k]
        e = np.array([x[1] for x in dl]); pk = np.maximum.accumulate(e); dd = (e / pk - 1).min() * 100
        a = np.array([t["pl_usd"] for t in T]); pc = np.array([t["pl_pct"] for t in T])
        print(f"\n===== {lab}")
        print(f"  $100,000 -> ${e[-1]:,.0f}  ({(e[-1] / START - 1) * 100:+.1f}%)  | max drawdown {dd:.1f}% | {len(T)} trades "
              f"| win {np.mean(a > 0):.0%} | avg {pc.mean():+.3f}%/trade | avg ${a.mean():+.1f}/trade | profit factor "
              f"{a[a > 0].sum() / -a[a < 0].sum():.2f}")
        for y, (lo, hi) in (("YEAR 1 2024-10..2025-09", ("2024-10-01", SPLIT)), ("YEAR 2 2025-10..2026-10", (SPLIT, "2026-12"))):
            Y = [t for t in T if lo <= t["day"] < hi]; ya = np.array([t["pl_usd"] for t in Y])
            ed = [x[1] for x in dl if lo <= x[0] < hi]; e_start = [x[1] for x in dl if x[0] < lo][-1:] or [START]
            print(f"  {y}: {len(Y)} trades, ${ya.sum():+,.0f} ({(ed[-1] / e_start[0] - 1) * 100:+.1f}%), win {np.mean(ya > 0):.0%}, "
                  f"avg {np.mean([t['pl_pct'] for t in Y]):+.3f}%")
        print("  by setup:  " + " | ".join(f"{s} {len(X)} ${sum(t['pl_usd'] for t in X):+,.0f} ({np.mean([t['pl_pct'] for t in X]):+.2f}%)"
                                             for s in ORDER for X in [[t for t in T if t["setup"] == s]] if X))
        w = defaultdict(lambda: [0, 0.0])
        for t in T:
            w[t["why"]][0] += 1; w[t["why"]][1] += t["pl_usd"]
        print("  by exit:   " + " | ".join(f"{x} {n} ${v:+,.0f}" for x, (n, v) in sorted(w.items(), key=lambda z: -z[1][0])))
        mo = defaultdict(float)
        for t in T:
            mo[t["day"][:7]] += t["pl_usd"]
        print("  by month:  " + " ".join(f"{m_[2:]} {v:+,.0f}" for m_, v in sorted(mo.items())))
        pos = sum(1 for v in mo.values() if v > 0)
        print(f"  months positive {pos}/{len(mo)} | trade days {len({t['day'] for t in T})} of {len(dl)} | skipped: {Q['skipped'][k]}")
        dpl = defaultdict(float)
        for t in T:
            dpl[t["day"]] += t["pl_usd"]
        srt = sorted(dpl.items(), key=lambda z: z[1])
        print(f"  worst days {[(d_, round(v)) for d_, v in srt[:3]]} | best days {[(d_, round(v)) for d_, v in srt[-3:]]}")
        big = sorted(T, key=lambda t: -t["pl_usd"])[:5]
        print("  biggest trades " + ", ".join(f"{t['sym']} {t['day']} {t['setup']} {t['pl_pct']:+.1f}% ${t['pl_usd']:+,.0f}" for t in big))
        tot = a.sum(); top = sum(t["pl_usd"] for t in big)
        print(f"  without the 5 biggest trades: ${tot - top:+,.0f}")
    try:
        import matplotlib; matplotlib.use("Agg"); import matplotlib.pyplot as plt
        from datetime import date
        fig, ax = plt.subplots(2, 1, figsize=(11.7, 8.3), gridspec_kw={"height_ratios": [2.2, 1]})
        for k, col, lab in (("NEW", "#1565c0", "new config (10/26): EV3 + R1D/R1E, N2+N3 gate, C3 exit"), ("BASE", "#9e9e9e", "original EV3: no gate, GB1")):
            dl = Q["daily"][k]; x = [date.fromisoformat(d_) for d_, _ in dl]; e = np.array([v for _, v in dl])
            ax[0].plot(x, e, color=col, lw=1.4, label=f"{lab}: ${e[-1]:,.0f}")
            ax[1].fill_between(x, (e / np.maximum.accumulate(e) - 1) * 100, 0, color=col, alpha=0.4)
        ax[0].axhline(START, color="black", lw=0.6, ls=":"); ax[0].axvline(date(2025, 10, 1), color="#c62828", lw=0.7, ls="--")
        ax[0].set_title("Trade bot 2-year account simulation, $100,000 start (paper rules: 5 slots, 1% risk, 19% cap, 0.2% cost)", fontsize=10, loc="left")
        ax[0].legend(fontsize=8); ax[0].grid(alpha=0.25); ax[1].set_ylabel("drawdown %", fontsize=8); ax[1].grid(alpha=0.25)
        fig.savefig(HERE / "sim_2y.pdf"); fig.savefig(HERE / "sim_2y.png", dpi=110)
    except Exception as ex:
        print("chart failed", ex)
    print("SIM_DONE")


if __name__ == "__main__":
    report() if "--report" in sys.argv else main()
