Turtle Trading Donchian Breakout and ATR Pyramiding in RealTest (Validated Code)
To code Turtle Trading Donchian breakouts with 4-unit ATR pyramiding in RealTest, set MaxSameSym to 4, use intraday EntryStop switching from HHV(H, 20) on initial entry to FillPriceMax + 0.5 * atr20 for pyramid units, and wrap both Shares and FillPriceMax inside Extern(@strategy_name, ...) so RealTest aggregates across all open sub-positions rather than returning NaN inside ExitStop.
- RealTest places genuine intraday Donchian breakout stop orders via EntryStop with EntryTime Intraday (STP DAY), filling when price breaks above yesterday's 20-day high (or at the open on a gap above the channel).
- Raising MaxSameSym from its default of 1 to 4 unlocks classic Turtle pyramiding, while MaxPositions caps total simultaneous units across all markets.
- Inside a multi-unit pyramiding strategy, RealTest evaluates ExitStop separately for each sub-position, so bare FillPriceMax returns NaN and silently disables stops; wrapping it as Extern(@turtle_pyramided_4u, FillPriceMax) pulls the aggregate highest fill across all open units and ratchets every unit's unified 2-ATR stop upward together.
- Similarly, checking Extern(@turtle_pyramided_4u, Shares) = 0 cleanly distinguishes an initial 20-day Donchian breakout (Unit 1) from subsequent +0.5 ATR pyramid adds (Units 2 through 4).
- Validated in RealTest 2.0 on diversified multi-asset ETFs (2010 to 2024), the fixed Extern(@strat, FillPriceMax) script executed 888 trades cleanly across up to 4 stacked units (such as a 4-unit VNQ run from July to September 2024 netting +$8,990 and a 4-unit SPY run from January to April 2024 netting +$8,715).
- Empirically on modern liquid ETFs, raw 1980s 4-unit +0.5 ATR pyramiding doubled drawdown (-24.48 percent vs -10.86 percent) and trailed single-unit Donchian entries ($106,517 vs $130,583), proving why modern breakout systems need stricter trend filters and sizing caps.
Richard Dennis's classic Turtle Trading rules (20-day Donchian high breakout, 1 percent ATR risk sizing, pyramiding up to 4 units every half-ATR, unified 2-ATR trailing stop, and 10-day Donchian low exit) are one of the most requested code templates traders ask AI assistants to build in RealTest. They are also one of the easiest scripts to break silently.
In fact, when we tested the obvious textbook formula ExitStop: Max(FillPriceMax - 2 * atr20, ll10) directly in RealTest 2.0, bare FillPriceMax inside ExitStop evaluated to NaN, disabling stops completely and holding all 12 initial trades open for 15 straight years! Every fix below comes directly from the official RealTest MaxSameSym and multi-leg pyramiding documentation and was verified across 15 years of diversified multi-asset ETF data in RealTest 2.0.
Why Pyramiding in RealTest Needs Extern(@strategy_name, ...)
By default, RealTest sets MaxSameSym: 1 so a single strategy never holds more than one position in the same ticker. Raising MaxSameSym: 4 allows up to four simultaneous sub-positions (legs) in one symbol, each stored with its own entry date and fill price.
That multi-leg architecture introduces one critical rule documented in the RealTest User Guide. On each bar, RealTest loops through every open sub-position separately when evaluating exits. Inside that per-leg context, referencing bare Shares reads only the single leg's share count rather than total shares across all legs, and bare FillPriceMax inside ExitStop can evaluate to NaN, turning Max(FillPriceMax - 2 * atr20, ll10) into NaN (no stop order!). Prefixing both variables with Extern(@strategy_name, ...) forces RealTest to query the strategy's aggregate symbol state across all open pyramid units.
| Turtle Pyramiding Mechanism | Broken Single-Leg Syntax | Validated RealTest 2.0 Multi-Leg Syntax |
|---|---|---|
| Allow up to 4 units per market | Omitted (defaults to MaxSameSym: 1) |
MaxSameSym: 4 plus MaxPositions: 12
|
| Unit 1 vs Pyramid Add Stop Price | IF(Shares = 0, hh20, FillPriceMax + 0.5 * atr20) |
IF(Extern(@turtle_pyramided_4u, Shares) = 0, hh20, Extern(@turtle_pyramided_4u, FillPriceMax) + 0.5 * atr20) |
| Unified 2-ATR Stop Across All 4 Units |
Max(FillPriceMax - 2 * atr20, ll10) (silently NaN -> 0 stop exits!) |
Max(Extern(@turtle_pyramided_4u, FillPriceMax) - 2 * atr20, ll10) |
| ATR Unit Sizing (1% risk per unit) |
Quantity: 0.01 * S.Alloc / (2 * atr20) (uncapped on quiet bonds) |
Min(riskPct * S.Alloc / (2 * atr20), 0.25 * S.Alloc / C) + QtyType: Shares
|
Notice that fourth row too. On quiet instruments like short-term bonds or low-volatility currency ETFs (UUP), pure 1 percent ATR risk sizing can easily request more than 40 percent of equity for a single unit. Wrapping Quantity inside Min(..., 0.25 * S.Alloc / C) alongside MaxExposure: 100 and Reduce: 2000 keeps every unit strictly within realistic cash bounds (see our RealTest ATR position sizing guide and RealTest ExitStop reference).
Complete Validated RealTest Script (Single-Unit vs 4-Unit Pyramided Turtle)
Below is a complete, runnable comparison script built on 11 diversified multi-asset Norgate Data ETFs spanning equities (SPY, VEU), bonds (TLT), gold (GLD), real estate (VNQ), commodities (DBC, DBA), US Dollar (UUP), and major sectors (XLE, XLP, IYH). Both strategies share identical ATR risk sizing, commissions, and 5 bps StopSlip / OpenSlip via Template: turtle_base so you can isolate the exact impact of 4-unit pyramiding.
Notes:
Classic Turtle 20-day Donchian breakout comparing single-unit entries
against 4-unit ATR pyramiding using MaxSameSym, EntryStop, and
Extern(@strat, FillPriceMax) unified trailing stops on multi-asset ETFs.
Import:
DataSource: Norgate
IncludeList: SPY, VEU, TLT, GLD, VNQ, DBC, XLE, XLP, IYH, UUP, DBA
StartDate: 2007-01-01
EndDate: Latest
SaveAs: turtle_etfs.rtd
Settings:
DataFile: turtle_etfs.rtd
StartDate: 2010-01-01
EndDate: 2024-12-31
AccountSize: 100000
BarSize: Daily
Parameters:
riskPct: 0.01
maxUnits: 4
maxTotalUnits: 12
Data:
isUniv: Symbol = $SPY or Symbol = $VEU or Symbol = $TLT or Symbol = $GLD or Symbol = $VNQ or Symbol = $DBC or Symbol = $XLE or Symbol = $XLP or Symbol = $IYH or Symbol = $UUP or Symbol = $DBA
atr20: ATR(20)
hh20: HHV(H, 20)
ll10: LLV(L, 10)
ma200: Avg(C, 200)
Template: turtle_base
Side: Long
QtyType: Shares
QtyRound: 1
Quantity: Min(riskPct * S.Alloc / (2 * atr20), 0.25 * S.Alloc / C)
MaxExposure: 100
Reduce: 2000
Commission: Max(1, Shares * 0.005)
StopSlip: 0.0005 * FillPrice
OpenSlip: 0.0005 * FillPrice
Ambiguity: Stop
Strategy: turtle_single_unit
Using: turtle_base
EntrySetup: isUniv and C > ma200
EntryStop: hh20
EntryTime: Intraday
SetupScore: ROC(C, 60)
MaxPositions: 6
MaxSameSym: 1
ExitStop: Max(FillPrice - 2 * atr20[BarsHeld], ll10)
Strategy: turtle_pyramided_4u
Using: turtle_base
// Unit 1 breaks 20-day high; Units 2..4 add every +0.5 ATR above highest fill
EntrySetup: isUniv and C > ma200
EntryStop: IF(Extern(@turtle_pyramided_4u, Shares) = 0, hh20, Extern(@turtle_pyramided_4u, FillPriceMax) + 0.5 * atr20)
EntryTime: Intraday
SetupScore: ROC(C, 60)
MaxPositions: maxTotalUnits
MaxSameSym: maxUnits
// Move protective stop for ALL open units up to 2 ATR below newest fill or 10-day low
ExitStop: Max(Extern(@turtle_pyramided_4u, FillPriceMax) - 2 * atr20, ll10)
Charts:
Donchian_20H: hh20
Donchian_10L: ll10
What RealTest 2.0 Revealed About Classic 1980s Turtle Pyramiding
Running both strategies side by side from January 2010 through December 2024 confirmed every mechanic cleanly. On strong directional breakouts, turtle_pyramided_4u stacked all four units cleanly and exited all four units together on the unified trailing stop. For example, on VNQ in 2024, units entered at 87.54 (July 12), 89.09 (July 22), 89.71 (July 23), and 90.42 (July 30), then all four closed simultaneously on September 27 at 97.03 for +$8,990 total profit. On SPY in early 2024, four stacked units entered between 477.83 and 487.82 and exited together on April 4 at 514.82 for +$8,715.
Yet look at the full 15-year equity comparison across all 888 pyramided trades versus 595 single-unit trades.
| Metric (2010 to 2024 Multi-Asset ETFs) | turtle_single_unit (MaxSameSym 1) | turtle_pyramided_4u (MaxSameSym 4) |
|---|---|---|
| Completed Trades | 595 | 888 (all cleanly closed via exit stop) |
| Ending Equity ($100k start) | $130,583 | $106,517 |
| Maximum Daily Drawdown | -10.86% | -24.48% |

This empirical result is why backtesting classic rules honestly matters before putting real capital behind them. In the 1980s futures markets that made the Turtles famous, long multi-quarter macro trends ran straight through +0.5 ATR add-ons. On modern liquid ETFs and equities, 20-day Donchian breakouts frequently pause and pull back right after unit 3 or 4 fills, triggering the tightened unified stop across all four units at once and turning an otherwise positive single-unit breakout edge into deeper drawdowns. Modern equity breakout trading solves this by tightening breakout quality, filtering broader market regimes (see our RealTest Extern() and breadth filter guide), and pairing breakout sleeves with mean reversion inside one combined portfolio (see our RealTest multi-strategy combining guide).
Moving From 1980s Turtle Rules to Modern RealTest Breakout Systems
Once you see how clean RealTest's EntryStop, MaxSameSym, and Extern(@strat, FillPriceMax) mechanics are, testing modern trend improvements takes minutes. To inspect working RealTest code templates locally right away, browse our free RealTest strategy and code library or download the Free Mean-Reversion Strategy for RealTest and Python.
If you want complete, out-of-sample researched trend-following systems designed specifically for modern markets with full transparent RealTest .rts source code, explore our Turtle Max Growth Strategy and the RealTest Modern Breakout Strategy. To run trend breakouts alongside mean-reversion edges under one low-drawdown equity cap, review the RealTest Portfolios collection or check our RealTest fill timing and slippage guide before deploying live orders.
Key terms
- EntryStop
- A RealTest Strategy element placing a one-day intraday stop entry order (STP DAY), filling long when High reaches or exceeds the stop price (or at Open on a gap above).
- FillPriceMax
- A RealTest pyramiding position variable returning the highest entry fill price across open legs in a symbol; reference via Extern(@strategy_name, FillPriceMax) inside multi-leg rules.
- Donchian Channel
- A breakout channel defined by the N-bar highest high (HHV(H, 20)) and M-bar lowest low (LLV(L, 10)), popularized by Richard Dennis's Turtle Trading rules.
- ATR Unit Sizing
- Risk-normalized position sizing that divides a fixed percentage of S.Alloc (typically 1 percent) by the initial ATR stop distance (2 * ATR(20)) to compute share quantity.
- Unified Trailing Stop
- A pyramiding exit rule that ratchets the protective stop of every open unit up to 2 ATR below the newest pyramid fill price simultaneously.
Frequently asked questions
How do you enable pyramiding in RealTest?
Set MaxSameSym inside your Strategy block to the maximum number of simultaneous units per ticker (such as MaxSameSym 4 for classic Turtle rules) and set MaxPositions to your total portfolio unit limit across all symbols.
Why does FillPriceMax fail or return NaN inside ExitStop in RealTest?
When multiple sub-positions are open in one symbol, RealTest evaluates ExitStop separately per sub-position, where bare FillPriceMax can evaluate to NaN and disable stops. Wrap it as Extern(@your_strategy_name, FillPriceMax) so RealTest retrieves the highest fill price across all open units in that symbol.
How do you write a 20-day Donchian breakout entry order in RealTest?
Precompute hh20 as HHV(H, 20) in Data, then set EntryStop to hh20 with EntryTime Intraday inside your Strategy block. RealTest places a daily intraday stop-buy order at yesterday's 20-day high and fills if today's High crosses it (or at the Open on a gap above the channel).
How do you add Turtle pyramid units every +0.5 ATR in RealTest?
Use EntryStop set to IF(Extern(@your_strategy, Shares) = 0, hh20, Extern(@your_strategy, FillPriceMax) + 0.5 * atr20) alongside MaxSameSym 4. Unit 1 enters on the 20-day Donchian high breakout, and Units 2 through 4 trigger intraday each time price advances another 0.5 ATR above the highest existing fill.
How do you code Turtle 1 percent ATR risk position sizing in RealTest?
Set QtyType to Shares, QtyRound to 1, and Quantity to Min(0.01 * S.Alloc / (2 * atr20), 0.25 * S.Alloc / C). Dividing 1 percent of S.Alloc by the 2-ATR stop distance equalizes dollar risk per unit, while the 25 percent cap prevents quiet bond or currency ETFs from taking oversized allocations.
Does classic 4-unit Turtle pyramiding improve returns on modern ETFs?
In our 2010 to 2024 RealTest validation across 11 diversified multi-asset ETFs, raw +0.5 ATR 4-unit pyramiding increased max drawdown from -10.86 percent to -24.48 percent and trailed single-unit breakouts ($106,517 vs $130,583) because modern equity and ETF breakouts pull back more frequently than 1980s futures trends.