Loading…

Watchdog Pair Template

A single EA has no way to notice its own death - if the terminal crashes, the chart is removed, or the process is killed, nothing about that EA detects it, because the code that would detect it died with it. The standard answer is a second, independent watcher. This template makes that watcher symmetric: the same .mq4 file, attached twice with InpRole=ROLE_A on one chart and InpRole=ROLE_B on another, each publishing its own timestamp to a shared GlobalVariable on a timer and reading its peer's. Escalation is staged rather than binary, because a single missed poll is not the same as a genuinely dead peer. Three thresholds are checked in order against how many seconds the peer has been silent: InpWarnAfterSec logs and alerts only; InpCriticalAfterSec alerts louder; InpTakeoverAfterSec is the point where the surviving instance can optionally flatten the peer's own order scope - its magic number and (optionally) its symbol - so a dead EA's open positions do not sit completely unmanaged until a human notices. Takeover is latched once reached: it does not re-fire every poll while the peer stays silent, and it only clears when the peer's heartbeat resumes, so a flapping connection cannot toggle takeover on and off every few seconds. Takeover is opt-in on two separate switches deliberately: InpPeerMagicNumber must be set to a real magic number (0 disables it entirely, alert-only), and InpAllowTakeoverFlatten must be explicitly true. A watchdog that can silently close another program's real positions the first time it is attached with default settings would be worse than no watchdog at all. Notes and limitations - This template's watchdog logic does not trade; InpOwnMagicNumber exists purely for the on-chart display, so a trader can see at a glance which trading EA each watchdog instance is paired with. - On a clean shutdown, this EA deliberately does NOT delete its own heartbeat GlobalVariable - a graceful stop and a crash both leave the heartbeat to go stale, by design, since a heartbeat that announces its own planned death would defeat the purpose of watching for silence. - Heartbeats use GlobalVariables, which are per-terminal, not shared across two different MetaTrader installations - both instances must run in the same terminal (they can be on different charts/symbols within it). - Takeover flattens by magic number (and optional symbol); it has no way to know whether a foreign-magic order it is NOT configured to touch also belongs to the same trader - scope it carefully via InpPeerMagicNumber.

Expert advisor · MT4 · v1.0 · Free

About Watchdog Pair Template

Watchdog Pair Template is a script for MetaTrader 4. It can alert you with a pop-up, sound, email or push message. It has 10 settings, such as Role, Pair name and Heartbeat sec. MQL source code included.

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

Settings

SettingDefaultWhat it does
RoleROLE_AWhich half of the pair this attached instance is
Pair nameDefaultPairShared name tying the two instances together (must match on both)
Heartbeat sec5How often this instance publishes its own heartbeat
Warn after sec15Peer silence before WARN
Critical after sec45Peer silence before CRITICAL
Takeover after sec120Peer silence before TAKEOVER
Peer magic number0The PEER instance's own magic number (0 = do not manage peer orders)
Peer symbolThe PEER instance's own symbol filter ("" = any symbol, this account)
Allow takeover flattenfalsetrue = actually flatten the peer's scope on TAKEOVER; false = alert only
Own magic number0This instance's own magic, purely for the health file (0 = not a trading role)

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