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);
SetTradeDelays(0,0,0,0);

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)
{
	Buy = LERatio;
	Sell = LXRatio;
	Short = SERatio;
	Cover = SXRatio;
}

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

BuyPrice = Close;
SellPrice = Close;
ShortPrice = Close;
CoverPrice = Close;
}

First make sure that you are using registered email address for this forum as we can't verify your license status if you are using different address.

The techniques that you should use for pair trading are covered in KB:
https://www.amibroker.com/kb/2014/09/20/broad-market-timing-in-system-formulas/