Delaying an EA ...

 

Hi to all,


Let's say I started Metatrader at 6 am in the morning, but I want my EA to delay until 7 am.

In MQL 4 my code was ...

while(TimeHour(iTime("EURUSD", PERIOD_M30, 0)) < 7);

But as there is no TimeHour function in MQL 5 it gets compiler error.

How can I translate it in MQL 5?

Thanks.

 
  1. You do not want a while loop. That would max out your CPU doing nothing. You want to return and wait for a new tick.

  2. There are several ways to code TimeHour vs 7AM.

    1. Migrating from MQL4 to MQL5 - MQL5 Articles № 10 (2010)

    2. int TimeHour(datetime when){ return (when / 3600) % 24; }
    3. #define HR0700 25200 // (7*3600)
      if( time() < HR0700 ) …
                Find bar of the same time one day ago - MQL4 programming forum #1 & #6 (2017)


 
Alp.Duman:

Hi to all,


Let's say I started Metatrader at 6 am in the morning, but I want my EA to delay until 7 am.

In MQL 4 my code was ...

But as there is no TimeHour function in MQL 5 it gets compiler error.

How can I translate it in MQL 5?

Thanks.

In the OnTick function, you set the time condition you want the EA to stop working (for example, that time is the bool variable is_time_to_delay).

For example:

bool is_time_to_delay = ........;
if(is_time_to_delay )
return;

 

Thanks a lot for your replies 

 
If we need to delay an EA in OnInit, what would be the best tool to do this?
 
Alp Duman #: If we need to delay an EA in OnInit, what would be the best tool to do this?

Don't! You should not do that. You will be blocking the thread. Handle events properly. Don't block the thread.

 
Fernando Carreiro #:

Don't! You should not do that. You will be blocking the thread. Handle events properly. Don't block the thread.


Searched the documentation and ... probably I need to use a structure like the following.


int OnInit() 
  { 
//--- create a timer with a 1 second period 
   EventSetTimer(1); 
  
//--- 
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| Expert deinitialization function                                 | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- destroy the timer after completing the work 
   EventKillTimer(); 
  
  } 
//+------------------------------------------------------------------+ 
//| Expert tick function                                             | 
//+------------------------------------------------------------------+ 
void OnTick() 
  { 
//--- 
  
  } 
//+------------------------------------------------------------------+ 
//| Timer function                                                   | 
//+------------------------------------------------------------------+ 
void OnTimer() 
  { 

  }
 

It seems that OnTimer is not what I am looking for ...

I have some code in OnInit section for preanalyzing the trade day, so after starting the EA, I need to delay it before the implementation of the code in OnInit section.

How about a solution like the one below at the beginning of OnInit?

I would be grateful for an advice ...

   MqlDateTime baslangic;
   TimeToStruct(iTime("EURUSD", PERIOD_M30, 0), baslangic);
   while(baslangic.hour < 7)
   {
    Sleep(5000);
    TimeToStruct(iTime("EURUSD", PERIOD_M30, 0), baslangic);
    }

 
Alp Duman #: It seems that OnTimer is not what I am looking for ... I have some code in OnInit section for preanalyzing the trade day, so after starting the EA, I need to delay it before the implementation of the code in OnInit section. How about a solution like the one below at the beginning of OnInit? I would be grateful for an advice ...

Don't do any trade related tasks in the OnInit() nor even data feed realted tasks. That is not the purpose of that event handler.

During the execution of OnInit() there may be no connection to the trade server yet, so do it in the OnTick() only after the arrival of the first tick.

And again, don't use a loop with "sleep". That blocks the thread. Use an event driven architecture with a state machine instead.

 

I see, then only option is to move my code into OnTick ...

Thanks a lot for your help ... and the help of William and Nguyen.

 
Alp Duman #:

I see, then only option is to move my code into OnTick ...

Thanks a lot for your help ... and the help of William and Nguyen.


You can try this solution:

input int myStartingHour=7;

void OnTick()  { 
MqlDateTime myTime; 
TimeCurrent(myTime); 
if(myTime.hour < myStartingHour) return;

// --- your code here ---

}
Reason: