"""
ext_compare.py -- [2026-10-05] For each extension fix (ext_tests_1005.sh), compare its trades with the
live-rules baseline (entry1003_arm07, same days, every signal) TRADE BY TRADE. The user: a fix must not
cut the winners (ARCO 10/5 +$37, DXC 1st buy +$29 were 'extended' winners).
  blocked  = baseline trades (symbol + buy minute) missing in the variant: winners vs losers, $
  new      = variant trades not in the baseline (re-timed / later entries)
  top 10%  = the baseline's best trades the fix removes
    python3 ext_compare.py
"""
import glob
import re
from pathlib import Path

import numpy as np

OUT = Path("/var/www/screener/trade/reports/open_window/tick20/out")
LINE = re.compile(r"\s+(\d\d):(\d\d):\d\d (\S+)\s+BUY.*?P/L ([-+\d.]+)% \(\$([-+\d.]+)\)")


def trades(prefix, day):
    p = OUT / f"{prefix}_{day}.out"
    T = {}
    if not p.exists():
        return None
    for l in open(p):
        g = LINE.match(l)
        if g:
            m = (int(g.group(1)) - 4) * 60 + int(g.group(2)) - 570
            T[(g.group(3), m)] = float(g.group(5))
    return T


def main():
    days = sorted(f[-14:-4] for f in glob.glob(str(OUT / "entry1003_arm07_*.out")))
    half = days[len(days) // 2]
    base = {d: trades("entry1003_arm07", d) for d in days}
    allb = sorted(v for d in days for v in base[d].values())
    top_cut = allb[int(len(allb) * 0.9)] if allb else 0
    print(f"baseline: {sum(len(v) for v in base.values())} trades, ${sum(sum(v.values()) for v in base.values()):+.2f}; "
          f"top-10% winners are >= ${top_cut:.2f}")
    names = {"f1": "real levels only", "f2": "max 1 daily ATR over the open", "f3": "no buy at >= 3x normal move",
             "f5": "not in the top 10% of the day's range", "f6": "max 2.5 ATR over yesterday's close",
             "f12": "real levels only + 1 ATR ceiling"}
    for v, name in names.items():
        tot = {"bw": [], "bl": [], "new": [], "top": []}
        net = {True: 0.0, False: 0.0}; n_days = 0; better = worse = 0
        for d in days:
            t = trades(f"ext1005_{v}", d)
            if t is None:
                continue
            n_days += 1
            b = base[d]
            for k, pl in b.items():
                if k not in t:
                    (tot["bw"] if pl > 0 else tot["bl"]).append((d, k[0], pl))
                    if pl >= top_cut and pl > 0:
                        tot["top"].append((d, k[0], pl))
            for k, pl in t.items():
                if k not in b:
                    tot["new"].append((d, k[0], pl))
            diff = sum(t.values()) - sum(b.values())
            net[d < half] += diff
            better += diff > 0.005; worse += diff < -0.005
        if not n_days:
            print(f"\n{v} ({name}): not run yet"); continue
        bw = sum(x[2] for x in tot["bw"]); bl = sum(x[2] for x in tot["bl"]); nw = sum(x[2] for x in tot["new"])
        print(f"\n{v} ({name}), {n_days} days: net change ${net[True] + net[False]:+.2f} "
              f"(halves ${net[True]:+.0f} / ${net[False]:+.0f}), days better/worse {better}/{worse}")
        print(f"   blocked WINNERS {len(tot['bw'])} (${bw:+.2f}) | blocked LOSERS {len(tot['bl'])} (${bl:+.2f}) | "
              f"new/re-timed trades {len(tot['new'])} (${nw:+.2f})")
        if tot["top"]:
            print("   !! removes top-10% winners: " + ", ".join(f"{d} {s} ${p:+.0f}" for d, s, p in tot["top"]))


if __name__ == "__main__":
    main()
