Place a buy stop at the highest high of the last N bars so the entry fills where price actually breaks out, and exit on a stop at the lowest low of a shorter channel. Filling at the breakout bar's close or the next open instead gives a different, usually better, price than a real stop order would get.
The channel
A Donchian channel, named after Richard Donchian, is the highest high and lowest low of the last N bars. The best-known rule set comes from the Turtle traders of the 1980s: enter long on a break of the 55-day high (or 20-day in their shorter system), and exit on a break of the 20-day low (or 10-day). Shorts mirror it.
The rule is simple, and its results depend heavily on how the backtest fills the breakout.
Why the fill matters
A trader running this rule rests a buy stop at the channel high. When price trades through it, the order fills at about that level, plus slippage, during the bar. A backtest that waits for a close above the channel and buys at that close, or at the next bar's open, fills somewhere else:
- On strong breakouts the close is far above the channel, so the backtest pays more than the stop would have.
- On failed breakouts price pokes above the channel and closes back inside. The stop fills and loses; a close-based backtest never enters. Leaving these out makes the strategy look better than it is.
- On gaps the stop fills at the open, above the level. A backtest that fills at the stop price on a gap is too kind.
So test with real stop orders, and check how the backtester fills a gap.
How Reamer Research handles it
Reamer Research fills stop orders inside each bar, from simulated ticks, and a gap fills at the first tick after the open. A stop placed with tif='IOC' lives for one bar, so re-placing it each bar keeps it at the current channel level:
import reamer_research as rr
class Donchian:
lookback = 55 # entry channel
exit_n, qty = 20, 10
def on_bar(self, d):
if d.close[0] == 0:
return None
if d.position() == 0:
return rr.buy_stop(d.high.max(), self.qty, tif='IOC')
return rr.sell_stop(d.low[-self.exit_n:].min(), d.position(), tif='IOC')
result = rr.backtest(Donchian, 'data/GC_continuous.bin', slippage=0.1, spread=0.1)
The window holds the last 55 closed bars, so d.high.max() is the channel the next bar has to break. The same rule runs as a function on_bar(data) or as an object configured with both channel lengths. When a bar touches both a stop and a target, the engine decides which came first from the tick path; stop and target in the same bar explains the rule.
Breakout systems are usually run on futures. A continuous futures series also needs roll handling, which roll_cycle_months provides, as described in futures rolls and FX swap.
Limits to know
- Sizing is yours. The Turtles sized by volatility; the position sizing guide shows ATR sizing in the same engine.
- Evaluation Mode runs 30 days or 50,000 bars of one ticker. Daily breakout systems need years of history, which needs a licence.
A $20 month includes the full kit, so you can run your own channel lengths on your own markets and cancel if it does not fit.