TimeCurrent() and TimeTradeServer() functions

 

Part 1:

According to the documentation, TimeCurrent() “returns the last known server time” and is not dependent on your computer's clock while TimeTradeServer() “returns the calculated current time of the trade server” and is dependent on your computer's clock. 

Can somebody explain in detail what the difference is between their return values and how TimeTradeServer() uses the computer’s clock? When should I use one and when should I use the other?

When I print both of them TimeTraderServer() is often (but not always) 1 second behind TimeCurrent().


Part 2:

I assume that the simulated time in the Strategy Tester (x axis) was the trade server time during the period of the backtest and not the computer's simulated local time during the period of the backtest.

I assume this because price data history comes from the broker. Is this wrong?

Files:
MQL5_times.JPG  22 kb
 
emerson2102:

Part 1:

According to the documentation, TimeCurrent() “returns the last known server time” and is not dependent on your computer's clock while TimeTradeServer() “returns the calculated current time of the trade server” and is dependent on your computer's clock. 

Can somebody explain in detail what the difference is between their return values and how TimeTradeServer() uses the computer’s clock? When should I use one and when should I use the other?

When I print both of them TimeTraderServer() is often (but not always) 1 second behind TimeCurrent().

Part 2:

I assume that the simulated time in the Strategy Tester (x axis) was the trade server time during the period of the backtest and not the computer's simulated local time during the period of the backtest.

I assume this because price data history comes from the broker. Is this wrong?

I believe, but I am not sure, that TimeCurrent() only gets updated when a new Tick arrives. So, if no tick arrives or the connection goes down for a while, it gets stuck. Please note that I have not tested this.

TimeTraderServer() on the other hand "simulates" the server's time using the PC clock, so even when the tick does not come in or when there is delay in communications, the time continues to update.

I believe, it serves the purpose of allowing code to be able to detect the passing of time, even when there is no tick, or maybe to allow the code to detect when there is a delay, either by lack of data or a problem in communications.

However, in the back-test, they are both set to the same time as simulated by the back test.

 
Fernando Carreiro:

I believe, but I am not sure, that TimeCurrent() only gets updated when a new Tick arrives. So, if no tick arrives or the connection goes down for a while, it gets stuck. Please note that I have not tested this.

TimeTraderServer() on the other hand "simulates" the server's time using the PC clock, so even when the tick does not come in or when there is delay in communications, the time continues to update.

I believe, it serves the purpose of allowing code to be able to detect the passing of time, even when there is no tick, or maybe to allow the code to detect when there is a delay, either by lack of data or a problem in communications.

However, in the back-test, they are both set to the same time as simulated by the back test.

Thank you! That explanation makes sense if its correct.
 
Fernando Carreiro:

I believe, but I am not sure, that TimeCurrent() only gets updated when a new Tick arrives. So, if no tick arrives or the connection goes down for a while, it gets stuck. Please note that I have not tested this.

This is correct, I confirm.
 
Alain Verleyen:
This is correct, I confirm.

In regards to part 2 of my original post; if the time axis in the Strategy Tester represents the brokers time (trade server time) and TimeGMT() is always equal to that time, then how would you get the actual GMT at that simulated point in time?

I would think that the Expert needs to be aware of the trade servers time offset from GMT....

 
emerson2102: In regards to part 2 of my original post; if the time axis in the Strategy Tester represents the brokers time (trade server time) and TimeGMT() is always equal to that time, then how would you get the actual GMT at that simulated point in time? I would think that the Expert needs to be aware of the trade servers time offset from GMT....
You will have code an exception, so that when it detects that it is run in the Tester, that it calculates the time offset, based off for example an input parameter.
 
Fernando Carreiro:
You will have code an exception, so that when it detects that it is run in the Tester, that it calculates the time offset, based off for example an input parameter.

This means that the user would need to know the GMT offset of the trade server and input the value each time.

This works but I wonder if there is a way for the EA to get that information automatically.

The only way I can think of doing it is by first running a script on live to write TimeGMT() and TimeCurrent() to a file. The EA being tested would then read this file and calculate the difference in hours between the two times.

 
emerson2102: The only way I can think of doing it is by first running a script on live to write TimeGMT() and TimeCurrent() to a file. The EA being tested would then read this file and calculate the difference in hours between the two times.

No need to write it to a file. Simply write the time difference/offset in seconds (or minutes) to a Global Terminal Variable which can then be read by any EA or Indicator, whether in the tester or not.

EDIT: However, how will you compensate for Daylight Savings time changes in a back-test?

 
Fernando Carreiro:

No need to write it to a file. Simply write the time difference/offset in seconds (or minutes) to a Global Terminal Variable which can then be read by any EA or Indicator, whether in the tester or not.

EDIT: However, how will you compensate for Daylight Savings time changes in a back-test?

Thats a good idea! I was not aware that timezones differed by seconds and minutes, I thought they were all just an hour apart... ( UTC+0, UTC+1, UTC+2, etc.)

As for DST, does TimeDaylightSavings() work in the Strategy Tester?

If it does then you could find out the standard time (winter time) offset from GMT of the brokers trade server and only use that as the input... assuming we use the previous input idea.

The EA would then adjust this standard offset if TimeDaylightSavings() returned != 0

 
emerson2102:

Thats a good idea! I was not aware that timezones differed by seconds and minutes, I thought they were all just an hour apart... ( UTC+0, UTC+1, UTC+2, etc.)

As for DST, does TimeDaylightSavings() work in the Strategy Tester?

I wrote seconds because it is the easiest to calculate. I don't know of any broker using minute time zones, but in real life there are places with time zones that differ by the 30 minutes and I believe there are even a few that differ by 15 or 45 minutes.

As for TimeDaylightSavings , I don't think that will work in the Tester just as the TimeGMT does not work because is dependent the computer's settings 

 
emerson2102:

This means that the user would need to know the GMT offset of the trade server and input the value each time.

This works but I wonder if there is a way for the EA to get that information automatically.

The only way I can think of doing it is by first running a script on live to write TimeGMT() and TimeCurrent() to a file. The EA being tested would then read this file and calculate the difference in hours between the two times.

It's simply not possible to get correct GMT/DST in the Strategy Tester, because there is NO WAY to get all the needed information automatically. You can calculate the current GMT offset of the broker server, but it doesn't say you anything about the past. And the only way to know the DST is be asking to your broker. From there, it's possible to calculate it correctly in the tester, IF there was no change on the server side in the past.
Reason: