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

 
Alain Verleyen:

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).


And what is your way?

 
thomas2004:

And what is your way?

If your goal is to close all orders use a script. When you decide to close, launch the script. Done.
 

I've change a little bit of as follow:

void OnDeinit(const int reason)
  {
//---
   Alert("You have moved me!");
   // Close all opened orders
      for(int i=0;i<OrdersTotal();i++)
//      for(int i=OrdersTotal()-1; i >= 0; i--)
      {
         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
         {
            if(OrderType() == OP_BUY)
              {
               if(!OrderClose(OrderTicket(), OrderLots(), Bid, 50, Blue))
               {
                  Print("OrderClose has problem");
                  int errorCode = GetLastError();
                  Print("ERROR_CODE:" + errorCode + " by closing :" + OrderTicket());
               }
              }
            else if(OrderType() == OP_SELL)
                {
                  if(!OrderClose(OrderTicket(), OrderLots(), Ask, 50, Blue))
                  {
                     Print("OrderClose has problem");
                     int errorCode = GetLastError();
                     Print("ERROR_CODE:" + errorCode + " by closing :" + OrderTicket());
                  }
                }           
            Alert("Close Order:" + OrderTicket());
        }
      }
   
  }

As I remove the EA out of the chart I can see now the error messages as follow (screenshot):

Files:
 
Alain Verleyen:
If your goal is to close all orders use a script. When you decide to close, launch the script. Done.

What I want is, when I remove an EA, all the orders must be closed. This is somewhat like when you remove an indicator, all the objects must be removed or cleared, without running another script.

 

If you insist on using Bid and Ask instead of OrderClosePrice() you will need to call RefreshRates() before every attempt to close an order. Bid and Ask can get out of date when closing a number of trades.

You have been told that it is not a good idea to try to close trades in OnDeinit.

Why not use OnChartEvent with a keydown to disable the EA,close all trades and then ExpertRemove() to remove the EA once all trades are closed? If doing this, it makes sense to use MessageBox so that you can confirm in case of an accidental keystroke.

 
  1. You've already been told that you must count down, (#3.2) but you ignored it.
  2. You've already been told "EAs/Indicators/Scripts must exit withing 2.5 seconds of being shut down. You can not close orders, reliability, in OnDeinit," (#3.1) but you ignored it.
  3. Just because you change timeframes, or EA parameters, shouldn't mean you kill open orders, but it's your money lost. You've been told it's a bad idea, (#8) but you ignored it.
  4. Key event, GUI event, or even a EA parameter change could be used to trigger a close all and then remove EA, but not in OnDeinit.
 

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.

  1. Yes it is possible.
  2. Your -1, as already explained, is wrong. Keith Watford
  3. For FIFO, I've updated my standard quote:
In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) because while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
  1. For non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you can simply count down in a position loop, and you don't miss orders. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
  2. For FIFO (US brokers,) and you (potentially) process multiple orders, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.)
  3. and check OrderSelect. What are Function return values ? How do I use them ? - MQL4 forum
    Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  4. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use the Predefined Variables (Bid/Ask) or OrderClosePrice() instead, on the next order/server call.
 

Thanks to all of you. I will consider your opinions and will have a try.