"""
atr8_star_test.py -- [2026-10-10] The user: test the LIVE v2 buys on volatile stocks only (daily ATR >= 8%) with
STAR doing the sells, on 2 years. Clue: in v2_rule_variants (10/9) the live buys on ATR >= 8% stocks were ~break-even
(-0.007%/trade at 0.2% cost) with the bot's own exits.
  buys     the 'base (live)' trades of v2_rule_variants.jsonl (real entry_v2 + exit_v2 code, every signal)
  sells    a) the bot's own exit (from that replay)
           b) Star: sf3 (as star_exit_daily.py: lib_v2 library, all confirmed marks, still-matching exit 80) from the
              bot's buy minute and price, + the live 3% safety stop; only the library days BEFORE the trade day
  both at the real 0.2% round-trip cost; ATR >= 8% first, then ATR < 8% for comparison; halves + quarters.
Note: Star's library starts Oct 2024; trades are skipped until it holds >= 450 earlier stock-days, and the early
months still have few references (Star weaker there).
    python3 atr8_star_test.py          -> atr8_star_test.jsonl / .log
"""
import gzip
import json
import 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")
import sf3_batch as S3  # noqa: E402
import lib_v2 as L2  # noqa: E402
import reference as REF  # noqa: E402

OW = Path("/var/www/screener/trade/reports/open_window")
OUT = HERE / "atr8_star_test.jsonl"
COST = 0.2


def main():
    T = [json.loads(l) for l in open(HERE / "v2_rule_variants.jsonl")]
    T = [t for t in T if t["v"] == "base (live)"]
    for t in T:
        t["hi_atr"] = (t["atr"] or 0) >= 8
    lib = L2.Lib(); marks = S3.load_all(); wmask = np.array([k in marks for k in lib.keys]); nb, ns = S3.normal_rates(marks)
    S3.MIN_MATCH = 80; S3.SAFETY_STOP = 3.0; S3.SELL_FILTER = None
    done = set()
    if OUT.exists():
        done = {(r["day"], r["sym"], r["buy_m"]) for r in map(json.loads, open(OUT))}
    by_day = defaultdict(list)
    for t in sorted(T, key=lambda t: (not t["hi_atr"], t["day"])):     # ATR >= 8% first
        by_day[(t["hi_atr"], t["day"])].append(t)
    with open(OUT, "a") as fh:
        for (hi, day), ts in by_day.items():
            ts = [t for t in ts if (t["day"], t["sym"], t["buy_m"]) not in done]
            if not ts:
                continue
            d = json.load(gzip.open(OW / "bars" / f"bars_{day}.json.gz", "rt"))
            for t in ts:
                s, f = t["sym"], t["buy_m"]
                ref = REF.load(day, s)
                a = L2.day_arrays(d["symbols"][s]["bars"], ref, day) if ref and s in d["symbols"] else None
                r = {"day": day, "sym": s, "buy_m": f, "hi_atr": hi, "atr": t["atr"], "v3": t["v3"],
                     "bot_pl": round(t["pl_gross"] - COST, 3), "bot_why": t["why"]}
                enough = int(((lib.dates < day) & wmask).sum()) > max(3 * S3.K, S3.POOL + 50)   # Star needs 150 refs + a 500 pool from earlier days
                r["lib_n"] = int(((lib.dates < day) & wmask).sum())
                if a is not None and 5 <= f <= 384 and enough:
                    st = S3.run_day(lib, wmask, marks, nb, ns, day, a, can_buy=lambda m, f=f: m >= f - 1, force_buy_m=f - 1)
                    if st and st[0]["buy_m"] == f:
                        x = st[0]
                        r.update(star_pl=round((x["sell"] / x["buy"] - 1) * 100 - COST, 3), star_why=x["why"],
                                 star_held=x["sell_m"] - x["buy_m"])
                fh.write(json.dumps(r) + "\n"); fh.flush()
            REF._CACHE.clear()
            del d
            print(day, "ATR>=8" if hi else "ATR<8", len(ts), flush=True)
    report()


def report():
    R = [json.loads(l) for l in open(OUT)]
    R = [r for r in R if r.get("star_pl") is not None]
    days = sorted({r["day"] for r in R}); half = days[len(days) // 2] if days else ""
    print(f"\n{len(R)} trades with both exits, {len(days)} days; per trade after {COST}% cost")
    for lab, sel in (("ATR >= 8%", lambda r: r["hi_atr"]), ("ATR >= 8% and a v3 pick", lambda r: r["hi_atr"] and r["v3"]),
                     ("ATR < 8%", lambda r: not r["hi_atr"]), ("ALL", lambda r: True)):
        X = [r for r in R if sel(r)]
        if not X:
            continue
        b = np.array([r["bot_pl"] for r in X]); s = np.array([r["star_pl"] for r in X]); h = np.array([r["day"] < half for r in X])
        print(f"\n{lab}: {len(X)} trades")
        print(f"  bot exits : {b.mean():+.3f}%/trade, total {b.sum():+.1f}%, win {np.mean(b > 0):.0%} | halves {b[h].mean():+.3f}% / {b[~h].mean():+.3f}%")
        print(f"  Star sells: {s.mean():+.3f}%/trade, total {s.sum():+.1f}%, win {np.mean(s > 0):.0%} | halves {s[h].mean():+.3f}% / {s[~h].mean():+.3f}%"
              f" | median held {np.median([r['star_held'] for r in X]):.0f} min | Star better on {np.mean(s > b + 1e-9):.0%} of trades")
        q = defaultdict(list)
        for r in X:
            q[r["day"][:4] + "Q" + str((int(r["day"][5:7]) - 1) // 3 + 1)].append((r["bot_pl"], r["star_pl"]))
        print("  by quarter (bot / Star): " + "  ".join(f"{k} n{len(v)} {np.mean([x[0] for x in v]):+.2f}/{np.mean([x[1] for x in v]):+.2f}"
                                                   for k, v in sorted(q.items())))
        print(f"  in $ at $1,640 per trade: bot ${b.sum() * 16.4:+.0f}, Star ${s.sum() * 16.4:+.0f}")


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