Strategy optimization result do not match single run test result - page 7

 
Alain Verleyen #:
Thank you.

I've found out that the wrong use of the GetTickCount() function was the root cause of my inconsistent results. Instead I had to use TimeCurrent() to check the status of an pending order x seconds after placing it.

Two remarks:
Not sure why this wasn't an issue in older MT5 versions. 
This seems only an issue in testing. In live trading GetTickCount() worked fine.

Hopefully this finding might help those who made the same coding mistake.

 
virtualreal #:

This seems only an issue in testing. In live trading GetTickCount() worked fine.

GetTickCount is a system real timer, while the tester simulates internal timing based on history of ticks and their playback (in visual or fast mode). During one tick of the real timer the tester can replay a thousand of historical ticks.

TimeCurrent is simulated by the tester.

 
Stanislav Korotky #:

GetTickCount is a system real timer, while the tester simulates internal timing based on history of ticks and their playback (in visual or fast mode). During one tick of the real timer the tester can replay a thousand of historical ticks.

TimeCurrent is simulated by the tester.

Thank you for your clear explanation.
 
Francesco Baldi #:

Hi, I've prepared a package containing an EA that triggers the problem, I've included the setfile and a snapshot of the settings, optimized params are in the setfile.

I'm using tickstory data but it should not make any difference, I had the problem with broker data as well.

It's not necessary to run the whole optimization (it's quite lengthy), after a few results it can be stopped and a single test run.

In particular I've tested this result:

OpenBuyHour: 1

CloseBuyHour: 18

which reports a profit in the optimization and a loss in the single test run.

Hope this helps.


Francesco, 

Thanks to your ex5 and information, the reason for your issue was found :

As you know, with an optimization, there is no log output. From some old build, an ERROR was SET, affecting _LastError. Per se, it should not be a problem. BUT that behaviour combined with an MQL5 code that check _LastError when it should not be, leads to the discrepancies between some optimization results and "Single Test" results, as with a Single Test, printing to log is output normally and no error is set.

From build >=5880 the Tester behaviour will be changed, no more error set when a log can't be output.

Though, I want to recall that GetLastError() (or _LastError) should only be checked when APPROPRIATE, so just after a function call which can set an error as documented and after ResetLastError() was previously call.

 
Alain Verleyen #:

Francesco, 

Thanks to your ex5 and information, the reason for your issue was found :

As you know, with an optimization, there is no log output. From some old build, an ERROR was SET, affecting _LastError. Per se, it should not be a problem. BUT that behaviour combined with an MQL5 code that check _LastError when it should not be, leads to the discrepancies between some optimization results and "Single Test" results, as with a Single Test, printing to log is output normally and no error is set.

From build >=5880 the Tester behaviour will be changed, no more error set when a log can't be output.

Though, I want to recall that GetLastError() (or _LastError) should only be checked when APPROPRIATE, so just after a function call which can set an error as documented and after ResetLastError() was previously call.

Wow, that post was from 2024.

Well, better late than never.

 
Francesco Baldi #:

Wow, that post was from 2024.

Well, better late than never.

I never give up 😉