"""
situation_map.py -- [2026-10-21] DYNAMIC EV3, STEP 1 (measurement only, no rules). The user: entry rules chosen by the
SITUATION of each stock at every minute bar; keep the situation list as proposed; let the measurements find the best
levels; use the avoid-rules list where it fits.
2 years, daily top-30 lists, every minute 9:45-15:00 (finished bars only). Each minute gets ONE situation (priority order):
  E after_flush   a sell flush (down bar, sellers > buyers, >= 3x the 15 bars before) in the last 15 min
  H falling       under VWAP and the 15-bar trend down (straightness < 0)
  F stretched     > 2% over VWAP or > 3% over the open
  D breakout      a new day high on the last bar with the last 3 bars' volume >= 1.5x normal
  G choppy        >= 3 Vs today and straightness < 0.3
  B absorption    above VWAP, 15-min buy share < 50% but higher lows and price holding (close >= 5 bars ago)
  C pullback      above VWAP, up over 30 min, >= 0.5% off the 15-min high, last 5 bars' volume below the 10 before
  A steady_rise   above VWAP, straightness >= 0.5 and smoothness >= 0.7
  N other         none of the above
Overlays measured INSIDE every situation: the avoid rules (min_atr 8%, gap > 10%, after 14:30, vwap > 2%, open > 3%,
heavy volume 15-min > 3x, recent climax 10x / 30 min, net-selling drop, lower low by 10:30, >= 3 Vs, flat bars at the low,
thin < 1,000 sh/min) and EV3's "buy volume increasing" (last 3 bars' buying > the 3 before, buy share not falling).
OUTCOME from the NEXT bar's open (a buy there): bracket +1% / -1% within 60 min (first touch; neither = the 60-min
return); EV = bracket result - 0.2% cost. "first" = the first minute a stock ENTERS the situation (signal-like).
    nice -n 19 python3 situation_map.py   -> situation_map.log (+ situation_map.json)
"""
import gzip
import json
import sys
from collections import defaultdict
from pathlib import Path

import numpy as np

HERE = Path(__file__).resolve().parent
OW = HERE.parent / "open_window"
REFD = Path("/var/www/screener/trade/data/reference")
sys.path.insert(0, str(OW))
from up2_styles_build import arrays  # noqa: E402

SPLIT = "2025-10-01"
COST = 0.2
SIT = {"A": "steady rise", "B": "absorption", "C": "pullback in a rise", "D": "breakout", "E": "after a flush",
       "F": "stretched", "G": "choppy", "H": "falling", "N": "other"}
BINS = {
    "vwap %": ([-99, 0, 0.5, 1, 1.5, 2, 3, 99], "vw"),
    "over open %": ([-99, -2, 0, 1.5, 3, 99], "eo"),
    "buy share 15m": ([0, 0.45, 0.55, 0.65, 1.01], "bs"),
    "volume 15m x normal": ([0, 1, 2, 3, 5, 999], "vx"),
    "daily ATR %": ([0, 6, 8, 12, 999], "atr"),
    "time": ([15, 60, 150, 270, 331], "m"),
    "straightness": ([-9, 0, 0.3, 0.5, 0.7, 9], "eff"),
}
# acc[key] = [n, sum_ev, up_first, dn_first, sum_ret30]
acc = defaultdict(lambda: np.zeros(5))


def add(key, ev, first_flag, r30):
    a = acc[key]
    a[0] += 1; a[1] += ev; a[2] += first_flag == 1; a[3] += first_flag == -1; a[4] += r30


def roll(x, n):
    c = np.cumsum(np.concatenate([[0.0], x]))
    out = np.full(len(x), np.nan)
    out[n - 1:] = c[n:] - c[:-n]
    return out


def stock(O, H, L, C, V, bu, se, ref, y):
    n = 390
    atrp = ref["atr14"] / ref["prev_close"] * 100 if ref.get("atr14") and ref.get("prev_close") else np.nan
    gap = (O[0] / ref["prev_close"] - 1) * 100 if ref.get("prev_close") else 0.0
    vpm = np.array(ref.get("vol_per_min") or [np.nan] * n, float)[:n]
    tp = (H + L + C) / 3
    vw = np.cumsum(tp * V) / np.maximum(np.cumsum(V), 1e-9)
    b15, s15 = roll(bu, 15), roll(se, 15)
    bs15 = b15 / np.maximum(b15 + s15, 1e-9)
    b3, s3 = roll(bu, 3), roll(se, 3)
    bs3 = b3 / np.maximum(b3 + s3, 1e-9)
    v15 = roll(V, 15); n15 = roll(vpm, 15); vx = v15 / np.maximum(n15, 1)
    v3 = roll(V, 3); n3 = roll(vpm, 3)
    # signed R^2 and straightness of the last 15 closes
    x = np.arange(15, dtype=float); Sx, Sxx = x.sum(), (x * x).sum()
    Sy, Syy = roll(C, 15), roll(C * C, 15)
    Sxy = np.full(n, np.nan)
    for m in range(14, n):
        Sxy[m] = (x * C[m - 14:m + 1]).sum()
    num = 15 * Sxy - Sx * Sy
    den = (15 * Sxx - Sx * Sx) * (15 * Syy - Sy * Sy)
    r2 = np.where(den > 0, np.sign(num) * num * num / np.maximum(den, 1e-18), 0)
    path = roll(np.abs(np.diff(C, prepend=C[0])), 14)
    eff = np.full(n, np.nan); eff[14:] = (C[14:] - C[:-14]) / np.maximum(path[14:], 1e-9)
    hi_prev = np.maximum.accumulate(np.concatenate([[H[0]], H[:-1]]))
    # sell flushes (vs the 15 bars BEFORE: no look-ahead) and big-volume bars
    flush_m = np.full(n, -999); last = -999
    climax_m = np.full(n, -999); lc = -999
    for m in range(n):
        if m >= 15 and C[m] < O[m] and se[m] > bu[m] and V[m] >= 3 * max(V[m - 15:m].mean(), 1):
            last = m
        if vpm[m] == vpm[m] and V[m] >= 10 * max(vpm[m], 1):
            lc = m
        flush_m[m] = last; climax_m[m] = lc
    # V count (low of 7 bars, >= 1% under the prior 30-bar high, confirmed 3 bars later)
    vs = np.zeros(n, int); cnt = 0
    for m in range(n):
        j = m - 3
        if j >= 15 and L[j] <= L[j - 3:j + 4].min() and L[j - 1] != L[j] and H[max(0, j - 30):j].max() / L[j] - 1 >= 0.01:
            cnt += 1
        vs[m] = cnt
    flat = ((H == L) & (V > 0)).astype(float); flat5 = roll(flat, 5)
    low30 = np.array([L[max(0, m - 29):m + 1].min() for m in range(n)])
    ll1030 = L[30:60].min() < L[:30].min()
    prev_state = None
    for m in range(15, 331):
        P = O[m + 1]
        if not P > 0:
            continue
        # outcome
        e = min(m + 61, 389)
        hh, llw = H[m + 1:e + 1], L[m + 1:e + 1]
        iu = np.argmax(hh >= P * 1.01) if (hh >= P * 1.01).any() else 999
        idn = np.argmax(llw <= P * 0.99) if (llw <= P * 0.99).any() else 999
        ff = 1 if iu < idn else -1 if idn < iu else 0
        if iu == idn and iu != 999:
            ff = -1                                    # same bar: assume the stop first
        br = 1.0 if ff == 1 else -1.0 if ff == -1 else (C[e] / P - 1) * 100
        evv = br - COST
        r30 = (C[min(m + 30, 389)] / P - 1) * 100
        # features
        vwd = (C[m] / vw[m] - 1) * 100; eo = (C[m] / O[0] - 1) * 100
        hl = L[m - 4:m + 1].min() > L[m - 9:m - 4].min()
        hold = C[m] >= C[m - 5]
        # situation
        if m - flush_m[m] <= 15:
            st = "E"
        elif vwd < 0 and eff[m] < 0:
            st = "H"
        elif vwd > 2 or eo > 3:
            st = "F"
        elif C[m] > hi_prev[m] and n3[m] > 0 and v3[m] >= 1.5 * n3[m]:
            st = "D"
        elif vs[m] >= 3 and eff[m] < 0.3:
            st = "G"
        elif vwd >= 0 and bs15[m] < 0.5 and hl and hold:
            st = "B"
        elif vwd >= 0 and C[m] > C[m - 30] and H[m - 14:m + 1].max() / C[m] - 1 >= 0.005 and V[m - 4:m + 1].sum() < V[m - 14:m - 4].sum() / 2:
            st = "C"
        elif vwd >= 0 and eff[m] >= 0.5 and r2[m] >= 0.7:
            st = "A"
        else:
            st = "N"
        first = st != prev_state
        prev_state = st
        ev3 = b3[m] > b3[m - 3] and bs3[m] >= bs3[m - 3]
        feat = {"vw": vwd, "eo": eo, "bs": bs15[m], "vx": vx[m], "atr": atrp, "m": m, "eff": eff[m]}
        av = {"min_atr <8%": atrp < 8, "max_gap >10%": gap > 10, "after 14:30": m >= 300, "vwap >2%": vwd > 2,
              "open >3%": eo > 3, "heavy volume >3x": vx[m] > 3, "recent climax 10x": m - climax_m[m] <= 30,
              "net-selling drop": (C[m] / C[m - 15] - 1) * 100 <= -1 and bs15[m] < 0.5,
              "lower low by 10:30": m >= 60 and ll1030, ">= 3 Vs": vs[m] >= 3,
              "flat bars at low": flat5[m] >= 2 and C[m] <= low30[m] * 1.01,
              "thin <1000 sh/min": np.median(V[m - 14:m + 1]) < 1000}
        for kind in (("all",) + (("first",) if first else ())):
            add((st, kind, "ALL", "", y), evv, ff, r30)
            add(("ALL", kind, "ALL", "", y), evv, ff, r30)
            add((st, kind, "EV3 buy volume increasing", "yes" if ev3 else "no", y), evv, ff, r30)
            for nm, (edges, k) in BINS.items():
                v = feat[k]
                if v == v:
                    i = int(np.searchsorted(edges, v, side="right")) - 1
                    if 0 <= i < len(edges) - 1:
                        add((st, kind, nm, f"{edges[i]:g}..{edges[i + 1]:g}", y), evv, ff, r30)
            for nm, hit in av.items():
                add((st, kind, "avoid: " + nm, "HIT" if hit else "clear", y), evv, ff, r30)


def main():
    nsd = 0
    for p in sorted(OW.glob("bars/bars_20*.json.gz")):
        day = p.name[5:15]
        if day > "2026-10-02":
            continue
        d = json.load(gzip.open(p, "rt")); y = 1 if day < SPLIT else 2
        for s in d["top30_current"]:
            fp, rp = OW / "flow" / day / f"{s}.json", REFD / day / f"{s}.json"
            if s not in d["symbols"] or not fp.exists() or not rp.exists():
                continue
            a = arrays(d["symbols"][s]["bars"])
            if a is None:
                continue
            fl = json.loads(fp.read_text())
            try:
                stock(*a, np.array(fl["buy"], float), np.array(fl["sell"], float), json.loads(rp.read_text()), y)
                nsd += 1
            except Exception as e:
                print(day, s, "error", e, flush=True)
        del d
        print(day, nsd, "stock-days", flush=True)
    json.dump({"|".join(map(str, k)): v.tolist() for k, v in acc.items()}, open(HERE / "situation_map.json", "w"))
    report()


def report():
    A = {tuple(k.split("|")): np.array(v) for k, v in json.load(open(HERE / "situation_map.json")).items()}
    g = lambda *k: A.get(tuple(map(str, k)), np.zeros(5))

    def cell(a):
        return f"{a[1] / a[0]:+.3f}%" if a[0] >= 30 else "   -   "

    def row(lab, k1, k2):
        a1, a2 = k1, k2; t = a1 + a2
        if t[0] < 30:
            return
        print(f"   {lab:34} {int(t[0]):8} | EV {t[1] / t[0]:+.3f}% (y1 {cell(a1)} y2 {cell(a2)}) | +1% first {t[2] / t[0]:4.0%} "
              f"-1% first {t[3] / t[0]:4.0%} | 30m {t[4] / t[0]:+.3f}%")
    for kind in ("first", "all"):
        print(f"\n===== {'FIRST minute a stock enters the situation (signal-like)' if kind == 'first' else 'EVERY minute in the situation'} "
              f"===== EV = +1%/-1% bracket (60 min) after {COST}% cost")
        row("BASELINE all minutes", g("ALL", kind, "ALL", "", 1), g("ALL", kind, "ALL", "", 2))
        for s, nm in SIT.items():
            row(f"{s} {nm}", g(s, kind, "ALL", "", 1), g(s, kind, "ALL", "", 2))
    for s, nm in SIT.items():
        print(f"\n----- {s} {nm} (first minute in the situation) -----")
        row("all", g(s, "first", "ALL", "", 1), g(s, "first", "ALL", "", 2))
        for f in ["EV3 buy volume increasing"] + list(BINS):
            vals = sorted({k[3] for k in A if k[0] == s and k[1] == "first" and k[2] == f},
                          key=lambda v: (v not in ("yes", "no"), float(v.split("..")[0]) if ".." in v else 0))
            for v in vals:
                row(f"{f}: {v}", g(s, "first", f, v, 1), g(s, "first", f, v, 2))
        print("   avoid rules inside this situation (HIT = the rule would block):")
        for f in sorted({k[2] for k in A if k[0] == s and k[1] == "first" and k[2].startswith("avoid: ")}):
            row(f"{f[7:]} HIT", g(s, "first", f, "HIT", 1), g(s, "first", f, "HIT", 2))
    print("SITMAP_DONE")


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