"""
deep_v_study.py -- [2026-10-07] The user's DEEP-V REVERSAL entry (BETR 10/7, SMR/NOMD/BHC 10/6): "it is going up after a
V -- the bot should recognize that". Buy right after a deep V, as soon as the price STARTS TO SLOPE UP.
Data: reports/open_window/bars10y (every $5-15 stock with >= 500k shares that day, 1-min bars, 2022-2026+).
  DEEP V   a bar whose low is the lowest of the 3 bars on each side, after a drop of >= DROP % from the highest high of
           the previous 30 bars, from 9:45 (minute 15) to 15:15
  ENTRY    the first of the next 5 bars that closes above the previous close AND above the V bar's close, low not broken
           -> buy at that bar's close (the user: "right after, when it starts to slope up")
  STOP     the V low - 0.1%
  EXITS    a) 2R target / stop / 60-minute limit   b) hold to 15:55 with the stop
  LUCK     a random other stock of the same day bought at the same minute with the same % risk
  FILTERS  drop 2 / 3 / 5 %; volatile stocks only (daily ATR >= 5%, like the scanner's picks); volume drying up at the low
           (V bar volume < average of the 10 bars before); morning only (V before 11:00)
One signal per stock per day (the first). 0.1% cost per trade. Results per year.
    python3 deep_v_study.py
"""
import gzip
import glob
import json
import random
from collections import defaultdict
from datetime import datetime, timezone
from zoneinfo import ZoneInfo

import numpy as np

ET = ZoneInfo("America/New_York")
COST = 0.1
random.seed(5)


def day_arrays(d):
    syms, rows = [], []
    for s, bars in d["bars"].items():
        a = np.full((5, 390), np.nan)
        for t, o, h, l, c, v in bars:
            tt = datetime.fromtimestamp(t, timezone.utc).astimezone(ET); m = (tt.hour - 9) * 60 + tt.minute - 30
            if 0 <= m < 390:
                a[:, m] = (o, h, l, c, v)
        if np.isnan(a[3, 0]) or np.isnan(a[3]).sum() > 120:
            continue
        for m in range(1, 390):
            if np.isnan(a[3, m]):
                a[0:4, m] = a[3, m - 1]; a[4, m] = 0
        syms.append(s); rows.append(a)
    return syms, (np.array(rows) if rows else None)


def outcome(a, k, stop):
    O, H, L, C = a[0], a[1], a[2], a[3]
    e = C[k]; R = e - stop
    if R <= 0:
        return None
    pl2 = None
    for j in range(k + 1, min(k + 61, 386)):
        if L[j] <= stop:
            pl2 = (min(stop, O[j]) / e - 1) * 100; break
        if H[j] >= e + 2 * R:
            pl2 = (max(e + 2 * R, O[j]) / e - 1) * 100; break
    if pl2 is None:
        pl2 = (C[min(k + 60, 385)] / e - 1) * 100
    hold = None
    for j in range(k + 1, 386):
        if L[j] <= stop:
            hold = (min(stop, O[j]) / e - 1) * 100; break
    if hold is None:
        hold = (C[385] / e - 1) * 100
    r1 = r2 = False
    for j in range(k + 1, min(k + 61, 386)):
        if L[j] <= stop:
            break
        r1 |= H[j] >= e + R; r2 |= H[j] >= e + 2 * R
    return {"pl2R": pl2 - COST, "hold": hold - COST, "r1": r1, "r2": r2, "risk": R / e * 100}


def signals(A, drop):
    """first deep-V entry per stock: list of (stock index, V minute, entry minute)"""
    H, L, C = A[:, 1], A[:, 2], A[:, 3]
    n = len(A)
    out = {}
    hi30 = np.full_like(H, np.nan)
    for m in range(15, 346):
        hi30[:, m] = H[:, m - 30 if m >= 30 else 0:m].max(1)
    for m in range(15, 343):
        lo = L[:, m]
        locmin = np.all(lo[:, None] <= L[:, m - 3:m + 4], axis=1)
        deep = (hi30[:, m] / lo - 1) * 100 >= drop
        for i in np.where(locmin & deep)[0]:
            if i in out:
                continue
            for k in range(m + 1, min(m + 6, 386)):
                if L[i, k] < lo[i]:
                    break
                if C[i, k] > C[i, k - 1] and C[i, k] > C[i, m]:
                    out[i] = (m, k)
                    break
    return [(i, v, k) for i, (v, k) in out.items()]


def main():
    files = sorted(glob.glob("/var/www/screener/trade/reports/open_window/bars10y/*/*.json.gz"))
    hist = defaultdict(list)                       # symbol -> recent daily (h, l, c) for ATR
    res = defaultdict(list); luck = defaultdict(list)
    for f in files:
        d = json.load(gzip.open(f, "rt")); day = d["date"]; yr = day[:4]
        syms, A = day_arrays(d)
        daily = d.get("daily", {})
        atrp = {}
        for s in syms:
            hs = hist[s][-15:]
            if len(hs) >= 5:
                trs = [max(b[0] - b[1], abs(b[0] - p[2]), abs(b[1] - p[2])) for p, b in zip(hs, hs[1:])]
                atrp[s] = np.mean(trs) / hs[-1][2] * 100 if trs else None
        if A is not None:
            for drop in (2.0, 3.0, 5.0):
                for i, v, k in signals(A, drop):
                    if k > 345:
                        continue
                    a = A[i]; stop = a[2, v] * 0.999
                    r = outcome(a, k, stop)
                    if not r:
                        continue
                    vol_dry = a[4, v] < np.mean(a[4, max(0, v - 10):v]) if v >= 1 else False
                    vol_ok = (atrp.get(syms[i]) or 0) >= 5
                    keys = [f"drop {drop:.0f}%"]
                    if vol_ok: keys.append(f"drop {drop:.0f}% + volatile (ATR>=5%)")
                    if vol_dry: keys.append(f"drop {drop:.0f}% + volume dried up at the low")
                    if v < 90: keys.append(f"drop {drop:.0f}% + V before 11:00")
                    if vol_ok and vol_dry and v < 90: keys.append(f"drop {drop:.0f}% + volatile + dried up + before 11:00")
                    j = random.randrange(len(A))
                    rb = outcome(A[j], k, A[j][3, k] * (1 - r["risk"] / 100)) if j != i else None
                    for kk in keys:
                        res[kk].append({**r, "yr": yr})
                        if rb:
                            luck[kk].append({**rb, "yr": yr})
        for s in syms:
            if s in daily:
                o, h, l, c = daily[s][:4]
                hist[s].append((h, l, c)); hist[s] = hist[s][-20:]
        print(day, {k: len(v) for k, v in list(res.items())[:3]}, flush=True)
    yrs = sorted({r["yr"] for v in res.values() for r in v})
    f = lambda X, k: np.mean([x[k] for x in X]) if X else float("nan")
    print(f"\n{len(files)} days. Per trade after 0.1% cost; luck = a random stock at the same minute, same risk.")
    for kk in sorted(res):
        X, Lk = res[kk], luck[kk]
        print(f"\n{kk}: {len(X)} trades | +1R {f(X, 'r1'):.0%} (luck {f(Lk, 'r1'):.0%}) | +2R {f(X, 'r2'):.0%} ({f(Lk, 'r2'):.0%}) | "
              f"2R trade {f(X, 'pl2R'):+.3f}% (luck {f(Lk, 'pl2R'):+.3f}%) | hold to close {f(X, 'hold'):+.3f}% (luck {f(Lk, 'hold'):+.3f}%) | "
              f"risk {np.median([x['risk'] for x in X]):.2f}%")
        print("   by year (2R trade / hold):  " + "  ".join(
            f"{y}: {f([x for x in X if x['yr'] == y], 'pl2R'):+.3f}/{f([x for x in X if x['yr'] == y], 'hold'):+.3f} (n {sum(x['yr'] == y for x in X)})" for y in yrs))


if __name__ == "__main__":
    main()
