Strategy library

Donchian Breakout + MA strategy in Pine Script

A Donchian channel breakout in the direction of two moving averages, closed when the averages cross back.

Donchian Channels · SMA · long and short · by Robrecht99 · Mozilla Public License 2.0

What is its backtest worth?

A parameter sweep finds the best setting of the Donchian Breakout + MA strategy. PineTester tells you if it holds up: whether the best result was only picked after the fact, what it makes with fees, what you could have known at the time (a walk-forward on data the pick did not see) and how deep it fell along the way. On the free start, your first own backtest gets the full evaluation; its trade list and result file come with the full report or a plan.

Creates a draft on Binance BTC/USDT 4h over about six and a half years with 0.1% commission per order and 2 ticks slippage; you start it yourself. A backtest uses credits; new accounts receive free credits.

The rules

  • Enters long when the close breaks above the upper channel while the fast average is above the slow one; short on a break below the lower channel with the fast average below.
  • Closes the long when the fast average crosses under the slow one, the short when it crosses over.
  • Can add up to three entries in the same direction (pyramiding 3) and sizes each from a fixed risk per trade and the ATR.

What to watch for

  • Adding to a position raises the size in long trends and the loss when a trend ends.
  • The channel and the averages both follow price, so their settings move the result together – compare neighbouring settings.

How PineTester judges a backtest, with its sources: how we judge results.

The Pine Script code

The author's code with its licence notice (Mozilla Public License 2.0), with one change marked in the code: the same test conditions for every library strategy in its strategy() line – the whole account per trade (split over the entries where it adds to a position), 0.1% commission per order and 2 ticks slippage; the trading rules are unchanged. Pine Script v5; PineTester converts it to v6 automatically, so results can differ slightly from TradingView.

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Robrecht99
// Modified by PineTester (2026-11-14): the same test conditions for every library strategy in strategy(): 33% of equity per entry (pyramiding 3),
// 0.1% commission per order, 2 ticks slippage, initial capital 10,000. The trading rules are unchanged.
//@version=5
strategy("Simplicity at its finest", overlay=true, margin_long=0, margin_short=0, pyramiding=3, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=33, commission_type=strategy.commission.percent, commission_value=0.1, slippage=2)
// Backtest Range //
Start = input.time(defval = timestamp("01 Jan 2017 00:00 +0000"), title = "Backtest Start Date", group = "backtest window")
Finish = input.time(defval = timestamp("01 Jan 2100 00:00 +0000"), title = "Backtest End Date", group = "backtest window")
// Moving Averages //
len1 = input.int(8, minval=1, title="Length Fast MA", group="Moving Average Inputs")
len2 = input.int(32, minval=1, title="Length Slow MA", group="Moving Average Inputs")
src1 = input(close, title="Source Fast MA")
src2 = input(close, title="Source Slow MA")
maFast = input.color(color.new(color.red, 0), title = "Color Fast MA", group = "Moving Average Inputs", inline = "maFast")
maSlow = input.color(color.new(color.purple, 0), title = "Color Slow MA", group = "Moving Average Inputs", inline = "maSlow")
fast = ta.sma(src1, len1)
slow = ta.sma(src2, len2)
plot(fast, color=maFast, title="Fast MA")
plot(slow, color=maSlow, title="Slow MA")
// Donchian Channels //
Length1 = input.int(title="Length Upper Channel", defval=20, minval=1, group="Donchian Channels Inputs")
Length2 = input.int(title="Length Lower Channel", defval=20, minval=1, group="Donchian Channels Inputs")
h1 = ta.highest(high[1], Length1)
l1 = ta.lowest(low[1], Length2)
fillColor = input.color(color.new(color.gray, 100), title = "Fill Color", group = "Donchian Channels Inputs")
upperColor = input.color(color.new(color.white, 0), title = " Color Upper Channel", group = "Donchian Channels Inputs", inline = "upper")
lowerColor = input.color(color.new(color.white, 0), title = " Color Lower Channel", group = "Donchian Channels Inputs", inline = "lower")
u = plot(h1, "Upper", color=upperColor)
l = plot(l1, "Lower", color=upperColor)
fill(u, l, color=fillColor)
// ATR and Position Size //
leverage = input.int(title = "Leverage Used", defval = 1, minval = 1, group = "ATR Position Size Inputs")
pointvalue = input.int(title = "Point Value/ATR Multipliers from Entry", defval = 4, minval = 1, group = "ATR Position Size Inputs")
length = input.int(title = "ATR Period", defval = 14, minval = 1, group ="ATR Position Size Inputs")
risk = input(title = "Risk Per Trade", defval = 0.01, group = "ATR Position Size Inputs")
atr = ta.atr(length)
amount = (risk * strategy.initial_capital / (pointvalue * atr * leverage))
// Buy and Sell Conditions //
entrycondition1 = ta.crossover(fast, slow)
entrycondition2 = fast > slow
sellcondition1 = ta.crossunder(fast, slow)
sellcondition2 = slow > fast
// Buy and Sell Signals //
if (close > h1 and entrycondition2)
    strategy.entry("long", strategy.long, qty=amount)
if (sellcondition1 and sellcondition2)
    strategy.close(id="long")
if (close < l1 and sellcondition2)
    strategy.entry("short", strategy.short, qty=amount)
if (entrycondition1 and entrycondition2)
    strategy.close(id="short")

Open-source script by Robrecht99, shown under the Mozilla Public License 2.0. Not a recommendation; neither the author nor TradingView endorses PineTester. Backtests are hypothetical results on past data, not a forecast.