Loading…

Book Snapshot And Rebuild Engine

"Save the book, restore it later" sounds simple until the restore step is actually needed - after a terminal crash, a VPS migration, or simply comparing what a strategy SHOULD hold right now against what it actually holds. The obvious approach of matching orders by ticket number falls apart immediately: a saved ticket is meaningless the moment the position it named has closed and a new one has opened in its place, which is exactly the situation a rebuild tool exists to handle. This library never assumes tickets survive between snapshots. BseCaptureSnapshot() reads the live open book into a plain struct array; BseSerializeSnapshot() and BseParseSnapshot() round-trip it to and from CSV lines (the caller writes those lines to a file with ordinary FileWrite calls - this header never touches the filesystem itself). BseDiffSnapshots() then compares a "current" snapshot against a "target" one, matching orders by (symbol, type, magic number, lot size) instead of ticket, and produces a plain instruction list: OPEN for a target entry with no match in current, CLOSE for a current entry with no match in target, and MODIFY for a matched pair whose stop loss or take profit differs. The engine only ever produces this instruction list - it never calls OrderSend, OrderModify or OrderClose itself, so the caller stays in full control of when and how corrections are actually applied. Notes and limitations - Matching is by (symbol, type, magic, lots), not ticket - two positions of identical size on the same symbol and magic are indistinguishable to this diff and are matched in array order, which is the best any ticketless identity scheme can do. If exact per-position identity matters, keep lot sizes distinct or use separate magic numbers per unit. - BseDiffSnapshots() only produces the instruction list; it does not execute any of it. Applying an OPEN instruction still needs its own OrderPreflightValidator-style check before a real OrderSend() call - this library does not re-validate broker constraints. - A MODIFY instruction only ever compares stop loss and take profit - a lot-size change on an existing ticket is not representable as a modify in MQL4 (lot size cannot be changed on a live order), so a target with a different lot size on the same symbol/type/magic is treated as a separate CLOSE-then-OPEN pair rather than a resize. - CSV serialisation uses a comma delimiter with no escaping; do not feed it symbol names or data containing commas.

Script · MT4 · v1.0 · Free

About Book Snapshot And Rebuild Engine

Book Snapshot And Rebuild Engine is a script for MetaTrader 4. MQL source code included.

Files in the download: BookSnapshotEngine.mqh, BookSnapshotEngine.txt, BookSnapshotEngineDemo.mq4.

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