Loading…

Deterministic Replay Recorder

A bug report that says "it happened around 14:20 on EURUSD" is not reproducible. By the time a debugger is attached, the tick stream has moved on, the spread has changed and MathRand()'s internal state is somewhere else entirely - the exact conditions that triggered the bug are gone the moment they pass. This header turns "record mode" into a tagged, sequenced tape of every value a call site read - ReplayCapture() appends a row with a sequence number, a tag, up to three payload doubles, the server time and the GetTickCount() value at the moment of capture. "Playback mode" then feeds those exact rows back through ReplayFetch() instead of touching the live source, in the same order they were recorded. Playback fails loudly on the first tag mismatch rather than silently returning whatever the next row happens to contain. A tape recorded from one code path handed to a call site now taking a different one is a sign the harness itself has diverged - papering over that with the wrong row's values would make a replay look successful while actually testing nothing. ReplayFetch() reports the exact sequence number and tag involved and increments a mismatch counter instead. ReplayNextRandom() is the concrete answer to "what about random seeds": recording mode draws a live MathRand() value and writes it to the tape; playback mode returns the recorded draw untouched, never calling MathRand() again. This makes a strategy that uses randomised entry timing, randomised slippage simulation, or any other MathRand()-driven behaviour fully reproducible under replay even though MQL4 gives no way to read back MathRand()'s internal seed state directly. Notes and limitations - The tape is a plain FILE_CSV file in the sandboxed Files folder - readable in a text editor, and portable between machines as long as the file itself is copied along with the EA. - ReplayFetch() matches by TAG, not by payload content - two calls that legitimately use the same tag with different values are indistinguishable to the mismatch check; give call sites on genuinely different code paths distinct tags if that distinction matters. - ReplayNextRandom() replays the exact integer previously drawn; it does not attempt to reproduce MathRand()'s internal generator state, only its observable output at each call site, which is what determinism actually requires here. - Recording adds one FileWrite() per captured value on every tick - noticeable overhead on a very high-frequency scalper. Use it to capture a suspected failure window, not as a permanently-enabled feature of a live EA. - This header does not itself decide WHEN to record or replay a program's trading decisions - it only makes the inputs to those decisions reproducible. Wrap it around your own OnTick()/signal logic as shown above.

Script · MT5 · v1.0 · Free

About Deterministic Replay Recorder

Deterministic Replay Recorder is a script for MetaTrader 5 based on Divergence. It has 2 settings, such as Tape file name and Random seed. MQL source code included.

Files in the download: DeterministicReplayRecorder.mq5.mqh, DeterministicReplayRecorder.txt, DeterministicReplayRecorderDemo.ex5, DeterministicReplayRecorderDemo.mq5.

Settings

SettingDefaultWhat it does
Tape file nameReplayDemoTape.csvSandbox file used as the record/playback tape
Random seed12345Seed for the RECORD pass; playback never reseeds

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: EURUSD · Divergence · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria