Why I can't close the last order by using OrderClose()?

 

Hi,

I first open 3 orders manually. Then I start the EA which does nothing but close all the opened orders if I delte it from the chart.

My code look as follow:

...
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   Alert("You have moved me!");
   // Close all opened orders
      for(int i=0;i<OrdersTotal();i++)
      {
         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
         {
            if(OrderType() == OP_BUY)
              {
               OrderClose(OrderTicket(), OrderLots(), Bid, 10, Blue);
              }
            else if(OrderType() == OP_SELL)
                {
                  OrderClose(OrderTicket(), OrderLots(), Ask, 10, Blue);
                }           
            Alert("Close Order:" + OrderTicket());
        }
      }
   
  }
...


But stragely no matter how many orders I open manually, there are n-1 orders can be closed. For example, if I opened 3 orders, only two of them can be closed. 


Why?

 

      for(int i=0;i<OrdersTotal();i++)



Count down, not up when closing trades

  for(int i=OrdersTotal()-1;i>=0;i--)



 


          

  if(OrderType() == OP_BUY)
              {
               OrderClose(OrderTicket(), OrderLots(), Bid, 10, Blue);
              }
            else

  if(OrderType() == OP_SELL)              

              {         

               OrderClose(OrderTicket(), OrderLots(), Ask, 10, Blue);
              }

     No need to check for Buy or Sell  

    OrderClosePrice() can be used for both.

    Of course you may need to check for pending orders if they are going to be used.
 
  1. EAs/Indicators/Scripts must exit withing three (3) seconds of being shut down. You can not close orders, reliability, in OnDeinit.
  2. In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading)
  3. Do you really want to close order when all you did was change timeframe or recompile the program?
 
whroeder1:
  1. EAs/Indicators/Scripts must exit withing three (3) seconds of being shut down. You can not close orders, reliability, in OnDeinit.

Just to be precise, the documentation says :

The OnDeinit() function run is restricted to 2.5 seconds

Client Terminal Events - MQL4 programs - MQL4 Reference
Client Terminal Events - MQL4 programs - MQL4 Reference
  • docs.mql4.com
Client Terminal Events - MQL4 programs - MQL4 Reference
 
Keith Watford:

      for(int i=0;i<OrdersTotal();i++)



Count down, not up when closing trades

  for(int i=OrdersTotal()-1;i>=0;i--)




I believe it's still possible counting up for closing order.
It's just that, the original code missing -1 in array.
it comply with FIFO requirement, when closing all order at once.

for(int order = 0; order <= OrdersTotal()-1; order++)
 

Hi all,


I've tried all the alternatives you wrote. But it still failt.


I attach my mq4 here and you could have try. Please do followings:

1. Open several orders for example 2 buys and 2 sells.

2. Drag the EA on a opened chart for exampel EURUSD.

3. Then remove the EA fro mthe chart. You can see though the message box tells all the orders are closed, one order is still opened.


Quite strange.


And I notice as  compile the AE I see warning like:

"return value of 'OrderClose' should be checked DeleteOrders.mq4 43 16"





Files:
 
Alain Verleyen:

Just to be precise, the documentation says :


Hi,

The saying "The OnDeinit() function run is restricted to 2.5 seconds" seems interesting. I am the first time hearing that. But I've tried even when I just open one order, it can't be closed.

 
thomas2004:

Hi,

The saying "The OnDeinit() function run is restricted to 2.5 seconds" seems interesting. I am the first time hearing that. But I've tried even when I just open one order, it can't be closed.

Closing order(s) in OnDeinit() is a bad idea. Change your mind and find an other way.

About your persisting problem, you just have to follow advice you get above (Keith and WHRoeder).

 

"return value of 'OrderClose' should be checked DeleteOrders.mq4 43 16"


OrderClose returns a bool. If it returns false, print the error.

 
Mohamad Zulhairi Baba:

I believe it's still possible counting up for closing order.
It's just that, the original code missing -1 in array.
it comply with FIFO requirement, when closing all order at once.

for(int order = 0; order <= OrdersTotal()-1; order++)


WhRoeder has provided the link that explains why you must NOT count up.

<= OrdersTotal()-1


is exactly the same as

< OrdersTotal()