Loading…

Persistent State EA

A plain global variable inside an EA looks like state, but it is not persistent state. OnInit runs from scratch on a terminal restart, on recompilation, and on a chart symbol or period change, and every ordinary variable goes back to its initial value at that point. Code that assumes a counter "just keeps counting" across those events is silently wrong the first time the terminal is closed and reopened, and the bug usually goes unnoticed for a long time because the in-memory version behaves correctly right up until that boundary is crossed. There are two real ways to survive that boundary, on both MQL4 and MQL5. GlobalVariableSet/Get stores a double under a name, scoped to the current terminal installation, and survives a restart - but it can be cleared, expires after a configurable idle period, and does not travel if the data folder is copied to another machine. A file in the sandbox survives indefinitely and travels with the data folder, but has no built-in expiry handling and needs its own read/write code. This EA runs both at once by default and cross-checks them, printing a warning if the two disagree - which is itself a useful diagnostic for spotting a stale GlobalVariable store. Use it as a working reference the next time an EA in this catalogue, or one of your own, needs a counter, a first-run timestamp or any other figure that has to mean something across a restart rather than resetting to zero. Notes and limitations - GlobalVariables are keyed by account login so two accounts on the same terminal do not share state; the file is keyed only by its filename, so two instances writing the same InpStateFileName from different accounts would collide - use a distinct filename per account if that applies. - GlobalVariables only store a double. The last-stop-reason text is therefore only ever recovered from the file copy, not from GlobalVariables alone - documented directly in the panel and the code. - This is a template and demonstration, not a trading strategy. It never sends an order, on either platform.

Expert advisor · MT5 · v1.0 · Free

About Persistent State EA

Persistent State EA is a script for MetaTrader 5. It has 3 settings, such as Persist mode, State file name and Reset state. MQL source code included.

Files in the download: PersistentStateEa.ex5, PersistentStateEa.mq5, PersistentStateEa.txt.

Settings

SettingDefaultWhat it does
Persist modePERSIST_BOTHWhich persistence mechanism(s) to use
State file namePersistentStateEa_state.csvState file, in MQL5\Files
Reset statefalseSet true and re-apply inputs to wipe all persisted state

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 Expert Advisor · MQL5 Source Code

Expert advisors (EA) for MT4 & MT5 · PolyMaria