Loading…

Backup Global Variables To File

Terminal global variables are the usual place an EA keeps state that has to survive a restart - trade locks, one-shot flags, a counter that must not reset when the terminal reopens. None of that is backed up by anything. A reinstalled terminal, a move to a new broker server, or a new machine wipes every global variable at once, and whatever depended on them has nothing to resume from. This script is the backup-and-recovery half of a pair with Reset All Global Variables. In Backup mode it writes name, value and last-access time for every global variable to a CSV in MQL4\Files (or MQL5\Files), with the value stored at 8 decimal places - the terminal's own display precision - so a round trip through the file does not quietly truncate anything. In Restore mode it reads that same CSV back and recreates each variable. The naive version of a restore script trusts every row it reads; this one does not. Each value field is checked with a strict numeric test before GlobalVariableSet is called - MQL's StringToDouble silently returns 0.0 for garbage input, and a hand-edited or truncated file can otherwise overwrite a real value with a silent zero. Malformed rows are skipped and counted, not guessed at, and the summary lists exactly which line numbers were rejected. Restore never overwrites an existing variable unless you explicitly ask it to. The default behaviour is to leave anything that already exists alone and report it as skipped, on the basis that stomping on live state is the wrong default for a recovery tool. Notes and limitations - Global variables are terminal-wide, not account-wide or symbol-wide. A backup taken under one login includes every global variable in that terminal instance, regardless of which EA or account created them. - The restore file must be in MQL4\Files or MQL5\Files already - this script does not fetch files from anywhere else, and does not accept a full path. - A malformed line is skipped, not repaired. If every line in a file is malformed, nothing is restored and the script says so. - Restoring does not touch a variable's last-access time semantics beyond what GlobalVariableSet itself does - the "last_access" column in the backup file is informational, not something this script writes back. - This is a snapshot, not continuous protection. Nothing here runs on a schedule; back up before the event that might wipe the variables, not after.

Script · MT5 · v1.0 · Free

About Backup Global Variables To File

Backup Global Variables To File is a script for MetaTrader 5. It can alert you with a pop-up, sound, email or push message. It has 4 settings, such as Mode, Restore file and Overwrite existing. MQL source code included.

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

Settings

SettingDefaultWhat it does
ModeModeBackupMode
Restore fileRestore: CSV filename in MQL5\Files
Overwrite existingfalseRestore: overwrite variables that already exist
Confirm firsttrueRestore: ask for confirmation before writing

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

MQL4 & MQL5 scripts · PolyMaria