Loading…

Supervisor Worker Pattern

A single EA instance sees only its own chart's symbol and its own orders. Running several correlated strategies across several charts safely needs some way for one of them to see the whole picture - total exposure across all of them - and to tell the others to stop opening new trades when a shared risk limit is hit. MQL4 gives programs on different charts no shared memory and no message queue, so this template uses the one primitive that genuinely is visible terminal-wide: GlobalVariableSet()/GlobalVariableGet(). Every worker chart publishes four namespaced global variables each tick - its own heartbeat timestamp, its own open lot total, its own floating P/L and a status code - under keys built from its own identity (SWP_<id>_HEARTBEAT etc). The supervisor chart scans the global variable pool for every key ending in _HEARTBEAT under the SWP_ namespace, discovers whichever workers are currently publishing without needing to be told their symbols in advance, flags any whose heartbeat has gone stale (chart closed, EA removed, terminal disconnected), sums live exposure across the rest, and sets a single SWP_HALT_ALL flag when that sum reaches the configured limit. Every worker checks that flag before opening a new position, so the halt takes effect on the very next bar across every chart without any direct chart-to-chart call. A worker started before its supervisor reads the missing HALT flag as "not halted" rather than erroring, so start order between the two roles does not matter. A worker clears its own four keys in OnDeinit so removing it from a chart does not leave a permanent stale ghost entry in the supervisor's aggregation. Notes and limitations The worker's trading rule (a plain moving-average cross) exists only to prove the halt override actually stops a real order; it is a template, not a tuned strategy. Global Variables are per-terminal, not per-account or per-broker-login - running two different accounts from the same terminal instance would mix their worker/supervisor state, so this pattern assumes one terminal per account, which is the normal MetaTrader 4 setup. Global Variables persist for 4 weeks by default even after the terminal closes; a worker's OnDeinit cleanup covers a normal EA-removed/chart-closed exit, but a terminal crash could leave stale keys behind until the next supervisor poll ages them out via the heartbeat-staleness check. Only one supervisor should run per terminal - two supervisors would both write SWP_HALT_ALL and race harmlessly, but both printing duplicate aggregation logs, which is confusing rather than dangerous.

Expert advisor · MT5 · v1.0 · Free

About Supervisor Worker Pattern

Supervisor Worker Pattern is an expert advisor (trading robot) for MetaTrader 5 based on Moving Average. It has 7 settings, such as Role, Worker id and Worker lots. MQL source code included.

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

Trading robots can lose money. Check every setting and test on a demo account before using real funds.

Settings

SettingDefaultWhat it does
RoleROLE_WORKERAttach as ROLE_SUPERVISOR on one control chart, ROLE_WORKER elsewhere
Worker idWorker identity key, blank = this chart's own symbol
Worker lots0.01Worker: lot size for its demo entries
Worker magic48401Worker: magic number for its own orders
Supervisor poll sec5Supervisor: how often to poll worker heartbeats
Max total lots1.0Supervisor: total worker exposure (lots) that triggers a HALT
Max workers20Supervisor: upper bound on distinct worker ids scanned

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 → Experts.
  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 expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
  6. Test it first on a demo account or in the Strategy Tester.

Download the PDF guide

Tags: Moving Average · MT5 Expert Advisor · MQL5 Source Code

Expert advisors (EA) for MT4 & MT5 · PolyMaria