Loading…

Safety Case EA Template

Most EA source comments describing "safety" are aspirational - a comment claiming a risk is handled, sitting nowhere near any code that actually handles it, drifting further from reality with every edit. This template borrows the safety-case discipline used in safety-critical engineering: name each hazard explicitly (H1, H2, ...), and place its mitigating guard function immediately next to the hazard's description, so the two can never drift apart silently. Six hazards are identified and guarded in this file: a restart re-entering an already-open trade (mitigated by ticket-based state recovery scanning existing orders in OnInit before anything else runs), two chart instances of the same EA doubling exposure on one symbol (mitigated by a single- instance guard checked before every new order), a stop-loss silently failing to attach (mitigated by re-selecting the order immediately after sending it and emergency-closing if no stop is present), floating loss exceeding a tolerable ceiling regardless of cause (mitigated by an independent equity-floor circuit breaker checked every tick), a broken or suspicious price feed (mitigated by a spread and non-positive-price sanity check before every trade), and an attempted trade while the terminal's trade context is unavailable (mitigated by an explicit IsTradeAllowed() check logged plainly rather than a silent failed send). The trading logic itself - a simple fast/slow moving average cross - is deliberately minimal. The point of this project is the hazard/guard pairing pattern, which is meant to be copied into a real strategy's source, not the specific signal used to demonstrate it. Notes and limitations - The hazard table is not exhaustive - it covers six specific, concrete failure modes chosen to demonstrate the pairing pattern, not every conceivable hazard an EA could face. A real strategy built from this template should extend the table with hazards specific to its own logic. - H1's restart recovery finds an existing own-magic ticket and resumes managing it, but it cannot recover in-memory state that was never persisted anywhere (such as a partially-computed indicator state) - only the fact that a position is open and its own order fields. - H3's verification happens once, immediately after the order is sent. A stop removed later by a different process (manual intervention, a broker action) is not re-detected until the next new order attempt. - H4's circuit breaker only counts this EA's own magic number's positions on this symbol - it is not an account-wide guarantee (see Terminal Risk Kernel and Daily Loss Guarantee Sizer elsewhere in this catalogue for account-wide risk tools).

Expert advisor · MT4 · v1.0 · Free

About Safety Case EA Template

Safety Case EA Template is an expert advisor (trading robot) for MetaTrader 4 based on Moving Average. It has 9 settings, such as Fast ma period, Slow ma period and Lots. MQL source code included.

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

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

Settings

SettingDefaultWhat it does
Fast ma period10Fast moving average period
Slow ma period30Slow moving average period
Lots0.10Fixed lot size
Magic number49000Magic number stamped on orders this EA opens
Slippage points30Maximum acceptable slippage, in points
Stop loss pips40Stop loss distance in pips - H3 depends on this being > 0
Take profit pips80Take profit distance in pips; 0 = none
Max floating loss percent5.0H4: force-close all own positions if floating loss exceeds this percent of equity
Max spread points40H5: refuse to trade if current spread exceeds this many points

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 → Experts.
  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 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 · MT4 Expert Advisor · MQL4 Source Code

Expert advisors (EA) for MT4 & MT5 · PolyMaria