LowPass vs EMA

Posted By: pstoto76

LowPass vs EMA - 06/25/13 21:39

Hi I am new in this forum. I have been through zorro tutorial and I was very impressed by it so I decided to spend more time on it to understand more deeply how it worked. Naturally, I start reading the book "Cybernetic Analysis of Stocks and Futures" and the algorithm for the Low Pass function. I have to be honest, even if I have some background in math I haven't understand the algorithm explanation (which requires some electronic/physic background).
Still, I implemented it in mql4 in order to compare it with the exponential moving average. If you compare the LowPass(100) with the EMA(50) you get exactly the same peak and valley with no lagging. The point then, is I don't really see the added value of the LowPass filter. I wonder if I missed something??? (in the image the LowPass(100) is in red and the EMA(50) in green).
Posted By: Anonymous

Re: LowPass vs EMA - 06/25/13 23:08

I wonder how you checked that peak and valleys are exactly at the same place? Hopefully not by eyeballing few data points? Our eyes are imperfect tool for such precise measurement, that's why we have Zorro for such daunting tasks. grin

Here's quick and dirty script that trades peaks and walleys for your example. I took the liberty to add ALMA to the party (not yet available in Zorro, but soon...). EMA50 took 571 trades, and LP100 only 360. So, it looks like LP100 is much smoother than EMA50. ALMA got even less trades, only 292. And of all three, only ALMA actually came out profitable, but that is of course a pure coincidence. If it only was that easy, you trade MA or a simple crossover and your wallet gets fatter every day. grin

Code:
#define CYAN 0x00ffff
#define ORANGE 0xff8000

var ALMA(var *Data, int Period, int sigma, var offset)
{
	var m = floor(offset * (Period - 1));
	var s = Period / sigma;
	var alma, wSum;
	int i;

	for (i = 0; i < Period; i++) {
		var w = exp(-((i - m) * (i - m)) / (2 * s * s));
		alma += Data[Period - 1 - i] * w;
		wSum += w;
	}
	
	return alma / wSum;
}

var ALMA(var *Data, int Period) {
	return ALMA(Data, Period, 6, 0.85);
}

function run()
{
	set(PLOTNOW|PLOTPRICE);
	StartDate = 2012;
	LookBack = 400;
	BarPeriod = 60;
	PlotWidth = 8000;

	vars Close = series(priceClose());
	vars MA1 = series(EMA(Close, 50));
	vars MA2 = series(LowPass(Close, 100));
	vars MA3 = series(ALMA(Close, 100));

	if (peak(MA1)) {
		if (!NumOpenLong)
			enterLong();
	} else if (valley(MA1)) {
		if (!NumOpenShort)
			enterShort();
	}

	plot("EMA50", MA1[0], 0, BLUE);
	plot("LP100", MA2[0], 0, ORANGE);
	plot("ALMA100", MA3[0], 0, CYAN);
}



Posted By: jcl

Re: LowPass vs EMA - 06/26/13 07:41

I can confirm that LP100, when correctly implemented, has very different peaks and valleys than EMA50. It is not possible to emulate a LP function with an EMA. Regardless how you set the EMA's time frame, you can only match a few peaks or valleys, but most won't match.

The reason is the different frequency response of LP and EMA. EMA is a first order lowpass filter, LP is second order.

Admittedly the curves in your screenshot look quite similar - in fact they both look like EMA variants. A LP line is normally smoother and has no small ripples as in your screenshot at the left side. But that might be just chance, it's hard to tell at this scale.
© 2024 lite-C Forums