Skip to content
All library documents

Persisting MQL5 Expert Advisor State with a Flat-File Key-Value Store

Article MQL5 articles

Summary

The article describes a lightweight way to preserve an Expert Advisor’s operational state across terminal restarts using a human-readable key-value file. It proposes storing one key and serialized value per line in the permitted MQL5 file area, with an in-memory hash map for lookups and a typed interface for retrieving strings, numbers, booleans, and dates. The design separates value representation, parsing, serialization, caching, and the public store API. Its operations include loading, setting, deleting, and checking for keys.

The article contrasts this approach with terminal global variables, which share a namespace, store only doubles, and are less convenient to inspect. It gives complexity estimates for cache and disk operations and explains that writes rewrite the full file. The author identifies important constraints: no schema enforcement, unsafe concurrent writers to one file, and possible partial writes if the terminal crashes during a save. The design is intended for small state collections and prioritizes portability and inspectability; systems with stricter durability or concurrency needs may require a different storage mechanism.

Key ideas

  • A terminal restart clears an EA’s volatile runtime state unless the application persists it separately.
  • A flat file can store readable key-value records while an in-memory map supports fast access.
  • A typed API can convert serialized strings into values such as numbers, booleans, and dates.
  • Setting or deleting a record requires rewriting the file, so disk work grows with the number of entries.
  • Concurrent writes and interrupted saves are reliability limits that must be considered.

Tags

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.