PipLedger
Today's forex newsNFP, CPI, FOMC and other high-impact eventsEconomic calendarCalendar →

Gold breakout strategy: a Donchian channel system for XAUUSD, with a free Pine Script

Gold spends a lot of its time in ranges and then, every so often, trends hard for weeks. A breakout system tries to catch those trends with simple, mechanical rules. This one uses the Donchian channel, the same idea the Turtle traders used in the 1980s: buy a close above the 20-candle high, protect it with a stop two ATRs away, and get out when the price closes below the 10-candle low. Here are the rules, a worked 4-hour example and a free TradingView script.

By M. A. Horaira. Updated 10 October 2026. 12 minute read.

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

Entry 4,247Stop 4,206Close above the 20-candle highExit: close below the 10-candle low

20-candle high (entry channel) 10-candle low (exit channel)

Illustrative chart drawn for this guide to show the rules. It isn't a real day's prices. XAUUSD, 4-hour candles. The 200 EMA, at about 4,102, sits far below the candles and is left off the chart.

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
Entry4,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

Another way to let a trend run is a stop that trails the price; see the ATR trailing stop strategy.

Risk management

Common mistakes

  1. Buying the first tick above the channel. The rules use the close.
  2. Taking profit early. Cutting the winners is the quickest way to break a trend-following system. If you want a target, test it first.
  3. Skipping trades after a few losses. The trade you skip is often the one that trends.
  4. 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.

  1. Open the chart. On TradingView, open XAUUSD on the 4-hour chart.
  2. 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".
  3. 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.
  4. 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.
  5. 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.

Quick answers

What is a good breakout strategy for gold?

There isn't one that works in every market phase. A Donchian channel breakout is one of the simplest to follow and test: buy a close above the 20-candle high, use a 2 × ATR stop and exit on a close below the 10-candle low. The free script lets you test it on gold with your own costs.

What is a Donchian channel?

It's two lines drawn from the highest high and the lowest low of the last so many candles, often 20. A close above the upper line means the price has made a new 20-candle high. It's named after Richard Donchian, an early trend-following trader.

What were the Turtle trading rules?

The Turtles were traders trained by Richard Dennis and William Eckhardt in the 1980s. One of their systems entered on a 20-day breakout and exited on a 10-day breakout the other way, with a stop at twice the 20-day average true range. This page uses the same structure on 4-hour gold candles.

Which timeframe is best for a gold breakout strategy?

This strategy uses the 4-hour chart, which gives a handful of trades a month and holds them for days. The daily chart is closest to the Turtles' original, with fewer trades held for weeks. Lower timeframes produce more trades, more false breaks and higher costs per trade.

Why is the stop 2 ATR on gold?

A stop based on the ATR adjusts to how much gold is moving: wider in volatile weeks, tighter in quiet ones. Two ATRs gives a breakout room to pull back without being stopped out by normal noise. The lot size is then worked out from that distance.

Does a breakout strategy need a profit target?

Trend-following systems usually don't use one, because a few large winners have to pay for many small losers. This one exits when the price closes back through the 10-candle channel instead. You can test a fixed target, but it often cuts the trades that matter most.

More guides

All 75 guides →

हिंदी में पढ़ें: Lot size kaise nikale?, Prop firm challenge kaise pass kare?, Gold trading kaise kare?, Forex trading kya hai, और India में यह legal है या नहीं?

Trading psychology

The habits that decide whether a good strategy makes money.

All 14 trading psychology guides

8 popular prop firms

Well-known firms for forex, gold and index traders that were still operating in 2026. Compare their rules before you buy a challenge.

If a link in this list is a referral link, PipLedger may earn a commission if you sign up, at no extra cost to you. This list is not a recommendation. Programs, prices and rules change often, so check each firm's own site, including its margin and drawdown rules, before you buy a challenge.