Discussing the article: "Persistent Key-Value Store in MQL5: Using Flat Files as a Lightweight Database for EA State"

 

Check out the new article: Persistent Key-Value Store in MQL5: Using Flat Files as a Lightweight Database for EA State.

A lightweight persistence design lets EAs retain counters, flags, and timestamps between terminal restarts. Using only MQL5, CPersistentStore writes a human-readable key=value file in MQL5/Files and serves reads from a CHashMap write-through cache via a typed API. The article analyzes O(1)/O(n) operations, partial‑write risks, and lack of locking, compares with GlobalVariables/SQLite, and provides a demo that reloads state deterministically.

MetaTrader 5 Expert Advisors (EAs) execute within a runtime environment managed entirely by the trading terminal. When the terminal closes, all volatile runtime state is cleared, including counters, flags, and session timestamps. Upon initialization, the EA reverts to its compiled default state. This behavior is a standard characteristic of managed execution environments rather than a deficiency within the MQL5 runtime. Architectural vulnerabilities arise if an EA stores operational state only in global variables and has no independent persistence mechanism to restore it after termination.

In production environments, the absence of a structured persistence layer typically impacts several critical operational domains:

  • Session Metrics and Trade Counters: Cumulative realized profit targets, drawdown trackers, and daily or weekly trade limits reset to zero upon a terminal restart, which can cause the EA to resume execution in breach of defined session risk parameters.
  • Regime Detection States: Structural market classification flags—such as trend versus mean-reversion modes, volatility filters, and asset correlation states—are wiped, forcing the system to reclassify the immediate market environment.
  • Optimization Timestamps: EAs designed to trigger offline parameter optimization on a fixed schedule lose their historical execution records, resulting in redundant, resource-intensive reoptimizations upon restart.
  • Dynamic Risk Settings and Feature Toggles: Variables governing position-sizing multipliers, margin utilization metrics, and conditional runtime filters (such as news blocks or spread thresholds) disappear, disrupting risk management continuity.

These variables represent the core operational state required for robust algorithmic execution. Without an explicit persistence mechanism, every terminal interruption forces a reset equivalent to deploying a new instance with no historical session context.

Rather than relying on SQLite, terminal GlobalVariables, or external storage mechanisms, this article develops a lightweight persistence layer implemented entirely in native MQL5. The solution combines a human-readable flat-file format with a typed API, an in-memory cache, and a modular storage architecture that preserves EA state across terminal restarts while remaining simple to inspect, extend, and integrate into existing projects.

Author: Ushana Kevin Iorkumbul