"""
deepv_count.py -- [2026-10-10] The user: how many deep Vs per day over the last 2 years?
Deep V = the user's rule (analyze_trades.deep_v_entry, 10/6): the DEEPEST V of the day from 9:45 -- a low after a
>= 1% drop from the 30-bar high, a >= 1% rebound within 20 bars without breaking the low, entry at the turn bar
(first close above the V bar's high, within 30 min), right sell = the highest close before the low breaks.
This is a HINDSIGHT count (the day's deepest V is only known at the close) -- it measures how often the pattern
exists, not whether it can be traded live (deep_v_study.py, fixed 10/10: no edge when bought causally).
Universe: every $5-15 stock with >= 500k shares (bars10y) and the scanner-like v3 top 30.
Output: deepv_count.jsonl (one row per stock-day with a deep V) + summary.
    python3 deepv_count.py [START] [END]      default 2024-10-10 .. 2026-10-09
"""
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"
sys.path.insert(0, str(HERE)); sys.path.insert(0, str(OW))
from analyze_trades import deep_v_entry  # noqa: E402
from up2_styles_build import v3_top30, arrays  # noqa: E402

OUT = HERE / "deepv_count.jsonl"
hm = lambda m: f"{9 + (30 + m) // 60}:{(30 + m) % 60:02d}"


def main(start="2024-10-10", end="2026-10-09"):
    files = [f for f in sorted((OW / "bars10y").glob("*/*.json.gz")) if start <= f.name[:10] <= end]
    with open(OUT, "w") as fh:
        for f in files:
            day = f.name[:10]
            d = json.load(gzip.open(f, "rt"))
            top = v3_top30(day)
            n_all = n_dv = 0
            for s, bars in d["bars"].items():
                a = arrays(bars)
                if a is None:
                    continue
                n_all += 1
                O = float(a[0, 0])
                B = [{"m": m, "h": float(a[1, m]), "l": float(a[2, m]), "c": float(a[3, m])} for m in range(390)]
                dv = deep_v_entry(B, None)
                if not dv:
                    continue
                j, q, sl, g = dv
                prior = max(B[k]["h"] for k in range(max(0, j - 30), j))
                n_dv += 1
                fh.write(json.dumps({"day": day, "sym": s, "v3": s in top, "v_t": hm(j), "drop": round((prior / B[j]["l"] - 1) * 100, 2),
                                     "v_vs_open": round((B[j]["l"] / O - 1) * 100, 2), "turn_t": hm(q), "sell_t": hm(sl),
                                     "gain": round(g * 100, 2)}) + "\n")
            fh.write(json.dumps({"day": day, "_stocks": n_all, "_v3": len(top & set(d["bars"]))}) + "\n")
            del d
            print(day, n_all, n_dv, flush=True)
    report()


def report():
    R = [json.loads(l) for l in open(OUT)]
    meta = {r["day"]: r for r in R if "_stocks" in r}
    V = [r for r in R if "sym" in r]
    days = sorted(meta)
    print(f"\n{len(days)} days {days[0]}..{days[-1]}  (hindsight count, the user's deep-V rule, one = the day's deepest per stock)")
    for lab, sel, base in (("ALL $5-15 stocks", lambda r: True, "_stocks"), ("scanner-like v3 top 30", lambda r: r["v3"], "_v3")):
        per = defaultdict(lambda: defaultdict(int))
        for r in V:
            if sel(r):
                p = per[r["day"]]; p["any"] += 1
                p["drop>=2"] += r["drop"] >= 2; p["drop>=3"] += r["drop"] >= 3; p["drop>=5"] += r["drop"] >= 5
                p["gain>=2"] += r["gain"] >= 2; p["gain>=3"] += r["gain"] >= 3; p["gain>=5"] += r["gain"] >= 5
                p["drop>=3&gain>=3"] += r["drop"] >= 3 and r["gain"] >= 3
        tot = np.array([meta[d][base] for d in days], float)
        print(f"\n{lab}: {tot.mean():.0f} stocks/day on average")
        for k in ("any", "drop>=2", "drop>=3", "drop>=5", "gain>=2", "gain>=3", "gain>=5", "drop>=3&gain>=3"):
            x = np.array([per[d][k] for d in days], float)
            print(f"  {k:16} per day: mean {x.mean():6.1f}  median {np.median(x):5.0f}  min {x.min():4.0f}  max {x.max():5.0f}"
                  f"  | {x.sum() / tot.sum():5.1%} of stock-days")
        X = [r for r in V if sel(r)]
        vt = defaultdict(int)
        for r in X:
            h = int(r["v_t"].split(":")[0]); vt["9:45-11" if h < 11 else "11-13" if h < 13 else "13-15:15"] += 1
        print(f"  V low time: " + ", ".join(f"{k} {v / len(X):.0%}" for k, v in sorted(vt.items())))
        print(f"  median drop into the V {np.median([r['drop'] for r in X]):.2f}%, median gain turn -> right sell "
              f"{np.median([r['gain'] for r in X]):.2f}%, median V low vs open {np.median([r['v_vs_open'] for r in X]):+.2f}%")
        mo = defaultdict(list)
        for d in days:
            mo[d[:7]].append(per[d]["any"])
        print("  per month (mean deep Vs/day): " + "  ".join(f"{m} {np.mean(v):.0f}" for m, v in sorted(mo.items())))


if __name__ == "__main__":
    report() if "--report" in sys.argv else main(*sys.argv[1:3])
