A trend following template built from the rules Curtis Faith published in "The Original Turtle Trading Rules" and in "Way of the Turtle". It enters when price trades through the highest high or the lowest low of the last 20 days (System 1) or 55 days (System 2), and leaves when price trades through the opposite channel of the last 10 or 20 days. N is the 20 day average true range with Wilder's smoothing. The stop sits 2N from the entry price, and a unit is sized so that a move of 1N changes the balance by 1 per cent, which puts 2 per cent at risk at the stop.
The defaults are System 1, with its filter on: a breakout is skipped when the last breakout, taken or not, would have closed at a profit. A skipped breakout is still entered at the 55 day breakout (the failsafe), so a long trend is not missed. For System 2 set the entry to 55 days, the exit to 20 days and the filter off.
Entries are resting stop orders at the channel, placed on each bar's close for the next bar, so a breakout fills where the market crossed the channel (or at the open when it gapped through). The code is short and commented: read it, change it, and keep it as your own strategy.
It exports to MetaTrader 5, cTrader and Rust as it runs here. It keeps its own record of the trade, its stop orders filled on the ask or the bid and its stop on the other side, so the expert advisor and the cBot follow the bid and ask of the quotes they see. TradingView's bars carry one price and no bid or ask, so there is no export to it.
Parameters
Name
Type
Default
Range or options
Description
entryDays
int
20
min 2, max 300
Days in the entry channel: a breakout of the highest high or lowest low of this many days enters. 20 for System 1, 55 for System 2.
exitDays
int
10
min 2, max 300
Days in the exit channel: a break of the opposite side of this many days closes the position. 10 for System 1, 20 for System 2.
atrDays
int
20
min 2, max 100
Length of N, the average true range with Wilder's smoothing.
stopN
float
2
min 0.5, max 10
The stop's distance from the entry, in N.
riskPerN
float
1
min 0, max 5
Per cent of the balance a move of 1N is worth; the stop then risks this times the stop distance. 0 trades the run's lot size.
skipAfterWinner
int
1
min 0, max 1
1 skips a breakout when the last one, taken or not, would have closed at a profit (System 1). 0 takes every breakout (System 2).
failsafeDays
int
55
min 0, max 300
Days in the channel a skipped breakout is still entered at. 0, or no longer than the entry channel, turns it off.
allowShort
int
1
min 0, max 1
1 trades breakouts in both directions, 0 long only.
Use it in a strategy
python# The core rule in your own strategy: a buy stop at the 20 day high for the next bar,
# with the stop 2N away and the size set by risk.
n = ctx.ind("atr", length=20)
high = ctx.ind("dc", length=20).upper
if n is not None and high is not None:
ctx.order("LONG", stop=high, sl_pips=2 * n / ctx.pip(), bars=1, risk_percent=2.0)
To run the whole template, install it, switch it on and pick Turtle breakout under Plugins in the strategy list. Run it on D1: every length counts bars.
What it provides
turtle_breakout/turtleStrategy
The strategy, with its parameters: entry and exit channel days, the ATR length for N, the stop in N, the risk per N, the System 1 filter, the failsafe channel and the short side.
When values are known
Every decision is made on a bar's close from that bar and the ones before it. The entry orders rest during the next bar only; the channel exit is sent on the close of the bar that broke the channel and fills at the next bar's open.
Limits
One unit per breakout. The Turtles added a unit every half N in their favour, up to 4 units, and moved every stop up with the last one. The engine holds one position per instrument, so this template cannot add to a position.
One instrument per run. The Turtles traded a portfolio of futures with limits per market, per group of correlated markets and in total; none of those limits is modelled here.
The channel exit fills at the next bar's open, not at the channel price, because the engine has no resting exit order. On a fast day that open can be well past the channel.
The size is measured on the balance when the order is placed. The Turtles cut their trading size after a drawdown (a 10 per cent drawdown cut it by 20 per cent); this template does not.
An exit and an entry the other way on the same bar: the entry waits for the next new breakout. While a position is open no opposite order rests.
The template keeps its own record of the position from the bars, by the rules the engine fills by, as it did before a strategy could read the engine's position, so it trades as it always has. When the engine refuses an entry (not enough margin), the record still counts it until its stop or exit.
Long trends have made these rules famous, and long sideways periods are where they lose: many small losing breakouts in a row are normal for this kind of system. No result is promised.