Log Assertion Helpers
Testing that code produced the right log output sounds simple until you try it in MQL4: there is no supported way for a running program to read back its own Experts Journal file, since the sandbox cannot see the terminal's Logs folder. This header sidesteps that by not trying to read the real log at all - instead, code under test routes its log lines through LahCapture(), which both Prints the line immediately (so production behaviour is unaffected) and appends it to a small in-memory buffer that the assertions below actually check. Beyond simple content checks, LahAssertOrder verifies that a set of expected substrings appear in the captured lines in a given relative order - as a subsequence, not necessarily on consecutive lines - which is the check that actually matters for a trade lifecycle log ("signal" must be logged before "order sent", which must be logged before "order closed"). A content-only assertion would pass even if those lines came out in the wrong order; this one would not. Pass/fail counters accumulate across every assertion call in a run (mirroring the plain-assertion convention already used elsewhere in this catalogue), so a script can print one final summary line covering an entire test session. Notes and limitations This only sees lines explicitly routed through LahCapture() - a plain Print() call elsewhere in the same program is invisible to these assertions, by design, since there is no way to intercept the real Journal. The capture buffer holds at most LAH_MAX_CAPTURED_LINES (200 by default); once full, further lines are still Printed but silently stop being captured rather than overwriting earlier ones - call LahClear() between test cases to avoid running into the cap. LahAssertOrder checks relative order via substring search, not exact line equality, so a needle that happens to also match an earlier, unrelated line can make the search start later than intended; keep needles reasonably specific for a log that reuses similar wording.
Script · MT5 · v1.0 · Free
About Log Assertion Helpers
Log Assertion Helpers is a script for MetaTrader 5. MQL source code included.
Files in the download: LogAssertionHelpers.mq5.mqh, LogAssertionHelpers.txt, LogAssertionHelpersDemo.ex5, LogAssertionHelpersDemo.mq5.
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT5 Script · MQL5 Source Code