Loading…

Snapshot Diff Storage

Keeping every version of a growing text blob (a serialized state dump, a generated report, a config history) as a full copy wastes disk space proportional to version count times size, when most successive versions differ in only a few lines. This library stores version 0 in full, then for every later version computes the common leading and trailing lines against the previous version and stores only the changed middle block plus the two kept-line counts - reconstruction replays that against the previous version's lines to get the new version back exactly. The diff itself is a common-prefix/common-suffix trim, not a full LCS/Myers diff - stated plainly rather than oversold. MQL4 has no convenient dynamic 2D matrix type for an O(n*m) alignment table at any real size, so this library uses the cheaper trim instead. It is always exact on reconstruction; it just does not always find the SMALLEST possible delta when a snapshot has two separate unrelated edits far apart in the same version, since everything between the first and last changed line gets stored as one middle block even if some lines in between did not change. Any version whose delta's changed middle block would cover more than InpFullSnapshotThreshold of the larger snapshot's line count is stored as a full snapshot instead of a delta - both because a near-total rewrite compresses worse as a delta than as a plain copy, and because it bounds how many deltas Reconstruct() ever has to replay forward from the nearest earlier full snapshot to answer a query. Notes and limitations - The diff is line-based (split on '\n'); it has no concept of word-level or character-level changes within a line - a one- character edit to a long line stores that whole line as changed. - The prefix/suffix trim is not a minimal diff (see FULL DESCRIPTION) - it is always correct, just not always the smallest possible delta for snapshots with multiple separate unrelated edits. - Line count per snapshot is capped at 100000 (SDS_MAX_LINES) as a sanity bound against a corrupted length field being trusted on reconstruction. - Reconstruct() re-reads the data file from the nearest earlier full snapshot forward every call - it does not cache results. For a very long delta chain between full snapshots, repeated Reconstruct() calls near the end of that chain cost proportionally more.

Script · MT5 · v1.0 · Free

About Snapshot Diff Storage

Snapshot Diff Storage is a script for MetaTrader 5. MQL source code included.

Files in the download: SnapshotDiffStorage.mq5.mqh, SnapshotDiffStorage.txt, SnapshotDiffStorageDemo.ex5, SnapshotDiffStorageDemo.mq5.

How to install

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

MQL4 & MQL5 scripts · PolyMaria