The Butterworth filter is a type of signal processing filter designed to have a frequency response as flat as possible in the passband. It is also referred to as a maximally flat magnitude filter.
Properties of the Butterworth filter are: - monotonic amplitude response in both passband and stopband - Quick roll-off around the cutoff frequency, which improves with increasing order - Considerable overshoot and ringing in step response, which worsens with increasing order - Slightly non-linear phase response - Group delay largely frequency-dependent
Code
#include <profile.c>
var Butterworth2Pole(var* Data, int Period)
{
var a = exp(-1.414*PI/Period);
var b = 2*a*cos(1.414*1.25*180/Period);
var c2 = b;
var c3 = -a*a;
var c1 = 1-c2-c3;
var* Filt = series(*Data,3);
// syntax error? TODO: check what happened w/ SETSERIES
// SETSERIES(Data,0);
series(Data[0],0); // is that an equivalent?
return Filt[0] = c1*Data[0] + c2*Filt[1] + c3*Filt[2];
}
function run()
{
set(PLOTNOW);
BarPeriod = 1;
LookBack = 100;
StartDate = 20210702;
EndDate = 20210816;
vars seriesClose = series(priceClose());
var sma = SMA(seriesClose, 5);
var butt = Butterworth2Pole(seriesClose, 5);
var butt3Pole = Butterworth(seriesClose, 5);
plot("SMA 5", sma, MAIN, SILVER);
plot("2 Pole Butt", butt, MAIN, RED);
plot("3 Pole Butt", butt3Pole, MAIN, BLUE);
}