CloseAll Function in an EA not closing all the orders

 

Hi All,

I have an issue with the function below. It is part of an EA.

When a certain profit is attained for a grid and for a specific cross, the fuction is called and is supposed to close all the current orders and delete all the pending ones for the specific cross.

It seems to be working till a certain extent as often a few orders will stay open. 

Any suggestions on why and how to solve the issue_

Thanks,

MG



void closeAll(string cross)
 {

   double ClosePrice=MarketInfo(cross, MODE_BID);
   int OrdSlippage=3;
   int Counter;
   
   for (Counter=0; Counter <= OrdersTotal()-1; Counter++)
   {
      OrderSelect(Counter, SELECT_BY_POS);
     
       if(OrderSymbol()==cross)
      {  
      bool Closed = OrderClose(OrderTicket(), OrderLots(),ClosePrice,OrdSlippage);
      bool Deleted = OrderDelete(OrderTicket());
      }
         
   } 
   
 }
 

To close orders it is better to use OrderClosePrice () function instead of Bid or Ask or any other price.

Sample: "OrderClose ( OrderTicket ()OrderLots ()OrderClosePrice (), 0 ) ;"

OrderClose - Trade Functions - MQL4 Reference
OrderClose - Trade Functions - MQL4 Reference
  • docs.mql4.com
OrderClose - Trade Functions - MQL4 Reference
 
MacGiamma:

Hi All,

I have an issue with the function below. It is part of an EA.

When a certain profit is attained for a grid and for a specific cross, the fuction is called and is supposed to close all the current orders and delete all the pending ones for the specific cross.

It seems to be working till a certain extent as often a few orders will stay open. 

Any suggestions on why and how to solve the issue_

Thanks,

MG



Invert the loop (start from highest index, not the lowest)

 

Thanks both for the suggestions, I will try them both


MG

Reason: