Turtle Trading Strategies (Amibroker And Tradestation Code)

The article can be found here.

Code in plain English:

Turtle Trading backtest 1: Close higher than 6 months ago (momentum)

Trading rules (monthly bars):

  • If the close is higher than 6 months ago, buy and hold the position for one month.
  • If the close is lower than the close 6 months ago, sell and stay out for the coming month.
  • Rinse and repeat monthly.

Amibroker code:

Buy = C>ref(c,-6) ;

BuyPrice= Close ;

Sell = c<ref(c,-6) ;

SellPrice = Close ;

Tradestation code:

{
Strategy 113A - Turtle Trading backtest 1: Close higher than 6 months ago (momentum)
Trading rules:
This strategy requires to set Data-Timeframe: Monthly
If the close is higher than 6 months ago, buy and hold the position for one month.
If the close is lower than the close 6 months ago, sell and stay out for the coming month.
Rinse and repeat monthly.
}

Inputs:
	LookbackMonths(6);

if C > C[LookbackMonths] then buy this bar on close;
if C < C[LookbackMonths] then sell this bar on close;

{
Strategy 113B - Turtle Trading backtest 1: Close higher than 6 months ago (momentum)
Trading rules:
This strategy trades on daily data, but on the month end only
If the close is higher than 6 months ago, buy and hold the position for one month.
If the close is lower than the close 6 months ago, sell and stay out for the coming month.
It does not look 6 months back but 6*21=126 days back which is not always exactly the 6 month. I think the but doesn't hurt the Turtle idea
}

Inputs:
	LookbackMonths(6);

if TdaysTillMonthEnd = 0 then Begin
	if C > C[21 * LookbackMonths] then buy this bar on close;
	if C < C[21 * LookbackMonths] then sell this bar on close;
End

Turtle Trading backtest 2: Dual moving average

The strategy, in plain English, works like this:

  • Buy when the 100-day moving average crosses above the 350-day moving average.
  • Sell when the 100-day moving average crosses below the 350-day moving average.

Amibroker code:

Buy = Cross(MA(C,100),MA(C,350) ) ;

BuyPrice= Close ;

Sell = Cross(MA(C,350),MA(C,100)) ;

SellPrice = Close ;

Tradestation code:

{
Strategy 113C - Turtle Trading backtest 2: Dual moving average
Buy when the 100-day moving average crosses above the 350-day moving average.
Sell when the 100-day moving average crosses below the 350-day moving average.
}

Inputs:
	FastLookback(100),
	SlowLookback(350);

if Average(Close, FastLookback) cross over Average(Close, SlowLookback) Then
	Buy this bar on close;
	
if Average(Close, SlowLookback) cross over Average(Close, FastLookback) Then
	Sell this bar on close;

Turtle Trading backtest 3: ATR Channel breakout

The ATR channel breakout uses the following rules in plain English:

  • Buy when the price breaks above the 350-day moving average plus a seven-day ATR.
  • Sell when the price breaks below the 350-day moving average deducted a seven-day ATR.

Amibroker code:

ATRUpperBand = MA(C,350)+ATR(3) ;

ATRLowerBand = MA(C,350)-ATR(3) ;

Buy = Cross(C,Ref(ATRUpperBand,-1)) ;

BuyPrice= Close ;

Sell = Cross(Ref(MA(C,350),-1),Close) ;

SellPrice = Close ;

Tradestation code:

{
Strategy 113D - Turtle Trading backtest 3: ATR Channel breakout
The ATR channel breakout uses the following rules:
Buy when the price breaks above the 350-day moving average plus a seven-day ATR.
Sell when the price breaks below the 350-day moving average deducted a seven-day ATR.
}

Inputs:
	Lookback(350),
	AtrLookback(7);

Vars:
	AtrUpperBand(0),
	AtrLowerBand(0);
	
AtrUpperBand = Average(Close, Lookback) + AvgTrueRange(AtrLookback);
AtrLowerBand = Average(Close, Lookback) - AvgTrueRange(AtrLookback);

if Close cross over AtrUpperBand[1] Then
	Buy this bar on close;
	
if Close cross under AtrLowerBand[1] Then
	Sell this bar on close;

Turtle Trading backtest 4: Bollinger Band breakout

The Bollinger Band Breakout strategy uses the following rules in plain English (we have a separate article about Bollinger Bands):

  • Buy when the price breaks above the 350-day moving average plus a standard deviation of 2.5.
  • Sell when the price breaks below the 350-day moving average deducted a seven-day ATR.

Amibroker code:

BBUpperBand = BBandTop(Close,350,2.5) ;

BBLowerBand = BBandBot(Close,350,2.5);

Buy = Cross(C,Ref(BBUpperBand,-1)) ;

BuyPrice= Close ;

Sell = Cross(Ref(MA(C,350),-1),Close) ;

SellPrice = Close ;

Tradestation code:

{
Strategy 113E - Turtle Trading backtest 4: Bollinger Band breakout
The Bollinger Band Breakout strategy uses the following rules:
Buy when the price breaks above the 350-day moving average plus a standard deviation of 2.5.
Sell when the price breaks below the 350-day moving average deducted a seven-day ATR.
}

Inputs:
	Lookback(350),
	BBWidth(2.5),
	AtrLookback(7);

Vars:
	BBUpperBand(0),
	AtrLowerBand(0);
	
BBUpperBand = BollingerBand(Close, Lookback, BBWidth);
AtrLowerBand = Average(Close, Lookback) - AvgTrueRange(AtrLookback);

if Close cross over BBUpperBand[1] Then
	Buy this bar on close;
	
if Close cross under AtrLowerBand[1] Then
	Sell this bar on close;


{
Strategy 113F - Turtle Trading backtest 4: Bollinger Band breakout
The Bollinger Band Breakout strategy uses the following rules:
Buy when the price breaks above the 350-day moving average plus a standard deviation of 2.5.
Sell when the price breaks below the 350-day moving average minus a standard deviation of 2.5.
}

Inputs:
	Lookback(350),
	BBWidth(2.5);

Vars:
	BBUpperBand(0),
	BBLowerBand(0);
	
BBUpperBand = BollingerBand(Close, Lookback, BBWidth);
BBLowerBand = BollingerBand(Close, Lookback, -BBWidth);

if Close cross over BBUpperBand[1] Then
	Buy this bar on close;
	
if Close cross under BBLowerBand[1] Then
	Sell this bar on close;