This article presents a native MQL5 auditor for Benford's first- and second‑digit tests on trade logs and market data. It implements an eligibility gate (sample size and decade spread), a theory-based test (chi-square, MAD, per‑digit z‑scores), and a reference comparison calibrated by bootstrap quantiles. A timer-driven EA, include files, and a tamper simulator show detection of hand rounding and invented values, while padding and scaling remain measured near the false-alarm level.
Anyone who has traded for a while has looked at a statement or tester report and wondered whether the numbers are what they seem. That doubt applies both to a seller's track record and to your own data pipeline — for example, a feed that repeats quotes or a log tidied by hand before sharing. A statistical fingerprint is cheap to compute and makes a reasonable first filter.
Benford's law provides one such fingerprint: in many collections of numbers that grow multiplicatively, the leading digit is 1 about 30% of the time and 9 less than 5%. People who invent numbers tend to spread digits too evenly, and people who round by hand pile up zeros and fives — why forensic accountants use the test.
Applying it to MetaTrader data is harder than the textbook example suggests. A trade log from an EA that sizes lots by risk and places ATR-based stops can fail the test while being perfectly genuine. A plain Benford test, shown later, flags an untouched trade log non-conforming in 52% of resamples — raising alarms too often to be worth running alone.
Therefore, the auditor runs two checks in parallel rather than sequentially. The theory-based check asks whether the data is eligible, refuses a verdict if not, and otherwise measures digit shares against Benford's law with chi-square, MAD and per-digit z-scores. The reference check, used only for trade profit when a known-clean log is supplied, compares the audited data with that log's fingerprint. It can run even when the audited sample fails the Benford eligibility gate, but the reference log itself must pass the gate.
The implementation is entirely in native MQL5 and uses no DLLs, ALGLIB, or external services. Five files: BenfordCore.mqh is the digit engine, AuditTargets.mqh turns terminal data into arrays, IntegrityAuditor_EA.mq5 runs the audit on a timer, TamperSimulator.mq5 measures what it detects, and DealLogGenerator_EA.mq5 produces the test log. Each section below covers one file: the real code, what it does and why.
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
Check out the new article: Benford's Law for Trade Log and Quote Feed Integrity Auditing in Native MQL5.
Anyone who has traded for a while has looked at a statement or tester report and wondered whether the numbers are what they seem. That doubt applies both to a seller's track record and to your own data pipeline — for example, a feed that repeats quotes or a log tidied by hand before sharing. A statistical fingerprint is cheap to compute and makes a reasonable first filter.
Benford's law provides one such fingerprint: in many collections of numbers that grow multiplicatively, the leading digit is 1 about 30% of the time and 9 less than 5%. People who invent numbers tend to spread digits too evenly, and people who round by hand pile up zeros and fives — why forensic accountants use the test.
Applying it to MetaTrader data is harder than the textbook example suggests. A trade log from an EA that sizes lots by risk and places ATR-based stops can fail the test while being perfectly genuine. A plain Benford test, shown later, flags an untouched trade log non-conforming in 52% of resamples — raising alarms too often to be worth running alone.
Therefore, the auditor runs two checks in parallel rather than sequentially. The theory-based check asks whether the data is eligible, refuses a verdict if not, and otherwise measures digit shares against Benford's law with chi-square, MAD and per-digit z-scores. The reference check, used only for trade profit when a known-clean log is supplied, compares the audited data with that log's fingerprint. It can run even when the audited sample fails the Benford eligibility gate, but the reference log itself must pass the gate.
The implementation is entirely in native MQL5 and uses no DLLs, ALGLIB, or external services. Five files: BenfordCore.mqh is the digit engine, AuditTargets.mqh turns terminal data into arrays, IntegrityAuditor_EA.mq5 runs the audit on a timer, TamperSimulator.mq5 measures what it detects, and DealLogGenerator_EA.mq5 produces the test log. Each section below covers one file: the real code, what it does and why.
Author: Adewumi Babatunde Gbadebo