Loading…

Branch Coverage Counters

MQL4 has no built-in code-coverage tool. "Did my test suite actually reach the stop-loss-rejected path, or only the happy path" is normally answered by scrolling through Print() output by eye, which misses anything that did not happen to scroll past while someone was watching. This header turns that into a named counter per branch point. BranchCoverageHit(name) registers a branch the first time it is reached and increments its hit count on every call after that - nothing needs pre-declaring, the branch just needs one call placed inside it. BranchCoverageReportPrint() lists every branch that fired at least once. The more useful half is the missed-branch comparison. BranchCoverageMissed() takes a declared list of the branch names a test pass was SUPPOSED to exercise and returns the ones that never fired - a test run that "looks fine" in the log can still have skipped an entire path if nobody declared what should have been reached and checked the result mechanically. BranchCoveragePercent() turns the same comparison into a single percentage figure. The demo instruments a small lot-size classifier with one call per branch and deliberately skips one test input on its first run, so the missed-branch report has something real to show. Notes and limitations - This is manual instrumentation, not automatic source-level coverage - it only knows about branches where a BranchCoverageHit() call was actually placed. Missing a call in a branch means that branch is invisible to this header, not automatically flagged. - The registry is capped at 200 distinct names (BRANCHCOVERAGE_MAX_ COUNTERS). Exceeding it prints a warning and drops the extra hit rather than corrupting existing counters. - Counters live in ordinary global arrays inside the header, so each program that includes this header gets its own independent set - there is no cross-program or cross-run persistence.

Script · MT5 · v1.0 · Free

About Branch Coverage Counters

Branch Coverage Counters is a script for MetaTrader 5. It has 1 setting, such as Include above max case. MQL source code included.

Files in the download: BranchCoverageCounters.mq5.mqh, BranchCoverageCounters.txt, BranchCoverageCountersDemo.ex5, BranchCoverageCountersDemo.mq5.

Settings

SettingDefaultWhat it does
Include above max casefalseLeave false to see the "above max" branch reported as MISSED

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 → Scripts.
  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 script onto a chart to run it once.

Download the PDF guide

Tags: MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria