In TASC 8/24, John Ehlers presented a new algorithm for separating the trend line from a price curve, using spectral analysis functions. Trend lines are only useful for trading when they have little lag, so that trend changes can immediately trigger trade signals. The usual suspects like SMA, WMA, EMA are too laggy for this. Let’s see how good this new algorithm works. The functions below are a 1:1 conversion from Ehlers’ TradeStation code to C.
var HighPass3(vars Data, int Length) { var f = 1.414*PI/Length; var a1 = exp(-f); var c2 = 2*a1*cos(f); var c3 = -a1*a1; var c1 = (1+c2-c3)/4; vars HP = series(0,4); return HP[0] = c1*(Data[0] - 2*Data[1] + Data[2]) + c2*HP[1] + c3*HP[2]; } var TROC; var Trend(vars Data,int Length1,int Length2) { var HP1 = HighPass3(Data,Length1); var HP2 = HighPass3(Data,Length2); vars Trends = series(HP1-HP2,2); TROC = (Length2/6.28)*(Trends[0]-Trends[1]); return Trends[0]; }
The filter name is HighPass3 because Zorro got already threee other highpass filters in its library. We apply the trend function to an ES chart from 2022-2024, and plot also the highpass filter and the intermediate TROC variable:
void run() { BarPeriod = 1440; StartDate = 20221001; EndDate = 20240401; LookBack = 250; assetAdd("ES","YAHOO:ES=F"); asset("ES"); plot("HP",HighPass3(seriesC(),250),NEW,RED); plot("Trend",Trend(seriesC(),250,40),NEW,RED); plot("ROC",TROC,0,BLUE); }
The resulting chart:
We can see that the red trend line in the bottom chart gives a pretty good approximation of the price curve trend with no noticable lag. The code can be downloaded from the 2024 script repository.