FAQ · Getting systematic research right

How do I backtest a Donchian channel breakout strategy in Python?

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.

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.

Next questions

How do I backtest an ATR stop or trailing stop in Python?Set the initial stop a multiple of the average true range below the entry, then on each closed bar raise it to the close minus the same multiple, never lowering it. The stop must rest as a real order that fills inside the next bar, at the stop price or worse on a gap, rather than being checked against the next close.How do I backtest a momentum or trend following strategy in Python?Measure trend from past closes only, such as the return over the last 12 months or price above a long average, hold long while it is positive and exit when it turns, and size each position by its recent volatility. Test across many years and several markets, because trend following earns most of its return in a few large moves.How do I backtest an opening range breakout strategy in Python?Use intraday bars, take the high and low of the first minutes of each session, then rest a buy stop above the range and a sell stop below it, with the opposite side of the range as the stop loss. Allow one trade a day and close everything before the session ends. Daily bars cannot test this strategy, because they do not show which came first.How should a backtest fill a stop and a target that both fall inside one bar?Reamer Research settles it on a seeded tick path it generates inside the bar: whichever level the path touches first is the exit, the same way on every run. The bar alone cannot say which was hit first, so a backtest should settle it by one stated rule, apply that rule the same way on every run, and let you check how much the result depends on it. A strategy whose result changes when the rule changes depends on something the data does not contain.

Every answer here restates the documents that ship in the kit, which are authoritative. All questions.

Reamer Research

Your first backtest in a minute

Backtesting and live trading in Python and C++. Free to install in Evaluation Mode, with no key or sign-up.

pip install reamer-research

A licence at $20 a month removes every limit. The same code runs unchanged.

Reamer Server

The order-management core for your firm

Your venues, your risk rules on every order, one strategy per CPU core, on your own Linux servers.

$24,000 / year

Or start with a 30-day trial at $2,000.