LowPass filter is not the same as Workshop 4a

Posted By: vinitrinh

LowPass filter is not the same as Workshop 4a - 04/28/22 02:43

I've seen other posts that shared the formula of the LowPass filter and have tried the workshop 4a script. However, it seems like the formula is not producing the same results as the inbuilt LowPass filter.

Below is the code for recreating the experiment.
Comment the second low pass codes for Trends and MMI to replicate results.

Also attached are the backtest results of using the inbuilt LowPass and that of using Workshop 4a's lowpass.

Do we know the exact formula of the inbuilt lowpass?

Code
var lowpass(vars In,int Period)
{
  var a = 1./(1+Period);
  vars Out = series(In[0],3);
  return Out[0] = (a-0.25*a*a)*In[0]
    + 0.5*a*a*In[1]
    - (a-0.75*a*a)*In[2]
    + (2-2*a)*Out[1]
    - (1-a)*(1-a)*Out[2];
}

function run()
{
	EndDate = 20171231; // fixed simulation period 
	Verbose = 2;
	LookBack = 300;	// needed for MMI
	asset("EUR/USD");
	set(LOGFILE,PLOTNOW); // log all trades

	vars Prices = series(priceClose());
	vars Trends = series(LowPass(Prices,300));
    vars Trends = series(lowpass(Prices,300));
    vars Trends2 = series(lowpass(Prices,300));

	
	Stop = 30*ATR(100); // very distant stop
	
	vars MMI_Raws = series(MMI(Prices,300));
	vars MMI_Smooths = series(LowPass(MMI_Raws,300));
    vars MMI_Smooths = series(lowpass(MMI_Raws,300));
	
	if(falling(MMI_Smooths)) 
	{
		if(valley(Trends))
			enterLong();
		else if(peak(Trends))
			enterShort();
	}

    string Row = strf(
        "%02i/%02i/%02i %02i:%02i,%.5f,%.5f,%.5f,%.5f,%.5f,%.5f\n",
        day(),month(),year()%100,hour(),minute(),
        priceO(),priceH(),priceL(),priceC(), Trends[0], Trends2[0]);

    if(is(INITRUN))
        file_delete("Data\\export.csv");
        // Create header row here
        // file_append("Data\\export.csv",Row);
    else
        file_append("Data\\export.csv",Row);
}


Attached picture inbuilt LowPass.png
Attached picture workshop4a LowPass.png
Posted By: jcl

Re: LowPass filter is not the same as Workshop 4a - 04/28/22 07:48

That's right. The internal filters use a slightly different conversion of time period to a: 2./(1+Period). That's the usual 'smoothing formula' that is also used for EMA and other accumulating indicators.

The filter is the same, only the time period is different. I believe in the tutorial 1./(1+Period) was used for better explaining the int/var issue, but I think we'll also change the tutorial to the more common formula.
Posted By: rki

Re: LowPass filter is not the same as Workshop 4a - 04/08/23 19:22

this is the Python code

def lowpass (Data, period):
a=2.0/(1+period)
out=[]
for i in range(2, len(Data)):
if i==2:
LP=np.array([Data[0] for _ in range(3)])
print (LP)

LP[0]=(a-0.25*a*a)*Data[i-2] + 0.5*a*a*Data[i-1] - (a-0.75*a*a)*Data[i] + 2*(1.-a)*LP[1] - (1.-a)*(1.-a)*LP[2]
out.append (LP[0])
LP=np.roll(LP, 1)
return out
Posted By: vinitrinh

Re: LowPass filter is not the same as Workshop 4a - 07/02/23 13:26

Thanks for the helpful responses, both.
© 2024 lite-C Forums