Files
scalp-bot/strategy_rolling.py
anorandClaude Opus 4.6 af9c763923 test: эксперименты — rolling range, multi-session, volume-spike
Все три подхода убыточны на OOS:
- Rolling 24/7: -68%...-90% (слишком много шума)
- Multi-session (Asia+London+NYSE): -18%...-28%
- Volume-spike: -39%...-55%

Вывод: FVG+Engulfing edge работает ТОЛЬКО на NYSE open (5m).
Чем больше сигналов вне NYSE — тем хуже результат.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-13 03:58:49 +10:00

174 lines
5.6 KiB
Python

"""Rolling Range Breakout + FVG + Engulfing strategy (24/7, no time binding).
Instead of NYSE opening range, uses rolling high/low of last N bars
as breakout levels. Works on any market hours.
"""
from __future__ import annotations
from dataclasses import dataclass
import pandas as pd
from strategy import FVG, Trade
@dataclass
class RollingParams:
range_period: int = 12 # rolling range lookback (bars)
rr_ratio: float = 3.0
fvg_expiry: int = 12 # FVG valid N bars
cooldown: int = 6 # bars to wait after trade
min_range_pct: float = 0.0 # min range as % of price
min_fvg_pct: float = 0.0 # min FVG gap as % of price
first_fvg_only: bool = False
def run_continuous(df: pd.DataFrame, p: RollingParams | None = None) -> list[Trade]:
"""Process entire dataframe with rolling range breakout. No time restrictions."""
p = p or RollingParams()
n = len(df)
if n < p.range_period + 5:
return []
highs = df["high"].values
lows = df["low"].values
opens = df["open"].values
closes = df["close"].values
timestamps = df["timestamp"].values
trades: list[Trade] = []
fvgs: list[FVG] = []
active: Trade | None = None
cooldown_until = 0
for i in range(p.range_period + 2, n):
# Rolling range BEFORE the 3-candle FVG pattern (exclude i, i-1, i-2)
rng_start = max(0, i - 2 - p.range_period)
rng_end = i - 2
rh = float(highs[rng_start:rng_end].max())
rl = float(lows[rng_start:rng_end].min())
mid = (rh + rl) / 2
# Min range filter
if p.min_range_pct > 0 and mid > 0:
if (rh - rl) / mid < p.min_range_pct / 100:
continue
c_high = float(highs[i])
c_low = float(lows[i])
c_open = float(opens[i])
c_close = float(closes[i])
c_ts = timestamps[i]
# Check SL / TP on active trade
if active:
hit_sl = hit_tp = False
if active.direction == "long":
hit_sl = c_low <= active.stop
hit_tp = c_high >= active.tp
else:
hit_sl = c_high >= active.stop
hit_tp = c_low <= active.tp
if hit_sl:
active.exit_price = active.stop
active.exit_time = c_ts
active.result = "loss"
active.pnl_r = -1.0
trades.append(active)
active = None
cooldown_until = i + p.cooldown
elif hit_tp:
active.exit_price = active.tp
active.exit_time = c_ts
active.result = "win"
active.pnl_r = p.rr_ratio
trades.append(active)
active = None
cooldown_until = i + p.cooldown
else:
continue
# Cooldown after last trade
if i < cooldown_until:
continue
# Detect FVG (need 3 bars)
if i >= 2:
c0_high = float(highs[i - 2])
c0_low = float(lows[i - 2])
c1_close = float(closes[i - 1])
fvg = None
if c1_close > rh and c_low > c0_high:
fvg = FVG("bull", c0_high, c_low, i)
elif c1_close < rl and c_high < c0_low:
fvg = FVG("bear", c_high, c0_low, i)
if fvg:
gap = fvg.zone_high - fvg.zone_low
if p.min_fvg_pct > 0 and mid > 0 and gap / mid < p.min_fvg_pct / 100:
fvg = None
if fvg and not (p.first_fvg_only and fvgs):
fvgs.append(fvg)
# Expire old FVGs
fvgs = [f for f in fvgs if not f.used and (i - f.bar_idx) < p.fvg_expiry]
# Check retest + engulfing
if i < 1:
continue
prev_high = float(highs[i - 1])
prev_low = float(lows[i - 1])
prev_open = float(opens[i - 1])
prev_close = float(closes[i - 1])
for fvg in fvgs:
if fvg.used:
continue
if not (prev_low <= fvg.zone_high and prev_high >= fvg.zone_low):
continue
# Engulfing check
if fvg.direction == "bull":
ok = (prev_close < prev_open
and c_close > c_open
and c_close >= prev_open
and c_open <= prev_close)
else:
ok = (prev_close > prev_open
and c_close < c_open
and c_close <= prev_open
and c_open >= prev_close)
if not ok:
continue
entry = c_close
stop = c_low if fvg.direction == "bull" else c_high
risk = abs(entry - stop)
if risk <= 0:
continue
tp = entry + p.rr_ratio * risk if fvg.direction == "bull" else entry - p.rr_ratio * risk
active = Trade(
direction="long" if fvg.direction == "bull" else "short",
entry=entry, stop=stop, tp=tp, entry_time=c_ts,
)
fvg.used = True
break
# Close remaining position at last bar
if active:
last_close = float(closes[-1])
risk = abs(active.entry - active.stop)
pnl = ((last_close - active.entry) if active.direction == "long"
else (active.entry - last_close))
active.exit_price = last_close
active.exit_time = timestamps[-1]
active.pnl_r = pnl / risk if risk > 0 else 0
active.result = "win" if active.pnl_r > 0 else "loss"
trades.append(active)
return trades