Multi Instance Coordinator
Attaching the same EA to several charts (a symbol scanner running per-pair, a strategy tested on both the live chart and a spare one, an accidental double-attach) usually means every instance thinks it is the only one and every instance trades - the classic duplicate-order bug that looks fine in single-chart testing and breaks the moment the same EA runs twice. This library elects a leader among however many instances are attached, using the terminal-native equivalent of the "bully" leader-election algorithm: every instance periodically writes its own chart ID and a timestamp under a shared GlobalVariable name prefix (MicHeartbeat), and any instance can independently enumerate every currently-live entry under that prefix and elect the lowest chart ID as leader (MicIsLeader). No instance needs to know how many siblings exist, message any other directly, or read a config file - the GlobalVariable table itself is the coordination state, the same principle this catalogue's kill-switch convention uses for a single shared flag, just keyed per-instance here instead. A crashed or manually-removed instance is handled without any separate failure-detection code: its heartbeat simply stops updating, and once that heartbeat falls outside the staleness window every other instance's next election silently excludes it and, if it was the leader, promotes whichever live instance now has the lowest chart ID. A clean shutdown (OnDeinit calling MicUnregister) is faster still - the entry is deleted immediately, so the very next election on any surviving instance already reflects the change rather than waiting out the staleness window. MQL4 has no "list GlobalVariables matching a prefix" call, so MicLiveInstances() does an honest linear scan of every GlobalVariable name the terminal holds via GlobalVariablesTotal()/GlobalVariableName(), filtered by the group's prefix. That is the right trade at terminal scale (a handful to a few hundred GlobalVariables) - it is not built for a terminal running thousands of unrelated GlobalVariables. Notes and limitations - A single script instance cannot demonstrate several real chart attachments, so the demo simulates three by passing explicit chart IDs into every function instead of relying on the real ChartID() - real usage on separate charts omits the chartId argument and each attachment uses its own ChartID() automatically. - MicHeartbeat() must be called regularly (every tick is normal) for an instance to stay counted as live. An instance that stops ticking (chart paused, EA disabled without OnDeinit running, e.g. a terminal crash) ages out only after the staleness window, so a leader failover after a hard crash takes up to that many seconds, not instantly. - The staleness window (30 seconds by default) is a trade-off: too short and a briefly slow terminal (heavy history load, a stalled connection) can falsely demote and re-elect a leader that was never actually gone; too long and a genuine crash leaves nobody trading for that much longer. Tune it to how often your EA actually calls MicHeartbeat(). - Leadership can change between one MicIsLeader() call and the next if another instance's heartbeat goes stale in between - do not cache the result across ticks, call it fresh wherever a trading decision is made. - This coordinates one terminal only, the same scope every GlobalVariable- based tool in this catalogue has. Two separate terminal instances (even on the same machine, even trading the same account) do not share GlobalVariables and cannot coordinate through this library at all. - Nothing here stops a human from manually opening a duplicate trade, and nothing here inspects open orders itself - it only elects who is allowed to act; enforcing that the watcher instances actually skip their own trading logic is the calling EA's responsibility.
Script · MT5 · v1.0 · Free
About Multi Instance Coordinator
Multi Instance Coordinator is a script for MetaTrader 5. It has 1 setting, such as Group. MQL source code included.
Files in the download: MultiInstanceCoordinator.mq5.mqh, MultiInstanceCoordinator.txt, MultiInstanceCoordinatorDemo.ex5, MultiInstanceCoordinatorDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Group | DemoGroup | Coordination group name shared by all instances |
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: Dashboards & Scanners · MT5 Script · MQL5 Source Code