Loading…

Cross Terminal Risk Governor

A trader running several MetaTrader terminals on one machine - different brokers, different strategies, maybe a prop-firm evaluation alongside a live account - has no built-in way to see what all of them are carrying together. Each terminal's own risk rules only ever see that one terminal's book, so correlated risk can stack silently across accounts that individually all look fine. This EA reads this catalogue's ExposureLedgerBroadcaster CSV ledger format from MQL4\Files\ (or MQL5\Files\ on the native MQL5 build)Common (every terminal shares that folder), sums the gross lot exposure declared by every sibling ledger it can find, adds this terminal's own open exposure, and compares the total against InpMaxCombinedLots. A sibling ledger whose heartbeat has gone stale - the broadcasting terminal closed, crashed, or had its EA removed - is excluded from the total rather than counted as zero risk forever; every exclusion is logged with its reason. Two throttling layers apply once the combined total breaches the cap. First, a shared global variable (CTRG_TradeAllowed) that any cooperating EA on this terminal can poll before opening a new trade - this governor cannot reach into another EA's logic, only offer it a flag to respect. Second, an optional, off-by-default auto-reduce that closes this terminal's own oldest qualifying positions, a few at a time and only after the breach has persisted past InpGraceSeconds - a one-tick spike in a sibling's ledger must not trigger a forced liquidation of positions that were fine a moment ago. Notes and limitations Combined exposure is a sum of each ledger's per-symbol |net_lots|, i.e. gross lot count - it does not net directionally across terminals (a 1-lot long on one terminal and a 1-lot short on another still count as 2 combined lots), and it does not weight by symbol volatility or contract size, so it is a lot-count budget, not a money-at-risk figure. Auto-reduce closes this terminal's own positions only - it has no ability to close a sibling terminal's positions, only to reduce the terminal it is actually running on. If ExposureLedgerBroadcaster is not running on a sibling terminal, that terminal's exposure is invisible to the governor entirely, not detected as missing.

Expert advisor · MT4 · v1.0 · Free

About Cross Terminal Risk Governor

Cross Terminal Risk Governor is a script for MetaTrader 4. It can alert you with a pop-up, sound, email or push message. It has 11 settings, such as Max combined lots, File prefix and Stale after seconds. MQL source code included.

Files in the download: CrossTerminalRiskGovernor.ex4, CrossTerminalRiskGovernor.mq4, CrossTerminalRiskGovernor.txt.

Settings

SettingDefaultWhat it does
Max combined lots5.0Combined gross lots across every terminal before trading is throttled
File prefixExposureLedgerMust match ExposureLedgerBroadcaster's InpFilePrefix
Stale after seconds120A sibling ledger older than this is excluded, not counted as zero
Magic filter00 = govern every open position on this terminal, otherwise only this magic
Check interval sec15Seconds between recalculations
Auto reduce on breachfalsetrue = actually close this terminal's own positions on a sustained breach
Grace seconds90Breach must persist this long, continuously, before auto-reduce acts
Max closes per check1Safety cap - never close more than this many tickets in one check
Dry runfalsetrue = log what auto-reduce would do, never actually closes
Max retries5
Retry wait ms500

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 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 · MT4 Expert Advisor · MQL4 Source Code

Expert advisors (EA) for MT4 & MT5 · PolyMaria