Close only order of a Symbol

 

Good morning,

I wrote this function to close open orders of specific symbol but, when testing, it seems not working properly. Could you give it a look and tell me if is there something wrong?

Thank you very much

void CloseAllSymb()
  {
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      double price=0; color arrow=0;
      bool order=OrderSelect(i,SELECT_BY_POS);
      RefreshRates();
      if(OrderType()==OP_BUY) { price=Bid; arrow=clrRed; }
      if(OrderType()==OP_SELL) { price=Ask; arrow=clrGreen; }
      if(OrderSymbol()==_Symbol) bool closedord=OrderClose(OrderTicket(),OrderLots(),price,0,arrow);
     }
  }
 
  1. RefreshRates before the OrderSelect, then no need for price=Bid/Ask just use OrderClosePrice in the OrderClose.
  2. Check your return codes (OrderSelect and OrderClose) and find out why. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  3. No slippage allowed, lots of requotes. See #2
 
Thank you WHRoeder, I will do it and let you know!
 
int CloseAllSymb()
{
  int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
  {
    OrderSelect(i, SELECT_BY_POS);
    int type   = OrderType();

    bool result = false;
    if (OrderSymbol()==Symbol()){
    switch(type)
    {
      
      case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, CLR_NONE );
                          break;
      
      
      case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, CLR_NONE );
                          break;

      
      case OP_BUYLIMIT  :
      case OP_BUYSTOP   :
      case OP_SELLLIMIT :
      case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
    }
    }
    
  }
  
  return(0);
}
Reason: