IIf statement returning neither TrueArray or FalseArray


I have an IIf statement that is not returning either the TrueArray or the FalseArray.

The code I am using is as follows:

SetBacktestMode( backtestRotational );

Max_Open_Positions = 2;

SetOption("InitialEquity", 100000 );
SetOption("MaxOpenPositions", Max_Open_Positions );
SetPositionSize( 100/Max_Open_Positions, spsPercentOfEquity );
SetOption("AllowPositionShrinking", True );
RoundLotSize = 0;

delistedSecurity = datetime() >= GetFnData("DelistingDate");
PositionScore = IIf(delistedSecurity == True, 1, 2);

The code for delistedSecurity comes from the KB article AmiBroker Knowledge Base » How to handle delisted symbols in rotational test

I expect the PositionScore statement on the final line would set PositionScore for all symbols to either 1 or 2.

I run the code over a watchlist of two symbols, one that gets delisted during the backtest period and one that does not get delisted. The detailed log shows the PositionScore for the delisted symbol is correctly set to 2 while it is trading and then to 1 when it delists. However, the PositionScore for the non delisted stock is always set to 0.

When I force delistedSecurity to True all PositionScores are 1. When I force delistedSecurity to False all PositionScores are 2.

I can only think that

delistedSecurity = datetime() >= GetFnData("DelistingDate");

is returning something other than True or False.

Can anyone explain what is going on?



The score (PositionScore) for all securities is calculated first. Then all scores are sorted according to absolute value of PositionScore. Then top N are choosen to be traded. N depends on available funds and "max. open positions" setting. Backtester successively enters the trades starting from highest ranked security until the number of positions open reaches "max. open positions" or there are no more funds available. The score has the following meaning:

  • higher positive score means better candidate for entering long trade
  • lower negative score means better candidate for entering short trade
  • the score of zero means no trade (exit the trade if there is already open position on given symbol)
  • the score equal to scoreNoRotate constant means that already open trades should be kept and no new trades entered
  • the score equal to scoreExitAll constant causes rotational mode backtester to exit all positions regardless of HoldMinBars. Note that this is global flag and it is enough to set it for just any single symbol to exit all currently open positions, no matter on which symbol you use scoreExitAll (it may be even on symbol that is not currently held). By setting PositionScore to scoreExitAll you exit all positions immediatelly regardless of HoldMinBars setting

Use this instead,

delistedSecurity = Nz(DateTimeDiff(DateTime(), GetFnData("DelistingDate")) >= 0);
PositionScore = IIf(delistedSecurity, 1, 2);

@TrendSurfer - Thanks. From your code suggestion I can see GetFnData("DelistingDate") is not returning a valid datetime value for a security that does not have a delisting date which makes sense. However, I originally found the code for delistedSecurity (aka exitLastBar) in AmiBroker Knowledge Base » How to handle delisted symbols in rotational test . It looks like the code in the KB could only return True/False for a security with an actual delisting date and NULL/NaN for all other securities. Does the code in the KB work for anyone else or does the KB need to be updated?

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