Files
2026-10-21 20:47:59 -04:00

56 lines
2.2 KiB
Python
Executable File

"""Novel: volatility-squeeze breakout with a trend filter.
Idea: volatility clusters (Mandelbrot; Engle's ARCH), so unusually quiet periods tend to
end in large moves. Wait for Bollinger bandwidth to fall into its lowest quantile, then
buy only an upside break of the upper band while price is above its long SMA, and exit
on a close back below the middle band. Builds on Bollinger's "squeeze" but adds the
quantile test and trend filter. Unproven: validate with the backtester.
"""
import pandas as pd
from . import indicators as ind
from .base import Context, Strategy, register
@register("squeeze_breakout")
class SqueezeBreakout(Strategy):
default_params = {
"bb_window": 20,
"bb_std": 2.0,
"squeeze_lookback": 120,
"squeeze_quantile": 0.2,
"squeeze_memory": 5,
"regime_sma": 100,
"target_vol": 0.6,
"vol_window": 30,
}
def decide(self, ctx: Context) -> dict[str, float]:
p = self.params
need = max(p["squeeze_lookback"] + p["bb_window"], p["regime_sma"], p["vol_window"]) + 1
slot = 1 / len(self.pairs)
targets = {}
for pair in self.pairs:
close: pd.Series = ctx.candles[pair]["close"]
if len(close) <= need:
continue
mid = close.rolling(p["bb_window"]).mean()
std = close.rolling(p["bb_window"]).std()
last = close.iloc[-1]
in_position = ctx.weights.get(pair, 0) > 0
if in_position:
if last < mid.iloc[-1]:
targets[pair] = 0.0
continue
bandwidth = (2 * p["bb_std"] * std / mid).iloc[-p["squeeze_lookback"]:]
threshold = bandwidth.quantile(p["squeeze_quantile"])
recent_squeeze = (bandwidth.iloc[-p["squeeze_memory"] - 1:-1] <= threshold).any()
breakout = last > mid.iloc[-1] + p["bb_std"] * std.iloc[-1]
uptrend = last > ind.sma(close, p["regime_sma"])
if recent_squeeze and breakout and uptrend:
targets[pair] = slot * ind.vol_scale(close, p["target_vol"], p["vol_window"], self.interval)
return targets