Readiness Gate
An EA's OnTick or an indicator's OnCalculate is often written assuming history is already there: iMA/iClose/iBars just work, because on a chart that has been open for a while they usually do. The gap shows up on a freshly attached chart, right after the terminal opens, or the first tick after a symbol not previously in Market Watch gets touched - the requested bars can still be downloading, and a bare bar-count or price read can come back short or zero for a brief window before the feed catches up. This library turns "is it actually ready" into a declared, checkable fact instead of an assumption. A ReadinessGate holds a list of requirements, each naming a symbol, a timeframe and a minimum bar count, with an optional freshness bound (the most recent bar must not be older than N seconds). ReadinessGateCheck walks every requirement and returns false on the first one not yet satisfied, naming exactly which requirement failed and why - "only 40 of 100 required bars available" versus "last bar is 5400 seconds old" are different problems with different fixes, and this gate does not collapse them into one generic "not ready" flag. Native MQL4's iMA/iClose/iBars and the rest are synchronous calls, not the handle-then-CopyBuffer model MQL5 uses, so there is no indicator- handle-not-ready gap to guard against here - that MQL5-specific concern does not apply to this library. What remains genuinely true on MQL4 is narrower: insufficient downloaded history right after attach, and a stalled feed that has history but has stopped ticking. This gate checks exactly those two things and nothing more; it does not, and cannot, guarantee an indicator's computed VALUE is meaningful, only that the underlying price series it would read from is present and recent. Notes and limitations - A gate holds at most 20 requirements (READINESS_GATE_MAX_REQUIREMENTS); Add* returns false rather than overflow. - This checks that price history EXISTS and is recent - it does not, and cannot, verify that a caller's own indicator calculation over that history has "settled" to a stable value. If a project needs a true warm-up count for an indicator with a long lookback (e.g. a 200-period MA needing 200+N clean bars before its early values are trustworthy), pass that larger number as minBars explicitly; the gate does not compute it for you. - ReadinessGateCheckOne treats a zero last-close as "not ready", which is the correct read for almost every real instrument, but is a false negative on the rare synthetic symbol whose price genuinely IS zero (not something real-money brokers list). - maxStalenessSeconds is a flat number, not session-aware: a symbol that is legitimately quiet outside its own trading hours (e.g. an index CFD overnight) can trip a tight staleness bound during a period that is not actually a feed problem. Set it generously, or 0 to disable the freshness check, for instruments with real quiet hours. - This library only checks and reports; it takes no action of its own (no Sleep, no retry loop) - the caller decides how to wait or what to do while not yet ready.
Script · MT5 · v1.0 · Free
About Readiness Gate
Readiness Gate is a script for MetaTrader 5. It has 3 settings, such as Min bars, Max staleness seconds and Impossible min bars. MQL source code included.
Files in the download: ReadinessGate.mq5.mqh, ReadinessGate.txt, ReadinessGateDemo.ex5, ReadinessGateDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Min bars | 100 | bars required on the current chart's symbol/timeframe |
| Max staleness seconds | 3600 | how old the last bar may be before it is considered stale |
| Impossible min bars | 1000000 | deliberately unmeetable requirement, to show the fail path |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT5 Script · MQL5 Source Code