This article deals with some functions that be can used to filter signals of any kind. Each individual code snippet present in this blog post would operate as data smoothing or filtering in your own indicators or automated strategies development.Each function need its “Data” variable to be populate with with your own variable value. In these examples, the Close value is used. Please feel free to use them for any purpose!
John Ehlers’ “Super Smoother”
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//John Ehlers’ “Super Smoother”, a 2-pole Butterworth filter combined with a 2-bar SMA that suppresses the Nyquist frequency: Period = 10 Data = Close PI = 3.14159 f = (1.414*PI) / Period a = exp(-f) c2 = 2*a*cos(f) c3 = -a*a c1 = 1 - c2 - c3 if barindex>Period then S = c1*(Data[0]+Data[1])*0.5 + c2*S[1] + c3*S[2] endif return S |
Laguerre Filter
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//Laguerre, a 4-element Laguerre filter: Data = Close alpha = 0.2 if barindex>6 then L0 = alpha*Data + (1 - alpha)*L0[1] L1 = -(1 - alpha)*L0 + L0[1] + (1 - alpha)*L1[1] L2 = -(1 - alpha)*L1 + L1[1] + (1 - alpha)*L2[1] L3 = -(1 - alpha)*L2 + L2[1] + (1 - alpha)*L3[1] S = (L0 + 2*L1 + 2*L2 + L3) / 6 else S = Data endif return S |
ALMA – Arnaud Legoux Moving Average
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
//ALMA - Arnaud Legoux Moving Average: Window = 9 Sigma = 6 Offset = 0.85 Data = Close m = (Offset * (Window - 1)) s = Window/Sigma WtdSum = 0 CumWt = 0 for k = 0 to Window - 1 do Wtd = Exp(-((k-m)*(k-m))/(2*s*s)) WtdSum = WtdSum + Wtd * Data[Window - 1 - k] CumWt = CumWt + Wtd next S = WtdSum / CumWt return S |
LowPass filter
1 2 3 4 5 6 7 8 |
//LowPass, a second order lowpass filter with the following source code: Period = 14 Data = Close a = 2.0/(1+Period) if barindex>Period then S = (a-0.25*a*a)*Data[0]+ 0.5*a*a*Data[1]- (a-0.75*a*a)*Data[2]+ 2*(1-a)*S[1]- (1-a)*(1-a)*S[2] endif return S |
Hull moving average
1 2 3 4 5 6 |
//HullMA, the Hull Moving Average: Period = 14 Data = Close inner = 2*weightedaverage[round(Period/2)](Data)-weightedaverage[Period](Data) S = weightedaverage[round(sqrt(Period))](inner) return S |
Zero-Lag moving average
1 2 3 4 5 6 7 8 |
//ZeroLag MA: Period = 20 Data = Close lag = ROUND((Period-1)/2) d = (Data+(Data-Data[lag])) S = exponentialaverage[period](d) return S |
A lot of others digital filters exist, so I’ll post them here each time I’ll stumble upon new ones.
Of course anyone contribution would also be greatly appreciated! Thanks in advance for any kind of contribution that could benefit to the community! 🙂
Very nice review
Great work, Nicolas!
One question: Is there any way to apply an indicator to another indicator, without having to do a new indicator? For example, if I wanted to do a ZeroLag MA on an RSI value in a chart: Could I integrate in the code that user can select what value to apply the indicator to?
You’ll have to rebuild an indicator on your own, with the RSI value.
I thought I might have to. Thanks for answering anyway!