//Ehlers' Even Better Sine Wave
function run()
{
var Duration = 40; //highpass
var Cutoff = 10; //lowpass
vars Price = series(price());
static var alpha1,a1,b1,c2,c3,c1;
if(is(INITRUN)) {
alpha1 = (1 - sin(2*PI/Duration)) / cos(2*PI/Duration);
a1 = exp(-1.414*PI / Cutoff);
b1 = 2*a1*cos(1.414*PI / Cutoff);
c2 = b1;
c3 = -a1*a1;
c1 = 1 - c2 - c3;
}
//High pass filter cyclic components whose periods are shorter than duration input
vars HP = series(Price[0], 2);
HP[0] = 0.5*(1 + alpha1)*(Price[0] - Price[1]) + alpha1*HP[1];
//Smooth with a Super Smoother Filter
vars Filt = series(HP[0], 3);
Filt[0] = c1*(HP[0] + HP[1]) / 2 + c2*Filt[1] + c3*Filt[2];
//3 Bar average of Wave amplitude and power
//Normalize the Average Wave to Square Root of the Average Power
vars EBSW = series(0, 1);
EBSW[0] = ((Filt[0] + Filt[1] + Filt[2])/3)/sqrt((Filt[0]*Filt[0] + Filt[1]*Filt[1] + Filt[2]*Filt[2])/3);
plot("EBSW", EBSW, NEW, RED);
}