"""
setups_2y.py -- [2026-10-23] 2-year test of the user's chart setups R1, R1A, R2, R3, R4, R5, R6, R7, R8, R9 (built from the
practice charts). The user: "after the buy signal, how much the stock rose before fading".
Every top-30 stock-day (2024-10 .. 2026-10), every minute, every setup (finished bars only; each setup's own cool-down).
A trigger at bar close m = buy at the next open P. From there, to the end of the day:
  rise_stop   the highest price reached BEFORE the price falls 1% under the buy (the stop) -- or the day ends
  rise_fade   the highest price reached before the first 1% pullback from the running high (the fade)
  hit1/2/3/5  +1 / +2 / +3 / +5% reached before -1% under the buy
  trail       the result of a 1% trailing stop (stop -1% under the buy, raised to 1% under every new high), after 0.2% cost
  t_peak      minutes from the buy to the high before the fade
BASELINE: every 10th minute 9:45-15:00 of the same stocks, same measures (what an ordinary minute gives).
    nice -n 19 python3 setups_2y.py [START..END]   -> setups_2y.jsonl + report (setups_2y.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 v2_bar_replay as R  # noqa: E402
import r1_setup, r1a_setup, r2_setup, r3_setup, r4_setup, r5_setup, r6_setup, r7_setup, r8_setup, r9_setup  # noqa: E402,E401

SETUPS = [("R1", r1_setup), ("R1A", r1a_setup), ("R2", r2_setup), ("R3", r3_setup), ("R4", r4_setup), ("R5", r5_setup),
          ("R6", r6_setup), ("R7", r7_setup), ("R8", r8_setup), ("R9", r9_setup)]
OUT = HERE / "setups_2y.jsonl"; SPLIT = "2025-10-01"; COST = 0.2


def path_stats(bars, i):
    P = bars[i + 1]["o"]
    stop = P * 0.99
    hi = P; rise_stop = None; rise_fade = None; t_peak = 0; trail_stop = stop; trail_res = None
    hits = {1: False, 2: False, 3: False, 5: False}; stopped = False
    for j in range(i + 1, len(bars)):
        b = bars[j]
        # order inside a bar: assume the low first when the bar is down (conservative)
        if not stopped and b["l"] <= stop:
            rise_stop = (hi / P - 1) * 100; stopped = True
        if trail_res is None and b["l"] <= trail_stop:
            trail_res = (trail_stop / P - 1) * 100 - COST
        if rise_fade is None and b["l"] <= hi * 0.99 and j > i + 1:
            rise_fade = (hi / P - 1) * 100
        if b["h"] > hi:
            hi = b["h"]
            if rise_fade is None:
                t_peak = j - i
            trail_stop = max(trail_stop, hi * 0.99)
        if not stopped:
            for k in hits:
                if not hits[k] and b["h"] >= P * (1 + k / 100):
                    hits[k] = True
        if stopped and rise_fade is not None and trail_res is not None:
            break
    last = bars[-1]["c"]
    if rise_stop is None:
        rise_stop = (hi / P - 1) * 100
    if rise_fade is None:
        rise_fade = (hi / P - 1) * 100
    if trail_res is None:
        trail_res = (last / P - 1) * 100 - COST
    return {"rise_stop": round(rise_stop, 3), "rise_fade": round(rise_fade, 3), "t_peak": t_peak, "trail": round(trail_res, 3),
            "hit1": hits[1], "hit2": hits[2], "hit3": hits[3], "hit5": hits[5], "close": round((last / P - 1) * 100, 3)}


def run(rng):
    a, b = rng.split("..")
    with open(OUT, "w") as out:
        for p in sorted(R.OW.glob("bars/bars_20*.json.gz")):
            day = p.name[5:15]
            if not (a <= day <= b):
                continue
            d = json.load(gzip.open(p, "rt")); k = defaultdict(int)
            for s in d["top30_current"]:
                fp = R.OW / "flow" / day / f"{s}.json"; ref = R.REF.load(day, s)
                if s not in d["symbols"] or not fp.exists() or not ref or not ref.get("atr14"):
                    continue
                fl = json.loads(fp.read_text())
                bars = [x for x in R.bars_of(d["symbols"][s]["bars"]) if 0 <= R.minute(x["t"]) < 390]
                if len(bars) < 100:
                    continue
                atrp = ref["atr14"] / ref["prev_close"] * 100
                last = {nm: -9999 for nm, _ in SETUPS}
                for i in range(len(bars) - 1):
                    m = R.minute(bars[i]["t"])
                    if m >= 360:
                        break
                    done = bars[:i + 1]
                    for nm, mod in SETUPS:
                        if m - last[nm] < mod.DEFAULTS.get("cooldown", 15):
                            continue
                        try:
                            ok, info = mod.check(done, fl, ref, m)
                        except Exception:
                            ok = False
                        if ok:
                            last[nm] = m
                            out.write(json.dumps({"setup": nm, "day": day, "sym": s, "m": m, "atr": round(atrp, 1),
                                                  **path_stats(bars, i)}) + "\n"); k[nm] += 1
                    if 15 <= m <= 330 and m % 10 == 0:
                        out.write(json.dumps({"setup": "BASE", "day": day, "sym": s, "m": m, "atr": round(atrp, 1),
                                              **path_stats(bars, i)}) + "\n")
                R.REF._CACHE.clear()
            del d
            print(day, dict(k), flush=True)
    report()


def report():
    T = [json.loads(l) for l in open(OUT)]
    days = sorted({t["day"] for t in T})
    def stats(X):
        f = lambda k: np.array([t[k] for t in X], float)
        y = np.array([t["day"] < SPLIT for t in X])
        rf, rs, tr = f("rise_fade"), f("rise_stop"), f("trail")
        return (f"{len(X):6} {len(X) / len(days):5.2f}/day | rise before fade: median {np.median(rf):+.2f}% mean {rf.mean():+.2f}% | "
                f"before the -1% stop: median {np.median(rs):+.2f}% | +1% {f('hit1').mean():4.0%} +2% {f('hit2').mean():4.0%} "
                f"+3% {f('hit3').mean():4.0%} +5% {f('hit5').mean():4.0%} (before -1%) | peak after {np.median(f('t_peak')):.0f} min | "
                f"1% trailing stop {tr.mean():+.3f}% (y1 {tr[y].mean() if y.any() else 0:+.3f} y2 {tr[~y].mean() if (~y).any() else 0:+.3f})")
    print(f"\n{len(days)} days {days[0]}..{days[-1]}; buy at the next open after the trigger bar; to the end of the day\n")
    for nm in ["BASE"] + [n for n, _ in SETUPS]:
        X = [t for t in T if t["setup"] == nm]
        if X:
            print(f"{nm:5} {stats(X)}")
    print("\nby daily ATR:")
    for nm in ["BASE"] + [n for n, _ in SETUPS]:
        for lab, lo, hi in (("ATR <6%", 0, 6), ("ATR 6-10%", 6, 10), ("ATR >=10%", 10, 999)):
            X = [t for t in T if t["setup"] == nm and lo <= t["atr"] < hi]
            if len(X) >= 20:
                print(f"{nm:5} {lab:9} {stats(X)}")
    print("SETUPS2Y_DONE")


if __name__ == "__main__":
    if "--report" in sys.argv:
        report()
    else:
        run(sys.argv[1] if len(sys.argv) > 1 else "2024-10-01..2026-10-02")
