Loading…

Checkpoint Generations Manager

Most "save state to a file" code in EAs keeps exactly one snapshot, which gets silently overwritten every time. That is fine until the LATEST snapshot turns out to be built on bad data - a corrupted calculation, a broker anomaly, a bug that only shows up an hour later - and there is no older, still-good snapshot left to fall back to. This library keeps a bounded ring of generations instead of one file. Every checkpoint is written to a fixed-width slot, so the ring can seek directly to any generation's byte offset by arithmetic instead of having to scan the file to find where a variable-length record starts - a deliberate trade against MQL4's limitation that a struct holding a dynamic array has no safe default copy, so a fixed record layout is used throughout rather than one struct per checkpoint. Each generation carries a caller-chosen tag and a fixed count of double fields (agreed once, at Init time), so it can store a strategy's balance, open risk, and any other numeric state a rollback would need to restore. LoadGeneration(0) reads the newest generation, LoadGeneration(1) the one before that, and so on back to however many the ring still holds - the library refuses cleanly rather than returning garbage once a caller asks for a generation that has already been pushed out by newer saves. Notes and limitations - Every checkpoint in one file must have the same field count. A genuine schema change needs ClearAll() (or a new filename), not an in-place resize. - Tags are stored in a fixed 32-byte slot and are truncated if longer. - This is per-file state persistence, not a distributed or multi- terminal mechanism - it does not use FILE_COMMON. Two terminals writing the same filename outside the sandbox is undefined. - SaveCheckpoint() overwrites the oldest slot once the ring is full; there is no way to recover a generation that has already been overwritten, only ones still within Capacity() of the newest.

Script · MT4 · v1.0 · Free

About Checkpoint Generations Manager

Checkpoint Generations Manager is a script for MetaTrader 4. It has 2 settings, such as Capacity and Field count. MQL source code included.

Files in the download: CheckpointGenerations.txt, CheckpointGenerationsDemo.mq4.

Settings

SettingDefaultWhat it does
Capacity5Ring buffer size (generations retained)
Field count3Fields per checkpoint (balance, openRisk, streak)

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 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: MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria