"""
ev3.py -- [2026-10-23] ENTRY VERSION 3 (EV3) for the trade bot, the user's decision: "retire the trade bot for now and replace it
with EV3". Live from Monday 10/26 (paper). config.json -> smart_engine.entry_mode "ev3" + section "ev3". Revert: entry_mode "v2".
ENTRIES = the user's chart setups (built from the practice charts, tested on 2 years with the give-back exit):
  R3   opening push -> first pullback holds -> turn      ONLY stocks with daily ATR 6-10% (2y: +0.153%/trade, y1 +0.167 y2 +0.142)
  R1A  fast double bottom after a flush                  (2y: +0.076%/trade, +0.067 / +0.085)
  R4   runner's second leg after the base                (2y: +0.129%/trade, +0.002 / +0.201)
  R1B  undercut & reclaim after a flush (PEPG deep V)    (rare: 24 trades in 2 years, mixed -- included for data)
  R1D  spring: the flush bar itself, buyers absorbing it (CERT 2/27/25) -- added 10/23 night, setting undercut 0.3%, volume 3x,
       buyers 45%; EXEMPT from the N2+N3 gate (2y C3: +0.328%/trade, y1 +0.355 / y2 +0.312, 61 trades; gated it lost)
  R1E  selling exhaustion at the low (RKT 2/28/25) -- added 10/23 night, setting 8 bars, 15-min buyers +10 points, 1.5x
       volume (2y C3: +0.786%/trade, y1 +0.717 / y2 +0.827, only 16 trades)
  R7 is left out (to be improved); R5 / R8 / R9 / R1 / R2 / R6 lost with every exit.
  Each setup is checked once per FINISHED minute on the stream's bars and per-minute buy/sell flow (the same code the 2-year
  test ran: r1a_setup / r1b_setup / r3_setup / r4_setup), with its own cool-down per stock.
STOP = under the setup's own support (support x (1 - stop_buf)), clamped to stop_min_pct..stop_max_pct under the price.
EXIT = the 1/4 GIVE-BACK: nothing but the stop until the peak since the buy is >= arm_pct above the buy; then sell when the
  price falls to peak - giveback x (peak - buy) while it is falling (price under the last finished close). 15:55 by the bot.
NO-TRADE gate (2026-10-23, the user after QBTS 2/24/25; tested on the 833 2-year EV3 buys, live give-back exit: -0.052%/trade
  -> +0.018%, y1 +0.016 / y2 +0.020, 535 kept; the 298 blocked averaged -0.178%): a setup's buy is NOT taken when
    N2  the trigger bar closes under the previous day's low, or
    N3  the 15-min buy share (trigger minute + 14 before) is under 45%.
  A blocked trigger still starts that setup's cool-down (as in the test).
E1 (2026-10-23, the user's sell block from JOBY 10/23/24 + DERM 11/5/24; tested as "E2et" on the 833 2-year EV3 buys:
  give-back alone -0.052%/trade -> +0.096%, y1 +0.227 / y2 -0.009): when the give-back fires, E1 OVERRIDES it (holds) while
    buyers are in control: 5-min buy share >= 50% AND NOT the last 2 finished minutes both "seller minutes" (1-min buy share
    < 40% on volume >= 0.5x the 15-bar average -- a lone small print is not a seller minute), OR
    the dip is noise: peak - last finished close < 2 x the mean 1-min range of the last 15 bars.
  Re-checked every finished minute; when E1 stops holding and the give-back is still true -> sell. Never overrides the stop;
  never holds once the price is at/under the buy price. Reads finished bars + per-minute flow only.
C3 EXIT CONTROLLER (2026-10-23, the user: TTGT 6/4/25 X1 right +11.7% vs GB2 +2.9%; SVIA 6/16/25 GB2 right +3.4% vs X1 -0.1%;
  2y no look-ahead on the 535 N2+N3-kept buys: GB2 +0.108%/trade -> C3 +0.166%, y1 +0.345 / y2 +0.022; X1 took control 37x).
  When the GB2 give-back first wants to sell, X1 (exit_x1.py: stall at the top / fade into resistance / break-even after +2%)
  TAKES CONTROL instead when the gain was made BY BUYERS (finished bars only): buy share of all minutes since the buy >= 55%,
  the 15-min buy share higher than before the buy, no single bar made > 60% of the peak gain, and the dip bar's volume <= its
  15-bar average. From then on only X1 (checked once per finished minute) or the stop sells.
OPTION 3 (2026-10-25, the user): only R3 BUYS, with the per-setup gates "gates": ["A3", "A20"] (A3 = RVOL so far < 1.5x the
  14-day volume by minute; A20 = opened 0.5-3% under the previous close) -- the only combination positive in BOTH 2016-18 (out of
  sample, +4.7%) and 2024-26 (+8.1%). R1A R1D R1E R4 are "record_only": their buys are logged ([EV3] RECORD ONLY), not taken,
  and they do not take the minute from R3.
Pure logic + per-symbol state; no orders (monitor.py / position_manager.py place them).
"""
import logging
from datetime import datetime, timezone
from zoneinfo import ZoneInfo

import r1a_setup
import r1b_setup
import r3_setup
import r4_setup
import r1d_setup
import r1e_setup
import exit_x1

log = logging.getLogger("trade_bot.ev3")
ET = ZoneInfo("America/New_York")
MODS = {"R1A": r1a_setup, "R1B": r1b_setup, "R3": r3_setup, "R4": r4_setup, "R1D": r1d_setup, "R1E": r1e_setup}
CONTROL_KEYS = ("enabled", "atr_min", "atr_max", "no_trade_exempt", "_note", "gates", "record_only")
DEFAULTS = {
    "setups": {"R1A": {}, "R1B": {}, "R4": {}, "R3": {"atr_min": 6.0, "atr_max": 10.0},
               "R1D": {"undercut_pct": 0.3, "heavy_x": 3.0, "absorb_bs": 0.45, "no_trade_exempt": True},
               "R1E": {"bleed_n": 8, "div_up": 0.10, "bleed_x": 1.5}},
    "order": ["R1A", "R1B", "R1D", "R1E", "R4", "R3"],
    "arm_pct": 1.0, "giveback": 0.25, "stop_buf": 0.002, "stop_min_pct": 0.5, "stop_max_pct": 4.0,
    "last_entry_minute": 360,
    "controller": {"enabled": True, "run_bs": 0.55, "spike_max": 0.60, "rising15": True, "light_dip": True},
    "no_trade": {"enabled": True, "below_prev_low": True, "min_bs15": 0.45},
    "e1": {"enabled": True, "bs5_min": 0.50, "seller_bs": 0.40, "thin_x": 0.5, "noise_k": 2.0},
}


def minute_of(t):
    if not isinstance(t, datetime):
        t = datetime.fromtimestamp(t, timezone.utc)
    t = t.astimezone(ET)
    return (t.hour - 9) * 60 + t.minute - 30


class EV3:
    def __init__(self, cfg=None):
        c = {**DEFAULTS, **(cfg or {})}
        c["setups"] = {**DEFAULTS["setups"], **((cfg or {}).get("setups") or {})}
        c["e1"] = {**DEFAULTS["e1"], **((cfg or {}).get("e1") or {})}
        c["no_trade"] = {**DEFAULTS["no_trade"], **((cfg or {}).get("no_trade") or {})}
        c["controller"] = {**DEFAULTS["controller"], **((cfg or {}).get("controller") or {})}
        self.c = c
        self._last_m = {}         # symbol -> last finished minute checked
        self._last_trig = {}      # (symbol, setup) -> minute of the last trigger (cool-down)
        self._peak = {}           # symbol -> {"entry_time", "peak"}
        self._e1_last = {}        # symbol -> (minute, hold?) last E1 log

    # ------------------------------------------------------------------ entries
    def entry_check(self, symbol, bars, min_flow, ref):
        """bars: the stream's bars (last one forming); min_flow: {minute bucket datetime: [buy, sell]};
        ref: reference.load(). -> None or {"setup", "info", "stop", "price", "m"} once per finished minute."""
        if not ref or not ref.get("atr14") or not ref.get("prev_close"):
            return None
        done = [b for b in bars[:-1] if 0 <= minute_of(b["t"]) < 390]
        if len(done) < 5:
            return None
        m = minute_of(done[-1]["t"])
        if self._last_m.get(symbol) == m or m >= self.c["last_entry_minute"]:
            return None
        self._last_m[symbol] = m
        fb, fs = [0.0] * 390, [0.0] * 390
        for t, v in min_flow:
            k = minute_of(t)
            if 0 <= k < 390:
                fb[k] += v[0]; fs[k] += v[1]
        flow = {"buy": fb, "sell": fs}
        atrp = ref["atr14"] / ref["prev_close"] * 100
        for name in self.c["order"]:
            sc = self.c["setups"].get(name)
            if sc is None or sc.get("enabled") is False:
                continue
            if "atr_min" in sc and atrp < sc["atr_min"]:
                continue
            if "atr_max" in sc and atrp >= sc["atr_max"]:
                continue
            mod = MODS[name]
            if m - self._last_trig.get((symbol, name), -9999) < mod.DEFAULTS.get("cooldown", 15):
                continue
            try:
                ok, info = mod.check(done, flow, ref, m, {k: v for k, v in sc.items() if k not in CONTROL_KEYS} or None)
            except Exception as e:
                log.warning(f"[EV3] {symbol} {name} check failed: {e}")
                continue
            if ok:
                self._last_trig[(symbol, name)] = m
                P = done[-1]["c"]
                nt = [] if sc.get("no_trade_exempt") else self.no_trade(P, flow, ref, m)
                nt += self.setup_gates(sc.get("gates") or [], done, ref, m)
                if sc.get("record_only"):
                    log.info(f"[EV3] RECORD ONLY {symbol}: {name} would {'be blocked (' + '; '.join(nt) + ')' if nt else 'buy'} at the "
                             f"{(m + 30) // 60 + 9}:{(m + 30) % 60:02d} bar close {P:.4g} -- not traded")
                    continue
                if nt:
                    log.info(f"[EV3] NO-TRADE {symbol}: {name} triggered at the {(m + 30) // 60 + 9}:{(m + 30) % 60:02d} bar "
                             f"close {P:.4g} -- blocked: {'; '.join(nt)}")
                    continue
                sup = info.get("stop") or (info.get("support") or P * 0.99) * (1 - self.c["stop_buf"])
                stop = min(max(sup, P * (1 - self.c["stop_max_pct"] / 100)), P * (1 - self.c["stop_min_pct"] / 100))
                return {"setup": name, "info": info, "stop": round(stop, 4), "price": P, "m": m, "atr": round(atrp, 1)}
        return None

    @staticmethod
    def setup_gates(gates, done, ref, m):
        """[2026-10-25] per-setup do-not-trade gates -> reasons. A3: RVOL so far < 1.5x; A20: gap-down open 0.5-3%."""
        out = []
        reg = [b for b in done if 0 <= minute_of(b["t"]) < 390]
        if "A3" in gates:
            vpm = ref.get("vol_per_min") or []
            norm = sum(vpm[:m + 1]) if len(vpm) > m else 0
            rv = sum(b["v"] for b in reg) / norm if norm else None
            if rv is not None and rv < 1.5:
                out.append(f"A3 RVOL so far {rv:.2f}x < 1.5x")
        if "A20" in gates and reg and ref.get("prev_close"):
            gap = (reg[0]["o"] / ref["prev_close"] - 1) * 100
            if -3 <= gap <= -0.5:
                out.append(f"A20 gap-down open {gap:+.1f}% (0.5-3% under the previous close)")
        return out

    def no_trade(self, P, flow, ref, m):
        """N2 / N3 -> list of reasons (empty = trade allowed)."""
        g = self.c["no_trade"]
        if not g.get("enabled"):
            return []
        out = []
        if g.get("below_prev_low") and ref.get("prev_low") and P < ref["prev_low"]:
            out.append(f"N2 under the previous day's low {ref['prev_low']:.4g}")
        B = sum(flow["buy"][max(0, m - 14):m + 1]); S = sum(flow["sell"][max(0, m - 14):m + 1])
        bs15 = B / (B + S) if B + S else 0.5
        if g.get("min_bs15") is not None and bs15 < g["min_bs15"]:
            out.append(f"N3 15-min buy share {bs15:.0%} < {g['min_bs15']:.0%}")
        return out

    # ------------------------------------------------------------------ exits
    def e1_hold(self, bars_done, min_flow, peak):
        """E1: (hold?, why) from the FINISHED regular-session bars + per-minute flow."""
        e = self.c["e1"]
        done = [b for b in bars_done if 0 <= minute_of(b["t"]) < 390]
        if not e.get("enabled") or len(done) < 3:
            return False, "E1 off"
        fb, fs = [0.0] * 390, [0.0] * 390
        for t, v in min_flow:
            k_ = minute_of(t)
            if 0 <= k_ < 390:
                fb[k_] += v[0]; fs[k_] += v[1]
        def bs(m, n):
            B = sum(fb[max(0, m - n + 1):m + 1]); S = sum(fs[max(0, m - n + 1):m + 1])
            return B / (B + S) if B + S else 0.5
        def avgv(i):
            w = done[max(0, i - 15):i]
            return sum(x["v"] for x in w) / len(w) if w else 0
        def seller(i):
            return bs(minute_of(done[i]["t"]), 1) < e["seller_bs"] and done[i]["v"] >= e["thin_x"] * avgv(i)
        k = len(done) - 1; m = minute_of(done[k]["t"])
        b5 = bs(m, 5); two = seller(k) and seller(k - 1)
        rng = sum(x["h"] - x["l"] for x in done[-15:]) / len(done[-15:])
        dip = peak - done[k]["c"]
        if b5 >= e["bs5_min"] and not two:
            return True, f"buyers in control: 5-min buy share {b5:.0%}, no 2 seller minutes in a row"
        if dip < e["noise_k"] * rng:
            return True, f"noise: dip {dip:.4g} < {e['noise_k']:g} x 1-min range {rng:.4g}"
        return False, (f"buyers gone: 5-min buy share {b5:.0%}" + (", 2 seller minutes in a row" if two else "")
                       + f"; dip {dip:.4g} >= {e['noise_k']:g} x range {rng:.4g}")

    @staticmethod
    def _flow_arrays(min_flow):
        fb, fs = [0.0] * 390, [0.0] * 390
        for t, v in min_flow:
            k_ = minute_of(t)
            if 0 <= k_ < 390:
                fb[k_] += v[0]; fs[k_] += v[1]
        return {"buy": fb, "sell": fs}

    def c3_controls(self, reg, flow, entry, bi, peak):
        """C3: does X1 take control? reg = finished regular-session bars, bi = index of the buy bar. -> (bool, why)"""
        g = self.c["controller"]
        k = len(reg) - 1
        if bi < 1 or k <= bi:
            return False, "too early"
        mins = [minute_of(b["t"]) for b in reg]
        def bs(m0, m1):
            B = sum(flow["buy"][max(0, m0):m1 + 1]); S = sum(flow["sell"][max(0, m0):m1 + 1])
            return B / (B + S) if B + S else 0.5
        run = bs(mins[bi], mins[k])
        if run < g["run_bs"]:
            return False, f"run buy share {run:.0%} < {g['run_bs']:.0%}"
        b15, b15_0 = bs(mins[k] - 14, mins[k]), bs(mins[bi - 1] - 14, mins[bi - 1])
        if g.get("rising15") and not b15 > b15_0:
            return False, f"15-min buy share not rising ({b15_0:.0%} -> {b15:.0%})"
        ups = [reg[i]["c"] - reg[i - 1]["c"] for i in range(bi + 1, k + 1)] + [reg[bi]["c"] - entry]
        if peak - entry <= 0 or max(ups) > g["spike_max"] * (peak - entry):
            return False, f"a spike: one bar made {max(ups) / (peak - entry) if peak > entry else 1:.0%} of the gain"
        w = reg[max(0, k - 15):k]
        if g.get("light_dip") and w and reg[k]["v"] > sum(x["v"] for x in w) / len(w):
            return False, "the dip bar is heavy"
        return True, (f"buyers made the run: {run:.0%} buy share since the buy, 15-min {b15_0:.0%} -> {b15:.0%}, "
                      f"biggest bar {max(ups) / (peak - entry):.0%} of the gain, light dip")

    def exit_check(self, symbol, pos, bars, price, min_flow=()):
        """-> (should_exit, reason, is_risk_exit). pos: position_manager position (entry_price, stop_price, entry_time)."""
        entry, stop = pos["entry_price"], pos.get("stop_price") or pos["entry_price"] * 0.99
        st = self._peak.get(symbol)
        if not st or st.get("entry_time") != pos.get("entry_time"):
            st = self._peak[symbol] = {"entry_time": pos.get("entry_time"), "peak": entry}
        et = datetime.fromisoformat(pos["entry_time"]) if pos.get("entry_time") else None
        done = bars[:-1]
        since = [b for b in done if et is None or b["t"] >= et.replace(second=0, microsecond=0)]
        if since:
            st["peak"] = max(st["peak"], max(b["h"] for b in since))
        if price <= stop:
            return True, f"EV3 stop: {price:.4g} <= {stop:.4g} (under the {pos.get('ev3_setup', 'setup')} support)", True
        if st.get("mode") == "X1":
            # [C3] X1 has control: checked once per finished minute; sells at the next price
            reg = [b for b in done if 0 <= minute_of(b["t"]) < 390]
            if reg and st.get("x1_m") != minute_of(reg[-1]["t"]):
                st["x1_m"] = minute_of(reg[-1]["t"])
                bi = next((i for i, b in enumerate(reg) if et is None or b["t"] >= et.replace(second=0, microsecond=0)), None)
                if bi is not None:
                    sell, why = exit_x1.check(reg, self._flow_arrays(min_flow), entry, bi)
                    if sell:
                        return True, f"EV3 {why} (X1 had control since {st.get('x1_since')})", why.startswith("X1 break-even")
            st["peak"] = max(st["peak"], price)
            return False, "", False
        gain = st["peak"] - entry
        if gain >= entry * self.c["arm_pct"] / 100 and done:
            lvl = st["peak"] - self.c["giveback"] * gain
            if price <= lvl and price < done[-1]["c"]:
                gb = (f"EV3 give-back: peak {st['peak']:.4g} (+{gain / entry * 100:.2f}%), "
                      f"price {price:.4g} <= {lvl:.4g} (1/{round(1 / self.c['giveback'])} given back)")
                if self.c["controller"].get("enabled"):
                    try:
                        reg = [b for b in done if 0 <= minute_of(b["t"]) < 390]
                        bi = next((i for i, b in enumerate(reg) if et is None or b["t"] >= et.replace(second=0, microsecond=0)), None)
                        took, cwhy = self.c3_controls(reg, self._flow_arrays(min_flow), entry, bi, st["peak"]) if bi is not None else (False, "")
                    except Exception as ex:
                        took, cwhy = False, f"C3 failed ({ex})"
                    if took:
                        st["mode"] = "X1"; st["x1_since"] = f"{reg[-1]['t'].astimezone(ET):%H:%M}"
                        st["x1_m"] = minute_of(reg[-1]["t"])
                        log.info(f"[EV3] C3: X1 TAKES CONTROL of {symbol} @ {price} instead of the give-back ({lvl:.4g}) -- {cwhy}")
                        return False, "", False
                if price > entry:
                    try:
                        h, why = self.e1_hold(done, min_flow, st["peak"])
                    except Exception as ex:
                        h, why = False, f"E1 failed ({ex})"
                    mk = minute_of(done[-1]["t"])
                    if h:
                        if self._e1_last.get(symbol) != (mk, True):
                            self._e1_last[symbol] = (mk, True)
                            log.info(f"[EV3] E1 HOLD {symbol} @ {price}: give-back level {lvl:.4g} touched, held -- {why}")
                        return False, "", False
                    if self._e1_last.get(symbol, (None, False))[1]:
                        gb = gb.replace("EV3 give-back", "EV3 give-back after E1 released") + f" | {why}"
                    self._e1_last.pop(symbol, None)
                return True, gb, False
        st["peak"] = max(st["peak"], price)
        return False, "", False

    def forget(self, symbol):
        self._peak.pop(symbol, None)
        self._e1_last.pop(symbol, None)
