How to Mandatory Close if Breakeven Price Level (BEPx) is Met ? (MT4)

Hi programmers, my EA has 4 arrays in the Exit function to store the relevant symbols (with open trades) and BEPx (BUY and/or SELL) :-

string bSym[];
double bSymBEPx[];

string sSym[];
double sSymBEPx[];

My mission is to close a bundle of open trades completely (BUY group of trades or SELL group of trades) for a symbol at BEPx if emergency conditions are met.

If a symbol with only 1 single open trade, the exit can close nicely whenever the BEPx Level’s is met. However, if the symbol (BUY and/or SELL) has > 0 open trades, the EA cannot always bundle close the symbol’s open trades concurrently and completely because the moment EA closes the first open trade, the BEPx level will change immediately, always resulting most of the bundle open trades are yet open.

I need your codes so that I can implement the “Mandatory Bundle Close” (but not the Basket Close for the whole account) for the relevant symbol with multiple open trades. For examples :-

Exit Emergency Conditions (To Close BUY OpOrd #) :-

   if(Conditions && symbol==bSym && BID >= bSymBEPx)

Exit Emergency Conditions (To Close SELL OpOrd #) :-

   if(Conditions && symbol==sSym && ASK <= sSymBEPx)

** Kindly have a look at my picture for better understanding. Thanks

Notes :-

  1. Kindly look at the picture, the CADJPY has both 5 BUY and 2 SELL open orders. If there is an exit signal (BID >= bSymBEPx) for BUY, the codes should only close the 5 BUY open trades simultaneously and should not touch the 2 SELL open trades.

  2. The exit function based on OrderSelect(i,SELECT_BY_POS,MODE_TRADES) to provide symbols with open trades. In my exit function, it uses “symbol” to represent all the symbols with open trades. If my EA is running 30 symbols, 5 symbols with open trades and total number of open trades is 18 (including both BUY and SELL open trades), if I write the following codes :-

   Print(symbol+" High Price "+DoubleToStr(iHigh(symbol,60,1),5)+" Low Price "+DoubleToStr(iLow(symbol,60,1),5));

there would be 18 different sets of output (5 symbols) to be printed out.