Pairs trading mixes signals in a portfolio

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!

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
OpenLong = RSI() < 40;
openshort = RSI() > 60;
CloseSpd = Cross(RSI(),50) OR Cross(50,RSI());

Buy = Short = Sell = Cover = 0;

if( Name() == symbol2 )
    Buy = OpenLong;  
    Short = OpenShort;  
    Sell = Cover = CloseSpd;     
else if( Name() == symbol1 ) 
    Short = OpenLong;  
    Buy = OpenShort;  
    Sell = Cover = CloseSpd;    


The code specifies maxopenpositions is 4, but it generates signals for 6 symbols. In addition, there is no code to set the position scores which is needed to prioritize the signals. What happens if you set maxopenpositions to 6 and set position size to 16%?


@Armin_Tamzarian, I think that probably you can avoid the mixup of pairs assigning a different positionscore to each pair (when, as pointed by @Steve, you have a max positions value set to less than the total of pairs).

symbol1 = symbol2 = "";
positionScore = 0;
if( InWatchListName("par1") )	{	symbol1 = "ESS";	symbol2 = "UDR";	positionScore=100;}  
if( InWatchListName("par2") )	{	symbol1 = "FITB";	symbol2 = "CATY";	positionScore=90;}  
if( InWatchListName("par3") )	{	symbol1 = "APA";	symbol2 = "COP";	positionScore=80;}  

Just try it (I did not have the opportunity to test is as needed).


Thank you, that works :))))


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