Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

AccurateTimer - MetaTrader 5 Library | MT5 EA Download - MetaTrader 5 Resources

author EAcpu | 3 reads | 0 comments |

AccurateTimer - library for MetaTrader 5

AccurateTimer - library for MetaTrader 5

AccurateTimer - library for MetaTrader 5

MetaTrader 4/5 standard timers are based on system timer calls and therefore may not work accurately. We can check this by running the following simple Expert Advisor:

 Input integer timer = 1000 ; // Number of milliseconds for the timer to fire

#define string(A) #a + ”=" + ( string )(A) + "Ms.\n"

const boolean init = eventset milliseconds_timer (timer); // Display the current timer error and its average as an annotation on the chart
blank timer ()
{
Stationary own start time = 0 ;
rest integer count = 0 ;
sum of integers at rest = 0 ;

if (start time)
  {
const integer running time = ( integer )( getmicrosecond count () - start time) / 1000 ;
const int error = runtime - timer * count;

    sum+=error;

comment (TOSTRING(timer) + string time (runtime/ 1000 , TIME_SECONDS ) + "\n" +
            TOSTRING(error) + TOSTRING(( double ) sum/count));
  }
Others start time = get microsecond count ();

  count++;
}

In the chart annotation (top left) it shows how the timer delay grows:

AccurateTimer - library for MetaTrader 5

Screenshots show that in just one minute, the second timer produced more than a second of lag. And this lag increases over time!

This library can improve the accuracy of standard timers of any Expert Advisor/Indicator. To do this, add the following line at the beginning of your code:

 #Include <AccurateTimer.mqh> // Improve the accuracy of standard timers

The following comments will then be displayed:

AccurateTimer - library for MetaTrader 5

After running for ten minutes, the average deviation from the ideal (theoretical) timer is about 1 millisecond, and the error does not increase.

It's always good to have an accurate timer. But for some tasks it is necessary. For example, the second timer is with the trading server time .

This cross-platform library works with all standard timers (OnTimer) . It does not affect the execution speed in the strategy tester.

Improve the accuracy of existing and new programs with just one line!


Attachment download

📎 accuratetimer_example1.mq5 (0.85 KB)

📎 accuratetimer_example2.mq5 (0.82 KB)

📎 accuratetimer.mqh (2.97 KB)

Source: MQL5 #19859

Verification code Refresh