EA With Built In Unit Tests
Every EA in this catalogue leans on a handful of small helper functions - convert pips to points, normalise a lot size to the broker's step, derive money-per-point from tick value and tick size, decide whether a broker error is worth retrying. A typo or a copy-paste slip in any one of them does not throw an error; it just quietly sizes an order wrong or retries something it should have given up on, and nothing in a normal backtest run tells you that happened. This template answers that by splitting every helper into two halves: a pure function that takes plain numbers and has no dependency on the live symbol (PipsToPointsForDigits, NormalizeLotsForBroker, MoneyPerPointForTick, IsRetryableErrorCode, StopDistanceIsValid), and a thin MarketInfo-backed wrapper used by the actual trading code. Because the pure half takes fixed inputs, it can be checked against a fixed set of expected outputs at OnInit - four digit-count cases for pip conversion, rounding and clamping cases for lot normalisation, standard and coarse tick cases for money-per-point, and known retryable versus permanent error codes. The on-chart panel lists every case with a PASS or FAIL verdict. If any test fails and InpBlockTradingOnFailure is left at its default, trading is refused outright regardless of InpEnableTrading, even though the EA still loads so the failure is visible rather than silent. A small fast/slow moving average cross is wired up purely to demonstrate a real order path running behind that gate - it is not offered as a strategy worth trading on its own. Notes and limitations - The self-test suite checks the pure helper functions only - it is a unit test layer, not a full integration test against a live broker connection. Broker-specific behaviour (actual fill prices, real stop levels) is outside what a startup self-test can verify. - The MA-cross demo signal exists solely to show a tested helper used in a real order path. It is a two-line signal with no filter and is not a strategy recommendation. - At most one demo position is held at a time, purely to keep the panel readable during a demo run.
Expert advisor · MT4 · v1.0 · Free
About EA With Built In Unit Tests
EA With Built In Unit Tests is an expert advisor (trading robot) for MetaTrader 4 based on Moving Average. It has 10 settings, such as Run tests on init, Block trading on failure and Enable trading. MQL source code included.
Files in the download: SelfTestingEa.ex4, SelfTestingEa.mq4, SelfTestingEa.txt.
Trading robots can lose money. Check every setting and test on a demo account before using real funds.
Settings
| Setting | Default | What it does |
|---|---|---|
| Run tests on init | true | Run the self-test suite when the EA loads |
| Block trading on failure | true | Refuse to trade if any self-test fails |
| Enable trading | false | Off by default, and only honoured if all tests pass |
| Fast ma period | 10 | Fast MA period for the demo signal |
| Slow ma period | 30 | Slow MA period for the demo signal |
| Lots | 0.01 | Lot size for the demo entry |
| Stop loss pips | 30 | Stop loss distance, in pips |
| Slippage pips | 3 | Slippage tolerance, in pips |
| Max retries | 5 | OrderSend attempts before giving up |
| Magic | 20261302 | Magic number this EA tags its own orders with |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Experts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
- Test it first on a demo account or in the Strategy Tester.
Tags: Risk & Lot Size · Moving Average · MT4 Expert Advisor · MQL4 Source Code