Closing all orders in deinit function

 

Hi guys,


I am a bit lost with the deinit function (MT4)..

I want to close all opened orders when something happen that stop the EA, but seems to not do anything. Here is what I wrote but no closing action :


void deinit()
{
   total=OrdersTotal();
   for (cnt = total - 1; cnt >= 0; cnt--)
   ok=OrderSelect(cnt,SELECT_BY_POS);
   if(OrderType()==OP_SELL)
   ok=OrderClose(OrderTicket(),OrderLots(),Bid,3,Red);
   if(OrderType()==OP_BUY)
   ok=OrderClose(OrderTicket(),OrderLots(),Ask,3,Red);
}


Please help

 

You can't do that. OnDeinit() needs to be done in less than 2.5 seconds.

Anyway, it's not a good idea even if it was working, OnDeinit() is called in following cases :

  • before reinitialization due to the change of a symbol or chart period, to which the mql5 program is attached;
  • before reinitialization due to the change of input parameters;
  • before unloading the mql5 program.
 
Yes, OnDeinit function is not intended for closing orders, it is better not to do so.

But if you really want it ...
void OnDeinit(const int reason)
{
for (int cnt=OrdersTotal()-1; cnt>=0; cnt--) 
   if (OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES) && OrderCloseTime()==0)
      bool i2= (OrderType()<2) ? OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,Red) :
                                 OrderDelete(OrderTicket());
}
 
Taras Slobodyanik: But if you really want it ...
  1. Not all orders closed if it takes more than 2.5 seconds to run.
  2. 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
  3. No need to check OrderCloseTime since you are not looking at history.
  4. You must RefreshRates after sleep and between multiple server calls if you want to use the Predefined Variables (Bid/Ask) or OrderClosePrice() instead.
 
whroeder1:
  1. Not all orders closed if it takes more than 2.5 seconds to run.
  2. 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
  3. No need to check OrderCloseTime since you are not looking at history.
  4. You must RefreshRates after sleep and between multiple server calls if you want to use the Predefined Variables (Bid/Ask) or OrderClosePrice() instead.
Yes, all these checks are needed in a normal situation.

In my experience, if you do all the checks in this place, then the orders will not be closed, the expert will close sooner.
Without these checks, orders are closed, the terminal hangs for this time.

ps. about checking the magic and symbol nothing was said, so the task is to close all.

 
Taras Slobodyanik:
Yes, all these checks are needed in a normal situation.

In my experience, if you do all the checks in this place, then the orders will not be closed, the expert will close sooner.
Without these checks, orders are closed, the terminal hangs for this time.

ps. about checking the magic and symbol nothing was said, so the task is to close all.

Hi guys,

Thanks for your advices. I dont know why, but my script works now quite fine. Dont know why it seemed to not work at first, but your comments are helping anyway.. Normally I have only one position at time so it should be ok.

 
Volcanbleu:

Hi guys,

Thanks for your advices. I dont know why, but my script works now quite fine. Dont know why it seemed to not work at first, but your comments are helping anyway.. Normally I have only one position at time so it should be ok.

No it's not ok. But you will learn by experience I suppose.
 
Alain Verleyen:

You can't do that. OnDeinit() needs to be done in less than 2.5 seconds.

Anyway, it's not a good idea even if it was working, OnDeinit() is called in following cases :


Hi Alain, is OnDeinit called when an indicator is manually deleted from the Indicators List? I ask because the follwing code does not delete any onjects or print any messages or return any errors - I just get "Custom indicator XX removed" in the Experts window...

int OnDeinit()
  {
   EventKillTimer();
   Comment("");
   Print("OnDeinit function has run");

   int i,ot=ObjectsTotal();
   
   for(i=ot-1;i>=0;i--)
     {
      if(StringFind(ObjectName(i),"xx",0))
        {
         ObjectDelete(i);
         Print("OnDeinit FOR loop has run");
        }
     }
   return(0);
  }

I also tried using StringSubstr method but it similarly doesn't work.

 
5158401: I ask because the follwing code does not delete any onjects or print any messages or return any errors
if(StringFind(ObjectName(i),"xx",0))

Zero is false, non-zero is true, and StringFind returns -1 if it doesn't find the string. So all strings that have "xx" as the first two characters StringFind returns 0 (false.) and thus the function deletes all objects except for those.

 
5158401:

Hi Alain, is OnDeinit called when an indicator is manually deleted from the Indicators List? I ask because the follwing code does not delete any onjects or print any messages or return any errors - I just get "Custom indicator XX removed" in the Experts window...

I also tried using StringSubstr method but it similarly doesn't work.

 ObjectDelete(i); will not work either

Check here : https://www.mql5.com/en/docs/objects/objectdelete


Documentation on MQL5: Object Functions / ObjectDelete
Documentation on MQL5: Object Functions / ObjectDelete
  • www.mql5.com
An asynchronous call is always used for ObjectDelete(), that is why the function only returns the results of adding the command to a chart queue. In this case, true only means that the command has been successfully enqueued, but the result of its execution is unknown. To check the command execution result, you can use the ObjectFind() function...
 
5158401:

Hi Alain, is OnDeinit called when an indicator is manually deleted from the Indicators List? I ask because the follwing code does not delete any onjects or print any messages or return any errors - I just get "Custom indicator XX removed" in the Experts window...

I also tried using StringSubstr method but it similarly doesn't work.

You are off-topic, it's about close orders.

Beside the errors already reported, you could just use :

ObjectsDeleteAll(0,"xx");
Reason: