Download MetaTrader 5

Changing Refresh Timer from 15.6ms to 1ms

To add comments, please log in or register
jan100
145
jan100  

In MT4, when callling EventSetMillisecondTimer(1), the code inside in fact is not executed every millisecond. It gets executed about every 15.6ms (1000 / 64 ms).

I want it to be executed every millisecond, so I tried to call timeBeginPeriod(1) from winmm.dll on Windows Server 2008, 2012 as well as on Windows 7:

#import "winmm.dll"
int timeBeginPeriod(uint per);
#import

void OnInit()
  {
   timeBeginPeriod(1); }

However this did not change a thing. The code inside EventSetMillisecondTimer(1) was still only executed every 15.6ms. DLL calls were allowed of course and there was no error message at all.

Does anybody know a way to make MT4 execute the code every millisecond in this case?

Many thanks in advance! 

honest_knave
Moderator
2326
honest_knave  

Yes, my own experiments with this on MT5 never got the resolution to drop below 15.6ms

2017.02.18 00:45:16.453 Test (Ger30Mar17,M1) Average delay: 15618 μs

2017.02.18 00:45:32.075 Test (Ger30Mar17,M1) Average delay: 15621 μs

2017.02.18 00:45:47.700 Test (Ger30Mar17,M1) Average delay: 15624 μs

 

The winmm.dll call appears to have no effect, despite it being used in this article

So, I'm interested to see what other people have to say about this one  

PS If your trading strategy depends on 1ms time resolution, I may humbly suggest MT4 isn't the right platform for you! 


jan100
145
jan100  
honest_knave:

Yes, my own experiments with this on MT5 never got the resolution to drop below 15.6ms

2017.02.18 00:45:16.453 Test (Ger30Mar17,M1) Average delay: 15618 μs

2017.02.18 00:45:32.075 Test (Ger30Mar17,M1) Average delay: 15621 μs

2017.02.18 00:45:47.700 Test (Ger30Mar17,M1) Average delay: 15624 μs

 

The winmm.dll call appears to have no effect, despite it being used in this article

So, I'm interested to see what other people have to say about this one  

PS If your trading strategy depends on 1ms time resolution, I may humbly suggest MT4 isn't the right platform for you! 


Thanks for the reply!

My trading strategy does not depend on 1ms time resolution.

But just being curious, do you know a trading platform that supports this? 

honest_knave
Moderator
2326
honest_knave  

I did some more experimentation, using a 3rd party tool to monitor the timer.

Interestingly, it showed that winmm.dll was working exactly as expected. But the timer resolution wasn't reducing.

Before (default timer) 

 

During (timer reduced to 1 ms)

 

After (timer reset to default)

 

So, I'm officially out-of-my-depth now and will leave it to the pros.

jan100:

But just being curious, do you know a trading platform that supports this? 

Afraid not!

JC
1515
JC  
honest_knave:

So, I'm officially out-of-my-depth now and will leave it to the pros.

No a "pro", but... it's overwhelmingly likely that MT4 implements EventSetMillisecondTimer() using the Win32 SetTimer(), and that will control the minimum resolution.

The only way of executing code every millisecond is almost certainly going to be to run in an infinite loop (until IsStopped() is true), using a high-resolution timer such as QueryPerformanceCounter() to watch for the time changing by 1 millisecond.

The difficulty is going to be finding a way of yielding processor time, and not consuming 100% of a processor core, while waiting for the millisecond-wait to elapse, and without yielding in such a way that it re-invokes the 15.6ms limit. 

Alain Verleyen
Moderator
33336
Alain Verleyen  
This is a Windows limitation, nothing to do wit MT4 or any other trading platform.

It can be modified but this is probably not a good idea unless you know well what you are doing.
Windows Timer Resolution: Megawatts Wasted
Windows Timer Resolution: Megawatts Wasted
  • brucedawson
  • randomascii.wordpress.com
The default timer resolution on Windows is 15.6 ms – a timer interrupt 64 times a second. When programs increase the timer frequency they increase power consumption and harm battery life. They also waste more compute power than I would ever have expected – they make your computer run slower! Because of these problems Microsoft has been telling...
honest_knave
Moderator
2326
honest_knave  
Alain Verleyen:
This is a Windows limitation, nothing to do wit MT4 or any other trading platform.

It can be modified but this is probably not a good idea unless you know well what you are doing.

Hi Alain, that is what we are doing. In fact, the timer tool you see in my screenshots above is from the links in that article's comments.

The Metaquotes article I linked above adjusts the system's timer in this manner. However, they are not using it in the context of reducing OnTimer()'s resolution. So perhaps JC's comments are on the mark.

_____________________ 

Detailed MetaTrader 5 vs QUICK Comparison Reports

An application in QLUA measures time by calling the operating system timer with the default timer resolution of 10…15.6 milliseconds (usually 15.6 ms). We had to improve the accuracy of time measurement in QLUA by simply reducing the system timer resolution to 1 ms. This is done by using the script SpeedupSystemTimer.mq5, which calls the timeBeginPeriod function of the Winmm.dll system library 

#import "winmm.dll"
int timeBeginPeriod(uint per);
#import

void OnInit()
  {
   timeBeginPeriod(1);
  }

void OnTick()
  {
  }

We had run the script before starting tests in MetaTrader 5 additionally allowing DLL calls, which resulted in the operation measurement error in QUIK of no more than 1 ms.

The MQL5 language provides a ready function GetMicrosecondCount(), so the accuracy of speed measurement in the MetaTrader 5 terminal is 1 microsecond (1 millisecond=1000 microseconds). 

honest_knave
Moderator
2326
honest_knave  
JC:

No a "pro", but... it's overwhelmingly likely that MT4 implements EventSetMillisecondTimer() using the Win32 SetTimer(), and that will control the minimum resolution.

The only way of executing code every millisecond is almost certainly going to be to run in an infinite loop (until IsStopped() is true), using a high-resolution timer such as QueryPerformanceCounter() to watch for the time changing by 1 millisecond.

The difficulty is going to be finding a way of yielding processor time, and not consuming 100% of a processor core, while waiting for the millisecond-wait to elapse, and without yielding in such a way that it re-invokes the 15.6ms limit. 

I have no frame of reference, but this certainly seems plausible to my limited understanding of the situation.
Alain Verleyen
Moderator
33336
Alain Verleyen  
honest_knave:

Hi Alain, that is what we are doing. In fact, the timer tool you see in my screenshots above is from the links in that article's comments.

...
Thanks, sorry I missed the link. Was reading too fast.
honest_knave
Moderator
2326
honest_knave  
It's actually been quite insightful, using that tool to see what keeps messing with my system timer!
Alain Verleyen
Moderator
33336
Alain Verleyen  
honest_knave:
It's actually been quite insightful, using that tool to see what keeps messing with my system timer!
By the way the link I posted is quite interesting...
CPU Utilisation:Individual process with significant processor utilisation.
This process is responsible for a significant portion of the total processor utilisation recorded during the trace.
Process Name     terminal64.exe
PID     6424
Average Utilisation (%)     4.09
Module     Average Module Utilisation (%)
\Device\HarddiskVolume2\Program Files\MT5\MetaTrader 5\terminal64.exe     3.64
\SystemRoot\system32\ntoskrnl.exe     0.12
\Device\HarddiskVolume2\Windows\System32\user32.dll     0.11
MT4 supporters will like it :-D
12
To add comments, please log in or register