Restart Survival Bundle
A terminal crash leaves no event to catch - OnDeinit simply never runs, so a program cannot directly ask "did I just crash?" The one fact that survives both a chart reload and a full terminal restart is a GlobalVariable, unlike ordinary memory which is gone either way. This header writes a pair of GlobalVariables per running instance: a "still alive" timestamp touched at OnInit, and a recorded OnDeinit reason written only when OnDeinit genuinely got to run. If the reason key is missing the next time the sentinel is checked, OnDeinit never fired at all - a hard restart, not a clean reload. RsbClassifyStartup turns that into one of three outcomes: FIRST_EVER (no sentinel found - a genuinely new attach), SOFT_REATTACH (the previous OnDeinit recorded REASON_RECOMPILE, REASON_PARAMETERS, REASON_CHARTCHANGE or REASON_TEMPLATE - the same logical session just reloading code), or HARD_RESTART (no reason recorded, or a final reason like REASON_CLOSE or REASON_ACCOUNT). A caller restores state silently on a soft reattach and restores state plus tells the user recovery happened on a hard restart, rather than treating every startup the same way. RsbSaveDouble/RsbLoadDouble and their int wrappers give a small generic key/value store on top of the same GlobalVariable mechanism for whatever state actually needs to survive (a last-processed bar time, an open-position count, a running total). Notes and limitations GlobalVariables created with GlobalVariableSet are auto-deleted by the terminal after roughly four weeks of no access - a program that restarts less often than that will see its sentinel and saved state silently wiped and reappear as FIRST_EVER, not a bug in this header but a platform limit worth knowing about for anything relying on this for long-dormant recovery. Sentinel and state keys are terminal-wide GlobalVariable names, not per-chart - RsbSentinelName folds in the symbol and a magic number to avoid collisions between instances, but two programs that reuse the same prefix, symbol and magic will collide; keep prefixes distinctive. Classification depends entirely on the calling program actually invoking RsbRecordDeinit from its real OnDeinit - a program that never calls it will always classify as HARD_RESTART, correctly, since from the sentinel's point of view no clean shutdown was ever observed. Unlike a file-based sentinel, this GlobalVariable approach cannot distinguish a genuine terminal crash from the terminal being force-killed mid-session in a way that also corrupts the terminal's own GlobalVariable persistence file - an extremely rare case, mentioned for completeness rather than as an expected failure mode.
Script · MT5 · v1.0 · Free
About Restart Survival Bundle
Restart Survival Bundle is a script for MetaTrader 5. It has 2 settings, such as Reset demo and Simulated deinit reason. MQL source code included.
Files in the download: RestartSurvivalBundle.mq5.mqh, RestartSurvivalBundle.txt, RestartSurvivalBundleDemo.ex5, RestartSurvivalBundleDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Reset demo | false | true wipes the demo's saved state first, forcing FIRST_EVER |
| Simulated deinit reason | REASON_PARAMETERS | reason this run "shuts down" with, read back on the NEXT run |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT5 Script · MQL5 Source Code