Persisting MQL5 Expert Advisor State with Versioned Binary Files
Summary
The article presents a way to preserve an Expert Advisor’s internal state across terminal restarts by serializing a fixed-size structure to a binary file. Examples of state worth saving include trade counts, win and loss streaks, lot multipliers, equity high-watermarks, and the last processed bar time. Broker-held positions and trade details can be queried from the server, so the method focuses on strategy bookkeeping that exists only in the EA’s memory.
The framework uses MQL5 binary file operations to write and reload the structure, with a version field to detect layout changes and a shared-folder option for access across terminals on one machine. It warns that only fixed-size value types are suitable for direct struct serialization; strings and dynamic data need other handling. The approach addresses restart continuity and can support risk controls such as daily limits or sizing progressions, but it does not itself validate those controls or protect against every storage failure. The article provides implementation guidance rather than evidence from measured live-trading outcomes.
Key ideas
- Persist strategy bookkeeping separately from positions, which can be retrieved from the broker’s server.
- A fixed-size struct can hold counters, flags, timestamps, and sizing state for binary serialization.
- A version field helps detect when stored bytes no longer match a changed struct layout.
- Direct struct serialization requires fixed-size value members rather than strings, pointers, or dynamic arrays.
- Persisted state can preserve risk-related logic across restarts, but does not itself verify strategy correctness.
Tags
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.