Function required?

Hi guys

I am trying to backtest an idea I recently had. I dont have a proper programming background so I am having a though time finding a way to program the logic.

Basically I want to draw a line. The lines recent value always relies on the value before. Its basically

x = IIf(Ma20 > x[1], MA20, x[1])

But it gives me an Error29 - Variable 'x' used without having been initialized.

So added a line of code above the IIf statement.

x = 0.0;

But as I understand it, in this case with every new candle x equals 0 again. How do I fix this properly? Is there a FirstCandle-Function so I can write an if-clause for that?

Thanks for the help.

See self referencing post by @Tomasz here.


But if you think about your formula yourself then it simply tries to look for highest MA20.
So if you would look up at AFL function reference guide then you would find it within seconds.

So you just need this short single line of code

x = Highest(MA(C,20));

So upper line together with output:

myma = MA(C,20);
x = Highest(myma);
Plot( C, "Price", colorDefault, styleBar );
Plot( myma, "MA20", colorRed );
Plot( x, "Highest MA20", colorOrange );

And if you compare it then it is getting the same result as this loop version but Highest() being much FASTER.

x = 0;
myma = MA(C,20);
for( i = 1; i < BarCount; i++ )
  x[i] = IIf(myma[i] > x[i-1], myma[i], x[i-1]);

Plot( C, "Price", colorDefault, styleBar );
Plot( myma, "MA20", colorRed );
Plot( IIf(x>0, x, Null), "Highest MA20", colorOrange );
1 Like

Thank you a lot for your help!

1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.