Rgd: How do I do a code to ensure my position is at least 30 pips gap

 

Rgd: How do I do a code to ensure my position is at least 30 pips gap

bool validGap;

validGap = false;

I want to write a code to run to check if Symbol() got open position, it will check all the current running position of Symbol() then check if there are at least 30 pips away from ALL the position, if true, validGap = true and then I can do my position opening.

This code I wanna do is because I trying build an EA base on position sizing and I want ensure all my position is not tightly close together.

Anyone can help me with the code? Thanks !

 

use: MathMax, MathMin, +, -, *

for example (pseudo code): if ( current price < ( MathMax(all prices)+(30 pips*Point) ) ) { too close }

 
//++++ These are adjusted for 5 digit brokers.
int      pips2points;                  // slippage  3 pips  3=points 30=points
double   pips2dbl;                     // Stoploss 15 pips  0.0015   0.00150
int      Digits.pips;                  // DoubleToStr(dbl/pips2dbl, Digits.pips)
int      init(){                                            OptInitialization();
    if(Digits % 2 == 1){   // DE30=1/JPY=3/EURUSD=5 https://www.mql5.com/en/forum/135345
            pips2dbl = Point*10;    pips2points = 10;    Digits.pips = 1;
   } else { pips2dbl = Point;       pips2points =  1;    Digits.pips = 0;     }
   // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
//---- These are adjusted for 5 digit brokers.
   //{On ECN brokers you must open first and THEN set stops
   // int      ticket = OrderSend(..., 0,0,...)
   // if(      ticket < 0)
   //    Alert("OrderSend failed: ", GetLastError());
   // else  if(!OrderSelect(ticket, SELECT_BY_TICKET))
   //    Alert("OrderSelect failed: ", GetLastError());
   // else  if(!OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0))
   //    Alert("OrderModify failed: ", GetLastError());
   //}
   market.pair    = Symbol();
   :
}
// extern int magic.number
bool     MySelect(int iWhat, int eSelect, int ePool=MODE_TRADES){
   if(!OrderSelect(iWhat, eSelect, ePool) )  return (false);
   if(OrderMagicNumber() != magic.number  )  return (false);
   if(OrderSymbol()      != market.pair   )  return (false);
   if(ePool != MODE_HISTORY               )  return (true);
   return(OrderType() <= OP_SELL);  // Avoid cr/bal https://www.mql5.com/en/forum/126192
                                    // https://www.mql5.com/en/forum/124726
                                    // Never select canceled orders.
}
//////
bool validGap = true; // Assume ok.
   for(int iPos = OrdersTotal()-1; iPos >= 0 ; iPos-- ) if(
      MySelect(iPos, SELECT_BY_POS)                   ) if(
      MathAbs( OrderClosePrice() - OrderOpenPrice() ) < 30*pips2dbl) validGap = false;
if(validGap) ...
Reason: