Not using LastValue in Backtest

Hello - I am grateful if someone can help me find a solution.

After reading through the forum, I understand that we should never use LastValue in a backtest to avoid future leaks. Now, what alternative can you use so that the value taken from an array can be used in an If statement (can only use numeric or boolean type) and is from the correct date and time that the backtest is currently running through. My goal is to backtest through an extensive time frame so ValueWhen is diffuclt to implement. Example:

CloseX = LastValue(Close);
OpenX = LastValue (Open);

if (Closex > OpenX)
	{variable = 1;}


@edmondmishaancohen , have you seen the Immediate IF ( IIF ) command?

myvarUpClose = IIF(C>O,1,0);

That might be what you need. See the Fine Manual on how to properly use it.

Such IIF is not needed. All comparisons already return 1 (true) or 0 false. So

myVarUpClose = Close > Open; // you don't need IIF()

MUST-READ for everyone Understanding how AFL works

Thank you both for answering.

Tomasz - when I use myVarUpClose as coded in your repose, I still get the following error message when using the variable in an If function.

"Error 6. Conditoin in IF, WHILE, FOR statements has to be Numeric or Boolean type. You can not use array here, please use [ ] (array subscript operator) to access array elements"

Is there a way to backtest using arrays within an if function? LastValue would only take the latest value (obviously) and not the value that corresponds to the dates being backtested.

Thanks again

You do not need IIf function to return other array condition as TRUE case of Close > Open.
Just combine conditions via AND/OR operators.

is_bull_bar = Close > Open;
other_condition1 = C > Ma(C,200);

Buy = is_bull_bar AND other_condition1 /*AND ...*/;

Thanks fxshart

I try to use your code to assign a NewVariable using the result of other_condition1 or 2 or 3...

to assign a value to the NewVariable, I used the If function and still get the same error

Oh for heaven's sake stop using arrays in if statement.
Why are flying towards closed windowpane multiple times?
It is explained in manual already not to do it and why not doing it.
See coding mistakes:

Common Coding mistakes

if-else statement needs boolean (or single numeric expression), not array

The if keyword executes statement1 if expression is true (nonzero); if else is present and expression is false (zero), it executes statement2. After executing statement1 or statement2, control passes to the next statement. Expression must be boolean ( True/False) type (so it CANNOT be ARRAY because there would be no way do decide whether to execute statement1 or not, if for example array was: [True,True,False,.....,False,True] )

if( expression )


if( Close > Open ) //  WRONG     
    Color = colorGreen; //statement 1 
    Color = colorRed; //statement 2 
Plot(Close,"Colored Price",Color,styleCandle);

The above example is wrong, as both Open and Close are arrays and such expression as Close > Open is also an ARRAY. The solution depends on the statement. It’s either possible to implement it on bar-by-bar basis, with use of FOR loop:

for(  i = 0; i < BarCount; i++ )  
   if( Close[ i ] > Open[ i ] ) // CORRECT        
      Color[ i ] = colorGreen;    
      Color[ i ] = colorRed;    
Plot( Close, "Colored Price", Color, styleCandle );

It is also possible in this case to use IIf( ) function:

Color = IIf( Close > Open, colorGreen, colorRed ); //  ALSO CORRECT - working directly on arrays 
Plot( Close, "Colored Price", Color, styleCandle );

Also there is thread regarding if-else vs. IIf.

What is your goal?
Please post entire code.

@edmondmishaancohen -


Everything that you really need to understand is written in that article. Carefully re-read it, 4-5 or more times until it sinks. You have to adopt thinking that EVERYTHING is an array, data series.

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