// =====EHLERS' EVEN BETTER SINEWAVE SYSTEM=====
function run()
{
set(PARAMETERS+TICKS);
NumSampleCycles = 6;
//High pass filter cyclic components whose periods are shorter than duration input
vars Price = series(price());
vars SmoothPrice = series(Smooth(Price, 10));
var Duration = optimize(40, 20, 70); //max length of trend trade
vars HP = series(HighPass1(Price, Duration));
plot("SmoothPrice", SmoothPrice, 0, ORANGE);
//Smooth with super smoother
vars Filter = series(Smooth(HP, 10));
//3-bar average of wave amplitude and power
vars Wave = series((Filter[0] + Filter[1] + Filter[2])/3);
vars Pwr = series((Filter[0]*Filter[0] + Filter[1]*Filter[1] + Filter[2]*Filter[2])/3);
//Normalize average wave amplitude to square root of the average power
vars ebsw = series(Wave[0]/sqrt(Pwr[0]));
////Decycler
var HPCutoff = optimize(40, 10, 60, 5);
vars HP = series(HighPass(SmoothPrice, HPCutoff));
vars Decycler = series(SmoothPrice[0] - HP[0]);
plot("Decycler", Decycler, 0, BLUE);
//Trade paramters
var threshold = optimize(0.8, 0.7, 0.95, 0.025);
vars Signals = series(0);
Stop = 4*ATR(20);
if (crossOver(ebsw, -threshold))
enterLong();
if (crossUnder(ebsw, threshold))
enterShort();
if (crossUnder(SmoothPrice, Decycler))
exitLong();
if (crossOver(SmoothPrice, Decycler))
exitShort();
plot("ebsw", ebsw, NEW, RED);
plot("up", threshold, 0, BLACK);
plot("down", -threshold, 0, BLACK);
}