Loading…

Data Freshness Guards

A symbol that is not already in Market Watch, a price feed that has silently stalled, or a chart whose history has not finished downloading can all leave MarketInfo(symbol, MODE_TIME) or iClose()/iTime() returning a stale timestamp or an outright zero, with no error raised anywhere - the calling code just quietly computes an entry decision, a risk figure, or a cross-symbol comparison on old or missing data. Every project that touches more than one symbol ends up needing this check, and it is usually hand-written slightly differently (and slightly wrong) each time. This header turns "is this data fresh enough to trust" into one small, reusable guard. DataFreshnessGuardCheck() reports quote age (from MODE_TIME) and bar age (from the current timeframe's bar-0 open time) separately, each against its own configurable threshold - a quote can go stale in seconds during a feed outage, while a bar's own natural lifetime is the timeframe's period, so the two need independent age limits rather than one shared number. DataFreshnessGuardAssert() wraps both into a single pass/fail check plus a human-readable reason string, suitable as a guard clause at the top of OnTick or OnCalculate. Notes and limitations - This checks whether the DATA looks fresh, not whether the market is genuinely open. A quote correctly stops updating over a weekend or a public holiday - the guard will (correctly) report it as stale by a wide margin at those times, which is expected behaviour, not a bug. - Passing a symbol that is not in Market Watch and has never been selected can return MODE_TIME as 0, which this guard already treats as not-fresh - it does not attempt to auto-select the symbol itself (a design choice: callers that want auto-select should call SymbolSelect() themselves before checking, since silently selecting symbols behind the caller's back has its own side effects). - The bar-age check only looks at bar 0 (the current/forming bar) on the given timeframe. It does not verify that a long enough run of HISTORICAL bars exists - pair this with an explicit iBars() count check if a strategy also needs a minimum amount of history loaded. - PERIOD_MN1's "period seconds" is a fixed 30-day approximation, since calendar months are not a fixed length - a bar-age multiple of 1 on MN1 can therefore flag a genuinely current monthly bar as stale in a 31-day month. Use a slightly larger multiple for MN1 if this matters.

Script · MT5 · v1.0 · Free

About Data Freshness Guards

Data Freshness Guards is a script for MetaTrader 5. It is set up for gold (XAUUSD). It has 3 settings, such as Max quote age seconds, Max bar age multiple and Other symbol. MQL source code included.

Files in the download: DataFreshnessGuards.mq5.mqh, DataFreshnessGuards.txt, DataFreshnessGuardsDemo.ex5, DataFreshnessGuardsDemo.mq5.

Settings

SettingDefaultWhat it does
Max quote age seconds120Quote considered stale beyond this many seconds
Max bar age multiple3Bar considered stale beyond (bar period * this) seconds
Other symbolXAUUSDA second symbol to test, e.g. one not in Market Watch

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: Gold (XAUUSD) · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria