strategy · on price · open source
Donchian Channel Breakout
Running…
Loading chart…
About
Turtle-style Donchian Channel breakout: enter on a close above the N-bar high, exit on a close below the M-bar low. Includes an ATR-based trailing stop.
- 20/10 is the canonical Turtle pair; 55/20 for slower swings.
- Raise
ATR Multfor a wider trailing stop, lower for tighter risk control. - Combine with an ADX > 25 filter for trending environments only.
Source code
//@version=5
strategy("Donchian Channel Breakout", overlay=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=10, commission_type=strategy.commission.percent, commission_value=0.05)
len = input.int(20, "Entry Channel", minval=5, maxval=200, step=1)
exitLen = input.int(10, "Exit Channel", minval=5, maxval=100, step=1)
atrMult = input.float(2.5, "ATR Stop Mult", minval=1, maxval=5, step=0.1)
stopPct = input.float(2, "Stop Loss %", minval=0.5, maxval=10, step=0.1)
tpPct = input.float(6, "Take Profit %", minval=0, maxval=20, step=0.1)
hi = ta.highest(high, len)[1]
lo = ta.lowest(low, exitLen)[1]
longSignal = strategy.position_size == 0 and close > hi
exitSignal = strategy.position_size > 0 and close < lo
if longSignal
strategy.entry("Long", strategy.long)
if strategy.position_size > 0
strategy.exit("ATR stop", stop=strategy.position_avg_price - ta.atr(14) * atrMult)
if tpPct > 0
strategy.exit("TP", limit=strategy.position_avg_price * (1 + tpPct / 100))
if exitSignal
strategy.close("Long", comment="Channel exit")
p_Entry_Level = plot(hi, "Entry Level", color=color.green, linewidth=1)
p_Exit_Level = plot(lo, "Exit Level", color=color.red, linewidth=1)
plotshape(longSignal, "Long", style=shape.triangleup, location=location.belowbar, color=color.green, text="L")
plotshape(exitSignal, "Exit", style=shape.triangledown, location=location.abovebar, color=color.red, text="X")
alertcondition(longSignal, "Donchian long entry", "Price broke above Donchian high")
alertcondition(exitSignal, "Donchian exit", "Price broke below Donchian exit low")
No comments yet.
Sign in to comment.