The rules at a glance
- Market: XAUUSD on the 4-hour chart (the daily chart works too)
- Entry: a candle that closes above the highest high of the previous 20 candles (long), or below the lowest low of the previous 20 (short); enter at that close
- Trend filter: longs only above the 200 EMA, shorts only below it
- Stop loss: 2 × ATR (20) from the entry, fixed when the trade opens
- Exit: when a candle closes below the lowest low of the previous 10 candles (longs), or above the highest high of the previous 10 (shorts)
- Target: none: the 10-candle exit lets a winning trade run
- Size: risk 0.5% to 1% of the account on the 2 × ATR stop
What a Donchian channel is
A Donchian channel is drawn from two lines: the highest high and the lowest low of the last so many candles. It's named after Richard Donchian, a pioneer of trend-following in the futures markets. When the price closes above the top line, it has gone higher than at any time in that period, which is about as plain a definition of a breakout as there is. TradingView has it as a built-in indicator called Donchian Channels.
The channel needs no prediction. You don't decide where the trend will start; you wait for the price to show you, then follow it.
Where the 20 and the 10 come from
In the 1980s the trader Richard Dennis and his partner William Eckhardt trained a group of new traders, later known as the Turtles, to follow fixed rules. One of their systems bought a 20-day breakout and exited on a 10-day breakout the other way. Stops were set at twice "N", their name for the average true range over 20 days. This page uses the same structure on 4-hour gold candles, where 20 candles is a little over three trading days. On the daily chart it follows the Turtles' 20/10 structure, though it uses closes rather than intraday highs and lows, and it leaves out their rules for adding to a position and for skipping a breakout after a winning one.
The trend filter, a 200 EMA, is an addition. It stops the system buying breakouts in a falling market and selling them in a rising one, at the cost of missing the turn when a new trend starts.
Why most breakouts lose, and why that can be fine
Trend-following systems lose on a lot of trades. Many breakouts reverse and hit the stop or come back through the exit channel for a small loss. The idea is that the few trades that catch a real trend run far enough to pay for all of them. There's no target to cap them.
That's just arithmetic, not a promise. If your winners averaged 3R and your losers 1R, you'd break even by winning one trade in four, before costs, and you'd need to win more often than that to make money. Whether gold has trended enough on your dates for that to work is exactly what the backtest is for. The risk/reward calculator shows the break-even win rate for any ratio.
The rules, step by step
1. Draw the channels
Add a 20-candle Donchian channel and a 10-candle one to a 4-hour XAUUSD chart, plus a 200 EMA. The script works out both channels from the candles before the current one, so a close above the line really is a new high.
2. Enter on a close beyond the 20-candle channel
When a candle closes above the highest high of the previous 20 candles and the price is above the 200 EMA, buy at the close. For a short, the close must be below the 20-candle low with the price under the 200 EMA.
3. Place the stop 2 × ATR away
The stop goes two times the 20-period ATR below the entry and stays there. Because the ATR measures how much gold has been moving, the stop is wider in volatile weeks and tighter in quiet ones, and the lot size adjusts to keep the risk the same.
4. Exit on the 10-candle channel
Hold the trade until a candle closes below the lowest low of the previous 10 candles (above the 10-candle high for a short). That exit follows the price up and lets the trade run as long as the trend lasts. One trade at a time.
Worked example: XAUUSD, 4-hour chart
20-candle high (entry channel) 10-candle low (exit channel)
After weeks of rising, gold goes sideways under the top of its channel. Then a candle closes at 4,247.30, above the 20-candle high of 4,242.42, with the price well above the 200 EMA. The ATR is 20.64, so the stop goes two ATRs below the entry, at 4,206.02.
| XAUUSD long | |
|---|---|
| Entry | 4,247.30 |
| Stop loss (2 × ATR) | 4,206.02, $41.28 away |
| Account and risk | $10,000, risking 1% = $100 |
| Lot size | $100 ÷ ($41.28 × 100 ounces) = 0.024, rounded down to 0.02 lots (the script trades the unrounded 2.4 ounces) |
| Loss if the stop had been hit | $82.56 |
| Exit (close below the 10-candle low) | 4,357.44, 40 candles later |
| Result | +$110.14 an ounce, about 2.7R: $220.28 on 0.02 lots |
The trend carries gold as high as 4,421.88 before it pulls back far enough to close below the 10-candle low. The exit gives back $64.44 from the top; that's the price of letting the trade run without a target. Rounding the lot size down to 0.02 also means the trade risked less than $100. Breakouts like this one are the minority. Expect a run of small losses between them.
Holding gold for days: gaps, swaps and margin
- Weekend gaps. Gold stops trading from Friday evening to Sunday evening New York time. A stop can be jumped by a Monday gap, so the loss can be bigger than planned.
- Overnight costs. Holding a CFD or spot gold position overnight usually costs a swap fee, which adds up over a multi-day trade. Futures don't charge swaps but have to be rolled over at expiry.
- Margin. Wider stops mean small lot sizes, which keeps margin low. Don't be tempted to trade bigger "because the stop is far away"; that's exactly backwards.
Another way to let a trend run is a stop that trails the price; see the ATR trailing stop strategy.
Risk management
- Size from the 2 × ATR stop, every time. On gold, $1 is $100 a lot, and a $40 stop at 1% of $10,000 is about 0.02 lots. The gold risk management guide covers ATR-based stops in detail.
- Expect losing streaks. Five, eight or more losses in a row are normal for breakout systems. At 1% a trade, ten losses cost about 10%; at 5% a trade they cost about 40%.
- Don't move the stop closer. A tighter stop turns small pullbacks into losses and changes the system you tested.
Common mistakes
- Buying the first tick above the channel. The rules use the close.
- Taking profit early. Cutting the winners is the quickest way to break a trend-following system. If you want a target, test it first.
- Skipping trades after a few losses. The trade you skip is often the one that trends.
- Using a 20-candle channel on the 5-minute chart. That's an hour and forty minutes of history: far more noise, more trades and much higher costs.
Backtest it yourself on TradingView
This page doesn't quote a win rate or a profit figure, and you should be wary of anyone who does without showing the test. Results change with the market, the dates, the data feed and the costs you put in. The honest way is to run the test yourself, and the free script below does the work in a couple of minutes.
- Open the chart. On TradingView, open XAUUSD on the 4-hour chart.
- Add the script. Open the Pine Editor from the Pine icon in the panel on the right of the chart (older layouts have it as a tab under the chart), delete the code that's already there, paste this strategy's code and click "Add to chart".
- Put in your costs. Open the strategy's settings and go to Properties. Enter your broker's commission and a little slippage. A test without costs flatters every strategy, and short-term ones most of all.
- Read the Strategy Tester. The panel under the chart shows the net profit, the maximum drawdown, the profit factor, the number of trades and a list of every trade. Click a few trades and check on the chart that each one follows the rules. Trade sizes are shown in units rather than lots: 100,000 units is one lot of a forex pair, and 100 ounces is one lot of gold.
- Change one setting at a time. Compare the 20/10 channels with 55/20 (the Turtles' slower system), the trend filter on and off, the stop at 2 × ATR against 3 × ATR, and the 4-hour chart against the daily. On the daily chart the free plan's 5,000 candles go back many years, which is what a trend-following test needs.
As a rough guide, don't trust a result with fewer than 100 trades, a profit factor under 1 once costs are in, or a drawdown you couldn't sit through with real money. Settings tuned to look good on one stretch of history often fail on the next, so check the same settings on dates you didn't tune them on, then demo trade the strategy for a few weeks. How much history you can test depends on your TradingView plan: the free plan loads 5,000 candles, which is a few months of 15-minute candles but years of daily ones.
The script sizes each trade from the stop loss and your risk percentage, starts with $10,000 and assumes 1:100 leverage (a 1% margin setting), so the tester doesn't flag margin calls on normal forex trade sizes. Change these under Properties to match your account. By default TradingView fills orders at the open of the candle after the signal, so the fill can differ a little from the signal candle's close.
Download the free Pine Script and PDF
The script is written in Pine Script version 6 for this guide and follows the rules above. It can't read the economic calendar, so skipping news days is up to you. It's free to use, change and share. The PDF is a one-page cheat sheet with the rules, the channel settings, a worked example and a checklist, to keep next to your chart.
Show the code (Pine Script v6, 61 lines)
//@version=6
// Gold Breakout (Donchian channel) strategy by PipLedger (https://pipledgerfx.com/gold-breakout-strategy)
// Buys a close above the highest high of the last 20 candles (or sells a close below the lowest low), with a 2 x ATR stop and an exit on a close beyond the 10-candle channel on the other side.
// Free to use and change. For education and backtesting only; not financial advice.
strategy("PipLedger: Gold Donchian Breakout", shorttitle="Gold Breakout", overlay=true, initial_capital=10000, pyramiding=0, default_qty_type=strategy.fixed, default_qty_value=1, margin_long=1, margin_short=1, currency=currency.USD)
// ---------- Inputs
entryLen = input.int(20, "Breakout channel (candles)", minval=5)
exitLen = input.int(10, "Exit channel (candles)", minval=2)
atrLen = input.int(20, "ATR length", minval=5)
stopMult = input.float(2.0, "Stop loss (x ATR)", minval=0.5, step=0.25)
useTrend = input.bool(true, "Only trade in the direction of the 200 EMA")
riskPct = input.float(1.0, "Risk per trade (% of equity)", minval=0.1, maxval=5.0, step=0.1)
// ---------- Helpers
fx = strategy.convert_to_account(1.0) // one unit of the chart's quote currency in account currency (1 on USD-quoted charts)
calcQty(float entry, float stop) =>
float dist = math.abs(entry - stop)
dist > 0 and not na(fx) ? strategy.equity * riskPct / 100 / (dist * syminfo.pointvalue * fx) : 0.0
atr = ta.atr(atrLen)
ema200 = ta.ema(close, 200)
// channels from the candles before this one, so a breakout means a close beyond them
upper = ta.highest(high, entryLen)[1]
lower = ta.lowest(low, entryLen)[1]
exitLo = ta.lowest(low, exitLen)[1]
exitHi = ta.highest(high, exitLen)[1]
// ---------- Exits first: a close through the shorter channel on the other side
if strategy.position_size > 0 and close < exitLo
strategy.close("Long", comment="10-candle low")
if strategy.position_size < 0 and close > exitHi
strategy.close("Short", comment="10-candle high")
// ---------- Entries, one position at a time
flat = strategy.position_size == 0
longSig = flat and close > upper and (not useTrend or close > ema200)
shortSig = flat and close < lower and (not useTrend or close < ema200)
if longSig
float sl = close - stopMult * atr
float q = calcQty(close, sl)
if q > 0
strategy.entry("Long", strategy.long, qty=q)
strategy.exit("Long stop", "Long", stop=sl)
if shortSig
float sl = close + stopMult * atr
float q = calcQty(close, sl)
if q > 0
strategy.entry("Short", strategy.short, qty=q)
strategy.exit("Short stop", "Short", stop=sl)
// ---------- Chart
plot(upper, "20-candle high", color=color.new(color.teal, 0))
plot(lower, "20-candle low", color=color.new(color.red, 0))
plot(exitLo, "10-candle low (long exit)", color=color.new(color.teal, 60), style=plot.style_circles)
plot(exitHi, "10-candle high (short exit)", color=color.new(color.red, 60), style=plot.style_circles)
plot(useTrend ? ema200 : na, "200 EMA", color=color.new(color.gray, 0))
plotshape(longSig, "Long breakout", shape.triangleup, location.belowbar, color.teal, size=size.small)
plotshape(shortSig, "Short breakout", shape.triangledown, location.abovebar, color.red, size=size.small)Trading this from India
The rules work on any gold chart, and TradingView's XAUUSD chart is a fine place to learn and backtest them. To trade gold legally from India, though, use MCX gold futures, including the smaller Gold Mini and Gold Petal contracts, through a SEBI-registered broker. Trading XAUUSD as a CFD with an offshore forex platform isn't allowed for residents under FEMA. MCX gold trades from 9 a.m. to 11:30 p.m. IST while the US is on daylight saving time and to 11:55 p.m. in the US winter, which covers the London session and the New York morning. The contract is priced in rupees, and a position held for days on MCX has to be rolled to the next contract before expiry. The full picture is in is forex trading legal in India and XAUUSD market timings in India.
Related strategies: XAUUSD intraday pivots, XAUUSD London session, XAUUSD New York session, XAUUSD FVG + order block, XAUUSD pin bar, XAUUSD scalping, previous day high and low. All 34 strategies are compared on one page in forex trading strategies.