Loading…

Event Sourced State Journal

Most on-chart tools keep their state in a handful of plain variables that get written wherever the code happens to touch them. After a re-init, a chart template reload, or a bug that writes to the wrong place, there is no way to tell whether the current value is actually correct - it just is whatever it is. This library flips that around: AppendEvent() only ever appends an immutable fact to a growing log (SET, ADD, or DELETE against a named key, numeric or text). Nothing derived is touched until Rebuild() replays every fact, in order, through a small fixed set of opcodes. The result is a pure function of the log - the same sequence of events always produces the same final state, and the log itself is a complete, inspectable history of how the state got there. A full replay from event zero is correct but gets slower forever as the log grows, since Rebuild() re-walks the entire history every time. Snapshot() captures the current derived state as a baseline and records how many events it already accounts for; a later Rebuild() starts from that baseline and only replays events appended since. The derived answer is identical either way - snapshotting only bounds the replay cost, it never changes what GetNumber() or GetText() return. Notes and limitations - This is an in-memory log. It does not persist across a terminal restart or survive a crash - AppendOnlyJournal.mqh elsewhere in this catalogue covers crash-safe on-disk record durability, a genuinely different concern, and is not included here to keep this header self-contained. - Keys are matched with a linear scan, which is the right trade for the dozens-of-settings scale a panel or EA's own state realistically has; it is not meant for a large keyed dataset. - ADD_NUM on a key that has never been SET starts from zero, exactly as if an implicit SET_NUM 0 had been appended first. - Rebuild() must be called explicitly after AppendEvent()/Snapshot() - the library never rebuilds on its own, so a caller that forgets to call it will keep reading stale derived state from before the latest events. - No trading and no money at risk - the demo only appends and replays settings-style events, it never sends an order.

Script · MT5 · v1.0 · Free

About Event Sourced State Journal

Event Sourced State Journal is a script for MetaTrader 5. It has 1 setting, such as Key prefix. MQL source code included.

Files in the download: EventSourcedStateJournal.mq5.mqh, EventSourcedStateJournal.txt, EventSourcedStateJournalDemo.ex5, EventSourcedStateJournalDemo.mq5.

Settings

SettingDefaultWhat it does
Key prefixDemoKey prefix, unused beyond labelling Print() output

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

MQL4 & MQL5 scripts · PolyMaria