Ehler's Instantaneous Trendline

Posted By: zcom001

Ehler's Instantaneous Trendline - 11/08/13 09:52

Hi guys,
I am just starting with Zorro and I am sorry for such a silly question.

I have been trying to code Instantaneous Trendline indicator from Ehler's Cybernetic Analysis book and I am unfortunately still getting screwed output... :-/ Could you please have a look on my code and advise me what I am doing wrong?

Instantaneous Trendline formula in EasyLanguage from Ehler's book:

Code:
InstTrend = (α − (α/2)2) * Price + (α2/2) * Price[1]
− (α − 3α2/4) * Price[2]) + 2 * (1 − α)
* InstTrend[1] − (1 − α)2 * InstTrend[2];



My Zorro script:

Code:
var Alfa = 0.05;
var ITrend[4];

function run()

{

StartDate = 2012;
EndDate = 2013;
BarPeriod = 1440;

var* Price = series(price());

//Fill Instantaneous Trendline at time 0, 1 and 2 with some 
//values in the beginning of the testing period.

if (Bar < 4)
{
	ITrend[0] = (Price[0] + 2 * Price[1] + Price[2])/4;
	ITrend[1] = (Price[1] + 2 * Price[2] + Price[3])/4;
	ITrend[2] = (Price[2] + 2 * Price[3] + Price[4])/4;
	}
else
{

// Formula for Instantaneous Trendline itself after Bar 3.

	ITrend[0] = (Alfa - Alfa * Alfa/4)*Price[0] + 0.5 * Alfa * Alfa * Price[1] - (Alfa - 0.75 * Alfa * Alfa)*Price[2]+2*(1-Alfa)*ITrend[1]-(1-Alfa)*(1-Alfa)*ITrend[2];
}


// Plot ITrend value

plot("ITrend",ITrend[0],0,RED);

// Plot ITrend values at time 0, 1 and 2 for troubleshooting.

plot("ITrend0",ITrend[0],NEW,RED);
plot("ITrend1",ITrend[1],0,BLUE);
plot("ITrend2",ITrend[2],0,YELLOW);

set(PLOTNOW);
	
}


The problem obviously is that value of ITrend[1] and ITrend[2] remains constant from the beginning of the test period. I really don't understand why, because I think that every new run of run function after the new bar should shift the values of ITrend...

Thank you very much for your help in advance,

zcom001
Posted By: swingtraderkk

Re: Ehler's Instantaneous Trendline - 11/08/13 12:55

zcom,

On the first bar are you not trying to set the price & trend of non existent bars, ITrend[1] & Price[1] or higher don't exist yet?

I'd try simply setting ITrend[0] = Price[0] for the first 2 or 3 Bars and use Lookback to ignore the first several bars as teh distortion works through.

Does Ehlers specify using close prices i.e. priceClose() or average prices therefore price()?
Posted By: DdlV

Re: Ehler's Instantaneous Trendline - 11/08/13 13:02

Hi zcom001. Don't have time right now to look in detail or try to correct, but my 1st reaction is you should use a series for ITrend, not an array. There are examples in indicators.c, among other places.

HTH.
Posted By: swingtraderkk

Re: Ehler's Instantaneous Trendline - 11/08/13 13:40

I agree with Ddlv, a series would be better.

I'd assumed on a quick look that you had used series, so my references to ITrend[1] not existing is of course only true if it was series like Price but not for the array.

Code:
vars Price	    	= series(price());
vars ITrendline 	= series();

if (Bar < 4)
{
	ITrendline[0] = Price[0];
}

else
{
	ITrendline[0] = (((Alfa - ((Alfa /2) * (Alfa/2))) * Price[0])
					  + (((Alfa * Alfa)/2) * Price[1])
					  - ((Alfa - ((3 * (Alfa * Alfa)) / 4)) * Price[2])
					  + ((2*(1-Alfa)) * ITrendline[1])
					  - (((1-Alfa) * (1-Alfa)) * ITrendline[2]));
}



Seems to work.
Posted By: zcom001

Re: Ehler's Instantaneous Trendline - 11/08/13 21:47

Hi swingtraderkk and DdlV, it's working, thank you very much for a quick help, much appreciated... :-)

Regards,

zcom001

Originally Posted By: swingtraderkk
I agree with Ddlv, a series would be better.

I'd assumed on a quick look that you had used series, so my references to ITrend[1] not existing is of course only true if it was series like Price but not for the array.

Code:
vars Price	    	= series(price());
vars ITrendline 	= series();

if (Bar < 4)
{
	ITrendline[0] = Price[0];
}

else
{
	ITrendline[0] = (((Alfa - ((Alfa /2) * (Alfa/2))) * Price[0])
					  + (((Alfa * Alfa)/2) * Price[1])
					  - ((Alfa - ((3 * (Alfa * Alfa)) / 4)) * Price[2])
					  + ((2*(1-Alfa)) * ITrendline[1])
					  - (((1-Alfa) * (1-Alfa)) * ITrendline[2]));
}



Seems to work.
Posted By: RTG

Re: Ehler's Instantaneous Trendline - 07/02/14 01:17

When I test ITrendline, it produces an oscillator, not a filtered moving average of price. Ehlers' book gives an example and shows the ITrendline moving along with an EMA of price much like a traditional moving average.

Is this the intention?
Posted By: jcl

Re: Ehler's Instantaneous Trendline - 07/02/14 15:15

You're probably mistaken. The code above does not look like an oscillator. Check if you plotted it correctly and used right parameter values.
Posted By: RTG

Re: Ehler's Instantaneous Trendline - 07/04/14 02:22

Which value are you using for Alfa?

Ehlers uses 0.07 in his book which is a 14 day average.

As Alfa approaches 1, ITrend more closely maps onto price. But an moving average of length 1 isn't averaging anything.
© 2024 lite-C Forums