How to use date/time in EA ?

 

Good morning,

I'm quite new in MQL4 programming so I only try a very simple program but it is not working perfectly.

Let's explain what I would like :

At 9:00 print a message

At 9:05 print another message

Result :

If I launch the expert advisor at 8:55 :

at 9:00 the expected message is written

at 9:05 nothing appens. I suppose there is something wrong with the "if / else".

What's wrong with my code ? Why is there only one action executed at 9:00 and nothing at 9:05 ?

//--------------------------------------------------------------------
// time_print.mq4
//--------------------------------------------------------------------

#include <stdlib.mqh>
#include <WinUser32.mqh>


extern double Time_1=09.00;           // Time message 1
extern double Time_2=09.05;           // Time message 2

           
bool Flag_Time1=false;                   
bool Flag_Time2=false; 

//--------------------------------------------------------------------

int start()                             
  {
   int    Cur_Hour=Hour();              
   double Cur_Min =Minute();            
   double Cur_time=Cur_Hour + Cur_Min/100;  

   if (Cur_time>=Time_1)
      {
      ExecutorTime1();                      
      }
      
   else if (Cur_time>=Time_2)              
      {
      ExecutorTime2();                      
      }
      
   return;                              
  }

//-----------------------------------------------------------------

int ExecutorTime1()                          
  {
   if (Flag_Time1==false)                 
     {                                  
      Print("Execution Time1");
      Flag_Time=true;                   
     }
   return;                              
  }

//-----------------------------------------------------------------

int ExecutorTime2()
  {
   if (Flag_Time2==false) 
     { 
      Print("Execution Time2");
      Flag_Close=true;                   
     }
   return;                              
  }
Any help would be appreciated.
 

Use this data type instead, much less error-prone and easier to use overall:

https://docs.mql4.com/basis/types/datetime

.

Jon

 
Agree with Archael. Definitely a strange way to work with time. But a couple of ways to change your existing code to get it to work (hopefully helping you with your logic). Option 1. In your first conditional statements, swap the 1s and 2s so that you're checking for 0905 first. Option 2. Keep the conditional statement the way round you have it but move your flag check up there so each check is for time and flag. CB
 

Thank you for your answers I will change my code tonight.

Can I ask you how you would have done to execute 2 actions at different hours/time ?

Which computer logica do you use to handle such things ?

 

int start(){

if(TimeHour(TimeCurrent()) == 9 && TimeMinute(TimeCurrent())) == 0) Print("Execution Time1");
if(TimeHour(TimeCurrent()) == 9 && TimeMinute(TimeCurrent())) == 5) Print("Execution Time2");

return(0);

}

 
phy:

if(TimeHour(TimeCurrent()) == 9 && TimeMinute(TimeCurrent())) == 0) Print("Execution Time1");

Tryprogmq4's original code (with CB's amendment) has the advantage of not assuming that there will be a tick between 09:00:00 and 09:00:59. It looks as though Tryprogmq4 wants an alert at 09:00/09:05 or the first tick after that. There are obviously plenty of symbols and times of day when you're not guaranteed to see a tick within a particular minute.

 
phy wrote >>

int start(){

if(TimeHour(TimeCurrent()) == 9 && TimeMinute(TimeCurrent())) == 0) Print("Execution Time1");
if(TimeHour(TimeCurrent()) == 9 && TimeMinute(TimeCurrent())) == 5) Print("Execution Time2");

return(0);

}

Hi Phy,

I do not know how to code, I have an idea how to code EA which is will open position every 4 hours. I am still learning how to code. Please.

thanks

axadnan

Reason: