"""BRK06 — Opening-Range Breakout (daily).

HYPOTHESIS: On 1d bars, go LONG when today's close > prior-day high (expansion/gap breakout).
SL = prior-day low. max_bars = configurable (3 or 5). No short side (breakdowns symmetric but
crypto skew is upward; testing long-only first). Entry at close[i] once close[i] > prior high[i-1].
Exit at SL=prior_low[i-1] or max_bars (time stop), whichever first.

Grid: max_bars in {3, 5} -> 2 configs × 1 TF × 2 assets = 4 backtests.

Honesty rules:
- decision uses close[i] vs high[i-1]: CAUSAL (prior-bar high is known by close of bar i).
- SL = low[i-1]: known causal.
- entry = close[i] (not high/low extreme of bar i).
- fee = 0.10% RT (Deribit taker).
"""
import sys
sys.path.insert(0, "/opt/docker/PythagorasGoal/scripts/research/alt")
import altlib as al
import numpy as np

def make_entries(df, max_bars: int):
    """Long when close[i] > high[i-1]. SL = low[i-1]. Exit at max_bars or SL."""
    c = df["close"].values
    h = df["high"].values
    lo = df["low"].values
    n = len(c)
    entries = [None] * n
    for i in range(1, n):
        prior_high = h[i - 1]
        prior_low = lo[i - 1]
        if c[i] > prior_high:
            # Long breakout: entry at close[i], SL below prior-day low
            # TP = None (let the time-stop manage exit)
            entries[i] = {
                "dir": 1,
                "tp": None,
                "sl": prior_low,
                "max_bars": max_bars,
            }
    return entries


configs = [
    {"max_bars": 3},
    {"max_bars": 5},
]

best_rep = None
best_score = -9999

for cfg in configs:
    name = f"BRK06-mb{cfg['max_bars']}"
    rep = al.study_signals(
        name,
        lambda df, mb=cfg["max_bars"]: make_entries(df, mb),
        tfs=("1d",),
    )
    print(al.fmt(rep))
    score = rep["verdict"].get("best_holdout_sharpe", -9999)
    if score is None:
        score = -9999
    if score > best_score:
        best_score = score
        best_rep = rep

print("\n=== BEST CONFIG ===")
print(al.fmt(best_rep))
print("JSON:", al.as_json(best_rep))
