A question about time.....

 
Morning All

So I have a snippet of code set up to prompt a MessageBox to make itself known at a certain time (closingTime) which is formatted to an Integer on the 24 hour clock, which in this case is 17 (or 1700 GMT).

I am aware that the terminal works to EET and that I have to make an adjustment to local time, which I have done, but the terminal still prompts the MessageBox at random times, and sometimes, it doesn't prompt at all, which makes no sense at all:

I can confirm that the localTime and the mql4Time (EET) times are correct from the print function.

int localTime                     = TimeHour(TimeLocal()); 	//Local Time
int mqlTime                       = Hour(); 			//EET Time
int GMTAdjustment                 = localTime - mqlTime; 	//(-2)
int closingTime                   = 17			//1700 GMT  


if(localTime == closingTime){
   
         bool isSaveWork = MessageBox(
         
            "...",
            
                  "....",
                  
                      MB_ICONINFORMATION | MB_OK
                                          
          ); 
          
          if(!isSaveWork){
          
            printf("Error sent %d", lastError);
          
          }
   
      } 


printf("The Local Time now is %d ", localTime);
         
   printf("The EET Time now is %d ", mqlTime);	

What am I missing here? 

Pip pip! 

Documentation on MQL5: Common Functions / Print
Documentation on MQL5: Common Functions / Print
  • www.mql5.com
Print - Common Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
TheHonestPrussian:
Morning All

So I have a snippet of code set up to prompt a MessageBox to make itself known at a certain time (closingTime) which is formatted to an Integer on the 24 hour clock, which in this case is 17 (or 1700 GMT).

I am aware that the terminal works to EET and that I have to make an adjustment to local time, which I have done, but the terminal still prompts the MessageBox at random times, and sometimes, it doesn't prompt at all, which makes no sense at all:

I can confirm that the localTime and the mql4Time (EET) times are correct from the print function.

What am I missing here? 

Pip pip! 

I think your GMTAdjustment may be wrong.

I usually do it like this:

   int   localTimeGMToffset  = int(TimeGMT() - TimeLocal());
   int   MQLTimeGMToffset    = int(TimeGMT() - TimeCurrent());
   int   localToMQLoffset    = localTimeGMToffset - MQLTimeGMToffset;

But an easier way could be something like this:

   int closingTime                   = 17;    //1700 GMT

   MqlDateTime timeLocStruct;
   TimeToStruct(TimeLocal(), timeLocStruct);
   if(timeLocStruct.hour == closingTime)
     {
      //message...
     }
 
R4tna C #:

I think your GMTAdjustment may be wrong.

I usually do it like this:

But an easier way could be something like this:

Thanks for this, this does not work, but I can't expect you to troubleshoot this for me for free (unless it's intriguing enough for you to do so). :) 

All the best, 

MqlDateTime timeLocStruct;
      
         TimeToStruct(TimeLocal(), timeLocStruct);
         
            printf("The timeLocStruct is %d",timeLocStruct.hour);

                int closingTime = 8;
      
      if(timeLocStruct.hour == closingTime){
   
         bool isSaveWork = MessageBox(
         
            "...",
            
                  "...",
                  
                      MB_ICONINFORMATION | MB_OK | MB_TOPMOST 
                                          
         );          
          
         if(!isSaveWork){
          
            printf("Error sent %d", GetLastError());
          
         }
 
TheHonestPrussian #:

Thanks for this, this does not work, but I can't expect you to troubleshoot this for me for free (unless it's intriguing enough for you to do so). :) 

All the best, 

These seem to work for me, so you would need to be more specific when you say it does not work, but I think you are more than capable of debugging and fixing it yourself so please do post the working version

 
R4tna C #:

These seem to work for me, so you would need to be more specific when you say it does not work, but I think you are more than capable of debugging and fixing it yourself so please do post the working version

Yep, you're right, I am capable & should have posted this in the first place. Sorry about that, and so I have posted my working here. 

 
TheHonestPrussian #:

Yep, you're right, I am capable and so I have posted my working here. 

Good - I now see you have posted your latest code. When you say it does not work, what is the problem you are experiencing?

 
R4tna C #:

Good - I now see you have posted your latest code. When you say it does not work, what is the problem you are experiencing?

Great, the problem is that the MessageBox is not appearing, nor are there any errors in the journal in the event that 

(!isSaveWork)

returns true. 

The same also applies when

if(timeLocStruct.min == 33){...} //33 Minutes past the hour
But it's printing the correct Hour time:

2022.07.22 08:42:46.243 xxxxxx USDZAR,H4: The timeLocStruct is 8:00 GMT
 
TheHonestPrussian #:

Great, the problem is that the MessageBox is not appearing, nor are there any errors in the journal in the event that 

returns true. 

The same also applies when

OK so the timing check works - this is a secondary problem.

I ran the code you posted - I got the message box (see pic)

The isSaveWork is thus true, so no final print





 
R4tna C #:

OK so the timing check works - this is a secondary problem.

I ran the code you posted - I got the message box (see pic)

The isSaveWork is thus true, so no final print





Finally works now. Much appreciated. :)  

 
TheHonestPrussian #:

Finally works now. Much appreciated. :)  

Good! I am sure you appreciate you need to add more filters/delays to ensure you don't get too many frequent messages during the hour, unless you want them... good luck

 
R4tna C #:

Good! I am sure you appreciate you need to add more filters/delays to ensure you don't get too many frequent messages during the hour, unless you want them... good luck

Yes! I've just done a control check, as follows. Initialised isSent to false, checked this on the OnTick section, then set this to true.

Work's a good'un :)  

onInit(){

isSent = false;

}

onTick(){

MqlDateTime timeLocStruct;
      
      TimeToStruct(TimeLocal(), timeLocStruct);
      
         int timeClosing = 10;
      
   if(timeLocStruct.hour == timeClosing){
   
      if(!isSent){
      
         bool isSaveWork = MessageBox(
         
            StringFormat(".....(Time %d:00 GMT)",timeLocStruct.hour),
            
                  "....",
                  
                      MB_ICONINFORMATION | MB_OK | MB_TOPMOST
                                          
         );
         
         isSent = true;
      
      }

   }

}