And here is code to consider only **single** signal occurrence at every single bar.

```
procedure fxTiedLongExit2(Entryname,Exitname,systemnum,delay) {
/// @link https://forum.amibroker.com/t/multiple-sell-tied-to-multiple-buy/22244/9
/// by fxshrat@gmail.com
/// Commercial use prohibited
global Buy, Sell;
local i, n, pow, BuySig, SellSig;
BuySig = SellSig = 0;
mat = Matrix(systemnum,1);
for (n = 0; n < systemnum; n++) {
//https://en.wikipedia.org/wiki/Power_of_two
pow = mat[n][0] = 2^n;
BuySig += (VarGet(Entryname+(n+1))*pow);
SellSig += (VarGet(Exitname+(n+1))*pow);
}
BuySig = Ref(BuySig,-delay);
SellSig = Ref(SellSig,-delay);
//
pow = 0;
for ( i = 0; i < BarCount; i++ ) {
for ( n = 0; n < systemnum; n++ ) {
if (BuySig[ i ] == mat[n][0] AND pow == 0) {
pow = BuySig[ i ];
Buy[ i ] = pow*10;
break;
} else Buy[ i ] = 0;
}
if (SellSig[ i ] == pow AND pow > 0 ) {
Sell[ i ] = SellSig[ i ]*10;
pow = 0;
} else Sell[ i ] = 0;
}
}
SetTradeDelays(0,0,0,0);
SetPositionSize(1, spsShares);
Buy1 = Cross( C, MA( C, 20 ) );
Buy2 = Cross( 30, RSI() );
Buy3 = Cross( MACD(), Signal() );
//add further
Sell1 = Cross( MA( C, 20 ), C );
Sell2 = Cross( RSI(), 70 );
Sell3 = Cross( Signal(), MACD() );
//add further
systemnum = n = 3;// set number of systems
delay = 0;
if ( delay > 0) BuyPrice = SellPrice = O;
else BuyPrice = SellPrice = C;
Short = Cover = 0;
fxTiedLongExit2("Buy","Sell",systemnum,delay);
Plot(C, "Price", colorDefault, styleBar);
Plot(Buy/10, "Buy", colorGreen, styleHistogram | styleLeftAxisScale, mini = (-2^n-1), maxi = (2^n-1));
Plot(-Sell/10, "Sell", colorRed, styleHistogram | styleLeftAxisScale, mini, maxi);
StaticVarSet( "BuySig_" + Name(), Buy/10 );
StaticVarSet( "SellSig_" + Name(), Sell/10 );
// Result list output
SetCustomBacktestProc("");
if ( Status( "action" ) == actionPortfolio ) {
bo = GetBacktesterObject();
bo.Backtest( 1 );
// Iterate through closed trades
for ( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() ) {
BuySig = StaticVarGet( "BuySig_" +trade.Symbol );
trade.AddCustomMetric( "BuySig", Lookup( BuySig, Trade.EntryDateTime ), 0 );
SellSig = StaticVarGet( "SellSig_" +trade.Symbol );
trade.AddCustomMetric( "SellSig", Lookup( SellSig, Trade.ExitDateTime ), 0 );
}
bo.ListTrades();
}
```

So since there are three entry/exit signals in upper code Buy and Sell signals at a bar will either be `1`

(Sig1) or `2`

(Sig2) or `4`

(Sig3).