Download MetaTrader 5

why order not close ?

To add comments, please log in or register
baharinhasan
9
baharinhasan  

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);  
    }    
  }  
  
}
Keith Watford
Moderator
10566
Keith Watford  

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.

Keith Watford
Moderator
10566
Keith Watford  

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

whroeder1
16336
whroeder1  
  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
honest_knave
Moderator
2296
honest_knave  
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.

whroeder1
16336
whroeder1  
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.
To add comments, please log in or register