Assertion Library
Neither MQL4 nor MQL5 has exceptions, so an assertion can never unwind a call stack the way it would in most languages. What it can still do honestly is refuse to lie quietly: log the failure the moment it happens, say exactly where in the code it happened, and keep a running total a caller can inspect before it does anything that costs money. Every entry point here - AssertTrue, AssertEquals, AssertInRange and the rest - is a preprocessor macro, not a plain function. That is a deliberate choice, not a style preference: __FUNCTION__ and __LINE__ are textual tokens that expand wherever the macro is written, so a failure inside your OnTick reports your OnTick's line number. Route the same call through an ordinary wrapper function first and every failure would report the inside of this header instead - accurate about nothing except where the header itself lives. Doubles are compared with an explicit tolerance rather than ==, because two values that are mathematically equal rarely share a bit pattern in binary floating point - AssertEquals(0.1 + 0.2, 0.3, ...) needs a tolerance or it fails every time despite being correct. AssertInRange treats both bounds as inclusive, which matters at exactly the values traders check most often - the broker's stated minimum and maximum lot size, not just the middle of the range. AssertPositive exists because zero is a perfectly valid double and an entirely invalid lot size, stop distance, or account balance to trade against - a bare AssertTrue(x > 0, ...) at every call site works, but a named check reads its intent without a comment. Every macro funnels into AssertRecord(), which increments a global pass or fail counter and Prints the failure immediately. AssertFailureCount() then gives calling code something to act on - an OnInit that checks assumptions about symbol properties can return INIT_FAILED when the count is non-zero, turning what would otherwise be a log line nobody reads into an actual stop. Notes and limitations - Nothing here stops execution on its own. Neither platform can throw, so a failed assertion is a logged fact and a counter increment, never a jump. Reading AssertFailureCount() and acting on it is the caller's responsibility. - The pass/fail counters are global to the compiled program, not scoped to a block or a function - call AssertResetCounters() before a section whose result needs to be judged in isolation from what ran before it. - AssertEquals needs a tolerance appropriate to the values being compared. A tolerance of 1.0e-9 is right for a price difference, hopelessly tight for two account balances that went through several currency conversions - choose it for the calculation, not out of habit. - The location string comes from __FUNCTION__ and __LINE__, which name the function and line, not the source file. On a project with two files reusing a function name this can be ambiguous - keep function names unique within a program if that matters to you.
Script · MT4 · v1.0 · Free
About Assertion Library
Assertion Library is a script for MetaTrader 4. MQL source code included.
Files in the download: Assertions.mqh, Assertions.txt, AssertionsDemo.mq4.
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 → Scripts.
- 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 script onto a chart to run it once.
Tags: MT4 Script · MQL4 Source Code