Buy and Sell using IIF condition in rotational type system

Hello,
I am trying to create system that rotate tickers based on percent rank of other ticker.

This is my basic code:

SetOption("InitialEquity",10000);                  
SetOption("InterestRate",0);	
SetOption("MaxOpenPositions",1);                
SetOption("UsePrevBarEquityForPosSizing",True);
SetOption("AllowPositionShrinking", True); 
SetOption("accountmargin",100); 

SetForeign("$VIX"); 
VIXRank=PercentRank(C,252);
Plot( VIXRank, "VIXRank", colorRed ); 
RestorePriceArrays();

Buy=Sell=0;

if(Name() == "SPY")
{
	IIf ((VIXRank>30 AND VIXRank<70), Buy=True,  Sell=True);

}

if(Name() == "TLT")
{
	IIf ((VIXRank>70), Buy=True,  Sell=True);

}

BuyPrice=Close;
SellPrice=Close;

Filter = 1; // show all bars
AddColumn( C, "Close" );
AddColumn( VIXRank, "VIXRank", 1.0 );
AddColumn( Buy, "Buy", 1.0, colorBlack );
AddColumn( Sell, "Sell", 1.0, colorBlack );

SPY should be bought if VIXRank is between 30 and 70.
But it is not working - I can see Buy and Sell signal all the time:

rotational

Can you help me were is my mistake?
Thank you.

@peterfin
The rotational systems are based on PositionScore and I don't see any in your code. In my opinion, you should have something like this:

PositionScore = VIXRank

Best regards,
Slawomir

You might also take a look here

Thank you.

I did change it to rotationalbackest. But the main problem was in the way I used Iff function.

This is working:

SetBacktestMode( backtestRotational );
SetOption("MaxOpenPositions",1);
SetOption("WorstRankHeld",0);
SetPositionSize( 100, spsPercentOfEquity );


SetForeign("$VIX"); 
VIXRank=PercentRank(C,252);
Plot( VIXRank, "VIXRank", colorRed ); 
RestorePriceArrays();


if(Name() == "SPY")
{
	PositionScore= IIf ((VIXRank>=30 AND VIXRank<=70), 1,  0);
}

if(Name() == "TLT")
{
	PositionScore= IIf ((VIXRank>70), 1,  0);
}

if (Name() == "GLD")
{

	PositionScore= IIf ((VIXRank<30), 1,  0);
}

BuyPrice=Close;
SellPrice=Close;

Filter = 1; // show all bars
AddColumn( C, "Close" );
AddColumn( VIXRank, "VIXRank", 1.0 );
AddColumn( PositionScore, "PositionScore", 1.0, colorBlack );
1 Like