why order not close ?

 

Hi....

I am running on 5 digits account and my EA not close the orders.... Could somebody help me ?

This is my code:


void CloseAllSell()
 {  
  int total = OrdersTotal();
  
  for (int i = total-1 ; i >=0 ; i--)
  {
    OrderSelect(i, SELECT_BY_POS);

    if (OrderType() == OP_SELL) {
         OrderClose( OrderTicket(), OrderLots(), Ask, 50, Red );
         Print ("Close Sell for Ticker Number:",OrderTicket());
         Sleep(1000);       
    }
  }  
}

void CloseAllBuy()
 {  
  int total = OrdersTotal();
   
  for (int i = total-1 ; i >=0 ; i--)
  {
    OrderSelect(i, SELECT_BY_POS);

    if (OrderType() == OP_BUY) {
         OrderClose( OrderTicket(), OrderLots(), Bid, 50, Red );
         Print ("Close Buy for Ticker Number:",OrderTicket());
         Sleep(1000);  
    }    
  }  
  
}
 

Why Sleep()? You will need to RefreshRates() to get current Ask & Bid.

Why bother with 2 loops? Just use OrderClosePrice() instead of Bid and Ask

Check the return from the OrderClose and see what error prevents the closure.

 

Also, when using Bid and Ask, they will only be correct for the Chart Symbol,use OrderClosePrice().

 
  1. In the presence of multiple orders (one EA multiple charts, multiple EA's, manual trading) you must count down when closing/deleting/modifying in a position loop and check OrderSelect when dealing with multiple orders. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum 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
  2. You must RefreshRates after sleep and between multiple server calls.
  3. Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.) Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 forum
 
WHRoeder:
  1. You must RefreshRates after sleep and between multiple server calls.

Just to build on this, it is worth noting OrderClosePrice() remains static from the point the order gets selected.

So even if you RefreshRates, OrderClosePrice() will not change until you select the order again. Therefore it could well be outdated if there is a delay between selecting the order and closing the order.

 
honest_knave: Just to build on this, it is worth noting OrderClosePrice() remains static from the point the order gets selected.
Correct, but you must RefreshRates before selecting an order so it can be set correctly.