Translating EasyLanguage to AFL - recursivity

Hello,

Probably a noob question, but any help will be appreciated. :slight_smile:
I'm trying to translate some simple Easy Language code into AFL, but I think I'm having issues with recursivity and initial conditions. I have read other posts on this matter, and I understand that AFL computes everything in an array in 1 pass, with no "current bar" concept (unless you use a for loop).

Here's the Easy Language code source:

Inputs:	Price((H+L)/2);

Vars:	InPhase(0),
	Quadrature(0);

If CurrentBar > 5 then begin

	Value1 = Price - Price[6];
	Value2 =Value1[3];
	Value3 =.75*(Value1 - Value1[6]) + .25*(Value1[2] - Value1[4]); 	
	InPhase = .33*Value2 + .67*InPhase[1];
	Quadrature = .2*Value3 + .8*Quadrature[1]; 

	Plot1(Inphase, "I");
	Plot2(Quadrature, "Q");

end;

I first translated the recursive code like this:

Quadrature = 0.2*Value3 + 0.8*Ref(Quadrature,-1);

But my results were very different than the ones I got with the Multicharts trial version.
After reading some posts here, I found that my code wasn't recursive and I was just filling Quadrature will the contents of 0.2*VAlue3 + 0

I understand I would probably have to use the SUM function, but I don't know how exactly. Could someone please give me a hint to put me in the right track? I guess there is a way to do this without using for... loops.

Many thanks in advance! :slight_smile:

@jpeirano, have you tried translating any of the other lines?

On quick look, I don't think it should be too difficult.

Try converting a line at a time and see what you get.

Good job posting your work, but if you try a bit harder you might just surprise yourself.

Your Ref() code is incorrect. Correct code uses AMA/AMA2

Quadrature = AMA( Value3, 0.2 ); // that does 0.2 * Value3 + (1-0.2) * Previous

or using AMA2:

Quadrature = AMA2( Value3, 0.2, 0.8 ); // that does 0.2 * Value3 + 0.8 * Previous

Your question is already answered here: Doubt when using self referencing

Use search before posting.

Your Tradestation code can be translated to single call to IIR function http://www.amibroker.com/f?iir or AMA/AMA2 functions http://www.amibroker.com/f?ama

4 Likes

As been mentioned in thread it is mostly AMA* code.
Here is possible translation of post#1

function Momentum(array, period) {
	// From Charts - Indicators
	return array-Ref(array,-period);
}

/// EL to AFL translation
/// @link https://forum.amibroker.com/t/translating-easylanguage-to-afl-recursivity/19636/2
Price = (H+L)/2;
mom = Momentum(Price,6);// Momentum
mom2 = 0.75*Momentum(mom,6) + 
       0.25*Momentum(Ref(mom,-2),2); 
InPhase = AMA(Ref(mom,-3), 0.33);
Quadrature = AMA(mom2,0.2); 

Plot(Inphase, "I", colorOrange);
Plot(Quadrature, "Q", colorRed);
PlotGrid(0, colorGold);
3 Likes

Many thanks to all!

Your help if very much appreciated. :slight_smile: