Loading…

Runtime Invariant Monitor

A classic assert() is the wrong tool for live trading code. Stopping an EA dead the moment an internal expectation briefly breaks - a stale quote, a momentarily odd order count during a partial fill, a config value out of its expected range - can leave open positions unmanaged, which is usually worse than the original problem. What a production EA actually needs is a loud, persistent record that something was wrong, without the program stopping. RimCheck takes an already-evaluated boolean condition and a short context string under a named check. When the condition holds, nothing happens. When it fails, the violation is counted per check name and journaled with Print - except that a check which stays broken tick after tick would otherwise flood the Journal into uselessness within seconds, burying every other log line under one repeated message. This header solves that with a simple, predictable throttle: the first few violations of a check are always logged in full, then only every Nth further violation is logged afterward - the check is still counted every single time (RimGetViolationCount is exact), only the Journal output rate slows down. State is kept in a fixed-size global table (RIM_MAX_TRACKED_CHECKS distinct check names), deliberately capped rather than growing without bound - an EA should have a small, known set of invariants, not an open-ended list. Notes and limitations This is an observability tool, not a safety mechanism - RimCheck never stops, closes trades, or otherwise reacts on its own; it only records and journals. Pairing it with a real reaction (an alert, a trade-halt flag your EA checks) is the caller's responsibility. RIM_ALWAYS_LOG_COUNT and RIM_THROTTLE_INTERVAL are compile-time constants at the top of the header, not runtime inputs - change and recompile if a different throttling rate is needed. The tracked-check table is capped at RIM_MAX_TRACKED_CHECKS (64 by default); a 65th distinct check name still gets journaled on every occurrence (never silently dropped) but is not counted toward a per-check total, only reported as "untracked, table full" - raise the constant if a project genuinely needs more than 64 distinct invariant names. Violation timestamps use TimeCurrent() (server/chart time), not the local PC clock.

Script · MT5 · v1.0 · Free

About Runtime Invariant Monitor

Runtime Invariant Monitor is a script for MetaTrader 5. It has 1 setting, such as Simulated ticks. MQL source code included.

Files in the download: RuntimeInvariantMonitor.mq5.mqh, RuntimeInvariantMonitor.txt, RuntimeInvariantMonitorDemo.ex5, RuntimeInvariantMonitorDemo.mq5.

Settings

SettingDefaultWhat it does
Simulated ticks60how many fake ticks to simulate

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: MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria