Loading…

Broker Failover Trader

Running one strategy across two separate broker connections for redundancy needs some way for the backup connection to know the primary has actually stopped working. This EA is honest about what that detection can and cannot be: it cannot reach into another terminal's process and ask "are you actually still trading" - nothing running in a different terminal, on a different account, possibly on a different machine, can do that. All it can ever know is whether a shared FILE stopped being refreshed. Attached with InpRole=PRIMARY, it writes a heartbeat line plus a snapshot of its own open positions - symbol, direction, lot size, stop and target - to a file in the terminal-independent Common\Files folder on every configured interval. Attached with InpRole=BACKUP on a second terminal watching that same shared file, it compares the heartbeat's age against a threshold; once stale, it alerts, logs the failover, and opens matching positions for every leg in the last known snapshot on its own broker connection, at that broker's own current market price, with lot size converted to the backup's own lot step/min/max and hard-capped regardless of what the snapshot requested. Stop-loss and take-profit are deliberately NOT copied across - the two brokers' prices already differ, and re-deriving a stop distance from a stale primary price against the backup's current price would produce a number with no honest relationship to either broker's actual risk, so re-established positions start without protective orders and need risk management applied separately. Once triggered, the backup stays in the taken-over state until manually reset (the default), rather than silently reverting the moment the heartbeat happens to resume - a heartbeat coming back does not by itself prove the primary is safe to hand control back to. Notes and limitations A stale heartbeat file proves only that the file stopped updating - it does not prove the primary terminal, its EA, its broker connection, or its account are actually in trouble. A primary that is trading perfectly well but has lost write access to the shared folder, or a shared drive that itself becomes unavailable to both terminals, looks identical to a genuinely dead primary from the backup's point of view; treat any failover as "the file went stale" in the journal and alert text, exactly as this EA reports it, not as confirmed proof of an outage. The two terminals must be able to reach the same Common\Files folder, which in practice means the same Windows user profile on the same machine (or a synced path configured identically on both) - this does not bridge two physically separate machines on its own. Re-established positions carry no stop-loss or take-profit; apply risk management to them immediately after a failover.

Expert advisor · MT5 · v1.0 · Free

About Broker Failover Trader

Broker Failover Trader is an expert advisor (trading robot) for MetaTrader 5. It has 11 settings, such as Role, Heartbeat file name and Heartbeat interval seconds. MQL source code included.

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

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

Settings

SettingDefaultWhat it does
RoleROLE_BACKUP
Heartbeat file nameBrokerFailoverTrader_Heartbeat.csvShared (Common\Files) file both terminals use
Heartbeat interval seconds5PRIMARY: how often the heartbeat/snapshot is rewritten
Stale threshold seconds30BACKUP: heartbeat older than this triggers failover
Symbol suffixBACKUP: appended to each symbol read from the snapshot (broker naming differences, e.g. "EURUSD" ->
Magic number45100Magic number that tells this EA's orders apart
Require manual resettrueBACKUP: once failed over, stays taken-over until InpResetTakeover is toggled true, even if the heart
Reset takeoverfalseBACKUP: set true and re-attach (or toggle via inputs) to clear a prior takeover and resume watching
Max lots per position5.0BACKUP: hard cap on any single re-established position, regardless of the snapshot
Max retries5
Retry wait ms500

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: Hedging · MT5 Expert Advisor · MQL5 Source Code

Expert advisors (EA) for MT4 & MT5 · PolyMaria