The following code trades a portfolio of pairs. It works fine, except when there are several entry signals. In that case it mixes pairs, trading the long side from one pair and the short side from another pair. See in the pic that on date 01/05/2007 it opens the short from the pair (ESS/UDR, yellow) and the long from the pair (FITB/CATY, blue). It also fails when Trade Size Limit restricts entry in the second pair, leaving the first symbol alone.
How can I change it to always trade the right pairs or avoid completely the trade if it is not correct?
Thank you very much!
//options
SetPositionSize(20, spsPercentOfEquity);
SetOption("maxopenpositions", 4);
symbol1 = symbol2 = "";
if( InWatchListName("par1") ) { symbol1 = "ESS"; symbol2 = "UDR"; }
if( InWatchListName("par2") ) { symbol1 = "FITB"; symbol2 = "CATY"; }
if( InWatchListName("par3") ) { symbol1 = "APA"; symbol2 = "COP"; }
//open long, short and close signals
SetForeign(symbol2);
OpenLong = RSI() < 40;
openshort = RSI() > 60;
CloseSpd = Cross(RSI(),50) OR Cross(50,RSI());
RestorePriceArrays();
//initialize
Buy = Short = Sell = Cover = 0;
//IF OPENLONG WE BUY THE SPREAD
if( Name() == symbol2 )
{
Buy = OpenLong;
Short = OpenShort;
Sell = Cover = CloseSpd;
}
else if( Name() == symbol1 )
{
Short = OpenLong;
Buy = OpenShort;
Sell = Cover = CloseSpd;
}