Loading…

Stale Quote Execution Gate

A feed outage or a broker-side symbol freeze does not always drop the terminal connection. MarketInfo(symbol, MODE_TIME) can keep reporting the same timestamp for minutes while the terminal otherwise looks perfectly normal - no disconnect banner, no obvious sign anything is wrong. An EA that sends an order during that stretch is trading against a price that is no longer real. This EA tracks quote age for one or more watched symbols - MarketInfo's own last-quote-time minus the current server time - and closes a per-symbol "gate" the moment that age reaches a configured threshold, reopening it the moment fresh quotes resume. The gate state is published through a global variable per symbol (GlobalVariableSet), named by prefix + symbol, that any other EA's own trading logic can read before its own OrderSend call: treat a value of 1.0 as "fresh, order allowed" and 0.0 (or the variable simply not existing) as "stale, hold off." An optional self-test (off by default) demonstrates the mechanism directly: on a timer, it attempts a minimal-lot order on a chosen symbol only when that symbol's own gate is open, and skips with a logged reason when the gate is closed - proof the check genuinely blocks rather than just reporting a number nobody acts on. Notes and limitations The published gate is advisory - it does nothing on its own unless another EA's own order- sending code actually reads it. This EA cannot force a third-party strategy to respect it. Quote-age staleness detection depends entirely on MarketInfo(symbol, MODE_TIME) being reliable on your broker's feed; some brokers batch or delay quote timestamps in ways that could make a genuinely live feed look momentarily stale. Watch the Experts log for a period before relying on this in place of manual judgement. On OnDeinit, the published global variables are deleted rather than left at their last value, so a downstream EA reading a missing variable should treat that the same as "gate closed" - this is stated so nobody assumes a missing variable means "nothing wrong." The self-test, when enabled, sends and immediately closes a real minimal-lot order - spread and slippage cost real money on every attempt.

Expert advisor · MT4 · v1.0 · Free

About Stale Quote Execution Gate

Stale Quote Execution Gate is an expert advisor (trading robot) for MetaTrader 4. It has 9 settings, such as Watch symbols, Stale after seconds and Gate global var prefix. MQL source code included.

Files in the download: StaleQuoteExecutionGate.ex4, StaleQuoteExecutionGate.mq4, StaleQuoteExecutionGate.txt.

Trading robots can lose money. Check every setting and test on a demo account before using real funds.

Settings

SettingDefaultWhat it does
Watch symbolsComma-separated symbols to watch, "" = the attached chart's symbol only
Stale after seconds20Quote age, seconds, at or beyond which the gate closes
Gate global var prefixStaleQuoteGate_Global variable name prefix: prefix + symbol, 1.0 open / 0.0 closed
Alert on closetruePop a terminal Alert() the moment the gate closes for any watched symbol
Enable self testfalseDemonstrate the gate with a minimal-lot order attempt, off by default
Self test symbolSymbol for the self-test order, "" = attached chart's symbol
Self test every minutes30Minutes between self-test attempts when enabled
Self test magic number24105001Magic number that tells this EA's orders apart
Poll seconds2How often quote ages are re-checked

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Experts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
  6. Test it first on a demo account or in the Strategy Tester.

Download the PDF guide

Tags: MT4 Expert Advisor · MQL4 Source Code

Expert advisors (EA) for MT4 & MT5 · PolyMaria