"""
v_flat_study.py -- [2026-10-11] The user: is there a standard CONSOLIDATION before the recovery -- the price goes flat
(open = high = low = close) for a couple of minute bars, then buy volume becomes higher than sell volume?
2 years with trade-by-trade buy/sell flow (reports/open_window/bars + flow, daily top 30), all values causal.
  flat bar     high - low < half a cent (open = high = low = close); split: WITH trades (volume > 0) / NO trades
  PART 1  every V with a >= 2% drop (deepv_outcomes.vs_of): in the bars from the low to the confirmation (low..low+3)
          and the 5 before the low -- is there a run of >= 2 flat bars, then a bar with buy > sell?
          -> reversal (>= 3% from the low) / kept down rates with vs without that pattern
  PART 2  a live-style signal: at minute m, the k bars before m are flat (k >= 2), the price is >= 2% under the day high
          and within 1% of the 30-bar low, and bar m has buy > sell -> buy at m's close, stop under the flat zone
          (and the bar m low) - 0.1%, target 2R, 60 min limit; at the real 0.2% cost.
          Controls: same situation but bar m buy <= sell; and no flat bars (any narrow-range pause not required)
    python3 v_flat_study.py
"""
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 deepv_outcomes import vs_of  # noqa: E402
from up2_styles_build import arrays  # noqa: E402

COST, SPLIT = 0.2, "2025-10-01"


def trade(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
    for j in range(k + 1, min(k + 61, 386)):
        if L[j] <= stop:
            return (min(stop, O[j]) / e - 1) * 100
        if H[j] >= e + 2 * R:
            return (max(e + 2 * R, O[j]) / e - 1) * 100
    return (C[min(k + 60, 385)] / e - 1) * 100


def main():
    P1 = defaultdict(lambda: [0, 0, 0])       # pattern -> [n, reversed >= 3%, kept down]
    P2 = defaultdict(list)                     # signal kind -> [(year2, pl)]
    n_bars = n_flat = n_flat_tr = 0
    for p in sorted(OW.glob("bars/bars_20*.json.gz")):
        day = p.name[5:15]; y2 = day >= SPLIT
        d = json.load(gzip.open(p, "rt"))
        for s in d["top30_current"]:
            fp = OW / "flow" / day / f"{s}.json"
            if s not in d["symbols"] or not fp.exists():
                continue
            a = arrays(d["symbols"][s]["bars"])
            if a is None:
                continue
            fl = json.loads(fp.read_text()); bu, se = np.array(fl["buy"], float), np.array(fl["sell"], float)
            O, H, L, C, V = a
            flat = (H - L) < 0.005
            traded = V > 0
            n_bars += 390; n_flat += flat.sum(); n_flat_tr += (flat & traded).sum()
            # ---- PART 1: every V with a >= 2% drop
            for v in vs_of(a):
                if v["drop"] < 2:
                    continue
                j = v["m"]
                win = range(max(0, j - 5), min(j + 4, 390))
                run = best = 0; run_tr = best_tr = 0
                for m in win:
                    run = run + 1 if flat[m] else 0; best = max(best, run)
                    run_tr = run_tr + 1 if flat[m] and traded[m] else 0; best_tr = max(best_tr, run_tr)
                buy_first = any(bu[m] > se[m] and not flat[m] for m in range(j, min(j + 4, 390)))
                keys = ["all Vs (drop >= 2%)"]
                if best >= 2:
                    keys.append(">= 2 flat bars near the low")
                    keys.append(">= 2 flat bars + then buy > sell" if buy_first else ">= 2 flat bars, NO buy > sell after")
                else:
                    keys.append("no flat run near the low")
                    keys.append("no flat run + buy > sell" if buy_first else "no flat run, no buy > sell")
                if best_tr >= 2:
                    keys.append(">= 2 flat bars WITH trades")
                if best >= 2 and best_tr < 2:
                    keys.append(">= 2 flat bars, NO trades (illiquid)")
                for k in keys:
                    P1[k][0] += 1; P1[k][1] += v["bounce"] >= 3; P1[k][2] += v["cls"] == "kept down"
            # ---- PART 2: live-style signal
            hi_sofar = np.maximum.accumulate(H)
            last = -99
            for m in range(17, 346):
                if m - last < 10:
                    continue
                down = C[m - 1] <= hi_sofar[m - 1] * 0.98 and C[m - 1] <= L[max(0, m - 30):m].min() * 1.01
                if not down:
                    continue
                k = 0
                while k < 6 and flat[m - 1 - k]:
                    k += 1
                buy_now = bu[m] > se[m]
                if k >= 2:
                    zone_lo = min(L[m - k:m + 1].min(), L[m])
                    t = trade(a, m, zone_lo * 0.999)
                    if t is None:
                        continue
                    tr = all(traded[m - k:m])
                    kind = ("flat x%d+ %s, then buy > sell" % (2 if k < 3 else 3, "with trades" if tr else "no trades")) if buy_now else \
                           ("flat, then buy <= sell (control)")
                    P2[kind].append((y2, t - COST)); P2["ANY flat run near the low (all)"].append((y2, t - COST))
                    last = m
                elif buy_now and k == 0 and (H[m - 1] - L[m - 1]) / C[m - 1] > 0.002:
                    t = trade(a, m, L[m - 3:m + 1].min() * 0.999)
                    if t is not None:
                        P2["no flat bars, buy > sell (control)"].append((y2, t - COST))
                        last = m
        del d
        print(day, flush=True)
    print(f"\nflat bars (high = low): {n_flat / n_bars:.1%} of all minute bars; {n_flat_tr / n_bars:.1%} flat WITH trades, "
          f"{(n_flat - n_flat_tr) / n_bars:.1%} flat because NO trade printed (illiquid minute)")
    print("\nPART 1 -- every V with a >= 2% drop: flat run in the 5 bars before the low .. 3 bars after it")
    tot = P1["all Vs (drop >= 2%)"][0]
    for k, (n, r, kd) in P1.items():
        print(f"  {k:42} {n:7} ({n / tot:4.0%}) | reversed >= 3% {r / n:4.0%} | kept down {kd / n:4.0%}")
    print(f"\nPART 2 -- live-style signal (buy at the bar's close, stop under the flat zone, 2R, 60 min), {COST}% cost")
    for k, X in sorted(P2.items()):
        a1 = np.array([p for y, p in X if not y]); a2 = np.array([p for y, p in X if y])
        f = lambda z: f"{len(z):6} {z.mean():+.3f}% win {np.mean(z > 0):.0%}" if len(z) else "     0"
        print(f"  {k:44} year 1 {f(a1):24} | year 2 {f(a2)}")


if __name__ == "__main__":
    main()
