Close current symbol pending order if there is no open orders for MT5

EnrichWave

Master Trader
May 15, 2018
409
104
89
India
Dear fellow traders.
I would like to have EA that close all pending orders if there is no open order on current EA loaded chart symbol. Kindly somebody help me.

Kind Regards
 
The following MQ4 works fine. Could you please make it in mq5?

MQL4:
int init()
  {
   return(0);
  }
  int deinit()
  {
     return(0);
  }
int start()
  {
 
  if(Count() == 0)  {ClosePendings();}
  return(0);
  }
  int Count()
{
 int count=0;
 int i;
 int total=OrdersTotal();
 for(i=0;i<total;i++)
 {
  OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
 
  if(OrderSymbol() != Symbol() || OrderType() > OP_SELL) continue;
 
   count++;
 }
 return(count);
}
void ClosePendings()
  {
  int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
  {
    OrderSelect(i, SELECT_BY_POS);
    if(OrderSymbol() != Symbol()) continue;
    {
    int type   = OrderType();
    bool result = false;
 
    switch(type)
    {
      case OP_BUYLIMIT  :
      case OP_BUYSTOP   :
      case OP_SELLLIMIT :
      case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
    }
    } 
  }
  }
 
This one shouldn't be too difficult to code. You want it constantly checking for open positions on the current chart's symbol and if none found delete all pending orders in this symbol?
Yes you are right... It should check constantly for open positions. But not only for current loaded chart. Even the EA loaded on any symbol name. it should close the pending orders which doesn't have open positions .

for example. The EA loaded on XAUUSD, I've 1 open position on EURUSD & 2 pending orders on EURUSD, 1 open position on GBPUSD, and 2 pending orders on GBPUSD,.. If I close the 1 order of EURUSD, currently there is no open postion. Only pending orders in EURUSD.. So now the EURUSD pending orders should be deleted by the EA. Now the rest of the orders (GBPUSD open position and pending orders) will be on terminal.. Again If I close the open postion of GBPUSD the pending orders of GBPUSD should be deleted by the EA because there is no open orders..

I like to have code in your style which is always working nicer than any other program

BR