"""
e2_test.py -- [2026-10-23] the user: rules that catch DERM's behaviour (11/5/24) and BLOCK the sell. DERM = thin stock, minutes
of 100-1,000-share prints; the give-back sold 12:42 on ONE 620-share print (0.18x volume) while the 15-min buyers rose 52->76%;
it stair-stepped to +5.4% (15:01) with every dip holding above the previous low; a real sell came 15:17 (1,490 sh, 1% buyers).
E2 parts (read FINISHED bars only, never over the stop, never under the buy price):
  thin-print filter  a minute is a "seller minute" only if its volume >= 0.5x the 15-bar average (a lone small print is ignored)
  structure hold     hold while the last close stays above the higher-low support = the lowest low of the last 15 finished
                     bars (must be above the buy) minus 1 x the mean 1-min range -- release when a bar closes under it
Variants (hold while ...):
  E1a   (reference) 5-min buy share >= 50% and not 2 seller minutes (< 40% buyers) in a row
  E1e   (reference) E1a or the dip < 2x the mean 1-min range
  E2t   E1a with the thin-print filter
  E2s   structure hold only
  E2ts  E2t OR E2s
  E2et  E1e with the thin-print filter
Also by liquidity at the buy (mean shares/min over the 15 bars before): < 2,000 (DERM-like) vs >= 2,000.
    nice -n 19 python3 e2_test.py -> e2_test.log
"""
import gzip, json, sys
from collections import defaultdict
from pathlib import Path
import numpy as np
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
from exit_pepg import MODS
MODS = {**MODS, "R1B": r1b_setup}
FIX = "--fix" in sys.argv; BE = "--no-be" not in sys.argv
COST = 0.2; SPLIT = "2025-10-01"; HERE = Path(__file__).resolve().parent
VARS = ["B", "E1a", "E1e", "E2t", "E2et"]


def hold(v, bars, mins, fl, j, peak, P):
    if v == "B":
        return False
    k = j - 1; m = mins[k]; fb, fs = fl["buy"], fl["sell"]
    def bs(mm, n):
        B = sum(fb[max(0, mm - n + 1):mm + 1]); S = sum(fs[max(0, mm - n + 1):mm + 1]); return B / (B + S) if B + S else 0.5
    av = lambda i: sum(x["v"] for x in bars[max(0, i - 15):i]) / max(1, min(15, i))
    def seller(i, thin):
        if bs(mins[i], 1) >= 0.40:
            return False
        return not (thin and bars[i]["v"] < 0.5 * av(i))
    def buyers(thin):
        return bs(m, 5) >= 0.50 and not (seller(k, thin) and seller(k - 1, thin))
    rng = sum(x["h"] - x["l"] for x in bars[max(0, k - 14):k + 1]) / min(15, k + 1)
    noise = (peak - bars[k]["c"]) < 2 * rng
    def structure():
        sup = min(x["l"] for x in bars[max(0, k - 14):k + 1])
        return sup > P and bars[k]["c"] >= sup - rng
    if v == "E1a": return buyers(False)
    if v == "E1e": return buyers(False) or noise
    if v == "E2t": return buyers(True)
    if v == "E2s": return structure()
    if v == "E2ts": return buyers(True) or structure()
    if v == "E2et": return buyers(True) or noise


def sim(v, bars, mins, fl, bi, P, stop, arm=1.0, g=0.25):
    peak = P; held = 0
    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, "stop", j
        gain = peak - P
        if gain >= P * arm / 100 and j > bi:
            x = min(peak - g * gain, bars[j - 1]["c"] * 0.9999)
            if b["l"] <= x:
                if FIX:
                    # [fix 10/23] the hold for bar j is known at its open (finished bars); while held, the "never under the
                    # buy" guard sells when the price reaches the buy (or at the open if it opened under it)
                    if hold(v, bars, mins, fl, j, peak, P) and (b["o"] > P or not BE):
                        held += 1
                        if BE and b["l"] <= P:
                            return min(P, b["o"]) / P * 100 - 100 - COST, "held -> buy price", j
                    else:
                        return min(x, b["o"]) / P * 100 - 100 - COST, ("give-back" if not held else "released"), j
                elif b["l"] > P and hold(v, bars, mins, fl, j, peak, P):
                    held += 1
                else:
                    return min(x, b["o"]) / P * 100 - 100 - COST, ("give-back" if not held else "released"), j
        peak = max(peak, b["h"])
        if mins[j] >= 385:
            return b["c"] / P * 100 - 100 - COST, "15:55", j
    return bars[-1]["c"] / P * 100 - 100 - COST, "end", len(bars) - 1


def main():
    trig = defaultdict(list)
    for l in open("/var/www/screener/trade/reports/playbook/giveback_2y.jsonl"):
        t = json.loads(l)
        if t["setup"] in ("R1A", "R1B", "R4") or (t["setup"] == "R3" and 6 <= t["atr"] < 10):
            trig[t["day"]].append(t)
    rows = []
    for day in sorted(trig):
        d = json.load(gzip.open(R.OW / "bars" / f"bars_{day}.json.gz", "rt"))
        for t in trig[day]:
            s = t["sym"]; ref = R.REF.load(day, s); fl = json.loads((R.OW / "flow" / day / f"{s}.json").read_text())
            bars = [b for b in R.bars_of(d["symbols"][s]["bars"]) if 0 <= R.minute(b["t"]) < 390]
            mins = [R.minute(b["t"]) for b in bars]
            ti = next((i for i, m in enumerate(mins) if m == t["m"]), None)
            if ti is None or ti + 1 >= len(bars):
                continue
            bi = ti + 1; P = bars[bi]["o"]
            ok, info = MODS[t["setup"]].check(bars[:ti + 1], fl, ref, t["m"])
            sup = (info or {}).get("support") or P * 0.99
            stop = min(max(sup * 0.998, P * 0.96), P * 0.995)
            m0 = t["m"]; liq = sum(fl["buy"][max(0, m0 - 14):m0 + 1]) + sum(fl["sell"][max(0, m0 - 14):m0 + 1])
            r = {"setup": t["setup"], "day": day, "sym": s, "m": m0, "liq": liq / 15}
            for v in VARS:
                pl, why, j = sim(v, bars, mins, fl, bi, P, stop)
                r[v] = pl; r[v + "_why"] = why; r[v + "_hold"] = mins[j] - mins[bi]
            rows.append(r)
        R.REF._CACHE.clear(); del d
    json.dump(rows, open(HERE / ("e2_test.json" if not FIX else f"e2_fix{'' if BE else '_nobe'}.json"), "w"))
    def block(lab, X):
        y = np.array([r["day"] < SPLIT for r in X])
        print(f" {lab} ({len(X)})")
        for v in VARS:
            a = np.array([r[v] for r in X])
            print(f"   {v:5} {a.mean():+.3f}% ({a[y].mean() if y.any() else 0:+.3f} / {a[~y].mean() if (~y).any() else 0:+.3f})  "
                  f"win {np.mean(a > 0):.0%}  hold {np.median([r[v + '_hold'] for r in X]):.0f} min  total {a.sum():+.1f}%")
    print(f"{len(rows)} EV3 trades, per trade after {COST}% cost: all (year 1 / year 2)")
    block("ALL", rows)
    for s in ["R1A", "R1B", "R3", "R4"]:
        block(s, [r for r in rows if r["setup"] == s])
    block("THIN < 2,000 sh/min at the buy", [r for r in rows if r["liq"] < 2000])
    block("LIQUID >= 2,000 sh/min", [r for r in rows if r["liq"] >= 2000])
    for sym, day in (("DERM", "2024-11-05"), ("JOBY", "2024-10-23")):
        for r in rows:
            if r["sym"] == sym and r["day"] == day:
                print(f"  {sym} {day} m{r['m']} liq {r['liq']:.0f}/min:", {v: (round(r[v], 2), r[v + "_why"], r[v + "_hold"]) for v in VARS})
    print("E2_DONE")


if __name__ == "__main__":
    main()
