John Ehlers Trading Strategy: The Instantaneous Trendline (Amibroker Code)

The original article can be found here.

Strategy one: Long ES

// Instantaneous Trendline 
Price = (H+L)/2; 
Length = Optimize("Length", 15, 2, 100, 1 ); 
SMA = MA( Price, Length ); 
Slope = Price - Ref( Price, - ( Length - 1 ) ); 
SmoothSlope = ( Slope + 2 * Ref( Slope, -1 ) 
 + 2 * Ref( Slope, -2 ) + Ref( Slope, -3 ) )/6; 
ITrend = SMA + 0.5 * SmoothSlope; 
// Cyclic Component 
alpha = 0; 
HP = 0; 
SmoothHP = 0; 
PI = 3.1415926; 
alpha = ( 1 - sin( 2 * PI / Length ) )/cos( 2 * PI / Length ); 
HP = AMA2( Price - Ref( Price, -1 ), 0.5 * ( 1 + alpha ), alpha ); 
SmoothHP = ( HP + 2 * Ref( HP, -1 ) 
 + 2 * Ref( HP, -2 ) + Ref( HP, -3 ) ) / 6; 
Plot( Price, "Price", colorBlack, styleCandle ); 
// components of model 
// Plot( ITrend, "ITrend", colorRed ); 
// Plot( SmoothHP, "SmoothHP", colorRed ); 
Model = ITrend + SmoothHP; 
Plot( Model, "Model", colorRed ); 
  
RoundLotSize = 1;  
SetOption( "initialequity", 1000000 );  
SetOption( "commissionMode", 2 );  
SetOption( "commissionAmount", 0 );  
SetOption( "MaxOpenPositions", 1 );  
SetOption( "PriceBoundChecking", False );  
//SetPositionSize( 10000, spsValue );  
SetPositionSize( 10000/Ref(C, 0), spsShares ); 
SetPositionSize( 1, spsShares ); 
SetOption("FuturesMode", True ); 
 
Buy = Cover = Cross(ITrend, Ref(ITrend, -1)); 
 
Sell = Short = Cross(Ref(ITrend, -1), ITrend); 
 
BuyPrice = SellPrice = ShortPrice = CoverPrice = C; 

Strategy 2: Short ES

// Instantaneous Trendline 
Price = (H+L)/2; 
Length = Optimize("Length", 15, 2, 100, 1 ); 
SMA = MA( Price, Length ); 
Slope = Price - Ref( Price, - ( Length - 1 ) ); 
SmoothSlope = ( Slope + 2 * Ref( Slope, -1 ) 
 + 2 * Ref( Slope, -2 ) + Ref( Slope, -3 ) )/6; 
ITrend = SMA + 0.5 * SmoothSlope; 
// Cyclic Component 
alpha = 0; 
HP = 0; 
SmoothHP = 0; 
PI = 3.1415926; 
alpha = ( 1 - sin( 2 * PI / Length ) )/cos( 2 * PI / Length ); 
HP = AMA2( Price - Ref( Price, -1 ), 0.5 * ( 1 + alpha ), alpha ); 
SmoothHP = ( HP + 2 * Ref( HP, -1 ) 
 + 2 * Ref( HP, -2 ) + Ref( HP, -3 ) ) / 6; 
Plot( Price, "Price", colorBlack, styleCandle ); 
// components of model 
// Plot( ITrend, "ITrend", colorRed ); 
// Plot( SmoothHP, "SmoothHP", colorRed ); 
Model = ITrend + SmoothHP; 
Plot( Model, "Model", colorRed ); 
  
RoundLotSize = 1;  
SetOption( "initialequity", 1000000 );  
SetOption( "commissionMode", 2 );  
SetOption( "commissionAmount", 0 );  
SetOption( "MaxOpenPositions", 1 );  
SetOption( "PriceBoundChecking", False );  
//SetPositionSize( 10000, spsValue );  
SetPositionSize( 10000/Ref(C, 0), spsShares ); 
SetPositionSize( 1, spsShares ); 
SetOption("FuturesMode", True ); 
 
Buy = Cover = Cross(ITrend, Ref(ITrend, -1)); 
 
Sell = Short = Cross(Ref(ITrend, -1), ITrend); 
 
BuyPrice = SellPrice = ShortPrice = CoverPrice = C; 

Strategy 3: Long SPY

// Instantaneous Trendline 
Price = (H+L)/2; 
Length = Optimize("Length", 15, 2, 100, 1 ); 
SMA = MA( Price, Length ); 
Slope = Price - Ref( Price, - ( Length - 1 ) ); 
SmoothSlope = ( Slope + 2 * Ref( Slope, -1 ) 
 + 2 * Ref( Slope, -2 ) + Ref( Slope, -3 ) )/6; 
ITrend = SMA + 0.5 * SmoothSlope; 
// Cyclic Component 
alpha = 0; 
HP = 0; 
SmoothHP = 0; 
PI = 3.1415926; 
alpha = ( 1 - sin( 2 * PI / Length ) )/cos( 2 * PI / Length ); 
HP = AMA2( Price - Ref( Price, -1 ), 0.5 * ( 1 + alpha ), alpha ); 
SmoothHP = ( HP + 2 * Ref( HP, -1 ) 
 + 2 * Ref( HP, -2 ) + Ref( HP, -3 ) ) / 6; 
Plot( Price, "Price", colorBlack, styleCandle ); 
// components of model 
// Plot( ITrend, "ITrend", colorRed ); 
// Plot( SmoothHP, "SmoothHP", colorRed ); 
Model = ITrend + SmoothHP; 
Plot( Model, "Model", colorRed ); 
  
RoundLotSize = 1;  
SetOption( "initialequity", 100000 );  
SetOption( "commissionMode", 2 );  
SetOption( "commissionAmount", 0 );  
SetOption( "MaxOpenPositions", 1 );  
SetOption( "PriceBoundChecking", False );  
//SetPositionSize( 10000, spsValue );  
SetPositionSize( 100, spsPercentOfEquity ); 
//SetPositionSize( 1, spsShares ); 
SetOption("FuturesMode", False ); 
 
Buy = Cover = Cross(ITrend, Ref(ITrend, -1)); 
 
Sell = Short = Cross(Ref(ITrend, -1), ITrend); 
 
BuyPrice = SellPrice = ShortPrice = CoverPrice = C; 

Strategy 4: Short SPY

// Instantaneous Trendline 
Price = (H+L)/2; 
Length = Optimize("Length", 15, 2, 100, 1 ); 
SMA = MA( Price, Length ); 
Slope = Price - Ref( Price, - ( Length - 1 ) ); 
SmoothSlope = ( Slope + 2 * Ref( Slope, -1 ) 
 + 2 * Ref( Slope, -2 ) + Ref( Slope, -3 ) )/6; 
ITrend = SMA + 0.5 * SmoothSlope; 
// Cyclic Component 
alpha = 0; 
HP = 0; 
SmoothHP = 0; 
PI = 3.1415926; 
alpha = ( 1 - sin( 2 * PI / Length ) )/cos( 2 * PI / Length ); 
HP = AMA2( Price - Ref( Price, -1 ), 0.5 * ( 1 + alpha ), alpha ); 
SmoothHP = ( HP + 2 * Ref( HP, -1 ) 
 + 2 * Ref( HP, -2 ) + Ref( HP, -3 ) ) / 6; 
Plot( Price, "Price", colorBlack, styleCandle ); 
// components of model 
// Plot( ITrend, "ITrend", colorRed ); 
// Plot( SmoothHP, "SmoothHP", colorRed ); 
Model = ITrend + SmoothHP; 
Plot( Model, "Model", colorRed ); 
  
RoundLotSize = 1;  
SetOption( "initialequity", 100000 );  
SetOption( "commissionMode", 2 );  
SetOption( "commissionAmount", 0 );  
SetOption( "MaxOpenPositions", 1 );  
SetOption( "PriceBoundChecking", False );  
//SetPositionSize( 10000, spsValue );  
SetPositionSize( 100, spsPercentOfEquity ); 
//SetPositionSize( 1, spsShares ); 
SetOption("FuturesMode", False ); 
 
Buy = Cover = Cross(ITrend, Ref(ITrend, -1)); 
 
Sell = Short = Cross(Ref(ITrend, -1), ITrend); 
 
BuyPrice = SellPrice = ShortPrice = CoverPrice = C; 

Strategy 5: The mean reversion strategy:

Price = (H+L)/2; 
Length = Optimize("Length", 2, 2, 100, 1 ); 
SMA = MA( Price, Length ); 
Slope = Price - Ref( Price, - ( Length - 1 ) ); 
SmoothSlope = ( Slope + 2 * Ref( Slope, -1 ) 
 + 2 * Ref( Slope, -2 ) + Ref( Slope, -3 ) )/6; 
ITrend = SMA + 0.5 * SmoothSlope; 
// Cyclic Component 
alpha = 0; 
HP = 0; 
SmoothHP = 0; 
PI = 3.1415926; 
alpha = ( 1 - sin( 2 * PI / Length ) )/cos( 2 * PI / Length ); 
HP = AMA2( Price - Ref( Price, -1 ), 0.5 * ( 1 + alpha ), alpha ); 
SmoothHP = ( HP + 2 * Ref( HP, -1 ) 
 + 2 * Ref( HP, -2 ) + Ref( HP, -3 ) ) / 6; 
Plot( Price, "Price", colorBlack, styleCandle ); 
// components of model 
// Plot( ITrend, "ITrend", colorRed ); 
// Plot( SmoothHP, "SmoothHP", colorRed ); 
Model = ITrend + SmoothHP; 
Plot( Model, "Model", colorRed ); 
  
RoundLotSize = 1;  
SetOption( "initialequity", 100000 );  
SetOption( "commissionMode", 2 );  
SetOption( "commissionAmount", 0 );  
SetOption( "MaxOpenPositions", 1 );  
SetOption( "PriceBoundChecking", False );  
//SetPositionSize( 10000, spsValue );  
SetPositionSize( 100, spsPercentOfEquity ); 
//SetPositionSize( 1, spsShares ); 
SetOption("FuturesMode", False ); 
 
Sell = Short = Cross(ITrend, Ref(ITrend, -1)); 
 
Buy = Cover = Cross(Ref(ITrend, -1), ITrend); 
 
Sell = C > Ref(H,-1); 
Cover = C < Ref(L, -1); 
 
BuyPrice = SellPrice = ShortPrice = CoverPrice = C;