# Pair trading - Avoiding trades in a symbol which has open positions

I'm backtesting a universe of 50 equity pairs. From this universe, I don't want to trade more than 10 equity pairs (i.e. maxopenpositions - 20, positionsize = -5). Most equity symbols in the universe are used for multiple pairs (e.g. pair 1 AAPL & MSFT, pair 2 MSFT & NFLX). I want to avoid trades in symbols which already have open positions (i.e. if AAPL MSFT pair is currently open, the signal generated on MSFT NFLX (if true) should be avoided).

Attaching the code that I'm currently using, for 2 pairs with overlapping stocks. It is without any logic to avoid trades.

Any help is greatly appreciated.

Thanks

``````SetOption("AccountMargin",100);
SetOption("MaxOpenPositions",4);
SetOption("InitialEquity",100000);
SetPositionSize(25,spspercentofequity);

for( i = 1 ; i < 3; i++)
{
Pair = i;
if(Pair == 1) { StockA = "AAPL"; StockB = "MSFT" ; }
if(Pair == 2) { StockA = "MSFT"; StockB = "NFLX" ; }

A = Foreign(StockA,"C");
B = Foreign(StockB,"C");
Ratio = A / B;

Mean = MA(Ratio,Base);
UB = BBandTop(Ratio,Base,Dev);
LB = BBandBot(Ratio,Base,Dev);

LERatio = Cross(LB,Ratio);
LXRatio = Cross(Ratio,Mean);
SERatio = Cross(Ratio,UB);
SXRatio = Cross(Mean,Ratio);

LERatio = ExRem(LERatio,LXRatio);
LXRatio = ExRem(LXRatio,LERatio);
SERatio = ExRem(SERatio,SXRatio);
SXRatio = ExRem(SXRatio,SERatio);

if(Name() == StockA)
{
Sell = LXRatio;
Short = SERatio;
Cover = SXRatio;
}

if(Name() == StockB)
{
Sell = SXRatio;
Short = LERatio;
Cover = LXRatio;
}