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 · MT4 · v1.0 · Free
About Event Sourced State Journal
Event Sourced State Journal is a script for MetaTrader 4. It has 1 setting, such as Key prefix. MQL source code included.
Files in the download: EventSourcedStateJournal.mqh, EventSourcedStateJournal.txt, EventSourcedStateJournalDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Key prefix | Demo | Key prefix, unused beyond labelling Print() output |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT4 Script · MQL4 Source Code