Loading…

Kill Switch Protocol

Most "kill switch" code is private to one expert - a boolean the EA sets on itself and nobody else can see. That is fine until you run three EAs on three charts and need to stop all of them at once, or want a small monitor script that can halt everything without knowing which EAs are attached to which charts. This library publishes a fixed GlobalVariable name, KILLSWITCH_ACTIVE, as the convention. GlobalVariables are the one communication channel every program on a terminal shares without files or a DLL, and they survive a terminal restart, which a plain static bool in one EA does not. Any expert, from any author, that calls KillSwitchIsActive() at the top of OnTick obeys the same switch as every other expert doing the same - no shared code between them is required beyond agreeing on the name, which is exactly what a convention is for. Tripping the switch (KillSwitchTrip) is deliberately cheap and non-blocking - it never touches an order by itself, so it is safe to call from anywhere, including from inside a tight loop that must not stall. Actually closing exposure is a separate function, KillSwitchFlattenAndFreeze, which trips the switch first and only then starts closing - so any other EA polling the switch mid-tick already sees the stop before this function's own order-close loop even begins. It collects every matching ticket into an array before acting on any of them, the same discipline this catalogue requires everywhere: closing an order re-indexes the pool, so acting while still walking it by position silently skips roughly half the book on a forward loop and unpredictably on a backward one once several orders close in the same pass. Because GlobalVariables only hold doubles, there is no shared string for "why did this trip". The library ships a small integer reason-code convention instead (KILLSWITCH_REASON_MANUAL and a caller-extensible numeric scheme) plus a trip timestamp, and expects the caller to Print() or log the human-readable reason at the point it trips - the numeric code is enough for any other program to react differently to different reasons if it wants to. Notes and limitations - This is a convention, not enforcement. It only works if every expert that should obey it actually calls KillSwitchIsActive(). A rogue or unmodified EA that never checks the flag will keep trading regardless. - KillSwitchFlattenAndFreeze() makes one honest pass. An order that fails to close (frozen level, requote, momentary connection issue) is logged and left as-is rather than retried in a loop - call it again, or pair it with a retry wrapper, if guaranteed closure matters more than a bounded delay. - GlobalVariables are per-terminal, not per-account or per-broker. Two terminals (even on the same machine) do not share the switch. Running several accounts through several terminal instances needs its own kill switch tripped in each. - The reason-code and trip-timestamp GlobalVariables are cleared by KillSwitchClear() for TRIPPED_AT, but KillSwitchReasonCode() intentionally keeps returning the last code after a clear - that is a deliberate design choice so a post-mortem can still see why the terminal last stopped, not a bug in the clear function. - Nothing here prevents a human from manually opening a new trade in the terminal while the switch is tripped - it governs code, not the platform.

Script · MT5 · v1.0 · Free

About Kill Switch Protocol

Kill Switch Protocol is an expert advisor (trading robot) for MetaTrader 5. It has 1 setting, such as Reason code. MQL source code included.

Files in the download: KillSwitchProtocol.mq5.mqh, KillSwitchProtocol.txt, KillSwitchProtocolDemo.ex5, KillSwitchProtocolDemo.mq5.

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

Settings

SettingDefaultWhat it does
Reason codeKILLSWITCH_REASON_MANUALReason code to record when tripping

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: Pending Orders · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria