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

EnrichWave

Master Trader
May 15, 2018
327
82
74
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
 

EnrichWave

Master Trader
May 15, 2018
327
82
74
India
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() );
    }
    } 
  }
  }