Benford's Law for Trade Log and Quote Feed Integrity Auditing in Native MQL5
Contents
- Introduction
- Architecture
- BenfordCore.mqh: The Digit Engine and the Eligibility Gate
- AuditTargets.mqh: Turning Logs and Feeds into Arrays
- DealLogGenerator_EA.mq5: A Reproducible Log to Audit
- TamperSimulator.mq5: Measuring What the Audit Catches
- IntegrityAuditor_EA.mq5: The Timer-Driven Audit
- Testing and Results
- Limitations and Practical Considerations
- Conclusion
Introduction
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.
Scope. A Benford flag is a reason to look closer, not proof of manipulation. For any data series the auditor reports one of three outcomes: consistent, inconsistent, or unsuitable for the test. It never certifies data as genuine.
Architecture
The pipeline has four stages, shown in Fig. 1: data sources feed value arrays, the analyser extracts and counts leading digits, the eligibility gate decides whether a verdict may be issued, and the tests produce statistics and a verdict. A separate branch compares digit counts with a reference fingerprint for trade profit. This comparison does not require the audited sample to pass the eligibility gate, but the reference log must pass it before its fingerprint is used.
The statistics are short enough to write down in full. Digits are read from the magnitude of each value, so a loss of -12.5 and a win of 12.5 both start with 1.
// expected share of first digit d P1(d) = log10(1 + 1/d) d = 1..9 // expected share of second digit d: any first digit k may precede it P2(d) = sum over k=1..9 of log10(1 + 1/(10k+d)) d = 0..9 // chi-square over the digit classes, and mean absolute deviation chi2 = N * sum( (o_d - e_d)^2 / e_d ) MAD = mean( |o_d - e_d| ) // per-digit z-score with continuity correction and zero floor z_d = max(0, ( |o_d - e_d| - 1/(2N) ) / sqrt( e_d * (1 - e_d) / N )) // eligibility gate quantities, on the log10 of every value span = max(log10 v) - min(log10 v) spread = Q75(log10 v) - Q25(log10 v)
Negative values resulting from the continuity correction are clipped to zero in the reported z-scores, matching the implementation in Evaluate().
The eligibility gate. A series needs at least 300 values, a span of at least 3 decades and a spread of at least 1 decade. The third condition matters most: a sample can span five decades because of a few outliers while nine tenths of its values sit inside one decade, and a first-digit test there mostly measures where that decade starts.
The verdict. MAD decides, using the conventional bands: for the first digit, close up to 0.006, acceptable up to 0.012, marginal up to 0.015, non-conforming beyond; for the second digit, 0.008, 0.010 and 0.012. Chi-square is reported alongside but never decides alone, since it grows with sample size and rejects almost any real dataset once N reaches a few thousand.
The reference rule. Benford's shape is a poor yardstick when honest data isn't Benford-shaped. The reference rule replaces theoretical shares with digit shares from a known-clean log and measures drift. Limits are derived from bootstrap resamples as the 97.5% quantile of the distance for each digit test. That isn't a 5% combined false-alarm rate, since the distances aren't independent; the measured rate, in Testing, is 3.7%, not 5%.
Audit pipeline and file responsibilities

1. Data sources become value arrays, pass through digit extraction and the eligibility gate, and end in a verdict. The reference branch uses limits calibrated by TamperSimulator.
BenfordCore.mqh: The Digit Engine and the Eligibility Gate
BenfordCore.mqh knows everything about digits and nothing about where numbers come from, a deliberate separation: the same class audits a deal log, a tick stream or a pooled set of bars, and can be tested on its own with a plain array. It holds the constants, two enumerations, one result structure and one class.
Constants. Two chi-square critical values at the 1% level, one for the nine first-digit classes (8 degrees of freedom) and one for the ten second-digit classes (9 degrees of freedom), plus a tiny epsilon used when reading digits from floating point numbers.
//+------------------------------------------------------------------+ //| Constants | //| | //| Chi-square critical values at the 1% level for the two digit | //| tests (8 degrees of freedom for the nine first digits, 9 for the | //| ten second digits), and the nudge used when reading digits from | //| floating point numbers. | //+------------------------------------------------------------------+ #define BF_CHI2_1ST_1PCT 20.090 // Critical value, 8 df, 1% level #define BF_CHI2_2ND_1PCT 21.666 // Critical value, 9 df, 1% level #define BF_DIGIT_EPS 1.0e-9 // Guards 2.9999999999999996 style noise
ENUM_BENFORD_GATE. The gate result names the reason a series was refused, so a report can say why no verdict was issued instead of just leaving the fields empty.
//+------------------------------------------------------------------+ //| ENUM_BENFORD_GATE | //| | //| Outcome of the eligibility gate. A series that fails the gate is | //| reported as not applicable and never as a pass, because | //| Benford's law only describes data that spreads across several | //| orders of magnitude. | //+------------------------------------------------------------------+ enum ENUM_BENFORD_GATE { BENFORD_GATE_OK = 0, // Series is eligible BENFORD_GATE_TOO_FEW = 1, // Fewer observations than the minimum BENFORD_GATE_SPAN_NARROW = 2, // Smallest to largest value covers too few decades BENFORD_GATE_SPREAD_NARROW = 3 // Middle half of the values covers too few decades };
ENUM_BENFORD_VERDICT. The verdict is numbered from best to worst on purpose, so two verdicts can be compared with the operators < and >. The auditor relies on that when it keeps the worst verdict across many series.
//+------------------------------------------------------------------+ //| ENUM_BENFORD_VERDICT | //| | //| Conformity verdict taken from the mean absolute deviation bands. | //| The numeric order matters: a larger value is always a worse | //| verdict, so two verdicts can be compared with < and >. | //+------------------------------------------------------------------+ enum ENUM_BENFORD_VERDICT { BENFORD_NOT_APPLICABLE = 0, // Gate failed, no verdict issued BENFORD_CLOSE = 1, // Close conformity BENFORD_ACCEPTABLE = 2, // Acceptable conformity BENFORD_MARGINAL = 3, // Marginally acceptable BENFORD_NONCONFORMING = 4 // Non-conforming };
SBenfordReport. One Evaluate() call fills this structure and nothing else is returned. Every digit array has ten slots indexed by the digit itself, so observed1[7] is the observed share of first digit 7 and slot 0 is unused — keeping the arrays in the report lets the caller print a digit table or draw a chart without touching the analyser again.
//+------------------------------------------------------------------+ //| SBenfordReport | //| | //| Everything one Evaluate() call produces: the sample description, | //| the gate result, and for each digit test the observed and | //| expected shares, per-digit z-scores, chi-square, mean absolute | //| deviation and verdict. Digit arrays are indexed by the digit | //| itself, so first-digit slot 0 is unused. | //+------------------------------------------------------------------+ struct SBenfordReport { //--- sample description and gate int n; // Observations analysed int dropped; // Values rejected (zero, NaN, infinite) double span_decades; // log10(largest / smallest) double iqr_decades; // log10 spread of the middle half ENUM_BENFORD_GATE gate; // Eligibility gate result //--- first-digit test double observed1[10]; // Observed first-digit shares double expected1[10]; // Expected first-digit shares double z1[10]; // Per-digit z-scores, first digit double chi2_1; // Chi-square, first digit double mad_1; // Mean absolute deviation, first digit int worst_digit_1; // First digit with the largest z-score ENUM_BENFORD_VERDICT verdict_1; // Verdict from the first-digit MAD //--- second-digit test double observed2[10]; // Observed second-digit shares double expected2[10]; // Expected second-digit shares double z2[10]; // Per-digit z-scores, second digit double chi2_2; // Chi-square, second digit double mad_2; // Mean absolute deviation, second digit int worst_digit_2; // Second digit with the largest z-score ENUM_BENFORD_VERDICT verdict_2; // Verdict from the second-digit MAD };
CBenfordAnalyzer. The class is a streaming counter. Add() reads the digits of one value and increments two counters, also keeping log10 of the value since the gate needs the ordered log values. Reset() clears the object so a single instance can audit many series in turn.
//+------------------------------------------------------------------+ //| CBenfordAnalyzer | //| | //| Streaming first- and second-digit Benford analyser. Values are | //| fed with Add() in any order and any sign, Evaluate() then runs | //| the eligibility gate and, only if the series passes it, the | //| chi-square, MAD and per-digit z-score tests. Digits are taken | //| from the leading significant figures of the magnitude, so a loss | //| of -12.5 and a win of 12.5 both count as first digit 1. | //+------------------------------------------------------------------+ class CBenfordAnalyzer { private: //--- gate limits and verdict bands int m_min_n; // Minimum sample size for a verdict double m_min_span; // Minimum log10(max/min) in decades double m_min_iqr; // Minimum log10 spread of the middle half double m_bands1[3]; // MAD limits: close, acceptable, marginal (1st digit) double m_bands2[3]; // MAD limits: close, acceptable, marginal (2nd digit) //--- accumulated data long m_count1[10]; // First-digit counts, index 1..9 long m_count2[10]; // Second-digit counts, index 0..9 double m_exp1[10]; // Expected first-digit shares double m_exp2[10]; // Expected second-digit shares double m_logs[]; // log10 of every accepted value int m_n; // Accepted observations int m_dropped; // Rejected observations //--- internal helpers void ExtractDigits(const double x,int &d1,int &d2); double Quantile(const double &sorted[],const double q); ENUM_BENFORD_VERDICT VerdictFromMad(const double mad,const double &bands[]); public: //--- construction, configuration and reset CBenfordAnalyzer(); void Configure(const int min_n,const double min_span,const double min_iqr); void Reset(); //--- feeding data bool Add(const double value); int AddArray(const double &values[]); int Count() const; //--- reference-relative comparison void GetShares(double &first[],double &second[]); double MadAgainst(const double &reference[],const bool first_digit); //--- analysis and reporting bool Evaluate(SBenfordReport &report); string SummaryLine(const string label,const SBenfordReport &r); void PrintReport(const string label,const SBenfordReport &r,const bool digit_table); //--- theory and text helpers static double ExpectedFirst(const int d); static double ExpectedSecond(const int d); static bool Chi2Rejects(const double chi2,const bool first_digit); static string VerdictText(const ENUM_BENFORD_VERDICT v); static string GateText(const ENUM_BENFORD_GATE g); };
Constructor. The MAD bands and the expected digit shares never change, so both are set once here and not on every call to Evaluate().
//+------------------------------------------------------------------+ //| CBenfordAnalyzer::CBenfordAnalyzer | //| | //| Sets the conventional MAD bands, precomputes the expected digit | //| shares once and applies default gate limits (300 values, 3 | //| decades, 1 decade of middle-half spread). | //+------------------------------------------------------------------+ CBenfordAnalyzer::CBenfordAnalyzer() { //--- MAD bands: close, acceptable and marginal upper limits m_bands1[0]=0.006; m_bands1[1]=0.012; m_bands1[2]=0.015; m_bands2[0]=0.008; m_bands2[1]=0.010; m_bands2[2]=0.012; //--- expected shares never change, so compute them here and not per Evaluate() m_exp1[0]=0.0; for(int d=1;d<=9;d++) m_exp1[d]=ExpectedFirst(d); for(int d=0;d<=9;d++) m_exp2[d]=ExpectedSecond(d); Configure(300,3.0,1.0); Reset(); }
ExtractDigits — reading digits from a double. The obvious approach, floor(x / 10^floor(log10 x)), breaks in two places: log10(1000) can return 2.9999999999999996, making the mantissa 10 instead of 1, and 0.3/0.1 can misread as first digit 2. The routine renormalises the mantissa if the exponent is off by one, adds 1e-9 before truncating, rolling over if that lands on 10 — a small bias near a digit boundary, swamped by ordinary two-decimal rounding.
//+------------------------------------------------------------------+ //| CBenfordAnalyzer::ExtractDigits | //| | //| Reads the first and second significant digits of x (x must be | //| positive and finite). The digits come from the mantissa m = x / | //| 10^floor(log10(x)), which lies in [1,10). Two guards deal with | //| floating point: m is renormalised if log10 rounded the exponent | //| the wrong way, and a tiny epsilon is added so 0.3 (stored as | //| 2.9999999999999996 after scaling) still reads as 3. | //+------------------------------------------------------------------+ void CBenfordAnalyzer::ExtractDigits(const double x,int &d1,int &d2) { double e = MathFloor(MathLog10(x)); // Decimal exponent of x double m = x/MathPow(10.0,e); // Mantissa, expected in [1,10) //--- log10(1000) can return 2.9999999999999996, which would give m = 10 if(m>=10.0) m/=10.0; else if(m<1.0) m*=10.0; m+=BF_DIGIT_EPS; //--- a mantissa of 9.9999999995 rolls over to 10.0000000005, which is really 1.0 if(m>=10.0) m/=10.0; d1=(int)m; d2=(int)((m-d1)*10.0); d1=MathMax(1,MathMin(9,d1)); d2=MathMax(0,MathMin(9,d2)); }
Add — one value in. The sign is dropped because the law describes magnitudes. Zero, NaN and infinite values have no leading digit. Forcing them into digit 1 would distort the result, so they are counted as dropped and skipped.
//+------------------------------------------------------------------+ //| CBenfordAnalyzer::Add | //| | //| Adds one value. The sign is discarded because Benford's law | //| describes magnitudes. Zero, NaN and infinite values have no | //| leading digit and are counted as dropped instead of being forced | //| into digit 1. | //+------------------------------------------------------------------+ bool CBenfordAnalyzer::Add(const double value) { double x = MathAbs(value); // Magnitude of the value if(!MathIsValidNumber(x) || x<=0.0) { m_dropped++; return(false); } int d1 = 1; // First significant digit int d2 = 0; // Second significant digit ExtractDigits(x,d1,d2); m_count1[d1]++; m_count2[d2]++; //--- keep log10(x) for the span and spread gate; the 4096 reserve avoids a reallocation per call ArrayResize(m_logs,m_n+1,4096); m_logs[m_n]=MathLog10(x); m_n++; return(true); }
Evaluate, part one — the gate. Checks run cheapest first: sample size, span, spread. Span and spread need sorted values, so Evaluate() sorts a copy and leaves m_logs in arrival order. Spread uses nearest-rank quartiles, precise enough for a gate. Any failure returns false with the reason in the report, and both verdicts stay BENFORD_NOT_APPLICABLE.
//+------------------------------------------------------------------+ //| CBenfordAnalyzer::Evaluate | //| | //| Runs the eligibility gate and, if it passes, both digit tests. | //| Returns false when the gate fails; the report then holds the | //| sample description and gate reason only, and both verdicts stay | //| BENFORD_NOT_APPLICABLE. | //| Chi-square is N * sum((o-e)^2 / e). MAD is the mean of |o-e| | //| over the digits. The per-digit z-score is (|o-e| - 1/(2N)) / | //| sqrt(e(1-e)/N), where 1/(2N) is the continuity correction. | //+------------------------------------------------------------------+ bool CBenfordAnalyzer::Evaluate(SBenfordReport &report) { ZeroMemory(report); report.n=m_n; report.dropped=m_dropped; report.gate=BENFORD_GATE_OK; report.worst_digit_1=1; report.verdict_1=BENFORD_NOT_APPLICABLE; report.verdict_2=BENFORD_NOT_APPLICABLE; //--- gate 1: enough observations if(m_n<m_min_n) { report.gate=BENFORD_GATE_TOO_FEW; return(false); } //--- gates 2 and 3 need ordered log values; sort a copy so m_logs keeps its arrival order double sorted[]; ArrayCopy(sorted,m_logs); ArraySort(sorted); report.span_decades=sorted[m_n-1]-sorted[0]; report.iqr_decades=Quantile(sorted,0.75)-Quantile(sorted,0.25); if(report.span_decades<m_min_span) { report.gate=BENFORD_GATE_SPAN_NARROW; return(false); } if(report.iqr_decades<m_min_iqr) { report.gate=BENFORD_GATE_SPREAD_NARROW; return(false); }
Evaluate, part two — the first-digit test. For each digit the loop stores the observed and expected share, adds to chi-square and MAD, and computes the z-score with the 1/(2N) continuity correction, keeping the digit with the largest z-score — the log's digit table is far more useful when it says where to look.
//--- first-digit test over digits 1..9 for(int d=1;d<=9;d++) { double o=(double)m_count1[d]/nn; double e=m_exp1[d]; report.observed1[d]=o; report.expected1[d]=e; chi+=nn*(o-e)*(o-e)/e; mad+=MathAbs(o-e); double z=(MathAbs(o-e)-corr)/MathSqrt(e*(1.0-e)/nn); report.z1[d]=MathMax(0.0,z); //--- remember the digit that departs most from theory if(report.z1[d]>worst) { worst=report.z1[d]; report.worst_digit_1=d; } } report.chi2_1=chi; report.mad_1=mad/9.0; report.verdict_1=VerdictFromMad(report.mad_1,m_bands1);
Evaluate, part three — the second-digit test. The second-digit loop has the same shape as the first-digit loop, over digits 0 to 9 instead of 1 to 9, with its own worst-digit tracker. z2[10] and worst_digit_2 give it the same per-digit detail as the first-digit test, so a bad second-digit verdict traces to a specific digit through PrintReport() and the digit table.
//--- second-digit test over digits 0..9 double chi2=0.0,mad2=0.0,worst2=0.0; // Accumulators for the second-digit test report.worst_digit_2=0; for(int d=0;d<=9;d++) { double o=(double)m_count2[d]/nn; double e=m_exp2[d]; report.observed2[d]=o; report.expected2[d]=e; chi2+=nn*(o-e)*(o-e)/e; mad2+=MathAbs(o-e); double z=(MathAbs(o-e)-corr)/MathSqrt(e*(1.0-e)/nn); report.z2[d]=MathMax(0.0,z); //--- remember the digit that departs most from theory if(report.z2[d]>worst2) { worst2=report.z2[d]; report.worst_digit_2=d; } } report.chi2_2=chi2; report.mad_2=mad2/10.0; report.verdict_2=VerdictFromMad(report.mad_2,m_bands2); return(true); }
ExpectedSecond — the second-digit law. A second digit of d can follow any first digit from 1 to 9, so the expected share is a sum of nine logarithms; the result is nearly flat, from 0.1197 for digit 0 to 0.0850 for digit 9, which is why hand rounding toward 0 and 5 stands out so clearly in the second digit.
//+------------------------------------------------------------------+ //| CBenfordAnalyzer::ExpectedSecond | //| | //| Benford's expected share of second digit d (0..9). A second | //| digit of d can follow any first digit k = 1..9, so the share is | //| the sum over k of log10(1 + 1/(10k + d)). | //+------------------------------------------------------------------+ double CBenfordAnalyzer::ExpectedSecond(const int d) { if(d<0 || d>9) return(0.0); double sum = 0.0; // Accumulated probability for(int k=1;k<=9;k++) sum+=MathLog10(1.0+1.0/(10.0*k+d)); return(sum); }
GetShares and MadAgainst — the reference fingerprint. GetShares() copies the observed digit shares out of the analyser. MadAgainst() computes the same mean absolute deviation as the theory-based test, but against any shares the caller supplies, turning the analyser into a change detector for a clean log's fingerprint; a genuine MAD can never be negative, so an invalid call returns -1.0 rather than 0.0.
//+------------------------------------------------------------------+ //| CBenfordAnalyzer::MadAgainst | //| | //| Mean absolute deviation between the current sample's digit | //| shares and a reference set of shares instead of Benford's. It is | //| the same statistic as the theory-based MAD, but the yardstick is | //| the fingerprint of a known-clean log. That matters when the | //| honest data is not Benford-shaped: theory then flags the clean | //| log itself, while the reference only moves when the log changes. | //| Returns -1.0, never a real distance, when the inputs cannot be | //| compared at all, so an invalid call cannot be read as a perfect | //| match. | //+------------------------------------------------------------------+ double CBenfordAnalyzer::MadAgainst(const double &reference[],const bool first_digit) { //--- a MAD of 0.0 would read as a perfect match, so an invalid call returns a value no real distance can take if(m_n<=0 || ArraySize(reference)<10) return(-1.0); double nn = (double)m_n; // Sample size as a double double sum = 0.0; // Running sum of absolute deviations if(first_digit) { for(int d=1;d<=9;d++) sum+=MathAbs((double)m_count1[d]/nn-reference[d]); return(sum/9.0); } for(int d=0;d<=9;d++) sum+=MathAbs((double)m_count2[d]/nn-reference[d]); return(sum/10.0); }
AuditTargets.mqh: Turning Logs and Feeds into Arrays
Three classes convert terminal data into plain double arrays: CTradeLogSource for deal history, CQuoteFeedSource for ticks, CMarketBarSource for pooled bars. Zero values are skipped everywhere, since they have no leading digit, and every reader appends through one helper, so loading half a million ticks stays linear.
ENUM_TRADE_SERIES. The selector offers profit, commission and swap. Lot size is deliberately absent: volumes come from a handful of discrete steps and cannot follow a digit law.
//+------------------------------------------------------------------+ //| ENUM_TRADE_SERIES | //| | //| Which column of the deal history to audit. Lot size is | //| deliberately absent: volumes are drawn from a handful of | //| discrete steps and cannot follow a first-digit law. | //+------------------------------------------------------------------+ enum ENUM_TRADE_SERIES { TRADE_SERIES_PROFIT = 0, // Profit of closing deals TRADE_SERIES_COMMISSION = 1, // Commission of all deals TRADE_SERIES_SWAP = 2 // Swap of all deals };
PushValue. Every reader appends through this helper. It reserves 4096 slots per ArrayResize() call, so the array grows in large blocks and not once per value.
//+------------------------------------------------------------------+ //| PushValue | //| | //| Appends one value to a dynamic array. The 4096 reserve makes | //| ArrayResize allocate in large blocks, so loading hundreds of | //| thousands of values stays linear instead of reallocating on | //| every call. | //+------------------------------------------------------------------+ void PushValue(double &out[],const double v) { int n = ArraySize(out); // Current length ArrayResize(out,n+1,4096); out[n]=v; }
LoadHistory — the live account. Accepts() drops balance, credit and correction operations and applies the symbol and magic filters. Profit comes from closing deals only, since an opening deal always books zero; commission and swap come from every accepted deal, and on a demo account where both are zero, those series come back empty and unsuitable.
//+------------------------------------------------------------------+ //| CTradeLogSource::LoadHistory | //| | //| Loads one series from the live account history between from and | //| to and returns the number of values. Profit is read from closing | //| deals only, because an opening deal always books zero profit. | //| Commission and swap are read from every accepted deal. | //+------------------------------------------------------------------+ int CTradeLogSource::LoadHistory(const datetime from,const datetime to,const ENUM_TRADE_SERIES series,double &out[]) { ArrayResize(out,0); if(!HistorySelect(from,to)) return(0); int total = HistoryDealsTotal(); // Deals in the selected range for(int i=0;i<total;i++) { ulong ticket=HistoryDealGetTicket(i); if(ticket==0 || !Accepts(ticket)) continue; double v = 0.0; // Value for the requested series if(series==TRADE_SERIES_PROFIT) { if(HistoryDealGetInteger(ticket,DEAL_ENTRY)==DEAL_ENTRY_IN) continue; v=HistoryDealGetDouble(ticket,DEAL_PROFIT); } else if(series==TRADE_SERIES_COMMISSION) v=HistoryDealGetDouble(ticket,DEAL_COMMISSION); else v=HistoryDealGetDouble(ticket,DEAL_SWAP); //--- zero has no leading digit if(v!=0.0) PushValue(out,v); } return(ArraySize(out)); }
ExportHistoryCsv — a portable copy. The tester discards its deal history when a run ends, so the log is written to CSV: time, ticket, entry type, profit, commission, swap and volume per deal, as UTF-16 text; LoadCsv() reads it back the same way. Profit, commission and swap use DoubleToString(...,2), fine for a standard deposit currency but rounding a sub-cent crypto symbol's values, so CSV and live-history audits aren't guaranteed to agree.
//+------------------------------------------------------------------+ //| CTradeLogSource::ExportHistoryCsv | //| | //| Writes every buy and sell deal in the range to a CSV in | //| Common\Files. Numbers are formatted with DoubleToString so the | //| file holds exactly two decimals, matching what the terminal | //| shows. FILE_COMMON is used because a Strategy Tester agent | //| cannot write to the terminal's own Files folder. | //+------------------------------------------------------------------+ bool CTradeLogSource::ExportHistoryCsv(const string file,const datetime from,const datetime to) { if(!HistorySelect(from,to)) return(false); EnsureFolder(file); int h = FileOpen(file,FILE_WRITE|FILE_CSV|FILE_COMMON,','); // File handle if(h==INVALID_HANDLE) { Print("CTradeLogSource: cannot write ",file,", error ",GetLastError()); return(false); } FileWrite(h,"time","deal","entry","profit","commission","swap","volume"); int total = HistoryDealsTotal(); // Deals in the selected range for(int i=0;i<total;i++) { ulong ticket=HistoryDealGetTicket(i); if(ticket==0 || !Accepts(ticket)) continue; FileWrite(h, (string)(long)HistoryDealGetInteger(ticket,DEAL_TIME), (string)ticket, EntryText(HistoryDealGetInteger(ticket,DEAL_ENTRY)), DoubleToString(HistoryDealGetDouble(ticket,DEAL_PROFIT),2), DoubleToString(HistoryDealGetDouble(ticket,DEAL_COMMISSION),2), DoubleToString(HistoryDealGetDouble(ticket,DEAL_SWAP),2), DoubleToString(HistoryDealGetDouble(ticket,DEAL_VOLUME),2)); } FileClose(h); return(true); }
LoadCsv — reading it back. The header and every data row are read as seven fields in the same order; profit comes from rows whose entry is not "in", with a blank-field guard against a trailing empty line. The reader trusts the file's shape, never checking header names, field count, decimal separator or encoding, so a file edited with a comma decimal can silently parse into the wrong number rather than failing, with no error.
//+------------------------------------------------------------------+ //| CTradeLogSource::LoadCsv | //| | //| Loads one series from a CSV written by ExportHistoryCsv() (or by | //| the tamper simulator) in the Common\Files folder. The file is | //| opened without FILE_ANSI, so it is read as the UTF-16 text the | //| writer produced. | //+------------------------------------------------------------------+ int CTradeLogSource::LoadCsv(const string file,const ENUM_TRADE_SERIES series,double &out[]) { ArrayResize(out,0); int h = FileOpen(file,FILE_READ|FILE_CSV|FILE_COMMON,','); // File handle if(h==INVALID_HANDLE) { Print("CTradeLogSource: cannot open ",file,", error ",GetLastError()); return(0); } //--- the first row holds the seven column names for(int c=0;c<7;c++) FileReadString(h); while(!FileIsEnding(h)) { string t_time = FileReadString(h); // Deal time (not used for the digit tests) string t_deal = FileReadString(h); // Deal ticket string t_entry = FileReadString(h); // in, out, inout or outby string t_prof = FileReadString(h); // Profit string t_comm = FileReadString(h); // Commission string t_swap = FileReadString(h); // Swap string t_vol = FileReadString(h); // Volume //--- an empty first field at the end of the file is a trailing blank line, not a row if(t_time=="" && FileIsEnding(h)) break; double v = 0.0; // Value for the requested series if(series==TRADE_SERIES_PROFIT) { if(t_entry=="in") continue; v=StringToDouble(t_prof); } else if(series==TRADE_SERIES_COMMISSION) v=StringToDouble(t_comm); else v=StringToDouble(t_swap); if(v!=0.0) PushValue(out,v); } FileClose(h); return(ArraySize(out)); }
FetchChunk — a tolerant tick request. The first CopyTicksRange() call for a period can fail while the terminal synchronises history. The helper retries up to three times with a short pause and only then gives up with zero ticks.
//+------------------------------------------------------------------+ //| CQuoteFeedSource::FetchChunk | //| | //| Copies the ticks of one time window. The first request for a | //| period can fail while the terminal is still synchronising | //| history, so it retries up to three times with a short pause | //| before giving up. | //+------------------------------------------------------------------+ int CQuoteFeedSource::FetchChunk(const long from_msc,const long to_msc,MqlTick &ticks[]) { for(int attempt=0;attempt<3;attempt++) { ArrayFree(ticks); int got=CopyTicksRange(m_symbol,ticks,COPY_TICKS_ALL,(ulong)from_msc,(ulong)to_msc); if(got>=0) return(got); Sleep(200); } return(0); }
LoadAll — four series in one pass. Ticks are fetched in six-hour windows so memory stays bounded, carrying the previous tick's mid price and time across window edges. Four arrays come out: absolute mid change, spread, reported per-tick volume and inter-tick gap. The volume series uses volume_real when it is positive, falling back to volume otherwise. Price changes are rounded to six decimals to avoid floating-point artifacts affecting digit extraction . Ticks whose mid didn't move are counted separately as a frozen-feed indicator, never entering the digit test.
//--- walk the range one chunk at a time for(long start=from_msc;start<to_msc;start+=m_chunk_ms) { long stop = start+m_chunk_ms-1; // Last millisecond of this chunk if(stop>to_msc) stop=to_msc; MqlTick ticks[]; int got = FetchChunk(start,stop,ticks); // Ticks in this chunk for(int i=0;i<got;i++) { double mid = MidOf(ticks[i]); // Mid price of this tick if(mid<=0.0) continue; m_ticks_seen++; //--- price change: only ticks where the mid moved carry a leading digit if(prev_mid>0.0) { double dp = NormalizeDouble(MathAbs(mid-prev_mid)/m_point,6); // Change in points if(dp>0.0) PushValue(delta,dp); else m_zero_moves++; } //--- spread in points if(ticks[i].ask>0.0 && ticks[i].bid>0.0) { double sp = NormalizeDouble((ticks[i].ask-ticks[i].bid)/m_point,6); // Spread in points if(sp>0.0) PushValue(spread,sp); } //--- tick volume: real volume when the feed supplies it, tick volume otherwise double vol = (ticks[i].volume_real>0.0) ? ticks[i].volume_real : (double)ticks[i].volume; // Volume if(vol>0.0) PushValue(volume,vol); //--- gap between consecutive ticks if(prev_msc>0 && (long)ticks[i].time_msc>prev_msc) PushValue(gap,(double)((long)ticks[i].time_msc-prev_msc)); prev_mid=mid; prev_msc=(long)ticks[i].time_msc; } } return(ArraySize(delta)+ArraySize(spread)+ArraySize(volume)+ArraySize(gap));
LoadPooled — many symbols, one sample. One symbol's daily tick volume sits in a narrow band; instruments differ in scale by orders of magnitude, so pooling bars across symbols is the only way this kind of series can pass the gate. The forming bar is skipped (shift 1) since its values are still growing. An empty symbol list means every symbol in Market Watch; a named list fixes the basket, making a pooled result reproducible.
The volume series reads MqlRates.tick_volume, a count of price updates inside the bar, not real_volume (broker-reported traded lots). Pooling assumes only that every symbol supplies this field, not that symbols trade at comparable scale — the digit test checks whether that difference still produces a Benford-consistent pooled sample.
//+------------------------------------------------------------------+ //| CMarketBarSource::LoadPooled | //| | //| Fills out with one value per bar per symbol for the last 'bars' | //| closed bars of the chosen timeframe and returns the number of | //| values. An empty symbol_list means every symbol in Market Watch; | //| otherwise the comma-separated names are used in the order given, | //| so the basket is fixed and can be published. A listed name that | //| does not exist is counted as skipped. Range is high minus low | //| divided by the symbol's point, rounded to six decimals so float | //| noise cannot change a leading digit. Zero values are skipped | //| because they carry no leading digit. | //+------------------------------------------------------------------+ int CMarketBarSource::LoadPooled(const ENUM_TIMEFRAMES tf,const int bars,const ENUM_BAR_SERIES series,const string symbol_list,double &out[]) { ArrayResize(out,0); m_used=0; m_skipped=0; string names[]; int total = 0; // Symbols to read //--- an explicit list makes the basket reproducible; an empty list means all of Market Watch if(symbol_list=="") { total=SymbolsTotal(true); ArrayResize(names,total); for(int i=0;i<total;i++) names[i]=SymbolName(i,true); } else { total=StringSplit(symbol_list,',',names); for(int i=0;i<total;i++) { StringTrimLeft(names[i]); StringTrimRight(names[i]); SymbolSelect(names[i],true); } } for(int i=0;i<total;i++) { string name = names[i]; // Symbol being read MqlRates rates[]; //--- start at shift 1 so the forming bar is left out int got = CopyRates(name,tf,1,bars,rates); // Bars copied double point = SymbolInfoDouble(name,SYMBOL_POINT); // Point size of this symbol if(got<=0 || point<=0.0) { m_skipped++; continue; } m_used++; for(int k=0;k<got;k++) { double v = 0.0; // Value for this bar if(series==BAR_SERIES_TICK_VOLUME) v=(double)rates[k].tick_volume; else v=NormalizeDouble((rates[k].high-rates[k].low)/point,6); if(v>0.0) PushValue(out,v); } } return(ArraySize(out)); }
DealLogGenerator_EA.mq5: A Reproducible Log to Audit
The audit needs a log to work on, and a real EA's log was ruled out because its design is a confound: any skew in its profits would be impossible to separate from a flaw in the audit itself. DealLogGenerator_EA is a seeded random-entry trader built instead, producing a reproducible history shaped, via log-uniform lot size and stop distance, to spread across enough decades — a controlled synthetic benchmark, not a stand-in for a real EA's log.
Inputs. The seed makes a run repeatable: the same seed, symbol and dates give the same trades. The last two inputs control the CSV that OnTester() writes.
//+------------------------------------------------------------------+ //| Inputs | //| | //| Everything that shapes the deal history. The seed makes a run | //| repeatable: same seed, same symbol and same dates give the same | //| trade sequence. The last two inputs control the CSV written when | //| the test ends. | //+------------------------------------------------------------------+ input long InpMagic = 50110; // Magic number input int InpSeed = 7; // Random seed input double InpEntryProb = 0.10; // Entry probability per new bar (0..1) input double InpMinLots = 0.01; // Smallest lot size drawn input double InpMaxLots = 1.00; // Largest lot size drawn input double InpRiskCapPct = 1.0; // Max loss at the stop, % of balance input double InpMaxMarginFrac = 0.30; // Max share of free margin one trade may use input int InpATRPeriod = 14; // ATR period for stop distances input double InpMinStopATR = 0.3; // Smallest stop distance, in ATR input double InpMaxStopATR = 5.0; // Largest stop distance, in ATR input int InpMinHoldBars = 1; // Shortest timed hold, in bars input int InpMaxHoldBars = 200; // Longest timed hold, in bars input bool InpExportCsv = true; // Write the deal history to CSV when the test ends input string InpCsvPath = "BenfordAudit\\clean_deals.csv"; // CSV path inside the Common\Files folder
RandLogUniform — why profits spread across decades. A trade's profit is roughly lots times price move. Uniform lots between 0.01 and 1.00 would compress the range; drawing lots and stop distances uniformly in log space instead spreads the product across several decades — the property a first-digit audit needs.
//+------------------------------------------------------------------+ //| RandLogUniform | //| | //| Draws a value that is uniform in log space between lo and hi. A | //| log-uniform lot size and a log-uniform stop distance multiply | //| into profits that spread over several orders of magnitude, which | //| is the property a first-digit audit needs. | //+------------------------------------------------------------------+ double RandLogUniform(const double lo,const double hi) { if(lo<=0.0 || hi<=lo) return(lo); return(MathExp(MathLog(lo)+Rand01()*(MathLog(hi)-MathLog(lo)))); }
CapLotsByRisk — keeping the test alive. Without a cap, a 1.0 lot trade with a wide stop could end the run after a handful of deals. The cap limits loss at the stop to InpRiskCapPct of balance; where it would fall below the minimum lot, NormalizeLots() lifts it back, so the cap is soft at the bottom end.
//+------------------------------------------------------------------+ //| CapLotsByRisk | //| | //| Limits the lot size so a full stop-out loses at most | //| InpRiskCapPct of the balance. Without a cap a 1.0 lot trade with | //| a wide stop could end the test after a handful of deals. If the | //| cap falls below the minimum lot, NormalizeLots() lifts it back | //| to the minimum, so the cap is soft at the bottom end. | //+------------------------------------------------------------------+ double CapLotsByRisk(const double lots,const double sl_dist) { double tick_size = SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE); // Minimum price increment double tick_loss = SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE_LOSS); // Money per tick, losing side if(tick_size<=0.0 || tick_loss<=0.0 || sl_dist<=0.0) return(lots); //--- money lost per 1.0 lot if the stop is hit double balance = AccountInfoDouble(ACCOUNT_BALANCE); // Account balance double loss_per_lot = sl_dist/tick_size*tick_loss; // Loss at the stop, 1.0 lot double risk_money = balance*InpRiskCapPct/100.0; // Largest acceptable loss return(MathMin(lots,risk_money/loss_per_lot)); }
OpenRandomTrade — three exit rules. A bracket trade ends near a multiple of ATR, a timed trade ends wherever price is when its bar count runs out, and a trailing trade ends after locking in part of a move — mixing them widens the spread of closing profits. The block below covers the mode switch, then the lot draw, risk cap and margin fit.
//--- each mode configures its own exit rule switch(mode) { case EXIT_BRACKET: tp_dist=MathMax(sl_dist*RandLogUniform(0.5,3.0),min_dist); break; case EXIT_TIMED: //--- wide protective stop only; the time limit is the real exit sl_dist*=2.0; hold_limit=(int)MathMax(1.0,MathRound(RandLogUniform(InpMinHoldBars,InpMaxHoldBars))); break; case EXIT_TRAILING: break; } //--- lot size: log-uniform draw, then risk cap, then margin fit double lots=NormalizeLots(CapLotsByRisk(RandLogUniform(InpMinLots,InpMaxLots),sl_dist)); lots=FitLotsToMargin(is_buy,lots,price); if(lots<=0.0) return;
ManagePosition — time exits and the trailing stop. The hold limit acts as a hard cap for all three modes. Only the trailing mode moves the stop, and only ever toward price.
//+------------------------------------------------------------------+ //| ManagePosition | //| | //| Runs once per bar while a position is open. Closes it when the | //| hold limit is reached and, in trailing mode, moves the stop | //| toward price but never away from it. | //+------------------------------------------------------------------+ void ManagePosition() { g_bars_held++; //--- time exit (also acts as a hard cap for the other two modes) if(g_bars_held>=g_hold_limit) { g_trade.PositionClose(_Symbol); return; } if(g_exit_mode!=EXIT_TRAILING) return; //--- trailing stop: only ever tighten ENUM_POSITION_TYPE ptype = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE); // Long or short double cur_sl = PositionGetDouble(POSITION_SL); // Current stop-loss double cur_tp = PositionGetDouble(POSITION_TP); // Current take-profit if(ptype==POSITION_TYPE_BUY) { double new_sl=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID)-g_trail_dist,_Digits); if(new_sl>cur_sl) g_trade.PositionModify(_Symbol,new_sl,cur_tp); } else { double new_sl=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK)+g_trail_dist,_Digits); if(cur_sl==0.0 || new_sl<cur_sl) g_trade.PositionModify(_Symbol,new_sl,cur_tp); } }
OnTester — writing the log. When the test ends and before its deal history is discarded, the EA exports every deal to CSV. The end date is far in the future so nothing is cut off.
//+------------------------------------------------------------------+ //| OnTester | //| | //| Called once when the test finishes, before the deal history is | //| discarded. It writes every deal to a CSV in the Common\Files | //| folder so the auditor and the tamper simulator can read the same | //| log outside the tester. FILE_COMMON is used because each tester | //| agent has its own private Files folder. | //+------------------------------------------------------------------+ double OnTester() { if(InpExportCsv) { CTradeLogSource source; //--- an end date far in the future takes every deal the test produced bool ok=source.ExportHistoryCsv(InpCsvPath,0,D'2100.01.01'); Print("DealLogGenerator: CSV export ",ok ? "done: " : "FAILED: ",InpCsvPath); } return(0.0); }
TamperSimulator.mq5: Measuring What the Audit Catches
An audit is only as useful as its measured detection rate, and its false-alarm rate matters just as much. TamperSimulator applies four kinds of edit to the clean log and counts how often each rule flags the result, running hundreds of bootstrap resamples per cell to show how much a verdict moves from sampling noise alone.
The four edits. Invented replaces a value with a draw from a uniform distribution over the observed range. Rounded pushes the second digit to 0 or 5, as hand-tidied numbers do. Padded adds a constant to winners. Scaled multiplies losers by an independent random factor between 0.4 and 0.8 — not the textbook case of scaling an entire population by one constant, so no scale-invariance argument applies, though it's hard for a digit test to catch.
//+------------------------------------------------------------------+ //| ENUM_TAMPER | //| | //| The four edits the simulator applies. Invented replaces a value | //| with a made-up one. Rounded pushes the second digit toward 0 or | //| 5. Padded adds a constant to winners. Scaled multiplies losers | //| by an independent random factor below 1, applied only to losing | //| trades. This is not the scale invariance of an ideal Benford | //| population under one shared constant, so no guarantee follows | //| from theory; only the measured detection rate below does. | //+------------------------------------------------------------------+ enum ENUM_TAMPER { TAMPER_INVENTED = 0, // Value replaced by a uniform draw over the observed range TAMPER_ROUNDED = 1, // Value rounded so its second digit is 0 or 5 TAMPER_PADDED = 2, // Constant added to winning trades TAMPER_SCALED = 3 // Losing trades multiplied by a factor in 0.4..0.8 };
ApplyTamper. Each value is edited with probability frac, so frac is the share of records touched. Padding only touches winners and scaling only touches losers, so their effective share is lower by the fraction of the wrong sign.
//+------------------------------------------------------------------+ //| ApplyTamper | //| | //| Edits each value with probability frac, so frac is the share of | //| records touched. Padding only touches winners and scaling only | //| touches losers, so their effective share is lower than frac by | //| the share of the wrong sign. | //+------------------------------------------------------------------+ void ApplyTamper(const ENUM_TAMPER type,const STamperContext &ctx,const double frac,double &values[]) { for(int i=0;i<ArraySize(values);i++) { if(Rand01()>=frac) continue; double v = values[i]; // Original value switch(type) { case TAMPER_INVENTED: //--- a linear-uniform draw over the observed range puts most values in the top decade values[i]=(Rand01()<0.5 ? -1.0 : 1.0)*(ctx.lo+Rand01()*(ctx.hi-ctx.lo)); break; case TAMPER_ROUNDED: values[i]=RoundSecondDigit(v); break; case TAMPER_PADDED: if(v>0.0) values[i]=v+ctx.median; break; case TAMPER_SCALED: if(v<0.0) values[i]=v*(0.4+0.4*Rand01()); break; } } }
RoundSecondDigit. The routine finds the value of the second digit's place and rounds to the nearest multiple of five of those units. The first digit almost never changes, but the second digit can only be 0 or 5.
//+------------------------------------------------------------------+ //| RoundSecondDigit | //| | //| Rounds a value to the nearest 5 in its second significant digit, | //| which forces the second digit to 0 or 5 while leaving the first | //| digit almost untouched. This is the pattern a person produces | //| when tidying numbers by hand. | //+------------------------------------------------------------------+ double RoundSecondDigit(const double v) { double a = MathAbs(v); // Magnitude if(a<=0.0) return(v); double unit = MathPow(10.0,MathFloor(MathLog10(a))-1.0); // Value of the second digit's place double r = MathRound(a/(5.0*unit))*5.0*unit; // Nearest multiple of 5 units //--- a tiny value could round to zero; keep it at the smallest step instead if(r<=0.0) r=5.0*unit; return(v<0.0 ? -r : r); }
Calibrate — setting the reference limits. The function draws InpCalTrials untouched resamples, measures each one's first- and second-digit distance from the clean fingerprint, and takes the 97.5% quantile of each as a limit. A 2.5% tail on each of two tests doesn't add up to a clean 5% combined tail, since the distances move together, not independently. Read the measured rate in the results table instead.
//+------------------------------------------------------------------+ //| Calibrate | //| | //| Sets the two reference limits. It draws InpCalTrials bootstrap | //| resamples of the untouched clean log, measures each resample's | //| first- and second-digit distance from the clean fingerprint with | //| MadAgainst(), sorts the distances and takes the InpFlagQuantile | //| quantile of each as the limit. A resample that moves further | //| than that is unusual for an untouched log, so anything beyond it | //| is treated as edited. | //+------------------------------------------------------------------+ void Calibrate(const double &clean[]) { double d1[]; double d2[]; double work[]; ArrayResize(d1,InpCalTrials); ArrayResize(d2,InpCalTrials); for(int t=0;t<InpCalTrials;t++) { Bootstrap(clean,work); g_bf.Reset(); g_bf.AddArray(work); d1[t]=g_bf.MadAgainst(g_ref1,true); d2[t]=g_bf.MadAgainst(g_ref2,false); } ArraySort(d1); ArraySort(d2); //--- nearest-rank quantile: the value at position ceil(q*n)-1 int idx=(int)MathMax(0.0,MathMin((double)(InpCalTrials-1),MathCeil(InpFlagQuantile*InpCalTrials)-1.0)); g_lim1=d1[idx]; g_lim2=d2[idx]; }
RunCell — one experiment cell. Each trial resamples, edits (skipped when frac is zero, measuring the false-alarm rate), and updates two counts: the reference flag and the theory-based verdict against Benford. The reference flag skips the eligibility gate, since it compares two samples of the same log, not the log against Benford.
//+------------------------------------------------------------------+ //| RunCell | //| | //| Runs InpTrials trials for one edit type and one edit share. Each | //| trial resamples the clean log, applies the edit (skipped when | //| frac is 0, which measures the false-alarm rate), then updates | //| two sets of counts: the theory-based verdicts against Benford, | //| and the reference flag against the clean fingerprint. | //+------------------------------------------------------------------+ void RunCell(const ENUM_TAMPER type,const double frac,const double &clean[],const STamperContext &ctx,STamperStats &st) { ZeroMemory(st); double work[]; SBenfordReport r; for(int t=0;t<InpTrials;t++) { st.trials++; Bootstrap(clean,work); if(frac>0.0) ApplyTamper(type,ctx,frac,work); g_bf.Reset(); g_bf.AddArray(work); //--- reference rule: how far has this sample drifted from the clean fingerprint? if(g_bf.MadAgainst(g_ref1,true)>g_lim1 || g_bf.MadAgainst(g_ref2,false)>g_lim2) st.flagged++; //--- a refused series counts as a gate failure for the theory-based counts if(!g_bf.Evaluate(r)) { st.gate_fail++; continue; } if(r.verdict_1==BENFORD_NONCONFORMING || r.verdict_2==BENFORD_NONCONFORMING) st.nonconform++; if(r.verdict_1>=BENFORD_MARGINAL || r.verdict_2>=BENFORD_MARGINAL) st.marginal_up++; if(CBenfordAnalyzer::Chi2Rejects(r.chi2_1,true) || CBenfordAnalyzer::Chi2Rejects(r.chi2_2,false)) st.chi_reject++; } }
OnStart — the whole experiment. After the clean log loads, its fingerprint is stored, the limits are calibrated, a fresh baseline of untouched trials is run, and then every edit type is applied at 2%, 5%, 10% and 20%. The fixed seed makes the whole table repeatable; the run shown here used InpSeed = 11 (default), InpCalTrials = 1,000 calibration resamples, InpFlagQuantile = 0.975, InpTrials = 300 trials per cell, and read the clean log from the same CSV DealLogGenerator_EA exported (BenfordAudit\clean_deals.csv).
//--- fingerprint of the clean log, then the limits calibrated on untouched resamples g_bf.Reset(); g_bf.AddArray(clean); g_bf.GetShares(g_ref1,g_ref2); Calibrate(clean); PrintFormat("Reference limits at the %.1f%% quantile: first digit %.5f, second digit %.5f (use these in IntegrityAuditor_EA)", 100.0*InpFlagQuantile,g_lim1,g_lim2); double shares[4] = {0.02,0.05,0.10,0.20}; // Edit shares to test STamperStats st; EnsureFolder("BenfordAudit\\tamper_results.csv"); int h = FileOpen("BenfordAudit\\tamper_results.csv",FILE_WRITE|FILE_CSV|FILE_COMMON,','); // Results file if(h!=INVALID_HANDLE) FileWrite(h,"edit","share","trials","gate_fail","reference_flagged","band_nonconforming","band_marginal_or_worse","chi2_reject"); //--- baseline: fresh untouched trials, so every flag here is a false positive RunCell(TAMPER_INVENTED,0.0,clean,ctx,st); ReportCell("none",0.0,st,h); //--- every edit type at every share for(int k=0;k<4;k++) for(int s=0;s<4;s++) { RunCell((ENUM_TAMPER)k,shares[s],clean,ctx,st); ReportCell(TamperName((ENUM_TAMPER)k),shares[s],st,h); }
IntegrityAuditor_EA.mq5: The Timer-Driven Audit
The auditor ties the pieces together. It never trades. On a timer it loads the trade log and the market data, audits every series, prints one summary line per series to the Experts log, shows the same lines on the chart and raises an alert when a verdict or a reference limit is exceeded.
Reference inputs. When InpReferenceCsv names a known-clean log, the audited log's digit shares are compared with that log instead of Benford. A limit of 0 keeps a check in report-only mode, so the distances can be observed before limits are chosen.
//+------------------------------------------------------------------+ //| Inputs: reference comparison | //| | //| Optional second check for the trade profit series. When | //| InpReferenceCsv names a known-clean log, the audited log's digit | //| shares are compared with that log's shares instead of with | //| Benford's. The two limits come from the TamperSimulator | //| printout; a limit of 0 means report the distance without ever | //| raising a breach. This is the check to rely on when the clean | //| log is not itself Benford-shaped. | //+------------------------------------------------------------------+ input string InpReferenceCsv = ""; // Clean reference log in Common\Files, empty = off input double InpRefLimit1 = 0.0; // Breach limit, first-digit distance (0 = none) input double InpRefLimit2 = 0.0; // Breach limit, second-digit distance (0 = none)
OnInit. The gate limits come straight from the inputs. The timer is set in minutes and one audit runs immediately, so the result is on the chart without waiting for the first timer event.
//+------------------------------------------------------------------+ //| OnInit | //| | //| Applies the gate limits, binds the data sources and runs one | //| audit immediately so the result is on the chart without waiting | //| for the first timer event. | //+------------------------------------------------------------------+ int OnInit() { //--- the gate limits come straight from the inputs g_bf.Configure(InpMinSample,InpMinSpan,InpMinSpread); g_trades.SetFilters("",InpMagicFilter); g_symbol=(InpQuoteSymbol=="" ? _Symbol : InpQuoteSymbol); if(InpAuditQuotes && !g_quotes.Init(g_symbol)) { Print("IntegrityAuditor: cannot use symbol ",g_symbol); return(INIT_PARAMETERS_INCORRECT); } EventSetTimer(MathMax(1,InpTimerMinutes)*60); RunAudit(); return(INIT_SUCCEEDED); }
AuditSeries — one series, one verdict. The shared analyser is reset, fed one array and evaluated; the result prints in full to the Experts log, and a one-line summary comes back for the chart. A series counts as bad as its worse digit test, and the worst verdict over all series feeds the alert decision.
//+------------------------------------------------------------------+ //| AuditSeries | //| | //| Resets the shared analyser, feeds it one series and evaluates | //| it. The result goes to the Experts log through PrintReport() and | //| comes back as a summary line for the chart. The worst verdict | //| seen so far is kept in worst so RunAudit() can decide about the | //| alert once every series has been checked. | //+------------------------------------------------------------------+ string AuditSeries(const string label,const double &values[],ENUM_BENFORD_VERDICT &worst) { SBenfordReport r; // Report for the audited array g_bf.Reset(); g_bf.AddArray(values); g_bf.Evaluate(r); g_bf.PrintReport(label,r,InpPrintDigits); //--- a series is as bad as its worse digit test ENUM_BENFORD_VERDICT v=(ENUM_BENFORD_VERDICT)MathMax((int)r.verdict_1,(int)r.verdict_2); if(v>worst) worst=v; return(g_bf.SummaryLine(label,r)); }
CompareToReference. This runs right after the profit series is audited, while the analyser still holds it. It loads the reference CSV and runs it through the same eligibility gate first, since a short or clustered reference would otherwise become a fingerprint on its own, then measures the digit distance with MadAgainst(); an invalid distance is reported as invalid, never folded into the breach decision.
//+------------------------------------------------------------------+ //| CompareToReference | //| | //| Compares the series currently held in g_bf (the audited trade | //| profits, so call it right after AuditSeries) with the clean | //| reference log. The reference log must itself pass the | //| eligibility gate before its shares are trusted, measures the | //| first- and second-digit distance with MadAgainst() and builds a | //| report line. Returns true only when a non-zero limit is | //| genuinely exceeded, never on an invalid distance. | //+------------------------------------------------------------------+ bool CompareToReference(string &line) { double ref_values[]; double ref1[]; double ref2[]; SBenfordReport rep; if(g_trades.LoadCsv(InpReferenceCsv,TRADE_SERIES_PROFIT,ref_values)<InpMinSample) { line="Reference comparison: file missing or too small"; return(false); } g_ref.Reset(); g_ref.AddArray(ref_values); //--- the reference log must itself clear the gate, or its shares are not a trustworthy fingerprint if(!g_ref.Evaluate(rep)) { line="Reference comparison: reference log failed the eligibility gate ("+CBenfordAnalyzer::GateText(rep.gate)+")"; return(false); } g_ref.GetShares(ref1,ref2); double d1 = g_bf.MadAgainst(ref1,true); // First-digit distance from the reference double d2 = g_bf.MadAgainst(ref2,false); // Second-digit distance from the reference if(d1<0.0 || d2<0.0) { line="Reference comparison: invalid (audited series or reference too small)"; return(false); } //--- a limit of 0 switches that test to report-only bool breach=(InpRefLimit1>0.0 && d1>InpRefLimit1) || (InpRefLimit2>0.0 && d2>InpRefLimit2); line=StringFormat("Reference distance: 1st=%.5f 2nd=%.5f %s",d1,d2,breach ? "BREACH" : "within limits or report-only"); return(breach); }
RunAudit — the trade-log branch and the alert. A CSV path switches the source from live history to a file, so exported and edited logs can be audited; a series that fails the gate is refused, not passed, so an empty commission column reads as unsuitable, not conforming. The alert fires once per timer cycle, not once per breach: a persisting problem re-triggers Alert() every cycle.
//--- trade log: profit, commission and swap if(InpAuditTrades) { ENUM_TRADE_SERIES series[3]={TRADE_SERIES_PROFIT,TRADE_SERIES_COMMISSION,TRADE_SERIES_SWAP}; string names[3]={"Trade profit","Trade commission","Trade swap"}; datetime now=TimeCurrent(); for(int s=0;s<3;s++) { //--- a CSV path switches the source from live history to the exported file if(InpDealCsv!="") g_trades.LoadCsv(InpDealCsv,series[s],values); else g_trades.LoadHistory(now-(datetime)InpTradeDays*86400,now,series[s],values); report+=AuditSeries(names[s],values,worst)+"\n"; //--- g_bf still holds the profit series here, which is what the reference check needs if(s==0 && InpReferenceCsv!="") { string ref_line; if(CompareToReference(ref_line)) breach=true; report+=ref_line+"\n"; } } }
The tick-feed and pooled-bar branches follow the same pattern, one AuditSeries() call per array. The function's last step is the alert decision, comparing the worst verdict with the configured level and also firing on a reference breach, so a log that stays inside Benford's bands but drifts from its own fingerprint still raises the alarm.
//--- alert once, after every series has been checked if(InpAlertLevel==ALERT_OFF) return; ENUM_BENFORD_VERDICT limit = (InpAlertLevel==ALERT_MARGINAL) ? BENFORD_MARGINAL : BENFORD_NONCONFORMING; // Alert threshold if(worst>=limit || breach) Alert("IntegrityAuditor: worst verdict is ",CBenfordAnalyzer::VerdictText(worst),breach ? ", reference limit breached" : "",". See the Experts log.");
Testing and Results
Every number below comes from a real run using a USD-denominated demo trading account with a 100,000 USD initial deposit and 1:100 leverage. The test log was produced by DealLogGenerator_EA in the Strategy Tester with the settings in the table, and every audit was run on the exported CSV or on live terminal data from the same account on 2026.09.19. All detection and false-alarm figures in this section describe this one synthetic XAGUSD/M5 benchmark and its own bootstrap resamples; they have not been checked against independent genuine logs from other symbols, periods or EAs, and should be read as a demonstration of the method on this benchmark, not as a general detection-rate guarantee.
| Metric/Setting | Value |
|---|---|
| Symbol/Timeframe | XAGUSD/M5 |
| Period | 2026.03.01 to 2026.09.18 |
| Modelling | 1 minute OHLC, zero latency |
| Deposit/Leverage | 100,000 USD/1:100 |
| EA inputs | Defaults (seed 7, entry probability 0.10, lots 0.01 to 1.00, risk cap 1.0%) |
| Bars/Ticks | 39,030/780,408 |
The reported results were obtained under the historical prices and execution conditions of the test environment. The exported deal log is the fixed input for the subsequent audit experiments; reproducing the exact same trades and profit values from a new Strategy Tester run may require identical price history, spread and execution settings.
The generator loses money, as a random trader paying the spread should: net profit -6,623.84, profit factor 0.82. That has no bearing on the audit, which only reads the digits, and what matters is that they vary: 1,328 closing deals from 0.01 to 1,021.14, spanning 5.01 decades, all in USD. Fig. 2 shows the balance after each closed trade.
Balance after each closed trade

2. Balance built from the exported deal history. The dashed line marks the 100,000 starting deposit.
The balance curve is the starting deposit plus the running sum of closing profit alone, not profit plus commission plus swap — the real driver of an account's balance. That only holds because commission and swap are zero on every one of the 1,328 deals, confirmed by checking the exported CSV directly, not the setup table above, which shows neither. The curve's final value, 93,376.16, matches the tester's net-profit figure for the same reason.
| Metric | Value |
|---|---|
| Total trades/deals | 1,328/2,656 |
| Net profit/Gross profit/Gross loss | -6,623.84/29,393.76/-36,017.60 |
| Profit factor/Expected payoff | 0.82/-4.99 |
| Winning trades | 460 (34.64%) |
| Largest win/Largest loss | 1,021.14/-198.60 |
| Average win/Average loss | 63.90/-41.49 |
| Maximal balance drawdown | 9,687.41 (9.68%) |
The clean log against Benford. With the default gate (300 values, 3 decades, 1 decade spread), the profit series passes and comes out marginal on the first digit, close on the second: the excess is almost entirely digit 1, 35.5% against 30.1% expected (z = 4.29). A plausible cause is the bell-shaped distribution over-representing whichever digits sit at its centre. Commission and swap are zero here, so both series are unsuitable.
First-digit and second-digit shares of the clean log

3. Left: first digit, observed against Benford. Right: second digit. N = 1,328 closing deals.
| Series | N | Span/spread | 1st MAD | 1st chi-square | 1st verdict | 2nd MAD | 2nd chi-square | 2nd verdict |
|---|---|---|---|---|---|---|---|---|
| Trade profit (clean) | 1,328 | 5.01/1.14 | 0.01211 | 21.0 | marginal | 0.00776 | 10.8 | close |
| Trade commission | 0 | n/a | n/a | n/a | unsuitable | n/a | n/a | unsuitable |
| Trade swap | 0 | n/a | n/a | n/a | unsuitable | n/a | n/a | unsuitable |
The simulation. With 1,000 untouched resamples and the 97.5% quantile, the calibrated limits came out at 0.00971 for the first digit and 0.00994 for the second. The table below has 300 trials per cell, so a measured rate near 50% carries a binomial 95% confidence interval of roughly ±5.7 percentage points (narrower near 0% or 100%); treat a difference of a few points between two rows as noise, not a real effect. Fig. 4 shows the same data. The left panel is the reference rule and the right panel is the plain Benford rule, non-conforming on either digit test.
Start with the untouched row: the reference rule flags 3.7% of resamples, close to its 5% calibration, while the Benford rule flags 52.3% as non-conforming — at this sample size the theory-based rules can't separate a genuine log from an edited one. Rounding to 0/5 is caught from 5% of records (51.3%) and almost always from 10% (99.7%); invented values need 20% (95.3%); padding and scaling stay at the false-alarm level throughout.
One result looks odd until you think about it: for invented values, the Benford rule flags fewer trials as the share grows, from 37.7% at 2% down to 18.7% at 20%. The honest log already has a digit-1 surplus, and uniformly drawn values dilute that toward a flatter pattern closer to Benford, so the rule gets quieter the more the log is edited. The reference rule has no such blind spot, since it measures movement either way.
Detection rate by edit type and share of records edited

4. Left: reference rule. Right: plain Benford rule (non-conforming on either digit). The dashed line is the untouched log.
| Edit | Share edited | Reference rule (%) | Benford non-conforming (%) | Benford marginal or worse (%) | Chi-square rejects (%) |
|---|---|---|---|---|---|
| none | 0% | 3.7 | 52.3 | 91.3 | 87.0 |
| invented | 2% | 4.7 | 37.7 | 83.0 | 79.0 |
| invented | 5% | 9.7 | 33.7 | 75.3 | 73.7 |
| invented | 10% | 34.7 | 25.0 | 70.7 | 67.3 |
| invented | 20% | 95.3 | 18.7 | 63.3 | 79.0 |
| rounded | 2% | 8.7 | 46.0 | 87.0 | 85.0 |
| rounded | 5% | 51.3 | 60.7 | 91.3 | 91.0 |
| rounded | 10% | 99.7 | 97.7 | 100.0 | 100.0 |
| rounded | 20% | 100.0 | 100.0 | 100.0 | 100.0 |
| padded | 2% | 5.7 | 39.0 | 86.3 | 85.0 |
| padded | 5% | 5.3 | 46.3 | 86.7 | 85.0 |
| padded | 10% | 8.0 | 38.0 | 83.0 | 80.7 |
| padded | 20% | 8.7 | 32.0 | 78.3 | 76.0 |
| scaled | 2% | 6.7 | 49.3 | 89.3 | 84.3 |
| scaled | 5% | 7.0 | 48.3 | 86.7 | 86.0 |
| scaled | 10% | 5.0 | 34.7 | 78.7 | 74.3 |
| scaled | 20% | 10.0 | 28.3 | 79.3 | 72.7 |
One edited log through the auditor. The simulator also writes one edited CSV per edit type at 10%. The rounded file, audited with the reference log and the EA's calibrated limits, even improves to acceptable on the first digit while the second digit is flagged by both rules — the case for testing both digits, since a first-digit test alone would have called it better than the unedited log.
| Log | 1st MAD/chi-square/verdict | 2nd MAD/chi-square/verdict | Reference distance 1st/2nd | Result |
|---|---|---|---|---|
| Clean | 0.01211/21.0/marginal | 0.00776/10.8/close | 0.00000/0.00000 | within limits |
| Rounded (10%) | 0.01009/14.2/acceptable | 0.01542/52.0/NON-CONFORMING | 0.00251/0.01657 | BREACH |
The limits are 0.00971 and 0.00994. The first-digit distance (0.00251) is well inside its limit. The second-digit distance (0.01657) is well outside, and that is what raises the alert.
The tick feed. The tick audit read 72 hours of XAGUSD history from the same demo trading environment, from 2026.09.16 14:26 to 2026.09.19 14:26. All four tick series were refused by the gate: price changes cover only 2.49 decades with a nearly constant spread, and inter-tick gaps span 6.62 decades but only 0.44 in the middle half. The refusals are expected — the gate keeps the auditor from ruling on data the test cannot describe.
| XAGUSD series (72 hours) | N | Span (decades) | Spread (decades) | Outcome |
|---|---|---|---|---|
| Price change (points) | 541,162 | 2.49 | 0.78 | unsuitable: span too narrow |
| Spread (points) | 636,020 | 0.73 | 0.08 | unsuitable: span too narrow |
| Tick volume | 0 | n/a | n/a | unsuitable: no values |
| Inter-tick gap (ms) | 635,857 | 6.62 | 0.44 | unsuitable: middle half too narrow |
Pooled bars. Daily bars (60 per symbol, ending 2026.09.19, the same IntegrityAudit run as the tick feed above) pooled across symbols cover far more decades, since instruments differ in scale. Two baskets were audited: all 395 symbols in the terminal's Market Watch at test time, and a named list of 19 (EURUSD, GBPUSD, USDJPY, AUDUSD, USDCAD, XAUUSD, XAGUSD, #SPGI, #SPOT, #SQ, #SQM, #SRE, #STT, #STZ, #SWK, #SYF, #SYK, #SYY, #STLA.US), passed to LoadPooled() via InpBarSymbols as a comma-separated list. The 395-symbol basket's tick volume spans 4.42 decades but only 0.51 in the middle half, so it's refused: most of those symbols are similarly sized stocks whose values bunch together.
The 19-symbol basket passes the gate on tick volume (1,140 values, 3.69 decades) and comes out non-conforming: MAD 0.03201, chi-square 125.8. Digit 1 appears in 18.9% where Benford expects 30.1% (z = 8.24); the second digit is acceptable (chi-square 13.5). Bar range on the same basket is refused, with a middle half of 0.61 decades.
First-digit shares of pooled daily tick volume, 19 symbols

5. Observed first-digit shares of daily tick volume pooled over 19 symbols, against Benford.
| Basket | Series | N | Span/spread | Outcome |
|---|---|---|---|---|
| 395 symbols (all Market Watch) | Tick volume | 23,697 | 4.42/0.51 | unsuitable: middle half too narrow |
| 395 symbols (all Market Watch) | Bar range (points) | 23,700 | about 5.8/0.85 | unsuitable: middle half too narrow |
| 19 named symbols | Tick volume | 1,140 | 3.69/passes | 1st MAD 0.03201, non-conforming; 2nd acceptable |
| 19 named symbols | Bar range (points) | 1,140 | 3.46/0.61 | unsuitable: middle half too narrow |
This result is not evidence of broker feed manipulation. Each instrument's daily tick volume sits in its own narrow band, so 19 symbols form 19 clusters, and clusters produce this kind of digit pattern. The pooled audit needs a healthy reference feed to compare against, exactly like the trade-log audit.
Limitations and Practical Considerations
The gate refuses a lot. With the defaults, all four tick-level series were refused, along with both pooled series for the 395-symbol basket and the bar-range series for the 19-symbol basket — by design. Pooled tick volume for one named basket is the only quote-side series that passed.
Small-sample noise dominates the standard bands. The conventional MAD bands were designed for large samples. At 1,328 values, sampling noise and the profits' bell shape together put a genuine log in the marginal band, and half of untouched resamples come out non-conforming — the reason for the reference rule.
The calibration is optimistic. The limits are calibrated on bootstrap resamples of the same log the auditor later compares against; two independent genuine logs from the same EA differ from each other more than a log differs from its own resamples. The detection and false-alarm rates in Testing and Results inherit this: they were measured on one synthetic log and its own resamples, not on independent genuine logs, other symbols or other EAs, so treat them as a property of this benchmark rather than a verified general detection rate. Before trusting a limit in production, calibrate on several genuine logs, not resamples of one.
Blind spots. Scaling and padding stayed at the false-alarm level at every share tested, up to 20% of records; fabrication that samples from a realistic distribution is invisible to any digit test. Benford also says nothing about duplicated or repeated ticks; therefore, the unchanged-mid share is reported separately and never enters the test.
Pooled results depend on the basket. The same series passed the gate or failed it depending on which symbols were pooled: refused with 395 symbols, non-conforming with 19. The 19-symbol basket is listed by name under Pooled bars above and is reproducible as given; the 395-symbol basket is the broker's full Market Watch at test time, which is not reproducible from the article alone since Market Watch composition varies by broker and by day — name the basket in InpBarSymbols and publish the symbol list alongside any result that depends on it, chosen before you look at any verdict.
Environment details. The tick lookback window is measured back from the terminal clock, so a weekend audit with a six-hour window finds no ticks; use 72 hours or more. Loading hundreds of symbols can leave the terminal busy. Some feeds send no per-tick volume, demo accounts usually book zero commission and swap, and CSV files live in Common\Files, not the terminal's own folder.
The gate is a filter, not a proof — and a flag is a distribution check, not an authenticity check. Passing the gate only indicates that the series has enough observations and scale for digit tests to be meaningful. This condition is necessary but not sufficient: a series may pass the gate and still deviate from Benford for natural reasons (for example, exit rules clustering near round stops). A flag only measures how closely digit frequencies match a target shape. It cannot distinguish a genuine but unusual log from a falsified log that preserves the fingerprint, and it cannot distinguish manipulation from a legitimate style or regime change. Read a flag as "this distribution changed or never matched," not a verdict on anyone's honesty.
Sign, zero values and multiple comparisons are blind spots. ExtractDigits() reads every value's magnitude, so a win of 12.5 and a loss of -12.5 share the same digit counts, and Add() drops every exact zero first; a shift in win/loss balance or zero rate passes through untouched. Separately, IntegrityAuditor_EA checks up to nine series with no Bonferroni-style correction, so the alert needs the per-series log lines to name a suspect, not just the summary.
Conclusion
The auditor works as a screen, not a verdict. Its most useful property is refusing data the law cannot describe: every tick-level series was turned away, and so was a 395-symbol pooled sample. Where it does run, standard bands are unreliable at a few thousand values, flagging an untouched log non-conforming more than half the time; the reference rule measured 3.7% instead. In this synthetic benchmark, the reference rule detected hand rounding about half the time when 5% of records were edited, almost always at 10%, and detected invented values in 95.3% of trials when 20% were edited. Padding and scaling remained near the measured false-alarm level.
Natural extensions: store one reference fingerprint per EA and symbol as a CSV; calibrate limits on many genuine logs instead of resamples of one; add a two-sample test between audited and reference digit counts in place of the fixed limit; log each audit's summary so drift can be plotted over time. None of these change the digit engine itself.
| File | Type | Description |
|---|---|---|
| MQL5\Include\BenfordAudit\BenfordCore.mqh | Include | Digit engine: leading-digit counting, eligibility gate, chi-square, MAD, z-scores, verdict bands and reference distance. |
| MQL5\Include\BenfordAudit\AuditTargets.mqh | Include | Readers that turn deal history, CSV logs, tick history and pooled bars into value arrays. |
| MQL5\Experts\BenfordAudit\IntegrityAuditor_EA.mq5 | Expert Advisor | Timer-driven audit of trade logs, tick feeds and pooled bars, with reference comparison and alerts. |
| MQL5\Experts\BenfordAudit\DealLogGenerator_EA.mq5 | Expert Advisor | Seeded random-entry trader that produces a reproducible deal history and exports it to CSV. |
| MQL5\Scripts\BenfordAudit\TamperSimulator.mq5 | Script | Bootstrap experiment that calibrates the reference limits and measures detection and false-alarm rates. |
Warning: All rights to these materials are reserved by MetaQuotes Ltd. Copying or reprinting of these materials in whole or in part is prohibited.
This article was written by a user of the site and reflects their personal views. MetaQuotes Ltd is not responsible for the accuracy of the information presented, nor for any consequences resulting from the use of the solutions, strategies or recommendations described.