Weird Anomaly with Time Functions in Strategy Tester

 

Hope someone can explain this. I'm stumped. Look at this small example of the problem:

int start() {
string tt, tt_now;

tt_now = "" + Hour() + ":" + Minute();

Comment("Current Server Time is: ", tt_now, " IsTesting: ", IsTesting(), "\n");

tt = StringConcatenate("Now: ", tt_now, "\n");

Print(tt);

//----
return(0);

}

Each tick, it creates a string with the server time in hours and minutes. It writes this string to the terminal via Comment(), and to

the log via Print(). If I run this small "EA" under the strategy tester, the value displayed via Comment() is the correct simulated server time -- its starts at 01:01 or thereabouts, and ends with 23:59. Then, if I look in the log, the time printed is always the current server time. Since it takes less that a second to run for a period of a single day, the time in the log file never changes. This is even more weird because the string value is only computed once.

This is more than just a theoretical oddity. I am trying to write an EA which takes certain actions at a certain time of day. I need a statement like:

"if (Hour() == x && Minute() == y) {

. . . .

}"

to work correctly, and with the strategy, these functions are always returning the actual time of day that the test is being executed.
I am running this on an FXCM demo account, and they are a reputable Forex broker. If anyone has had any experiences with anything like this,
or has any useful insights, I'd appreciate your help.

Thanks.

dennisne

 

This code works properly

2011.09.12 22:28:00     2011.09.05 00:02  test EURUSD,M5: Now: 0:2
2011.09.12 22:28:00     2011.09.05 00:02  test EURUSD,M5: Now: 0:2
2011.09.12 22:28:00     2011.09.05 00:02  test EURUSD,M5: Now: 0:2
2011.09.12 22:28:00     2011.09.05 00:02  test EURUSD,M5: Now: 0:2
2011.09.12 22:28:00     2011.09.05 00:02  test EURUSD,M5: Now: 0:2
2011.09.12 22:28:00     2011.09.05 00:02  test EURUSD,M5: Now: 0:2
2011.09.12 22:28:00     2011.09.05 00:01  test EURUSD,M5: Now: 0:1
2011.09.12 22:28:00     2011.09.05 00:01  test EURUSD,M5: Now: 0:1
2011.09.12 22:28:00     2011.09.05 00:01  test EURUSD,M5: Now: 0:1
2011.09.12 22:28:00     2011.09.05 00:01  test EURUSD,M5: Now: 0:1
2011.09.12 22:28:00     2011.09.05 00:01  test EURUSD,M5: Now: 0:1
2011.09.12 22:28:00     2011.09.05 00:01  test EURUSD,M5: Now: 0:1
2011.09.12 22:28:00     2011.09.05 00:00  test EURUSD,M5: Now: 0:0
2011.09.12 22:28:00     2011.09.05 00:00  test EURUSD,M5: Now: 0:0
2011.09.12 22:28:00     2011.09.05 00:00  test EURUSD,M5: Now: 0:0
2011.09.12 22:28:00     2011.09.05 00:00  test EURUSD,M5: Now: 0:0
2011.09.12 22:27:59     2011.09.05 00:00  test EURUSD,M5: Now: 0:0
2011.09.12 22:27:59     2011.09.05 00:00  test EURUSD,M5: Now: 0:0
2011.09.12 22:27:59     2011.09.05 00:00  test EURUSD,M5: Now: 0:0
2011.09.12 22:27:59     2011.09.05 00:00  test EURUSD,M5: Now: 0:0

 

it is probably because hour() and minute() returns the current live server time as it is when you run the test.

 

As SDC said . . .

Hour()
"Returns the hour (0,1,2,..23) of the last known server time by the moment of the program start (this value will not change within the time of the program execution).

Note: At the testing, the last known server time is modelled."

from here: https://docs.mql4.com/dateandtime/Hour

Instead use TimeHour(TimeCurrent()); etc.

 
RaptorUK:

As SDC said . . .

Hour()
"Returns the hour (0,1,2,..23) of the last known server time by the moment of the program start (this value will not change within the time of the program execution).

Note: At the testing, the last known server time is modelled."

from here: https://docs.mql4.com/dateandtime/Hour

Instead use TimeHour(TimeCurrent()); etc.

I'm afraid I wasn't very clear. Looking at the code, notice that tt_now is computed only once. When the code calls Comment(), the string as displayed in the upper left corner of the chart is the server time associated with the hisotrical data. For example, if I run a short one-day test using the strategy test, the last time diplayed in the chart window is 23:59, the time at the end of the day. However, if I looke at the log file, which prints the value of the sam tt_now string, the value in the log file is 2:45 for each line printed - the local time when I ran the test. This is totally spooky. The only way that I can think of for this to happen is if MQL4 somehow stores a "deferrred execution" copy of some code which it executes each tiem tt_now is referenced, and if the internal Print() logic somehow executes a copy of Hours() which returns the crrent local time and the Comment()logic executes a different internal version of Hours() which returns the time associated with the hisotrical data. I need to somehow figure out how Comment() is getting the historical data time correctly so that I can do the same thing in my logic. I would have liked to attach a screenshot and a copy of the log file to show what I mean, but they are both at home right now, and I'm at work. Are there any MetaQuotes engineers around that can look into how the MQL4 code execution agent is doing this?


Thanks,


dennisne

 

Don't use Hours() use TimeHours(TimeCurrent())

tt_now = "" + Hour() + ":" + Minute();
Or simplier: TimeToStr(TimeCurrent(), TIME_MINUTES|TIME_SECONDS)
 
dennisne:

I'm afraid I wasn't very clear. Looking at the code, notice that tt_now is computed only once. When the code calls Comment(), the string as displayed in the upper left corner of the chart is the server time associated with the hisotrical data. For example, if I run a short one-day test using the strategy test, the last time diplayed in the chart window is 23:59, the time at the end of the day. However, if I looke at the log file, which prints the value of the sam tt_now string, the value in the log file is 2:45 for each line printed - the local time when I ran the test.

Show an excerpt from your log file . . . I ran your code, this is what I have in my log file . . .

The first part is the time of the log entry, local time, my first line local time is 22:47:47, then there is the date for the Server time, followed by the Server time, 2007.04.05 19:57, then the EA name and the symbol . . finally your output, now 19:57 . . . I'm not sure what your problem is ?

22:47:47 2007.04.05 19:57 EA_Test_Time AUDUSD,H1: Now: 19:57

22:47:47 2007.04.05 19:57 EA_Test_Time AUDUSD,H1: Now: 19:57

22:47:47 2007.04.05 19:57 EA_Test_Time AUDUSD,H1: Now: 19:57

22:47:48 2007.04.05 19:57 EA_Test_Time AUDUSD,H1: Now: 19:57

22:47:48 2007.04.05 19:58 EA_Test_Time AUDUSD,H1: Now: 19:58

22:47:48 2007.04.05 19:58 EA_Test_Time AUDUSD,H1: Now: 19:58

22:47:48 2007.04.05 19:58 EA_Test_Time AUDUSD,H1: Now: 19:58

22:47:48 2007.04.05 19:59 EA_Test_Time AUDUSD,H1: Now: 19:59

22:47:48 2007.04.05 20:00 EA_Test_Time AUDUSD,H1: Now: 20:0

22:47:48 2007.04.05 20:01 EA_Test_Time AUDUSD,H1: Now: 20:1

22:47:48 2007.04.05 20:01 EA_Test_Time AUDUSD,H1: Now: 20:1

22:47:49 2007.04.05 20:03 EA_Test_Time AUDUSD,H1: Now: 20:3

22:47:49 2007.04.05 20:03 EA_Test_Time AUDUSD,H1: Now: 20:3

22:47:49 2007.04.05 20:03 EA_Test_Time AUDUSD,H1: Now: 20:3


 
RaptorUK:

Show an excerpt from your log file . . . I ran your code, this is what I have in my log file . . .

The first part is the time of the log entry, local time, my first line local time is 22:47:47, then there is the date for the Server time, followed by the Server time, 2007.04.05 19:57, then the EA name and the symbol . . finally your output, now 19:57 . . . I'm not sure what your problem is ?

22:47:47 2007.04.05 19:57 EA_Test_Time AUDUSD,H1: Now: 19:57

22:47:47 2007.04.05 19:57 EA_Test_Time AUDUSD,H1: Now: 19:57

22:47:47 2007.04.05 19:57 EA_Test_Time AUDUSD,H1: Now: 19:57

22:47:48 2007.04.05 19:57 EA_Test_Time AUDUSD,H1: Now: 19:57

22:47:48 2007.04.05 19:58 EA_Test_Time AUDUSD,H1: Now: 19:58

22:47:48 2007.04.05 19:58 EA_Test_Time AUDUSD,H1: Now: 19:58

22:47:48 2007.04.05 19:58 EA_Test_Time AUDUSD,H1: Now: 19:58

22:47:48 2007.04.05 19:59 EA_Test_Time AUDUSD,H1: Now: 19:59

22:47:48 2007.04.05 20:00 EA_Test_Time AUDUSD,H1: Now: 20:0

22:47:48 2007.04.05 20:01 EA_Test_Time AUDUSD,H1: Now: 20:1

22:47:48 2007.04.05 20:01 EA_Test_Time AUDUSD,H1: Now: 20:1

22:47:49 2007.04.05 20:03 EA_Test_Time AUDUSD,H1: Now: 20:3

22:47:49 2007.04.05 20:03 EA_Test_Time AUDUSD,H1: Now: 20:3

22:47:49 2007.04.05 20:03 EA_Test_Time AUDUSD,H1: Now: 20:3

Hi Raptor & all,

Attached is a word document. At the top is a screenshot from just after completing running a 1 day backtest (8/1/2011 to 8/2/2011.

You may need to enlarge it a bit, but in the upper left corner is the output of the last Comment(...), indicating that the last tick came in at

a modelled server time of 8/1/2011, 23:59. Below the screenshot is the output of the log file from running this test. From examining the code, we see that the Print() function is outputting the same string variable as the Comment(...) function does. However, the log file shows

a "Mow: ..." time of 6:51. 6:51 is the current server time for the exact time I ran the strategy test (11:51 PM PDT (Los Angeles, CA) is 6:51 AM GMT, and the current server time is GMT time. So ... When I call Comment(...) I get one value, and when I call Print(...) I get a different value, and if I call Hour() and compare it to a numeric constant, like 8, will I be getting the current server time (even while running the strategy tester, which makes no sense and does not agree with the documentation that when testing the server time is modelled (i.e. forced to agree to the historical data). I must be going crazy, but now you can see what I'm seeing.

 
There is no Word document attached . . .
 
RaptorUK:

There is no Word document attached .


Sorry about that. Guess I couldn't find /see the "Upload" button. In any case, thanks to RaptorUK and everyone else. I finally discovered the two parts of the problem, and am now able to move on:

1. I discovered that, when using the strategy tests, the log file is created in tester\logs\... instead of in experts\logs.

2. Not sure why Hours() and Minutes() did not seem to work well, but WH ROeder's suggestion of usnging TimeHours(TimeCurrent()) seems to work fine. Thank you very much.

Thanks to all, and I'm off to trying to solve the next bug :).. .

 
Thanks for the update and well done. :-)
Reason: