Need help with time + GMT offset showing wrong

 

Hello. I have two MT4 terminals running on a VPS at the same time, on the same broker, etc. The broker GMT is +3. I have an EA where I calculate the offset in the OnInit() function, that is only once, and then I use it for my news filter is order to compare the news time (plus GMT offset) with MT4 time. I've noticed already a couple of times that when the market opens one of the terminals is showing the times wrong. The original file with the times seems to be OK, so the problem has to be with the GMT offset calculation. What I find really strange is that one terminal seems to show the offset perfectly fine...


whereas the other shows some very funky times...

I would understand if both are wrong, but why is it that only one of them is getting the times completely wrong with the same EA running on them? Again, same broker, everything seems to be the same, but I've noticed this upon market open. Is it possible that there's some kind of "residue" variable in some sort of buffer? By the way, I'm doing some wonky thing to avoid the typical offset 2:59:59. I know now that the right code should be something like...

int gmtOffset = (TimeCurrent() - TimeGMT() + 1800) / 3600) * 60 * 60; // In seconds

...but so far I'm doing this, and I'm not sure if this is the cause of my problem:

// Sometimes the time offset loses a milisecond in the calculation, making the offset time XXh59m59s.
// We correct this by subtracting the existing seconds and adding 60 more to make the full hour.
gmtOffset = TimeCurrent() - TimeGMT();
if(TimeMinute(gmtOffset) == 59)
        gmtOffset = gmtOffset - TimeSeconds(gmtOffset) + 60;

Also, should I use either of these codes in OnTick() rather than in OnInit()? I guess that should correct the offset by taking the correct current TimeCurrent(), but it seems rather unnecessary on every single tick.

Thanks in advance for any hint!

 

Before questioning your code, first verify that the clocks and time-zones on each of the VPS are correctly set.

The TimeGMT() is obtained based on the clock, and time-zone set on the machine. If that is incorrectly set, then your calculations will be off.

 

Also read the following ...

Articles

Dealing with Time (Part 1): The Basics

Carl Schreiber, 2021.10.01 17:48

Functions and code snippets that simplify and clarify the handling of time, broker offset, and the changes to summer or winter time. Accurate timing may be a crucial element in trading. At the current hour, is the stock exchange in London or New York already open or not yet open, when does the trading time for Forex trading start and end? For a trader who trades manually and live, this is not a big problem.

Articles

Dealing with Time (Part 2): The Functions

Carl Schreiber, 2021.10.08 10:31

Determing the broker offset and GMT automatically. Instead of asking the support of your broker, from whom you will probably receive an insufficient answer (who would be willing to explain a missing hour), we simply look ourselves how they time their prices in the weeks of the time changes — but not cumbersome by hand, we let a program do it — why do we have a PC after all.

Articles

MQL5 Programming Basics: Time

Dmitry Fedoseev, 2013.04.26 11:49

The article focuses on standard MQL5 functions for working with time, as well as programming techniques and practically useful functions for working with time that are required when creating Expert Advisors and indicators. Particular attention is paid to the general theory of time measurement. This article should be of interest primarily to novice MQL5 programmers.


Reason: