new mql4 providing millisecond in timestamps.... - page 5

 
AutoAbacus #:

Interesting links thank you, which led me to Microsecond Resolution Time Services for Windows. I have conducted some tests based on information from those pages.

My tests on a Win 7 PC and a Windows 2012 VPS indicate that GetTickCount() always has a resolution of 15.6 msecs (64 interrupts per second) regardless of the system timer setting, whereas the resolution when obtaining millisecond time by calling the kernel32.dll functions GetSystemTime() [or GetLocalTime()] or GetSystemTimeAsFileTime() is affected by the system timer settings, and can give down to 0.5 msec resolution on both machines I tested.

GetTickCount() Resolution

Here is code for a script to test GetTickCount() resolution:

This always gives 15 or 16 (i.e. 15.6) on both machines tested regardless of system timer resolution changes mentioned below for the other tests.

GetSystemTime() Resolution

Now things start to get interesting. Here is code for a script to test GetSystemTime() resolution:

That gives a resolution of 15/16 msecs on a freshly booted PC with no other software running, but 1 msec if Chrome is running on the PC! As angevoyageur's second link explains Chrome sets the system timer to 1 msec resolution, as does some other software.

I found two little utilities for setting the system timer resolution, so that 1 msec (or even 0.5 msecs) resolution can be obtained in a controlled way on a cleanly booted machine:

Windows System Timer Tool: http://vvvv.org/contribution/windows-system-timer-tool

Timer-Resolution: http://www.lucashale.com/timer-resolution/

I prefer the first one of the two, Windows System Timer Tool. With that I could reliably get 1 msec resolution via GetSystemTime(). GetLocalTime() could also be used similarly.

Btw the script code above is an example of how much better new MT4 code can be thanks to structs. In the old MT4 accessing GetSystemTime() required use of an integer array plus lots of messy bit manipulation.

GetSystemTimeAsFileTime() Resolution

Finally, I noted that Microsecond Resolution Time Services for Windows mentioned that GetSystemTimeAsFileTime() is a faster function for accessing system time, as well as requiring a smaller and simpler struct. The latter is certainly true for the new MT4 as the "struct" can be reduced to just a ulong.

Here is code for a script to test GetSystemTimeAsFileTiime() resolution:

If Windows System Timer Tool is used to set a system timer resolution of 0.5secs that little script reports a resolution of 5000 (or sometimes 5001) 100 nsecs units = 0.5 msecs.

Use of GetSystemTimeAsFileTiime() is indeed simpler and can show finer resolution.

Here are some pics of this one in use.

After a clean boot:

With Timer Tool used to set the system timer resolution to 1 ms:


And with Timer Tool used to set the system timer resolution to 0.5 ms:


Conclusion

The best function to use for obtaining millisecond timings in MT4 is GetSystemTimeAsFileTiime() called as shown in the test script above, with a utility such as Windows System Timer Tool used to set the desired system timer resolution.


Hi there, I know this is an old thread, but does anyone know if the `GetSystemTimeAsFileTime()` from `kernel32.dll` is still valid nowadays?


I'm asking because I get incorrect time value when calling this function by passing a `ulong` as you showed in your example.

I realize that the Microsoft documentation here, have different parameters for this function (not using a `ulong`), so I'm not sure if the ulong is compatible somehow or it was some day.

The values I'm getting after calling this function are like `133086874321194`, which is a microseconds date from 1974 :-S (I took the output from `Print(time)` or `PrintFormat("%I64d", time)` or `PintFormat("%llu", time)`).


The thing is that I would find useful to get the millis time since epoch from a function call.

Any ideas on that? Thanks!


N.B. The `GetTickCount()` seems to present the problems described here (the uint size): https://stackoverflow.com/a/44137948/6108874


EDIT [solved]:

For anyone landing here, this made the job: https://www.mql5.com/en/forum/146837/page4#comment_13522420

but for MQL4 it seems the Windows WinAPI is not available (though not 100% sure):

MQL5: Added support for working with WinAPI functions to Standard Library. Now, there is no need to import libraries manually and describe function signatures to use operating system functions in an MQL5 program. Simply include the header file from the MQL5\Include\WinAPI directory.


So you can still define the required struct by yourself before the `#import` as:


// MQL5 seems to have Include\WinAPI\windef.mqh - constants, structures and enumerations

#ifdef __MQL4__

//+------------------------------------------------------------------+

//| MQL4 equivalent of Windows _FILETIME struct: GetSystemTimeAsFileTime().

//| @see: https://learn.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime

//+------------------------------------------------------------------+

struct FILETIME {

  uint dwLowDateTime;

  uint dwHighDateTime;

};


//+------------------------------------------------------------------+

//| MQL4 equivalent of Windows GetSystemTime()/GetLocalTime()/... struct.

//| Credits: https://www.mql5.com/en/forum/146837/page3#comment_3702187

//+------------------------------------------------------------------+

struct SYSTEMTIME {

  ushort wYear;         // 2014 etc

  ushort wMonth;        // 1 - 12

  ushort wDayOfWeek;    // 0 - 6 with 0 = Sunday

  ushort wDay;          // 1 - 31

  ushort wHour;         // 0 - 23

  ushort wMinute;       // 0 - 59

  ushort wSecond;       // 0 - 59

  ushort wMilliseconds; // 0 - 999


  string toString() {

    return StringFormat("%hu-%02hu-%02hu %02hu:%02hu:%02hu.%03hu", wYear, wMonth, wDay, wHour, wMinute, wSecond, wMilliseconds);

  }

};

#endif


#import "kernel32.dll"

  // Millisecond (ms) precision, limited by Windows timer configuration?? (15-16 ms by default)

  void GetSystemTime(SYSTEMTIME &time); // (struct version)

  void GetLocalTime(SYSTEMTIME &time); // (struct version)

  // Microsecond (us) precision, limited by Windows timer configuration?? (15-16 ms by default)

  void GetSystemTimeAsFileTime(FILETIME& t); // (ulong version) Returns the system time in 100-nsec units

#import


GetSystemTimeAsFileTime function (sysinfoapi.h) - Win32 apps
GetSystemTimeAsFileTime function (sysinfoapi.h) - Win32 apps
  • 2021.10.13
  • karl-bridge-microsoft
  • learn.microsoft.com
Retrieves the current system date and time. The information is in Coordinated Universal Time (UTC) format.