Loading…

Server Clock Drift Estimator

A single sample of "server time minus local PC time" is noisier than it looks - PC clock jitter, a slow tick during a busy moment, a VPS pausing briefly under load - and the raw difference can swing by whole seconds between two calls taken a second apart, even when the true underlying offset is perfectly stable. Any code that gates a news-time filter, a session-open trigger or a scheduled action off a single TimeCurrent()- TimeLocal() read is trusting that noise. This library tracks an exponential moving average of the drift observation AND an exponential moving average of its squared deviation from that average - the standard streaming approximation to a running mean and variance that needs no stored history of past observations and adapts if the true drift itself slowly shifts, for example when the broker's server clock gets resynced against a time source. CDE_AddObservation feeds one raw drift sample in; CDE_ObserveNow is a convenience wrapper that samples TimeCurrent() against TimeLocal() directly and feeds the result. CDE_GetDrift reads the current smoothed estimate; CDE_StandardError and CDE_ConfidenceHalfWidth turn the running variance into an honest uncertainty band, scaled by an effective sample size capped at 1/alpha - an exponential moving average never "remembers" more than roughly that many independent observations' worth of information, however many samples have actually been fed in, so the standard error is not allowed to shrink toward zero after thousands of ticks the way a true running count's would. Notes and limitations - This estimates the drift BETWEEN the broker's TimeCurrent() and this PC's TimeLocal() - it does not, and cannot, tell which of the two is "correct" against a real external time reference (NTP). It is a relative estimate. - The confidence band assumes the underlying drift observations are approximately independent and normally distributed around the true drift - reasonable for clock jitter, not a rigorous statistical guarantee. - zScore is supplied directly by the caller (1.645/1.960/2.576 for 90%/95%/99%) rather than a confidence-level input, since only a handful of standard levels are ever needed and no inverse-normal routine is required this way. - An EMA's effective memory is capped at 1/alpha observations - increasing alpha to react faster to a genuine resync also makes the estimate noisier in steady state; there is no free lunch between the two.

Script · MT5 · v1.0 · Free

About Server Clock Drift Estimator

Server Clock Drift Estimator is a script for MetaTrader 5. It has 2 settings, such as Sample count and Alpha. MQL source code included.

Files in the download: ServerClockDriftEstimator.mq5.mqh, ServerClockDriftEstimator.txt, ServerClockDriftEstimatorDemo.ex5, ServerClockDriftEstimatorDemo.mq5.

Settings

SettingDefaultWhat it does
Sample count200Observations to feed per stream
Alpha0.10EMA smoothing factor (0, 1]

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: Trading Sessions · News Trading · Time Filter · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria