Signal filtering and smoothing functions

Category: Programming

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”

//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

john ehler super smoother

 Laguerre Filter

//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

laguerre filter

 

ALMA – Arnaud Legoux Moving Average

//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

ALMA moving average

 

LowPass filter

//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

lowpass filter

Hull moving average

//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

hull moving average

 

Zero-Lag moving average

//ZeroLag MA:
Period = 20
Data = Close
lag = ROUND((Period-1)/2)
d = (Data+(Data-Data[lag]))
S = exponentialaverage[period](d)
return S

zero lag moving average


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! 🙂

Logo Logo
Loading...