State Serialization
A terminal global variable holds exactly one number. Real EA state is rarely one number - it is an open position count, a trailing comment, a "did we already scale in" flag, several fields of mixed type that need to move together. The obvious approach is string concatenation with "key=value;" and no escaping, and it breaks the moment a stored value contains a ';' or an '=' itself - a symbol name, a broker comment, anything a user typed. It looks correct in every test that does not happen to use those characters, which is exactly how it survives review and then corrupts a real account's state. Every value passed to this header is escaped on the way out and unescaped on the way back in, so arbitrary text - including text containing the record's own delimiters - round-trips exactly. The demo proves this directly: one of its test values is a string containing a semicolon, an equals sign and a backslash, and it comes back unchanged. The library works as a small in-memory record: set fields with StateSetInt / StateSetDouble / StateSetString / StateSetBool, then either read them straight back with the matching getters, or call StateSerialize() to get one string suitable for a single global variable (see GlobalVarStore.mqh) or a single line in a file. StateSaveToFile() / StateLoadFromFile() wrap that file case directly. StateParse() is deliberately forgiving: a malformed pair (missing '=') is skipped and logged rather than aborting the whole record, so a file damaged in one field does not lose every other field alongside it. Notes and limitations - This is a single global record, not a database - there is one set of fields at a time. Call StateReset() before building a new record, or StateParse() will merge into whatever was already loaded. - Doubles are stored with 8 decimal places, enough to round-trip a normalised price or a lot size exactly. Values needing more precision than that will lose it. - The serialized string has no length limit imposed by this header, but a single terminal global variable holds a double, not a string - route a serialized record through StateSaveToFile() or GvSet on a string-encoding scheme of your own, not through GlobalVariableSet() directly. - Not thread-safe in the sense of concurrent access from two programs at once; both platforms' single-threaded-per-chart execution model makes that a non-issue within one program, but do not share one file between two EAs writing at once.
Script · MT5 · v1.0 · Free
About State Serialization
State Serialization is a script for MetaTrader 5. It has 1 setting, such as State file name. MQL source code included.
Files in the download: StateSerializer.mq5.mqh, StateSerializer.txt, StateSerializerDemo.ex5, StateSerializerDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| State file name | StateSerializerDemo_state.txt | File written under MQL5\Files\ |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Trailing Stop & Break-even · MT5 Script · MQL5 Source Code