37 lines
1.5 KiB
Python
Executable File
37 lines
1.5 KiB
Python
Executable File
"""Donchian channel breakout ("Turtle" System 2: 55-candle entry, 20-candle exit).
|
|
|
|
Evidence: the original Turtle rules (Faith, "Way of the Turtle", 2007); breakout trend
|
|
rules are one of the trend-following families studied by Hurst, Ooi & Pedersen (2017).
|
|
Breakouts are the trend signal most often found to survive costs in crypto because
|
|
they trade rarely.
|
|
"""
|
|
|
|
from . import indicators as ind
|
|
from .base import Context, Strategy, register
|
|
|
|
|
|
@register("donchian_breakout")
|
|
class DonchianBreakout(Strategy):
|
|
"""Buy a close above the prior N-candle high; sell a close below the prior M-candle low.
|
|
Position size is volatility-scaled at entry and then left alone until exit."""
|
|
|
|
default_params = {"entry": 55, "exit": 20, "target_vol": 0.6, "vol_window": 30}
|
|
|
|
def decide(self, ctx: Context) -> dict[str, float]:
|
|
p = self.params
|
|
slot = 1 / len(self.pairs)
|
|
targets = {}
|
|
for pair in self.pairs:
|
|
df = ctx.candles[pair]
|
|
if len(df) <= max(p["entry"], p["vol_window"]) + 1:
|
|
continue
|
|
close = df["close"].iloc[-1]
|
|
in_position = ctx.weights.get(pair, 0) > 0
|
|
if not in_position and close > df["high"].iloc[-p["entry"] - 1:-1].max():
|
|
targets[pair] = slot * ind.vol_scale(
|
|
df["close"], p["target_vol"], p["vol_window"], self.interval
|
|
)
|
|
elif in_position and close < df["low"].iloc[-p["exit"] - 1:-1].min():
|
|
targets[pair] = 0.0
|
|
return targets
|