Download MetaTrader 5
To add comments, please log in or register
Have you installed MetaTrader 5? Find out more about it!
baharinhasan
9
baharinhasan 2016.09.28 02:11 

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
7986
Keith Watford 2016.09.28 03:10  

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
7986
Keith Watford 2016.09.28 03:12  

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

whroeder1
13641
whroeder1 2016.09.28 11:41  
  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
969
honest_knave 2016.09.28 15:18  
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
13641
whroeder1 2016.09.28 17:13  
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