Wrong Account Guard Demo
MetaTrader will attach the same script or EA to a demo account, a live account, or the wrong broker entirely with no warning of its own. The only thing standing between "this ran on my demo as intended" and "this ran live on the wrong account" is a check the program itself performs before doing anything real - and it is very easy to skip writing that check, because nothing forces you to. This script is that check, isolated and runnable on its own so the pattern can be copied into a real EA or script. It verifies three things independently: the account number, the trade server name, and the account currency. Each is optional - leaving an expected value blank or zero skips that particular check rather than refusing to run, because a freshly copied template with nothing filled in should tell you what to fill in, not fail closed on every account by default. Once you set at least one expected value, that field is enforced strictly and a mismatch is reported loudly. The server check defaults to a partial (substring) match rather than an exact one, because the same broker commonly runs "Broker-Demo" and "Broker-Live" as two different server names, and a strict compare would force keeping two nearly identical copies of every tool just to handle that. A stricter exact match is available via an input for brokers where that distinction matters. Run with nothing set, it acts as a harmless reconnaissance tool: it prints the current account number, server and currency to the Experts tab so you can copy them straight into the inputs for a future, protected run. Notes and limitations - This is a demonstration of the pattern, not a wrapper library - it deliberately has no #include, per house style, so the three check functions are meant to be copied into the tool that needs them. - A script only runs once per drag-onto-chart, so this guard protects a single invocation. An EA embedding the same pattern in OnInit() gets the protection on every attach and every terminal restart, which is the more realistic use case. - Leaving all three fields blank makes the guard a no-op by design - it will not stop you from running on the wrong account until you actually fill something in. This is stated plainly in the Experts tab output for that case so it is never mistaken for "checked and passed." - The currency and account-number checks are exact matches only; there is no fuzzy comparison for those two fields.
Script · MT5 · v1.0 · Free
About Wrong Account Guard Demo
Wrong Account Guard Demo is a script for MetaTrader 5. It can alert you with a pop-up, sound, email or push message. It has 5 settings, such as Expected account, Expected server and Server partial ok. MQL source code included.
Files in the download: WrongAccountGuardDemo.ex5, WrongAccountGuardDemo.mq5, WrongAccountGuardDemo.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Expected account | 0 | Expected account number (0 = do not check) |
| Expected server | Expected server name, exact or partial (blank = do not check) | |
| Server partial ok | true | Accept a partial/substring match on server (demo/live suffixes vary) |
| Expected currency | Expected account currency, e.g. USD (blank = do not check) | |
| Abort on mismatch | true | Raise an Alert() in addition to the log if any check fails |
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: Alerts · MT5 Script · MQL5 Source Code