Loading…

Undo Stack Framework

A chart panel with buttons that move objects, change parameters, or toggle settings usually has no undo at all - every action is irreversible the moment it happens, because building a proper undo stack from scratch inside one EA's OnChartEvent is more plumbing than most panels are worth. This library is that plumbing, factored out. Record() is called right after the panel applies a change, capturing a caller-defined opcode plus generic before/after fields (a numeric pair and a text pair, so one struct covers moving a line's price and renaming a label's text alike). Undo() pops the most recent record off the undo stack onto the redo stack; Redo() does the reverse. A fresh Record() after an undo correctly clears the stale redo branch, the standard rule that a new action invalidates whatever could have been redone. MQL4 has no function pointers or delegates, so this library cannot itself call the code that actually moves the object or restores the parameter - it only manages the two stacks. Undo()/Redo() hand the caller back the popped record, and the caller runs its own switch(record.cmdType) to apply the reverse (for Undo, using the record's "old" fields) or forward (for Redo, using its "new" fields) change. This enum-dispatch handoff is the idiomatic MQL4 substitute for a callback-based command pattern, and it keeps the library entirely ignorant of what any given cmdType actually does to the panel. Notes and limitations - UndoCommand is a plain value struct with no dynamic array members, chosen deliberately so it can be assigned and copied between array elements safely - a struct holding a dynamic array has no safe default copy in MQL4. - The stack is bounded by maxDepth; once full, a new Record() drops the single oldest entry rather than refusing to record or growing unbounded, so very old actions eventually become genuinely unrecoverable. - The library never touches the chart, an object, or any parameter itself - Record()/Undo()/Redo() only move data between two stacks. All actual effects are applied by the caller's own dispatch code. - There is no persistence across terminal restart - the stacks live in memory only, matching what most panels actually need (undo history reset on reopen), not a durable action log.

Script · MT5 · v1.0 · Free

About Undo Stack Framework

Undo Stack Framework is a script for MetaTrader 5. MQL source code included.

Files in the download: UndoStackFramework.mq5.mqh, UndoStackFramework.txt, UndoStackFrameworkDemo.ex5, UndoStackFrameworkDemo.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: MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria