Is Your Expert Advisor Still the One You Backtested? An Edge Drift Detector in MQL5
Introduction
Every Expert Advisor I have put on a live account started with the same promise: a Strategy Tester report that looked good enough to fund. The hard part came later. After a few months of live trading, the account was still in profit, but something felt different. The question I could not answer with the standard reports was simple: is this still the strategy I tested, or has its edge quietly disappeared?
The question comes up regularly on the forum. A recent thread titled "How do you detect when your EA stops matching its backtest?" describes the usual situation well. By the time the degradation is visible on the equity curve, the trader is already 10–15 trades into the drawdown. The author proposes comparing live trades with the backtest distribution using a CUSUM statistic and leaves three questions open. The fixed 4σ threshold feels arbitrary. Trade results are not normally distributed. It is also unclear how to tell a temporary regime change from a real decay of the strategy.
This article builds a tool for the first two questions and is explicit about the limits of the third. The reader brings two samples of closing deals: the backtest that justified going live (the baseline) and the live account history since then. The tool then tests one specific hypothesis in measurable terms: has the average live result per deal fallen below a chosen share of the baseline edge? Every probability it reports is conditional on a stated model. The live deals are treated as draws from the same distribution as the baseline deals until the evidence says otherwise.
By the end, you will have an Edge Drift Detector that:
- Runs a CUSUM shortfall detector whose alarm threshold is calibrated by resampling your own backtest deals instead of assuming a normal distribution.
- Estimates the deal after which the change most likely began.
- Describes the recent average in a short and a long window of deals.
- Splits the change in expectancy into win/loss-rate, win-size, loss-size, and breakeven effects, and checks the deviation and the trade frequency separately.
- Reports how precisely the baseline pins down its own edge and whether its deals look independent.
- Stops when the live account trades a different mix of symbols than the baseline, because per-lot results of different instruments are not comparable.
- Reports how many deals the detector needs to flag one specific scenario: a uniform shift of the mean to zero.
- Summarizes the evidence in a configurable heuristic Edge Health Score and a rule-based verdict with written recommendations.
Everything runs natively inside MetaTrader 5. There are no external libraries, no Python, and no machine learning. The tool consists of one MQL5 script that reads the Strategy Tester report directly, plus a small optional script that exports live deals to a CSV file.
Why the Naive Approach Is Not Enough
Most traders monitor a live Expert Advisor with three informal checks. Is the account still in profit? Is the average result per deal close to the backtest? Do the last few deals look normal? Fig. 1 shows the live sample that ships with the tool as a demonstration dataset. It passes all three checks.

Fig. 1. Cumulative live result per lot against the baseline expectation.
The live curve stays above the dashed baseline expectation for all 240 deals. The average live result is 1.31 per lot against 1.67 in the backtest, a gap that looks like ordinary noise. The last 20 deals average 1.74, which is even better than the backtest. Nevertheless, the demonstration data was built so that the win rate fell from 60% to 40% after deal 100. From that deal onward, the strategy has a negative expectancy. The early profits simply keep the curve high while the edge is gone.
A short numeric example shows why a few deals cannot settle the question. The baseline in this example earns 1.67 per lot per deal, with a standard deviation of 11.39. The standard error of a 15-deal average is 11.39 / sqrt(15) = 2.94. The entire edge is therefore only 0.57 standard errors of a 15-deal window. After 15 deals, a strategy with no edge at all is almost indistinguishable from a healthy one. Any reliable detector must accumulate evidence over many deals, and it must say honestly how many deals it needs.
Concepts and Definitions
Granularity and units. MetaTrader 5 history is deal-level. The tool works with closing deals: every deal with the direction OUT, OUT_BY, or INOUT. A position closed in three partial exits contributes three rows. A reversal (INOUT) contributes one row for the part of its volume that closes the open position. The part that opens the new position is treated as an entry. The net result of a closing deal includes profit, swap, commission, and fees. It also includes the closing deal's allocated share of the entry commission. In the account history, that share is exact per position. In a tester report, it is an average over the open volume of the symbol, as explained in the section on reading the report. Live accounts usually trade different lot sizes than the tester, so each outcome is normalized to one lot:
x_i = NetProfit_i / ClosedVolume_i
A result per lot is not a result per unit of risk. If the stop distance, the volatility, or the tick value changes over time, one lot carries a different risk. The comparison is therefore valid within one instrument whose risk per lot is reasonably stable.
Baseline profile. From the backtest deals, the tool computes the mean mu_0 (the edge per deal) and the standard deviation sigma_0. Their ratio mu_0 / sigma_0 is the edge-to-noise ratio. It decides how fast any detector can work: the smaller the ratio, the more deals are needed. Both numbers are estimates. The tool therefore also reports the 95% interval of mu_0 and the baseline size needed for a t-statistic of 2:
SE = sigma_0 / sqrt(n) t = mu_0 / SE n_needed = (2 / (mu_0 / sigma_0))^2
Outcome classes. A deal is a win when x is above the breakeven band, a loss when x is below the negative of the band, and a breakeven deal otherwise. The band is an input (InpBreakeven) and is zero by default, so an exact zero is breakeven, neither a win nor a loss.
CUSUM shortfall statistic. Page's cumulative sum [3] accumulates evidence of a shortfall below a reference value k. The tool sets k to a share c of the baseline edge, with c = 0.5 by default (input InpRefFraction). The statistic is reset at zero and grows only when the live deals fall short of k:
S_0 = 0 S_t = max(0, S_(t-1) + (k - x_t) / sigma_0), where k = c * mu_0
This is not a direct test of the hypothesis "the mean has become zero". It detects a cumulative shortfall below k. When the live mean stays above k, each step is negative on average, and the statistic stays near zero. When the live mean falls below k, the statistic drifts upward. A complete loss of edge (mean zero) makes it climb by c * mu_0 / sigma_0 per deal on average. A partial loss, such as a live mean of 0.4 * mu_0, also triggers it, only more slowly. An alarm therefore means "the live mean is very likely below k", not "the edge is gone".
Bootstrap threshold. Instead of a fixed multiple of sigma, the threshold h comes from the baseline itself. The tool builds B synthetic paths of H deals by drawing baseline deals with replacement [4]. It runs the same CUSUM on each path and records the highest value each path reaches. The threshold is the (1 - alpha) quantile of those maxima:
h = quantile_(1 - alpha)( max_(t <= H) S_t* )Under this model, if live deals follow the baseline distribution, the probability of reaching h within H deals is approximately alpha. That statement is conditional. It holds only when the live deals are independent draws from the same stationary distribution as the baseline. The real false-alarm rate differs for three reasons: (1) dependence between deals, (2) a non-representative baseline, and (3) estimation error in mu_0, sigma_0, and the distribution shape. The 5% is a model estimate, not a guarantee.
Resampling removes the normality assumption, but it does not remove the other requirements. The fat tails and the skew that are present in the baseline sample are reflected in h. Rare events that the baseline never contained, and any change in the market regime, are not reflected in it.
Dependent deals. Partial exits of one position, series of trades in the same market phase, and grid, basket, or martingale logic make consecutive deals dependent. Independent resampling then underestimates the spread of the path maxima, so h comes out too low. The tool reports the lag-1 autocorrelation of both samples together with the band ±2 / sqrt(n). An independent series stays within this band about 95% of the time. When the baseline value falls outside that band, the moving-block bootstrap [5] is available through the input InpBlockSize. It draws runs of consecutive baseline deals instead of single deals, which keeps short-range dependence inside each run.
Monitoring horizon. H is the number of live deals you plan to monitor, for example, the next 250 deals. The threshold covers the whole path up to H, so a weekly check during that horizon does not add new looks to the CUSUM itself. If the live sample grows beyond H, the tool recalibrates over the actual number of live deals.
Change point. When the alarm fires, the last deal at which the statistic was zero is the classical CUSUM estimate of the change point. It is an estimate, and it usually lags the true change by a few deals.
Tail probabilities. Every other test in the tool is also a bootstrap comparison with the baseline. For an observed statistic, the tail probability is the share of B resampled samples that look at least as extreme. The +1 terms keep the estimate above zero, which is the standard correction for Monte Carlo probabilities:
tail = (count + 1) / (B + 1)
These window-, win-rate-, and deviation tests are single-look comparisons at level alpha. The verdict combines several of them with the CUSUM, so the combined false-alarm rate of the verdict is higher than alpha and is not controlled.
Detection delay. To show how long the detector needs, the tool subtracts mu_0 from every resampled baseline deal. This creates paths with the same shape as the backtest but a zero mean. The median number of deals until such a path crosses h is the detection delay for this one scenario. Real degradation can take other forms: a lower win rate, larger losses, higher volatility, or stronger dependence between trades. Each of them is detected faster or slower depending on how much it lowers the mean.
Expectancy decomposition. Let p, q, and r be the win, loss, and breakeven rates. Let W, L, and Z be the average win, the average loss, and the average breakeven outcome. The expectancy per deal is then E = p * W - q * L + r * Z. The change from baseline (index 0) to live (index 1) splits exactly into four parts:
Delta_E = (p_1 - p_0) * W_0 - (q_1 - q_0) * L_0 — win/loss-rate effect
+ p_1 * (W_1 - W_0) — win-size effect
- q_1 * (L_1 - L_0) — loss-size effect
+ (r_1 * Z_1 - r_0 * Z_0) — breakeven effectBeyond the mean. The detector watches the average result. A strategy can keep its mean while its risk changes. The tool therefore reports two more comparisons that do not enter the verdict. One is the tail probability of the live standard deviation. The other is the number of deals per 30 days in both samples.
Design Choices
Seven decisions shape the tool:
- Outcomes are measured per lot. A live account that sizes positions by risk would otherwise look "better" or "worse" than the tester's fixed lot for reasons unrelated to the edge.
- The reference value is a share of the baseline edge, half by default. The detector is tuned to a large loss of edge, not to small fluctuations around the backtest mean.
- The threshold and every tail probability come from resampling the backtest, not from a normal model. This replaces the arbitrary 4σ threshold, but it keeps the assumptions of independence and a representative baseline.
- The tool uses its own xorshift64* generator with a fixed seed. The same inputs and the same data files produce the same report on every terminal and in every run.
- The verdict follows explicit rules, and the composite score is only a summary. The score and its letter band are heuristics for ranking and quick comparison, not a validated measure of strategy quality.
- The symbol mix is checked before any statistic is computed. One lot of a stock index and one lot of a currency pair produce results of a very different size. By default, the analysis stops when the mix differs.
- Every reader of input data validates what it reads. A row that cannot be parsed is skipped and counted, so the report states how much data it actually used.
Architecture of the Edge Drift Detector
The project consists of two scripts. EdgeDrift.mq5 is the detector itself: it loads the two samples, runs every test, and prints the report in the Experts tab. By default, it reads the baseline from the HTML report that the Strategy Tester saves, and the live sample from the account it runs on. No change to the Expert Advisor is needed, so the tool also works with Expert Advisors bought without source code. DriftExport.mq5 is optional. It writes the live deals to a CSV file when the live account runs on another terminal, for example on a VPS. The detector is organized into seven modules:
- Data model and CSV loading.
- Reproducible random numbers and resampling.
- Sample profile and baseline quality.
- CUSUM shortfall detector.
- Window tests and expectancy decomposition.
- Scoring, verdict, and recommendations.
- Demonstration data.
The declaration zone holds the inputs, the result structures, and the generator state. The first inputs select the data sources. The baseline can come from a Strategy Tester report (the default), a CSV file, or the account history before a chosen date. The live sample can come from the account the script runs on (the default) or from a CSV file. Every weight, threshold, and band boundary is also an input:
//+------------------------------------------------------------------+ //| EdgeDrift.mq5 | //| Copyright 2026, Cristian David Castillo Arrieta | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2026, Cristian David Castillo Arrieta" #property link "https://www.mql5.com" #property version "1.30" #property description "Edge Drift Detector: tests live deals against a backtest report." #property script_show_inputs #define DEMO_BASE_FILE "DemoBase.csv" #define DEMO_LIVE_FILE "DemoLive.csv" #define REPORT_COLUMNS 13 //--- where the baseline comes from enum ENUM_BASE_SOURCE { BASE_TESTER_REPORT, // Tester report (HTML) BASE_CSV_FILE, // CSV file BASE_ACCOUNT_HISTORY // Account history }; //--- where the live sample comes from enum ENUM_LIVE_SOURCE { LIVE_ACCOUNT_HISTORY, // Account history LIVE_CSV_FILE // CSV file }; //--- data sources input ENUM_BASE_SOURCE InpBaseSource = BASE_TESTER_REPORT; // Baseline source input string InpBaseFile = ""; // Baseline file (blank=newest) input ENUM_LIVE_SOURCE InpLiveSource = LIVE_ACCOUNT_HISTORY; // Live source input string InpLiveFile = "EdgeLive.csv"; // Live CSV file input long InpMagic = 0; // Magic number (0 = all) input string InpSymbol = ""; // Symbol (empty = all) input datetime InpLiveFrom = 0; // Live start date (0 = all) input bool InpPerLot = true; // Per-lot outcomes input double InpBreakeven = 0.0; // Breakeven band, abs. value //--- detector settings input int InpHorizon = 250; // Monitoring horizon, deals input double InpRefFraction = 0.5; // Reference k, share of edge input int InpBlockSize = 1; // Block size (1 = iid) input int InpShortWindow = 20; // Short window, deals input int InpLongWindow = 100; // Long window, deals input double InpAlpha = 0.05; // False-alarm rate, horizon input double InpWatchFraction = 0.70; // Threshold share for WATCH input int InpBootstrap = 2000; // Bootstrap resamples input int InpMaxDelay = 2000; // Delay simulation cap input ulong InpSeed = 12345; // Bootstrap seed (repeatable) input int InpMinBaseline = 100; // Floor: baseline deals input int InpMinLive = 20; // Floor: live deals input double InpMaxMixGap = 0.20; // Mix gap that stops the run input bool InpAllowMixed = false; // Run when the mix differs //--- heuristic score input double InpHealthyTail = 0.20; // Tail for full marks input double InpWeightCusum = 40.0; // Weight: CUSUM evidence input double InpWeightLong = 25.0; // Weight: long window input double InpWeightShort = 15.0; // Weight: short window input double InpWeightWinRate = 20.0; // Weight: win rate input double InpGradeAPlus = 95.0; // Minimum score for A+ input double InpGradeA = 85.0; // Minimum score for A input double InpGradeB = 70.0; // Minimum score for B input double InpGradeC = 55.0; // Minimum score for C input double InpGradeD = 40.0; // Minimum score for D //+------------------------------------------------------------------+ //| Data structures | //+------------------------------------------------------------------+ //--- one closing deal reduced to what the detector needs struct DealOutcome { datetime time; // close time of the deal double value; // net result (per lot if enabled) double money; // net result in account currency string symbol; // symbol of the deal }; //--- summary statistics of one sample struct EdgeProfile { int count; // number of closing deals double mean; // average outcome, the edge double stdev; // sample standard deviation int wins; // outcomes above the breakeven band int losses; // outcomes below the breakeven band int breakevens; // outcomes inside the breakeven band double winRate; // wins divided by count double lossRate; // losses divided by count double avgWin; // average win double avgLoss; // average size of a loss double avgBreakeven; // average breakeven outcome }; //--- result of the CUSUM edge-loss detector struct CusumResult { double reference; // reference k double threshold; // alarm threshold h (bootstrap) int horizon; // deals h is calibrated for double peak; // highest CUSUM on the live path int peakIndex; // zero-based index of the peak double peakTail; // tail probability of the peak int alarmIndex; // first index at h, -1 if none int startIndex; // deals before the change point int medianDelay; // median deals to flag a zero mean }; //--- result of one bootstrap window test struct WindowResult { int size; // deals in the window double mean; // mean of the last deals double tail; // tail probability of that mean }; //--- decomposition of the change in expectancy struct EdgeShift { double total; // live mean minus baseline mean double hitRate; // part due to win and loss rates double winSize; // part due to the average win double lossSize; // part due to the average loss double breakeven; // part due to breakeven outcomes }; //--- heuristic score and decision struct DriftScore { double cusumScore; // CUSUM dimension, 0-100 double longScore; // long-window dimension, 0-100 double shortScore; // short-window dimension, 0-100 double winRateScore; // win-rate dimension, 0-100 double total; // weighted composite, 0-100 string grade; // heuristic band from A+ to F string verdict; // CONTINUE/WATCH/REDUCE/SUSPEND }; //--- symbol composition of the baseline and the live sample struct MixResult { int baseSymbols; // distinct symbols in the baseline int liveSymbols; // distinct symbols in the live sample int shared; // symbols present in both samples double countGap; // mix difference by number of deals double weightGap; // mix difference by absolute result double unmatched; // live deal share on unknown symbols bool warning; // true when a gap exceeds InpMaxMixGap }; //--- one closing deal read from the account history struct ClosedDeal { datetime time; // close time of the deal string symbol; // symbol of the deal double volume; // closed volume in lots double net; // net result incl. entry cost }; //--- open state of one position (hedging) or one symbol (netting) struct OpenState { string key; // position ID or symbol long magic; // magic of the opening deal double volume; // open volume in lots double cost; // unallocated entry commission and fee }; //--- global state ulong g_rngState = (ulong)88172645463325252; // xorshift64* generator state int g_blockLeft = 0; // draws left in the current block int g_blockPos = 0; // next baseline index of the block
Module 1: Data Model and CSV Loading
Both CSV files use the same format: a header line followed by one row per closing deal, with the columns CloseTime,Symbol,Volume,NetProfit. The loader opens the file as text with FILE_TXT and takes the separator from the header: a comma, or a semicolon when the header contains no comma. SplitCsv() honors double quotes, so a quoted field may contain the separator. ParseNumber() accepts spaces as thousands separators, a decimal comma, the Unicode minus sign, and the accounting form (12.50) for negative numbers. ParseRow() validates every field before it accepts a row, so one malformed line is skipped and counted instead of stopping the analysis. SortByTime() then restores chronological order, because the CUSUM reads the deals in sequence:
//+------------------------------------------------------------------+ //| Number from text: spaces, decimal comma, unicode minus, (x) | //+------------------------------------------------------------------+ bool ParseNumber(string text, double &value) { StringReplace(text, " ", ""); StringReplace(text, ShortToString(0x00A0), ""); StringReplace(text, ShortToString(0x202F), ""); StringReplace(text, "'", ""); StringReplace(text, ShortToString(0x2212), "-"); //--- accounting style: (12.50) means -12.50 bool negative = false; if(StringGetCharacter(text, 0) == '(' && StringGetCharacter(text, StringLen(text) - 1) == ')') { negative = true; text = StringSubstr(text, 1, StringLen(text) - 2); } //--- with both separators, the first one groups thousands int dot = StringFind(text, "."); int comma = StringFind(text, ","); if(dot >= 0 && comma >= 0) StringReplace(text, (comma < dot) ? "," : ".", ""); StringReplace(text, ",", "."); if(text == "" || text == "-") return(false); //--- accept digits, one point and a leading minus only int points = 0; for(int i = 0; i < StringLen(text); i++) { ushort ch = StringGetCharacter(text, i); if(ch == '.') points++; else if(!(ch >= '0' && ch <= '9') && !(ch == '-' && i == 0)) return(false); } if(points > 1) return(false); value = StringToDouble(text); if(negative) value = -value; return(MathIsValidNumber(value)); } //+------------------------------------------------------------------+ //| Split one CSV line, honoring double quotes | //+------------------------------------------------------------------+ int SplitCsv(const string line, const ushort separator, string &fields[]) { ArrayResize(fields, 0); string current = ""; bool quoted = false; int len = StringLen(line); for(int i = 0; i < len; i++) { ushort ch = StringGetCharacter(line, i); if(ch == '"') { //--- a doubled quote inside a quoted field is a literal quote if(quoted && i + 1 < len && StringGetCharacter(line, i + 1) == '"') { current += "\""; i++; } else quoted = !quoted; continue; } if(ch == separator && !quoted) { int size = ArraySize(fields); ArrayResize(fields, size + 1, 8); fields[size] = current; current = ""; continue; } current += ShortToString(ch); } int n = ArraySize(fields); ArrayResize(fields, n + 1, 8); fields[n] = current; for(int i = 0; i <= n; i++) { StringTrimLeft(fields[i]); StringTrimRight(fields[i]); } return(n + 1); } //+------------------------------------------------------------------+ //| Parse one CSV row: CloseTime,Symbol,Volume,NetProfit | //+------------------------------------------------------------------+ bool ParseRow(const string line, const ushort separator, const bool perLot, DealOutcome &row) { string parts[]; if(SplitCsv(line, separator, parts) < 4) return(false); //--- validate every field before accepting the row datetime t = StringToTime(parts[0]); double volume = 0.0; double net = 0.0; if(t <= 0 || !ParseNumber(parts[2], volume) || volume <= 0.0 || !ParseNumber(parts[3], net)) return(false); row.time = t; row.value = perLot ? net / volume : net; // per lot makes lot sizes comparable row.money = net; row.symbol = parts[1]; return(true); } //+------------------------------------------------------------------+ //| Load all closing deals of one CSV file, returns -1 on failure | //+------------------------------------------------------------------+ int LoadOutcomes(const string fileName, const bool common, const bool perLot, DealOutcome &rows[]) { ArrayResize(rows, 0); int flags = FILE_READ | FILE_TXT | FILE_ANSI | FILE_SHARE_READ; if(common) flags |= FILE_COMMON; int handle = FileOpen(fileName, flags); if(handle == INVALID_HANDLE) return(-1); bool header = true; ushort separator = ','; int skipped = 0; while(!FileIsEnding(handle)) { string line = FileReadString(handle); // in FILE_TXT mode, one call returns one line StringTrimLeft(line); StringTrimRight(line); if(line == "") continue; //--- the header line also tells which separator the file uses if(header) { header = false; if(StringFind(line, ";") >= 0 && StringFind(line, ",") < 0) separator = ';'; continue; } DealOutcome row; if(!ParseRow(line, separator, perLot, row)) { skipped++; continue; } int n = ArraySize(rows); ArrayResize(rows, n + 1, 1024); rows[n] = row; } FileClose(handle); if(skipped > 0) PrintFormat("%s: %d malformed rows skipped", fileName, skipped); return(ArraySize(rows)); } //+------------------------------------------------------------------+ //| Stable insertion sort by close time | //+------------------------------------------------------------------+ void SortByTime(DealOutcome &rows[]) { int n = ArraySize(rows); for(int i = 1; i < n; i++) { DealOutcome key = rows[i]; int j = i - 1; while(j >= 0 && rows[j].time > key.time) { rows[j + 1] = rows[j]; j--; } rows[j + 1] = key; } }
Module 2: Reproducible Random Numbers and Resampling
The bootstrap draws hundreds of thousands of random indices. The xorshift64* generator below is small, fast, and fully defined by its seed. The uniform number uses the top 53 bits, which is the maximum integer precision of an IEEE 754 double. DrawBase() is the only place where baseline deals are resampled. With a block size of 1, it draws single deals. With a larger block size, it draws runs of consecutive deals, and ResetBlock() starts a new run at the beginning of every resampled path:
//+------------------------------------------------------------------+ //| Seed the xorshift64* generator (zero is not a valid state) | //+------------------------------------------------------------------+ void RngSeed(const ulong seed) { g_rngState = (seed == 0) ? (ulong)88172645463325252 : seed; } //+------------------------------------------------------------------+ //| Next 64-bit value of the xorshift64* generator | //+------------------------------------------------------------------+ ulong RngNext() { ulong x = g_rngState; x ^= (x >> 12); x ^= (x << 25); x ^= (x >> 27); g_rngState = x; return(x * (ulong)2685821657736338717); // unsigned overflow wraps modulo 2^64 } //+------------------------------------------------------------------+ //| Uniform number in [0, 1) built from the top 53 bits | //+------------------------------------------------------------------+ double RngUniform() { return((double)(RngNext() >> 11) * (1.0 / 9007199254740992.0)); } //+------------------------------------------------------------------+ //| Uniform index in [0, n) | //+------------------------------------------------------------------+ int RngIndex(const int n) { int k = (int)(RngUniform() * n); return((k >= n) ? n - 1 : k); } //+------------------------------------------------------------------+ //| Start a new resampled path (the next draw opens a new block) | //+------------------------------------------------------------------+ void ResetBlock() { g_blockLeft = 0; } //+------------------------------------------------------------------+ //| One resampled baseline deal: iid or moving-block bootstrap | //+------------------------------------------------------------------+ double DrawBase(const double &base[]) { int n = ArraySize(base); if(InpBlockSize <= 1) return(base[RngIndex(n)]); //--- a block keeps runs of consecutive deals together if(g_blockLeft <= 0) { int size = (InpBlockSize < n) ? InpBlockSize : n; g_blockPos = RngIndex(n - size + 1); g_blockLeft = size; } g_blockLeft--; return(base[g_blockPos++]); }
Module 3: Sample Profile and Baseline Quality
BuildProfile() computes the mean, the sample standard deviation, and the three outcome classes of one sample. The same function serves the baseline and the live sample. LagOneCorrelation() and DealsPerMonth() provide the dependence check and the trade frequency:
//+------------------------------------------------------------------+ //| Mean, deviation, win, loss and breakeven rates of one sample | //+------------------------------------------------------------------+ bool BuildProfile(const double &x[], EdgeProfile &p) { int n = ArraySize(x); p.count = n; if(n < 2) return(false); double sum = 0.0; for(int i = 0; i < n; i++) sum += x[i]; p.mean = sum / n; double ss = 0.0; for(int i = 0; i < n; i++) ss += (x[i] - p.mean) * (x[i] - p.mean); p.stdev = MathSqrt(ss / (n - 1)); //--- three classes: win, loss, and breakeven inside the band double sumWin = 0.0; double sumLoss = 0.0; double sumEven = 0.0; p.wins = 0; p.losses = 0; p.breakevens = 0; for(int i = 0; i < n; i++) { if(x[i] > InpBreakeven) { p.wins++; sumWin += x[i]; } else if(x[i] < -InpBreakeven) { p.losses++; sumLoss -= x[i]; } else { p.breakevens++; sumEven += x[i]; } } p.winRate = (double)p.wins / n; p.lossRate = (double)p.losses / n; p.avgWin = (p.wins > 0) ? sumWin / p.wins : 0.0; p.avgLoss = (p.losses > 0) ? sumLoss / p.losses : 0.0; p.avgBreakeven = (p.breakevens > 0) ? sumEven / p.breakevens : 0.0; return(true); } //+------------------------------------------------------------------+ //| Lag-1 autocorrelation of a sequence of outcomes | //+------------------------------------------------------------------+ double LagOneCorrelation(const double &x[], const double mean) { int n = ArraySize(x); double num = 0.0; double den = 0.0; for(int i = 0; i < n; i++) { den += (x[i] - mean) * (x[i] - mean); if(i > 0) num += (x[i] - mean) * (x[i - 1] - mean); } return((den > 0.0) ? num / den : 0.0); } //+------------------------------------------------------------------+ //| Closing deals per 30 days between the first and the last deal | //+------------------------------------------------------------------+ double DealsPerMonth(const DealOutcome &rows[]) { int n = ArraySize(rows); double days = (double)(rows[n - 1].time - rows[0].time) / 86400.0; return((days > 0.0) ? n * 30.0 / days : 0.0); }
Module 4: CUSUM Shortfall Detector
CusumStep() is the recursion from the definitions. CalibrateThreshold() replays the baseline B times over the horizon and takes the quantile of the path maxima. RunCusum() applies the same step to the live deals and records the peak, its tail probability, the first alarm, and the estimated change point. MedianDetectionDelay() runs the zero-mean scenario that tells you how long the detector needs:
//+------------------------------------------------------------------+ //| One CUSUM step: accumulate evidence of a shortfall below k | //+------------------------------------------------------------------+ double CusumStep(const double s, const double value, const double reference, const double stdev) { double next = s + (reference - value) / stdev; return((next < 0.0) ? 0.0 : next); // the statistic never drops below zero } //+------------------------------------------------------------------+ //| Bootstrap threshold h: the (1 - alpha) quantile of path maxima | //+------------------------------------------------------------------+ double CalibrateThreshold(const double &base[], const EdgeProfile &p, const double reference, const int horizon, const int resamples, const double alpha, double &maxima[]) { ArrayResize(maxima, resamples); //--- replay the baseline many times and record the highest CUSUM of each path for(int b = 0; b < resamples; b++) { double s = 0.0; double mx = 0.0; ResetBlock(); for(int t = 0; t < horizon; t++) { s = CusumStep(s, DrawBase(base), reference, p.stdev); if(s > mx) mx = s; } maxima[b] = mx; } //--- only a share alpha of resampled paths reaches h within the horizon double sorted[]; ArrayCopy(sorted, maxima); ArraySort(sorted); int k = (int)MathCeil((1.0 - alpha) * resamples - 1e-9) - 1; k = (int)MathMax(0, MathMin(resamples - 1, k)); return(sorted[k]); } //+------------------------------------------------------------------+ //| Run the CUSUM on the live deals: peak, alarm and change point | //+------------------------------------------------------------------+ void RunCusum(const double &live[], const double &maxima[], const EdgeProfile &p, CusumResult &c) { int m = ArraySize(live); double path[]; ArrayResize(path, m); double s = 0.0; c.peak = 0.0; c.peakIndex = 0; c.alarmIndex = -1; for(int i = 0; i < m; i++) { s = CusumStep(s, live[i], c.reference, p.stdev); path[i] = s; if(s > c.peak) { c.peak = s; c.peakIndex = i; } if(c.alarmIndex < 0 && s >= c.threshold) c.alarmIndex = i; } //--- share of resampled paths that reach the observed peak (+1 keeps it above zero) int b = ArraySize(maxima); int hits = 0; for(int i = 0; i < b; i++) if(maxima[i] >= c.peak) hits++; c.peakTail = (hits + 1.0) / (b + 1.0); //--- the change point is right after the last zero before the alarm (or the peak) int anchor = (c.alarmIndex >= 0) ? c.alarmIndex : c.peakIndex; c.startIndex = 0; for(int i = anchor; i >= 0; i--) { if(path[i] <= 0.0) { c.startIndex = i + 1; break; } } } //+------------------------------------------------------------------+ //| Median delay to flag a uniform shift of the mean to zero | //+------------------------------------------------------------------+ int MedianDetectionDelay(const double &base[], const EdgeProfile &p, const CusumResult &c, const int resamples, const int maxDelay) { int delays[]; ArrayResize(delays, resamples); for(int b = 0; b < resamples; b++) { double s = 0.0; delays[b] = maxDelay + 1; // marks a path that never reached h ResetBlock(); for(int t = 0; t < maxDelay; t++) { //--- one scenario only: same shape as the baseline, mean moved to zero s = CusumStep(s, DrawBase(base) - p.mean, c.reference, p.stdev); if(s >= c.threshold) { delays[b] = t + 1; break; } } } ArraySort(delays); return(delays[resamples / 2]); }
Module 5: Window Tests and Expectancy Decomposition
WindowTest() compares the mean of the last N live deals with the means of N-deal samples drawn from the baseline. The tool runs it twice, with a short window (20 deals by default) and a long window (100 deals). WinRateTail() does the same for the number of winning deals over the whole live sample. VolatilityTail() asks how often a resampled sample of the live size shows a standard deviation at least as high as the live one. DecomposeShift() implements the four-part split of the change in expectancy:
//+------------------------------------------------------------------+ //| Bootstrap tail probability of the mean of the last deals | //+------------------------------------------------------------------+ void WindowTest(const double &base[], const double &live[], const int window, const int resamples, WindowResult &w) { int m = ArraySize(live); w.size = (window < m) ? window : m; double sum = 0.0; for(int i = m - w.size; i < m; i++) sum += live[i]; w.mean = sum / w.size; //--- how often does a resampled window of the same size look this weak? int hits = 0; for(int b = 0; b < resamples; b++) { double s = 0.0; ResetBlock(); for(int t = 0; t < w.size; t++) s += DrawBase(base); if(s / w.size <= w.mean) hits++; } w.tail = (hits + 1.0) / (resamples + 1.0); } //+------------------------------------------------------------------+ //| Bootstrap tail probability of the live win count | //+------------------------------------------------------------------+ double WinRateTail(const double &base[], const EdgeProfile &live, const int resamples) { int hits = 0; for(int b = 0; b < resamples; b++) { int wins = 0; ResetBlock(); for(int t = 0; t < live.count; t++) if(DrawBase(base) > InpBreakeven) wins++; if(wins <= live.wins) hits++; } return((hits + 1.0) / (resamples + 1.0)); } //+------------------------------------------------------------------+ //| Bootstrap tail probability of a live deviation this high | //+------------------------------------------------------------------+ double VolatilityTail(const double &base[], const EdgeProfile &live, const int resamples) { int hits = 0; double draw[]; ArrayResize(draw, live.count); for(int b = 0; b < resamples; b++) { double sum = 0.0; ResetBlock(); for(int t = 0; t < live.count; t++) { draw[t] = DrawBase(base); sum += draw[t]; } double mean = sum / live.count; double ss = 0.0; for(int t = 0; t < live.count; t++) ss += (draw[t] - mean) * (draw[t] - mean); if(MathSqrt(ss / (live.count - 1)) >= live.stdev) hits++; } return((hits + 1.0) / (resamples + 1.0)); } //+------------------------------------------------------------------+ //| Split the change in expectancy into its exact components | //+------------------------------------------------------------------+ void DecomposeShift(const EdgeProfile &b, const EdgeProfile &l, EdgeShift &e) { //--- expectancy = p * W - q * L + r * Z, so the four parts sum exactly to the total e.total = l.mean - b.mean; e.hitRate = (l.winRate - b.winRate) * b.avgWin - (l.lossRate - b.lossRate) * b.avgLoss; e.winSize = l.winRate * (l.avgWin - b.avgWin); e.lossSize = -l.lossRate * (l.avgLoss - b.avgLoss); e.breakeven = (l.breakevens * l.avgBreakeven) / l.count - (b.breakevens * b.avgBreakeven) / b.count; }
Module 6: Scoring, Verdict, and Recommendations
Each of the four tail probabilities is mapped to a 0–100 score. A tail probability at or above InpHealthyTail (20% by default) earns full marks, because such a result is common when the live deals follow the baseline. The weights are normalized, so only their ratios matter. The score has no statistical interpretation of its own. It is a summary for ranking and quick comparison, and its letter band is a label, not a quality rating. The verdict does not use the score. It follows four explicit rules:
- SUSPEND when the CUSUM alarm fires and the long window is also in the lower alpha tail.
- REDUCE when only one of those two conditions holds.
- WATCH when the short window is in the lower tail, or the CUSUM has reached InpWatchFraction (70%) of the threshold.
- CONTINUE otherwise.
The two windows describe the shape of the recent results. They do not identify the cause of a change, and they do not prove that a change will last. HorizonReading() therefore only states which window sits in the lower tail. CONTINUE means that these statistics detected no deviation. It does not mean that the edge is intact:
//+------------------------------------------------------------------+ //| Map a tail probability to 0-100 (full marks at InpHealthyTail) | //+------------------------------------------------------------------+ double TailScore(const double tail) { return(100.0 * MathMin(1.0, tail / InpHealthyTail)); } //+------------------------------------------------------------------+ //| Heuristic band from the composite score | //+------------------------------------------------------------------+ string GradeFromScore(const double score) { if(score >= InpGradeAPlus) return("A+"); if(score >= InpGradeA) return("A"); if(score >= InpGradeB) return("B"); if(score >= InpGradeC) return("C"); if(score >= InpGradeD) return("D"); return("F"); } //+------------------------------------------------------------------+ //| Heuristic composite score and the rule-based verdict | //+------------------------------------------------------------------+ void ScoreDrift(const CusumResult &c, const WindowResult &sw, const WindowResult &lw, const double winTail, DriftScore &d) { d.cusumScore = TailScore(c.peakTail); d.longScore = TailScore(lw.tail); d.shortScore = TailScore(sw.tail); d.winRateScore = TailScore(winTail); //--- normalized weights, so the result does not depend on their sum double wSum = InpWeightCusum + InpWeightLong + InpWeightShort + InpWeightWinRate; if(wSum <= 0.0) wSum = 1.0; d.total = (d.cusumScore * InpWeightCusum + d.longScore * InpWeightLong + d.shortScore * InpWeightShort + d.winRateScore * InpWeightWinRate) / wSum; d.grade = GradeFromScore(d.total); //--- the verdict uses explicit rules, not the heuristic score bool alarm = (c.alarmIndex >= 0); if(alarm && lw.tail < InpAlpha) d.verdict = "SUSPEND"; else if(alarm || lw.tail < InpAlpha) d.verdict = "REDUCE"; else if(sw.tail < InpAlpha || c.peak >= InpWatchFraction * c.threshold) d.verdict = "WATCH"; else d.verdict = "CONTINUE"; } //+------------------------------------------------------------------+ //| Close time of a deal as text | //+------------------------------------------------------------------+ string DealDate(const DealOutcome &rows[], const int index) { return(TimeToString(rows[index].time, TIME_DATE)); } //+------------------------------------------------------------------+ //| One-line description of the two windows (no causal claim) | //+------------------------------------------------------------------+ string HorizonReading(const WindowResult &sw, const WindowResult &lw) { bool shortWeak = (sw.tail < InpAlpha); bool longWeak = (lw.tail < InpAlpha); if(shortWeak && longWeak) return("Both windows in the lower tail. The low average spans both horizons"); if(shortWeak) return("Short window in the lower tail, long window not. A recent dip"); if(longWeak) return("Long window in the lower tail, short window not. Recent deals look normal, " + "the longer average is still low"); return("Neither window in the lower tail. No deviation detected by this statistic"); } //+------------------------------------------------------------------+ //| Print the written recommendations | //+------------------------------------------------------------------+ void PrintRecommendations(const DealOutcome &liveRows[], const CusumResult &c, const WindowResult &sw, const WindowResult &lw, const EdgeShift &e, const double volTail, const int needBase, const int baseCount, const MixResult &mix) { Print("Recommendations:"); int printed = 0; if(mix.warning) { Print("- Treat this verdict as provisional. Analyze one symbol at a time with the " + "Symbol input"); printed++; } if(c.alarmIndex >= 0) { PrintFormat("- CUSUM crossed h at deal #%d. Stop adding risk and re-validate " + "on data since %s", c.alarmIndex + 1, DealDate(liveRows, c.startIndex)); printed++; } if(lw.tail < InpAlpha && sw.tail >= InpAlpha) { PrintFormat("- Do not read the last %d deals as a recovery. The last %d deals " + "still sit in the lower tail of the baseline", sw.size, lw.size); printed++; } //--- name the component that lost the most expectancy double worst = MathMin(e.hitRate, MathMin(e.winSize, e.lossSize)); if(e.total < 0.0 && worst < 0.0) { if(worst == e.hitRate) Print("- The win rate explains most of the lost expectancy. Check whether " + "the entry conditions still match the market"); else Print("- Trade sizes explain most of the lost expectancy. Review exits, " + "spreads and slippage"); printed++; } if(volTail < InpAlpha) { Print("- The live deviation per deal is higher than the baseline allows. The risk " + "per deal has changed, even if the mean has not"); printed++; } if(baseCount < needBase) { PrintFormat("- The baseline is too short to pin down its edge. Use at least %d " + "deals", needBase); printed++; } if(c.medianDelay > c.horizon) { PrintFormat("- A zero mean takes about %d deals to confirm. Extend the horizon " + "or accept slower detection", c.medianDelay); printed++; } if(printed == 0) Print("- No deviation detected by these statistics. This does not prove the edge " + "is intact. Keep monitoring"); }
Module 7: Demonstration Data
When no tester report exists yet, the script writes a reproducible demonstration pair, DemoBase.csv and DemoLive.csv, and analyzes it. The demonstration files have their own names, so they never mix with your real data. The baseline holds 500 deals at a fixed 0.10 lot. The live file holds 240 deals with lot sizes from 0.20 to 0.40. Winners follow an exponential distribution with a long right tail, and losers are bounded by the stop. After deal 100, the live win rate falls from 60% to 40%:
//+------------------------------------------------------------------+ //| Demonstration outcome per lot: exponential wins, bounded losses | //+------------------------------------------------------------------+ double DemoOutcome(const double pWin, const double winMean, const double lossLo, const double lossHi) { double u = RngUniform(); double v = RngUniform(); if(u < pWin) return(-winMean * MathLog(1.0 - v)); // winner with a long right tail return(-(lossLo + (lossHi - lossLo) * v)); // loser bounded by the stop } //+------------------------------------------------------------------+ //| Next demonstration close time, skipping weekends | //+------------------------------------------------------------------+ datetime DemoNextTime(datetime t) { t += (int)((6.0 + 42.0 * RngUniform()) * 3600.0); MqlDateTime st; TimeToStruct(t, st); while(st.day_of_week == 0 || st.day_of_week == 6) { t += 86400; TimeToStruct(t, st); } return(t); } //+------------------------------------------------------------------+ //| One demonstration CSV row | //+------------------------------------------------------------------+ string DemoRow(const datetime t, const double volume, const double perLot) { return(TimeToString(t, TIME_DATE | TIME_SECONDS) + ",EURUSD," + DoubleToString(volume, 2) + "," + DoubleToString(perLot * volume, 2) + "\r\n"); } //+------------------------------------------------------------------+ //| Write the demonstration files (live win rate drops at deal 101) | //+------------------------------------------------------------------+ bool WriteDemoFiles(const bool common) { //--- fixed seed: the demonstration is identical on every terminal RngSeed(20260926); int flags = FILE_WRITE | FILE_TXT | FILE_ANSI; if(common) flags |= FILE_COMMON; int hb = FileOpen(DEMO_BASE_FILE, flags); if(hb == INVALID_HANDLE) return(false); int hl = FileOpen(DEMO_LIVE_FILE, flags); if(hl == INVALID_HANDLE) { FileClose(hb); return(false); } FileWriteString(hb, "CloseTime,Symbol,Volume,NetProfit\r\n"); FileWriteString(hl, "CloseTime,Symbol,Volume,NetProfit\r\n"); //--- baseline: 500 deals at a fixed 0.10 lot, as a tester run would produce datetime t = D'2023.01.02 09:00'; for(int i = 0; i < 500; i++) { t = DemoNextTime(t); double x = DemoOutcome(0.60, 10.0, 5.0, 11.0); FileWriteString(hb, DemoRow(t, 0.10, x)); } //--- live: 240 deals with risk-based lots; the win rate falls after deal 100 t = D'2025.09.01 09:00'; for(int i = 0; i < 240; i++) { t = DemoNextTime(t); double x = (i < 100) ? DemoOutcome(0.60, 10.0, 5.0, 11.0) : DemoOutcome(0.40, 9.0, 5.0, 11.0); double volume = 0.20 + 0.01 * (int)(21.0 * RngUniform()); // 0.20-0.40 lot FileWriteString(hl, DemoRow(t, volume, x)); } FileClose(hb); FileClose(hl); return(true); }
Reading the Strategy Tester Report
The terminal saves the report as UTF-16 text. LoadReport() does not rely on its line breaks or on one row per line. ReadRowSegments() splits the whole file into <tr> segments, so a row that spans several lines, or several rows on one line, are both read correctly. RowCells() extracts every <td> cell and removes inner tags and entities with CleanCell(). In an English report, the header row of the Deals table maps the columns by name, so their order does not matter. In a translated report, the standard column order is used, and the script says so in the log.
The direction words of a translated report are learned from the data. The first deal of every report opens a position, so its direction word is taken as the word for "in", and every other direction closes volume. A closing deal with zero profit is therefore no longer mistaken for an entry. An English "in/out" deal closes only the open part of the symbol's net position. In a translated report, a reversal is approximated by its full volume.
The report has no position IDs, so an exit cannot be matched to its own entry. Each symbol therefore keeps a pool of open volume and unallocated entry commission. An exit takes a share of that commission in proportion to its closed volume. The result is exact when all open entries of a symbol pay the same commission per lot, which is the usual case. With overlapping positions that pay different rates, it is a volume-weighted average. On two real reports tested during development, a single-symbol report with 121 exits and a 12-symbol portfolio report with 5,894 exits, the net total read by the tool matched the report's own Total Net Profit to the cent:
//+------------------------------------------------------------------+ //| Text of a cell without inner tags, entities and extra spaces | //+------------------------------------------------------------------+ string CleanCell(string text) { string out = ""; bool inTag = false; for(int i = 0; i < StringLen(text); i++) { ushort ch = StringGetCharacter(text, i); if(ch == '<') inTag = true; else if(ch == '>') inTag = false; else if(!inTag) out += (ch == '\r' || ch == '\n' || ch == '\t') ? " " : ShortToString(ch); } StringReplace(out, " ", " "); StringReplace(out, "&", "&"); StringTrimLeft(out); StringTrimRight(out); return(out); } //+------------------------------------------------------------------+ //| Cells of one table row segment | //+------------------------------------------------------------------+ int RowCells(const string row, string &cells[]) { ArrayResize(cells, 0); int at = 0; while(true) { int open = StringFind(row, "<td", at); if(open < 0) break; int start = StringFind(row, ">", open); int close = (start < 0) ? -1 : StringFind(row, "</td>", start); if(close < 0) break; int n = ArraySize(cells); ArrayResize(cells, n + 1, 16); cells[n] = CleanCell(StringSubstr(row, start + 1, close - start - 1)); at = close + 5; } return(ArraySize(cells)); } //+------------------------------------------------------------------+ //| Split an HTML file into <tr> segments, whatever its line breaks | //+------------------------------------------------------------------+ int ReadRowSegments(const int handle, string &segments[]) { ArrayResize(segments, 0); string segment = ""; while(!FileIsEnding(handle)) { string line = FileReadString(handle); int at = StringFind(line, "<tr"); //--- a row may span several lines, and one line may hold several rows while(at >= 0) { segment += " " + StringSubstr(line, 0, at); int size = ArraySize(segments); ArrayResize(segments, size + 1, 4096); segments[size] = segment; segment = ""; line = StringSubstr(line, at + 3); at = StringFind(line, "<tr"); } segment += " " + line; } int n = ArraySize(segments); ArrayResize(segments, n + 1); segments[n] = segment; return(n + 1); } //+------------------------------------------------------------------+ //| Index of a symbol in a list, -1 if it is not there | //+------------------------------------------------------------------+ int FindSymbol(const string &list[], const string symbol) { for(int i = ArraySize(list) - 1; i >= 0; i--) if(list[i] == symbol) return(i); return(-1); } //+------------------------------------------------------------------+ //| Closing deals from the Deals table of a Strategy Tester report | //+------------------------------------------------------------------+ int LoadReport(const string fileName, const bool common, DealOutcome &rows[]) { ArrayResize(rows, 0); //--- the terminal saves HTML reports as UTF-16 text int flags = FILE_READ | FILE_TXT | FILE_UNICODE | FILE_SHARE_READ; if(common) flags |= FILE_COMMON; int handle = FileOpen(fileName, flags); if(handle == INVALID_HANDLE) return(-1); string segments[]; int total = ReadRowSegments(handle, segments); FileClose(handle); //--- default column order; an English header row overrides it by name int cTime = 0, cSym = 2, cType = 3, cDir = 4, cVol = 5, cCom = 8, cSwap = 9, cProf = 10; string inWord = "in", buyWord = "buy"; bool learned = false, headerSeen = false; string symbols[]; double openVol[], openCost[], netPos[]; int skipped = 0; for(int r = 0; r < total; r++) { string cells[]; int count = RowCells(segments[r], cells); //--- an English header names the columns, so their order no longer matters if(count >= REPORT_COLUMNS && cells[0] == "Time" && cells[2] == "Symbol") { for(int i = 0; i < count; i++) { if(cells[i] == "Symbol") cSym = i; if(cells[i] == "Type") cType = i; if(cells[i] == "Direction") cDir = i; if(cells[i] == "Volume") cVol = i; if(cells[i] == "Commission") cCom = i; if(cells[i] == "Swap") cSwap = i; if(cells[i] == "Profit") cProf = i; } headerSeen = true; continue; } if(count < REPORT_COLUMNS) continue; datetime t = StringToTime(cells[cTime]); if(t <= 0 || StringLen(cells[cTime]) < 19) continue; if(cells[cSym] == "") continue; // balance row double volume = 0.0, commission = 0.0, swap = 0.0, profit = 0.0; if(!ParseNumber(cells[cVol], volume) || !ParseNumber(cells[cCom], commission) || !ParseNumber(cells[cSwap], swap) || !ParseNumber(cells[cProf], profit) || volume <= 0.0) { skipped++; continue; } //--- a translated report: the first deal opens a position, so it names "in" if(!learned) { learned = true; if(cells[cDir] != "in" && cells[cDir] != "out" && cells[cDir] != "in/out") { inWord = cells[cDir]; buyWord = cells[cType]; } } string sym = cells[cSym]; int k = FindSymbol(symbols, sym); if(k < 0) { k = ArraySize(symbols); ArrayResize(symbols, k + 1, 16); ArrayResize(openVol, k + 1, 16); ArrayResize(openCost, k + 1, 16); ArrayResize(netPos, k + 1, 16); symbols[k] = sym; openVol[k] = 0.0; openCost[k] = 0.0; netPos[k] = 0.0; } double sign = (cells[cType] == buyWord) ? 1.0 : -1.0; //--- an entry adds volume and commission to the open pool of its symbol if(cells[cDir] == inWord) { openVol[k] += volume; openCost[k] += commission; netPos[k] += sign * volume; continue; } //--- every other direction closes volume; a reversal closes only the open part double closed = volume; if(cells[cDir] == "in/out" && MathAbs(netPos[k]) > 0.0) closed = MathMin(MathAbs(netPos[k]), volume); double fraction = closed / volume; double share = (openVol[k] > 0.0) ? openCost[k] * MathMin(1.0, closed / openVol[k]) : 0.0; double net = profit + swap + commission * fraction + share; openCost[k] -= share; openVol[k] = MathMax(0.0, openVol[k] - closed); netPos[k] += sign * volume; if(cells[cDir] == "in/out") { openVol[k] = volume - closed; openCost[k] = commission * (1.0 - fraction); } if(InpSymbol != "" && sym != InpSymbol) continue; int n = ArraySize(rows); ArrayResize(rows, n + 1, 1024); rows[n].time = t; rows[n].symbol = sym; rows[n].money = net; rows[n].value = InpPerLot ? net / closed : net; } if(!headerSeen) Print("Report header not recognized: using the standard column order"); if(skipped > 0) PrintFormat("%s: %d deal rows with unreadable numbers skipped", fileName, skipped); return(ArraySize(rows)); }
The next group of functions decides where the data comes from. The detector looks for files in two places: the common Files folder shared by all terminals, and the Files folder of the current terminal. With the default settings, FindNewestReport() picks the most recent ReportTester*.html file. That choice depends on the folder content: when a new report appears, the same inputs will analyze a different baseline. The script therefore prints the name and the save time of the chosen report, and entering that name in the Baseline file input keeps the choice fixed. When no report exists yet, the script prints the steps to create one and analyzes the demonstration data instead of failing with an error:
//+------------------------------------------------------------------+ //| Sort the rows by time and copy their outcomes into a plain array | //+------------------------------------------------------------------+ int SortAndCopy(DealOutcome &rows[], double &values[]) { SortByTime(rows); int n = ArraySize(rows); ArrayResize(values, n); for(int i = 0; i < n; i++) values[i] = rows[i].value; return(n); } //+------------------------------------------------------------------+ //| Full path of the common Files folder shared by all terminals | //+------------------------------------------------------------------+ string CommonFolder() { return(TerminalInfoString(TERMINAL_COMMONDATA_PATH) + "\\Files"); } //+------------------------------------------------------------------+ //| Full path of the Files folder of this terminal | //+------------------------------------------------------------------+ string LocalFolder() { return(TerminalInfoString(TERMINAL_DATA_PATH) + "\\MQL5\\Files"); } //+------------------------------------------------------------------+ //| Look for a file in the common folder first, then the local one | //+------------------------------------------------------------------+ bool LocateFile(const string fileName, bool &common) { common = true; if(FileIsExist(fileName, FILE_COMMON)) return(true); common = false; return(FileIsExist(fileName)); } //+------------------------------------------------------------------+ //| Newest ReportTester*.html in the common and the local folder | //+------------------------------------------------------------------+ bool FindNewestReport(string &fileName, bool &common, datetime &modified) { bool found = false; for(int pass = 0; pass < 2; pass++) { bool inCommon = (pass == 0); string name; long handle = FileFindFirst("ReportTester*.html", name, inCommon ? FILE_COMMON : 0); if(handle == INVALID_HANDLE) continue; do { datetime stamp = (datetime)FileGetInteger(name, FILE_MODIFY_DATE, inCommon); if(!found || stamp > modified) { found = true; modified = stamp; fileName = name; common = inCommon; } } while(FileFindNext(handle, name)); FileFindClose(handle); } return(found); } //+------------------------------------------------------------------+ //| Load the closing deals in [from, to) from the account history | //+------------------------------------------------------------------+ int LoadHistorySample(const datetime from, const datetime to, DealOutcome &rows[], double &values[]) { ClosedDeal deals[]; int n = CollectClosingDeals(InpMagic, InpSymbol, from, to, deals); if(n < 0) { PrintFormat("Cannot read the account history (error %d)", GetLastError()); return(-1); } ArrayResize(rows, 0); for(int i = 0; i < n; i++) { if(deals[i].volume <= 0.0) continue; int k = ArraySize(rows); ArrayResize(rows, k + 1, 1024); rows[k].time = deals[i].time; rows[k].symbol = deals[i].symbol; rows[k].money = deals[i].net; rows[k].value = InpPerLot ? deals[i].net / deals[i].volume : deals[i].net; } return(SortAndCopy(rows, values)); } //+------------------------------------------------------------------+ //| Load the baseline: 1 = loaded, 0 = file not found, -1 = error | //+------------------------------------------------------------------+ int LoadBaseline(DealOutcome &rows[], double &values[]) { //--- this account, before the live sample starts if(InpBaseSource == BASE_ACCOUNT_HISTORY) { if(InpLiveFrom <= 0) { Print("Set Live start date: the deals before that date form the baseline"); return(-1); } PrintFormat("Baseline: this account, magic %I64d, deals before %s", InpMagic, TimeToString(InpLiveFrom, TIME_DATE)); return((LoadHistorySample(0, InpLiveFrom, rows, values) >= 0) ? 1 : -1); } //--- a file: the named one, or the newest tester report string name = InpBaseFile; bool common = true; bool found = false; datetime modified = 0; bool automatic = (InpBaseSource == BASE_TESTER_REPORT && name == ""); if(automatic) found = FindNewestReport(name, common, modified); else { if(name == "") name = "EdgeBase.csv"; found = LocateFile(name, common); } if(!found) return(0); PrintFormat("Baseline: %s in %s", name, common ? CommonFolder() : LocalFolder()); //--- an automatic choice depends on the folder content, so say which file was taken if(automatic) PrintFormat("Chosen automatically as the newest report (saved %s). Enter its name " + "in Baseline file to keep the choice fixed", TimeToString(modified, TIME_DATE | TIME_MINUTES)); int n = (InpBaseSource == BASE_TESTER_REPORT) ? LoadReport(name, common, rows) : LoadOutcomes(name, common, InpPerLot, rows); if(n < 0) { PrintFormat("Cannot open %s (error %d)", name, GetLastError()); return(-1); } SortAndCopy(rows, values); return(1); } //+------------------------------------------------------------------+ //| Load the live sample from this account or from a CSV file | //+------------------------------------------------------------------+ bool LoadLive(DealOutcome &rows[], double &values[]) { if(InpLiveSource == LIVE_ACCOUNT_HISTORY) { PrintFormat("Live: this account, magic %I64d, deals from %s", InpMagic, (InpLiveFrom > 0) ? TimeToString(InpLiveFrom, TIME_DATE) : "the first one"); return(LoadHistorySample(InpLiveFrom, 0, rows, values) >= 0); } bool common = true; if(!LocateFile(InpLiveFile, common)) { PrintFormat("%s was not found in %s or in %s", InpLiveFile, CommonFolder(), LocalFolder()); Print("Run DriftExport.mq5 on the live terminal, or set Live source to this account"); return(false); } PrintFormat("Live: %s in %s", InpLiveFile, common ? CommonFolder() : LocalFolder()); if(LoadOutcomes(InpLiveFile, common, InpPerLot, rows) < 0) { PrintFormat("Cannot open %s (error %d)", InpLiveFile, GetLastError()); return(false); } SortAndCopy(rows, values); return(true); } //+------------------------------------------------------------------+ //| Explain how to create the baseline from a tester report | //+------------------------------------------------------------------+ void PrintReportHelp() { PrintFormat("No Strategy Tester report (ReportTester*.html) found in %s", CommonFolder()); Print("To analyze your Expert Advisor:"); Print("1. Backtest the Expert Advisor in the Strategy Tester"); Print("2. Right-click the Backtest results, choose Report and save it as HTML"); PrintFormat("3. Save the report in %s", CommonFolder()); Print("4. Run EdgeDrift on the live account with the Expert Advisor's magic number"); Print("Until then, EdgeDrift analyzes its demonstration data"); } //+------------------------------------------------------------------+ //| Write and load the demonstration pair | //+------------------------------------------------------------------+ bool LoadDemo(DealOutcome &baseRows[], double &base[], DealOutcome &liveRows[], double &live[]) { //--- demonstration files have their own names and never mix with real data if(!WriteDemoFiles(true)) { Print("Cannot write the demonstration files"); return(false); } PrintFormat("Source: %s (baseline) and %s (live)", DEMO_BASE_FILE, DEMO_LIVE_FILE); if(LoadOutcomes(DEMO_BASE_FILE, true, InpPerLot, baseRows) < 0 || LoadOutcomes(DEMO_LIVE_FILE, true, InpPerLot, liveRows) < 0) return(false); SortAndCopy(baseRows, base); SortAndCopy(liveRows, live); return(true); } //+------------------------------------------------------------------+ //| Load the baseline and the live sample from the chosen sources | //+------------------------------------------------------------------+ bool LoadSamples(DealOutcome &baseRows[], double &base[], DealOutcome &liveRows[], double &live[]) { int status = LoadBaseline(baseRows, base); if(status < 0) return(false); if(status == 0) { //--- no tester report yet: show the steps, then the demonstration if(InpBaseSource == BASE_TESTER_REPORT) { PrintReportHelp(); return(LoadDemo(baseRows, base, liveRows, live)); } PrintFormat("%s was not found in %s or in %s", (InpBaseFile == "") ? "EdgeBase.csv" : InpBaseFile, CommonFolder(), LocalFolder()); Print("Set Baseline source to Strategy Tester report to read a backtest directly"); return(false); } return(LoadLive(liveRows, live)); }
The Main Script: Putting It All Together
Before any statistic runs, CheckSymbolMix() compares the composition of both samples. It does so twice: by the number of deals, and by the absolute result in account currency. The second measure gives each symbol its economic weight, so a symbol with few but large deals counts as much as it should. Each mix difference is half the sum of the absolute differences between the shares of each symbol, which is the total variation distance:
MixGap = 0.5 * Sum( |BaseShare_s - LiveShare_s| )A value of 0% means the same mix, and 100% means that the two samples share no symbol at all. Neither measure captures differences in volatility or contract value inside a symbol. If either measure exceeds InpMaxMixGap (20% by default), the script prints the three symbols with the largest share differences. It also prints the share of live deals on symbols the baseline never traded. It then stops, because the CUSUM, the windows, and the score would look complete while comparing different instruments. The input InpAllowMixed overrides the stop, and the verdict is then marked as provisional:
//+------------------------------------------------------------------+ //| Deal count and absolute result per symbol in one sample | //+------------------------------------------------------------------+ void ShareBySymbol(const DealOutcome &rows[], string &names[], double &counts[], double &weights[]) { ArrayResize(names, 0); ArrayResize(counts, 0); ArrayResize(weights, 0); for(int i = 0; i < ArraySize(rows); i++) { int k = FindSymbol(names, rows[i].symbol); if(k < 0) { k = ArraySize(names); ArrayResize(names, k + 1, 16); ArrayResize(counts, k + 1, 16); ArrayResize(weights, k + 1, 16); names[k] = rows[i].symbol; counts[k] = 0.0; weights[k] = 0.0; } counts[k] += 1.0; weights[k] += MathAbs(rows[i].money); // economic weight in account currency } } //+------------------------------------------------------------------+ //| Share of one symbol in a list, 0 if the symbol is not there | //+------------------------------------------------------------------+ double ShareOf(const string &names[], const double &values[], const string symbol) { double total = 0.0; for(int i = 0; i < ArraySize(values); i++) total += values[i]; int k = FindSymbol(names, symbol); return((k >= 0 && total > 0.0) ? values[k] / total : 0.0); } //+------------------------------------------------------------------+ //| Compare the symbol mix of both samples and print the result | //+------------------------------------------------------------------+ void CheckSymbolMix(const DealOutcome &baseRows[], const DealOutcome &liveRows[], MixResult &mix) { string baseNames[], liveNames[]; double baseCounts[], liveCounts[], baseWeights[], liveWeights[]; ShareBySymbol(baseRows, baseNames, baseCounts, baseWeights); ShareBySymbol(liveRows, liveNames, liveCounts, liveWeights); mix.baseSymbols = ArraySize(baseNames); mix.liveSymbols = ArraySize(liveNames); mix.shared = 0; mix.countGap = 0.0; mix.weightGap = 0.0; mix.unmatched = 0.0; //--- the union of symbols of both samples string names[]; ArrayCopy(names, baseNames); for(int i = 0; i < mix.liveSymbols; i++) { if(FindSymbol(baseNames, liveNames[i]) >= 0) { mix.shared++; continue; } int size = ArraySize(names); ArrayResize(names, size + 1, 16); names[size] = liveNames[i]; mix.unmatched += ShareOf(liveNames, liveCounts, liveNames[i]); } //--- total variation distance, by deal count and by absolute result for(int i = 0; i < ArraySize(names); i++) { mix.countGap += MathAbs(ShareOf(baseNames, baseCounts, names[i]) - ShareOf(liveNames, liveCounts, names[i])); mix.weightGap += MathAbs(ShareOf(baseNames, baseWeights, names[i]) - ShareOf(liveNames, liveWeights, names[i])); } mix.countGap *= 0.5; mix.weightGap *= 0.5; mix.warning = (MathMax(mix.countGap, mix.weightGap) > InpMaxMixGap); Print("--- Symbol mix check ---"); PrintFormat("Symbols: %d in the baseline, %d live, %d shared", mix.baseSymbols, mix.liveSymbols, mix.shared); PrintFormat("Mix difference: %.1f%% by deals, %.1f%% by absolute result", mix.countGap * 100.0, mix.weightGap * 100.0); if(!mix.warning) return; Print("WARNING: the symbol mix differs, so per-lot results are not comparable"); //--- the three symbols whose deal shares differ the most double shown[]; ArrayResize(shown, ArraySize(names)); ArrayInitialize(shown, 0.0); for(int round = 0; round < 3; round++) { int best = -1; double bestDiff = 0.01; for(int i = 0; i < ArraySize(names); i++) { double diff = MathAbs(ShareOf(baseNames, baseCounts, names[i]) - ShareOf(liveNames, liveCounts, names[i])); if(shown[i] == 0.0 && diff > bestDiff) { bestDiff = diff; best = i; } } if(best < 0) break; shown[best] = 1.0; PrintFormat("%s: %.1f%% of baseline deals, %.1f%% of live deals", names[best], ShareOf(baseNames, baseCounts, names[best]) * 100.0, ShareOf(liveNames, liveCounts, names[best]) * 100.0); } if(mix.unmatched > 0.0) PrintFormat("Live deals on symbols missing from the baseline: %.1f%%", mix.unmatched * 100.0); Print("Set Symbol to analyze one instrument, or use a report of the same instruments"); }
OnStart() validates the inputs and loads both samples. It refuses to continue in three cases: too few baseline deals, too few live deals, or a baseline with no positive edge to monitor. The minimum sizes (100 and 20) are only technical floors. The sizes that matter statistically come from the data. The baseline quality block prints the baseline size needed to pin down the edge. The detection delay shows how many live deals the detector needs. All bootstrap steps draw from one generator seeded with InpSeed, in a fixed order, which keeps the report reproducible:
//+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { Print("=== Edge Drift Detector 1.30 ==="); //--- sanity checks on the inputs if(InpAlpha <= 0.0 || InpAlpha >= 1.0 || InpBootstrap < 100 || InpShortWindow < 1 || InpLongWindow < 1 || InpHealthyTail <= 0.0 || InpMaxDelay < 1 || InpRefFraction <= 0.0 || InpRefFraction >= 1.0 || InpBreakeven < 0.0) { Print("Invalid inputs: check alpha, windows, resamples, k and the breakeven band"); return; } //--- load both samples from the chosen sources DealOutcome baseRows[], liveRows[]; double base[], live[]; if(!LoadSamples(baseRows, base, liveRows, live)) return; int n = ArraySize(base); int m = ArraySize(live); if(n < InpMinBaseline || m < InpMinLive) { PrintFormat("Too few deals: baseline %d (minimum %d), live %d (minimum %d)", n, InpMinBaseline, m, InpMinLive); //--- tell the reader which setting moves the counts in the right direction if(InpBaseSource == BASE_ACCOUNT_HISTORY) Print("Move the start of the live sample to balance the two samples, or build " + "the baseline from a backtest"); else Print("Run a longer backtest for the baseline, or wait for more live deals"); return; } EdgeProfile pb, pl; BuildProfile(base, pb); BuildProfile(live, pl); if(pb.mean <= 0.0 || pb.stdev <= 0.0) { Print("The baseline shows no positive edge, so there is nothing to monitor"); return; } PrintFormat("Unit: net result %s, one row per closing deal", InpPerLot ? "per 1.00 lot" : "in account currency"); PrintFormat("Baseline: %d closing deals, %s to %s", n, DealDate(baseRows, 0), DealDate(baseRows, n - 1)); PrintFormat("Live: %d closing deals, %s to %s", m, DealDate(liveRows, 0), DealDate(liveRows, m - 1)); //--- the symbol mix comes first: without a comparable mix, nothing below is meaningful MixResult mix; CheckSymbolMix(baseRows, liveRows, mix); if(mix.warning && !InpAllowMixed) { Print("Analysis stopped. Set Run when the mix differs to see the statistics"); return; } Print("--- Baseline versus live ---"); PrintFormat("Mean per deal: %.2f -> %.2f", pb.mean, pl.mean); PrintFormat("Std deviation: %.2f -> %.2f", pb.stdev, pl.stdev); PrintFormat("Win rate: %.1f%% -> %.1f%%", pb.winRate * 100.0, pl.winRate * 100.0); PrintFormat("Breakeven deals: %d -> %d", pb.breakevens, pl.breakevens); PrintFormat("Average win: %.2f -> %.2f", pb.avgWin, pl.avgWin); PrintFormat("Average loss: %.2f -> %.2f", pb.avgLoss, pl.avgLoss); PrintFormat("Deals per 30 days: %.1f -> %.1f", DealsPerMonth(baseRows), DealsPerMonth(liveRows)); //--- how well the baseline itself pins down the edge Print("--- Baseline quality ---"); double se = pb.stdev / MathSqrt(n); double ratio = pb.mean / pb.stdev; int needBase = (int)MathCeil(4.0 / (ratio * ratio)); PrintFormat("Edge %.2f, 95%% interval %.2f to %.2f, t = %.2f", pb.mean, pb.mean - 1.96 * se, pb.mean + 1.96 * se, pb.mean / se); PrintFormat("Edge-to-noise ratio %.3f: a t of 2 needs at least %d baseline deals", ratio, needBase); double band = 2.0 / MathSqrt(n); double rhoBase = LagOneCorrelation(base, pb.mean); PrintFormat("Lag-1 autocorrelation: baseline %.3f (band +/-%.3f), live %.3f (band +/-%.3f)", rhoBase, band, LagOneCorrelation(live, pl.mean), 2.0 / MathSqrt(m)); if(MathAbs(rhoBase) > band) Print("The baseline deals look dependent. Set Block size above 1"); //--- CUSUM detector; every bootstrap step below draws from one seeded generator CusumResult c; c.reference = InpRefFraction * pb.mean; c.horizon = (InpHorizon > m) ? InpHorizon : m; RngSeed(InpSeed); double maxima[]; c.threshold = CalibrateThreshold(base, pb, c.reference, c.horizon, InpBootstrap, InpAlpha, maxima); RunCusum(live, maxima, pb, c); //--- two-horizon check, win-rate test, detection delay and volatility test WindowResult sw, lw; WindowTest(base, live, InpShortWindow, InpBootstrap, sw); WindowTest(base, live, InpLongWindow, InpBootstrap, lw); double winTail = WinRateTail(base, pl, InpBootstrap); c.medianDelay = MedianDetectionDelay(base, pb, c, InpBootstrap, InpMaxDelay); double volTail = VolatilityTail(base, pl, InpBootstrap); EdgeShift e; DecomposeShift(pb, pl, e); DriftScore d; ScoreDrift(c, sw, lw, winTail, d); //--- report Print("--- CUSUM shortfall detector ---"); PrintFormat("Reference k = %.2f (%.2f of the baseline edge), horizon %d deals", c.reference, InpRefFraction, c.horizon); PrintFormat("Threshold h = %.2f (%.0f%% of %d resampled baseline paths stay below it, " + "block size %d)", c.threshold, (1.0 - InpAlpha) * 100.0, InpBootstrap, InpBlockSize); PrintFormat("Peak %.2f at deal #%d (%s), tail probability %.2f%%", c.peak, c.peakIndex + 1, DealDate(liveRows, c.peakIndex), c.peakTail * 100.0); if(c.alarmIndex >= 0) PrintFormat("ALARM at deal #%d (%s)", c.alarmIndex + 1, DealDate(liveRows, c.alarmIndex)); else Print("No alarm within the live sample"); if(c.startIndex > 0) PrintFormat("Estimated change point: after deal #%d (%s)", c.startIndex, DealDate(liveRows, c.startIndex - 1)); PrintFormat("Median delay if the mean shifts to zero: %d deals", c.medianDelay); Print("--- Two-horizon check ---"); PrintFormat("Short window (last %d): mean %.2f, tail probability %.2f%%", sw.size, sw.mean, sw.tail * 100.0); PrintFormat("Long window (last %d): mean %.2f, tail probability %.2f%%", lw.size, lw.mean, lw.tail * 100.0); PrintFormat("Reading: %s", HorizonReading(sw, lw)); Print("--- Where did the edge go? ---"); PrintFormat("Change in expectancy: %+.2f per deal", e.total); PrintFormat("Win/loss-rate effect: %+.2f", e.hitRate); PrintFormat("Win-size effect: %+.2f", e.winSize); PrintFormat("Loss-size effect: %+.2f", e.lossSize); if(pb.breakevens + pl.breakevens > 0) PrintFormat("Breakeven effect: %+.2f", e.breakeven); PrintFormat("Win-rate tail probability: %.2f%%", winTail * 100.0); PrintFormat("Std deviation tail probability: %.2f%%", volTail * 100.0); Print("--- Edge Health Score (heuristic, not a validated quality metric) ---"); PrintFormat("CUSUM %.1f | Long %.1f | Short %.1f | Win rate %.1f", d.cusumScore, d.longScore, d.shortScore, d.winRateScore); PrintFormat("Edge Health Score: %.1f / 100 -> band %s", d.total, d.grade); PrintFormat("Verdict: %s%s", d.verdict, mix.warning ? " (provisional: the symbol mix differs)" : ""); PrintRecommendations(liveRows, c, sw, lw, e, volTail, needBase, n, mix); }
Running It: Sample Report
The first run happens before any tester report exists. EdgeDrift.mq5 then prints the steps to create one, followed by the report for the built-in demonstration data:
=== Edge Drift Detector 1.30 === No Strategy Tester report (ReportTester*.html) found in C:\Users\User\AppData\Roaming\MetaQuotes\Terminal\Common\Files To analyze your Expert Advisor: 1. Backtest the Expert Advisor in the Strategy Tester 2. Right-click the Backtest results, choose Report and save it as HTML 3. Save the report in C:\Users\User\AppData\Roaming\MetaQuotes\Terminal\Common\Files 4. Run EdgeDrift on the live account with the Expert Advisor's magic number Until then, EdgeDrift analyzes its demonstration data Source: DemoBase.csv (baseline) and DemoLive.csv (live) Unit: net result per 1.00 lot, one row per closing deal Baseline: 500 closing deals, 2023.01.04 to 2025.01.31 Live: 240 closing deals, 2025.09.03 to 2026.09.15 --- Symbol mix check --- Symbols: 1 in the baseline, 1 live, 1 shared Mix difference: 0.0% by deals, 0.0% by absolute result --- Baseline versus live --- Mean per deal: 1.67 -> 1.31 Std deviation: 11.39 -> 12.43 Win rate: 55.2% -> 49.2% Breakeven deals: 1 -> 0 Average win: 9.55 -> 11.05 Average loss: 8.07 -> 8.12 Deals per 30 days: 19.8 -> 19.1 --- Baseline quality --- Edge 1.67, 95% interval 0.67 to 2.67, t = 3.29 Edge-to-noise ratio 0.147: a t of 2 needs at least 186 baseline deals Lag-1 autocorrelation: baseline 0.044 (band +/-0.089), live 0.028 (band +/-0.129) --- CUSUM shortfall detector --- Reference k = 0.84 (0.50 of the baseline edge), horizon 250 deals Threshold h = 21.52 (95% of 2000 resampled baseline paths stay below it, block size 1) Peak 25.16 at deal #223 (2026.08.17), tail probability 1.95% ALARM at deal #213 (2026.07.29) Estimated change point: after deal #105 (2026.02.19) Median delay if the mean shifts to zero: 182 deals --- Two-horizon check --- Short window (last 20): mean 1.74, tail probability 54.52% Long window (last 100): mean -0.60, tail probability 1.85% Reading: Long window in the lower tail, short window not. Recent deals look normal, the longer average is still low --- Where did the edge go? --- Change in expectancy: -0.36 per deal Win/loss-rate effect: -1.08 Win-size effect: +0.74 Loss-size effect: -0.02 Breakeven effect: +0.00 Win-rate tail probability: 3.85% Std deviation tail probability: 8.80% --- Edge Health Score (heuristic, not a validated quality metric) --- CUSUM 9.7 | Long 9.2 | Short 100.0 | Win rate 19.2 Edge Health Score: 25.1 / 100 -> band F Verdict: SUSPEND Recommendations: - CUSUM crossed h at deal #213. Stop adding risk and re-validate on data since 2026.02.20 - Do not read the last 20 deals as a recovery. The last 100 deals still sit in the lower tail of the baseline - The win rate explains most of the lost expectancy. Check whether the entry conditions still match the market
Fig. 2. Edge Drift Detector report for the demonstration data.
Interpreting the Report
Start with the checks that decide whether the comparison is meaningful at all. The demonstration trades a single symbol, so both mix differences are 0.0%. The baseline edge of 1.67 has a 95% interval of 0.67 to 2.67 and a t of 3.29. Its 500 deals exceed the 186 deals needed for a t of 2. Both lag-1 autocorrelations stay inside their bands, so the independent resampling is a reasonable choice. The trade frequency is similar in both samples (19.8 and 19.1 deals per 30 days).
The CUSUM crossed its threshold of 21.52 at deal #213. Only 1.95% of the resampled baseline paths reach the observed peak of 25.16 within 250 deals. Under the model assumptions, a live mean at or above the reference is therefore an unlikely explanation. Fig. 3 plots the statistic. It hovers near zero for the first 105 deals and then climbs almost without interruption.

Fig. 3. CUSUM statistic of the demonstration live deals against the bootstrap threshold.
The estimated change point is deal #105, while the demonstration data changes after deal 100. The first deals after a change can still be winners by chance, so the estimate lags by a few deals. In practice, this date is a good starting point for a re-validation: rerun the backtest on the market data since that date.
The two windows show why the last deals look reassuring. The short window of 20 deals sits at the 54.52% tail probability, which is common for the baseline. The long window of 100 deals sits at 1.85%. The recent deals look normal, while the longer average is still low. The windows describe the recent results; they cannot say whether a low average is temporary or permanent. The four combinations read as follows:
- Neither window in the lower tail: these statistics detected no deviation. This does not show that the edge is intact.
- Short window in the lower tail, long window not: a recent dip that has not yet moved the longer average.
- Long window in the lower tail, short window not: the recent deals look normal, but the longer average is still low. Do not read the recent deals as a return of the edge.
- Both windows in the lower tail: the low average spans both horizons.
The decomposition answers the question "where did the expectancy go?". It fell by only 0.36 per deal overall. Behind that small number, the lower win rate cost 1.08 per deal, and larger average wins returned 0.74. The larger wins masked the damage. The recommendation therefore points at the entry conditions rather than at exits or execution costs. The baseline contains one breakeven deal, which now counts neither as a win nor as a loss, and its effect on the expectancy is zero. The live standard deviation sits at the 8.80% tail probability, so the risk per deal has not changed in a way the test can detect.
The detection delay is the most useful number for planning. With an edge-to-noise ratio of 0.147, a uniform shift of the mean to zero needs a median of 182 deals to trigger the alarm. That is the honest answer to the hope of knowing within 10–15 trades. For a strategy with a small edge relative to its noise, early certainty is not available, and the tool states how long the wait is in this scenario. If the median delay exceeds the planned horizon, the tool recommends a longer horizon or slower detection.
The demonstration was built around one type of degradation, a drop in the win rate. It shows how to read the report. It does not show that the detector performs equally well on other types of degradation, such as larger losses, higher volatility, or more dependent trades.
An early alarm says that the live deals differ from the baseline. It does not say why. An overfitted in-sample baseline is one possible cause. A change in the market, different execution costs, a different symbol mix, or an error in the data can produce the same alarm. In a test on a real 12-symbol portfolio report, the live account traded a different mix of instruments. Its per-lot standard deviation came out more than twice as large as in the baseline, and the CUSUM raised an alarm. Such a comparison mixes composition with performance, which is why version 1.30 stops before computing the statistics.
The Edge Health Score of 25.1 (band F) summarizes the same evidence. The CUSUM and the long window score below 10, and the win rate scores 19.2. Only the short window earns full marks.
Reading the Live Deals
The live sample comes from the account history. CollectClosingDeals() reads the whole history in one chronological pass and keeps an open state for every position. On a netting account, one symbol holds one position, so the state is keyed by symbol. On a hedging account, it is keyed by the position ID. An entry adds its volume, commission, and fee to the state. A closing deal (OUT, OUT_BY, or INOUT) takes a share of the open entry cost in proportion to the volume it closes. A reversal closes the open volume, and the rest of its volume opens a new state with the rest of its commission. Balance, credit, bonus, and correction rows are excluded because only DEAL_TYPE_BUY and DEAL_TYPE_SELL pass the filter.
The magic filter uses the magic number of the closing deal itself. A closing deal with magic 0, such as a manual close, falls back to the magic of the deal that opened the state. On a netting account where two Expert Advisors trade the same symbol, a manual close is attributed to the Expert Advisor that opened the current position:
//+------------------------------------------------------------------+ //| Index of an open state by key, -1 if it is not there | //+------------------------------------------------------------------+ int FindState(const OpenState &states[], const string key) { //--- search from the end: exits usually follow their entries closely for(int i = ArraySize(states) - 1; i >= 0; i--) if(states[i].key == key) return(i); return(-1); } //+------------------------------------------------------------------+ //| Collect closing deals in [from, to); to = 0 means no limit | //+------------------------------------------------------------------+ int CollectClosingDeals(const long magic, const string symbol, const datetime from, const datetime to, ClosedDeal &deals[]) { ArrayResize(deals, 0); if(!HistorySelect(0, TimeCurrent())) return(-1); //--- netting keeps one position per symbol, hedging one per position ID long mode = AccountInfoInteger(ACCOUNT_MARGIN_MODE); bool netting = (mode != ACCOUNT_MARGIN_MODE_RETAIL_HEDGING); OpenState states[]; int total = HistoryDealsTotal(); //--- one chronological pass over the whole history keeps every state exact for(int i = 0; i < total; i++) { ulong ticket = HistoryDealGetTicket(i); if(ticket == 0) continue; //--- trades only: balance, credit, bonus and correction rows are excluded long type = HistoryDealGetInteger(ticket, DEAL_TYPE); if(type != DEAL_TYPE_BUY && type != DEAL_TYPE_SELL) continue; long entry = HistoryDealGetInteger(ticket, DEAL_ENTRY); string sym = HistoryDealGetString(ticket, DEAL_SYMBOL); string key = netting ? sym : IntegerToString(HistoryDealGetInteger(ticket, DEAL_POSITION_ID)); double volume = HistoryDealGetDouble(ticket, DEAL_VOLUME); double cost = HistoryDealGetDouble(ticket, DEAL_COMMISSION) + HistoryDealGetDouble(ticket, DEAL_FEE); int k = FindState(states, key); if(k < 0) { k = ArraySize(states); ArrayResize(states, k + 1, 256); states[k].key = key; states[k].magic = HistoryDealGetInteger(ticket, DEAL_MAGIC); states[k].volume = 0.0; states[k].cost = 0.0; } //--- an entry only adds volume and cost to its state if(entry == DEAL_ENTRY_IN) { if(states[k].volume <= 0.0) states[k].magic = HistoryDealGetInteger(ticket, DEAL_MAGIC); states[k].volume += volume; states[k].cost += cost; continue; } if(entry != DEAL_ENTRY_OUT && entry != DEAL_ENTRY_OUT_BY && entry != DEAL_ENTRY_INOUT) continue; //--- a reversal (INOUT) closes the open volume and opens the rest double closed = volume; if(entry == DEAL_ENTRY_INOUT && states[k].volume > 0.0) closed = MathMin(states[k].volume, volume); double fraction = (volume > 0.0) ? closed / volume : 1.0; double share = (states[k].volume > 0.0) ? states[k].cost * MathMin(1.0, closed / states[k].volume) : 0.0; double net = HistoryDealGetDouble(ticket, DEAL_PROFIT) + HistoryDealGetDouble(ticket, DEAL_SWAP) + cost * fraction + share; //--- the deal's own magic wins; manual exits (magic 0) fall back to the state long dealMagic = HistoryDealGetInteger(ticket, DEAL_MAGIC); long owner = (dealMagic != 0) ? dealMagic : states[k].magic; states[k].cost -= share; states[k].volume = MathMax(0.0, states[k].volume - closed); if(entry == DEAL_ENTRY_INOUT) { states[k].volume = volume - closed; states[k].cost = cost * (1.0 - fraction); states[k].magic = dealMagic; } datetime t = (datetime)HistoryDealGetInteger(ticket, DEAL_TIME); if(t < from || (to > 0 && t >= to) || (symbol != "" && sym != symbol)) continue; if(magic != 0 && owner != magic) continue; int n = ArraySize(deals); ArrayResize(deals, n + 1, 1024); deals[n].time = t; deals[n].symbol = sym; deals[n].volume = closed; deals[n].net = net; } return(ArraySize(deals)); }
When the Expert Advisor trades on another terminal, for example on a VPS, run DriftExport.mq5 there with the magic number of the Expert Advisor. It contains identical copies of FindState() and CollectClosingDeals(), writes the same deals to EdgeLive.csv, and prints the full path of the folder it wrote to. Copy that file to the common Files folder of the terminal where you run the detector, and set Live source to CSV file:
//+------------------------------------------------------------------+ //| Write closing deals to the CSV format read by EdgeDrift.mq5 | //+------------------------------------------------------------------+ int ExportClosingDeals(const string fileName, const long magic = 0, const string symbol = "", const datetime from = 0, const bool common = true) { ClosedDeal deals[]; int n = CollectClosingDeals(magic, symbol, from, 0, deals); if(n < 0) return(-1); int flags = FILE_WRITE | FILE_TXT | FILE_ANSI; if(common) flags |= FILE_COMMON; int handle = FileOpen(fileName, flags); if(handle == INVALID_HANDLE) return(-1); FileWriteString(handle, "CloseTime,Symbol,Volume,NetProfit\r\n"); for(int i = 0; i < n; i++) FileWriteString(handle, TimeToString(deals[i].time, TIME_DATE | TIME_SECONDS) + "," + deals[i].symbol + "," + DoubleToString(deals[i].volume, 2) + "," + DoubleToString(deals[i].net, 2) + "\r\n"); FileClose(handle); return(n); } //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { int rows = ExportClosingDeals(InpFileName, InpMagic, InpSymbol, InpFrom, InpCommon); if(rows < 0) { PrintFormat("Export failed (error %d)", GetLastError()); return; } string folder = InpCommon ? TerminalInfoString(TERMINAL_COMMONDATA_PATH) + "\\Files" : TerminalInfoString(TERMINAL_DATA_PATH) + "\\MQL5\\Files"; PrintFormat("%d closing deals written to %s in %s", rows, InpFileName, folder); }
Practical Workflow
The routine needs no code changes and takes a few minutes per week:
- Backtest the Expert Advisor in the Strategy Tester, preferably on out-of-sample or forward data, with the settings you trade live.
- Right-click the Backtest results, choose Report, and save it as HTML in the common Files folder. The script prints the exact path of that folder on its first run.
- Enter the report name in Baseline file, so later reports do not change the baseline.
- Decide on the monitoring horizon H, for example 250 deals, and keep it fixed.
- Each week, run EdgeDrift.mq5 on the live account with the magic number of the Expert Advisor. Check the mix and the baseline quality first, then read the verdict, the windows, and the decomposition.
- Act on the verdict: keep trading on CONTINUE, review on WATCH, cut the risk on REDUCE, and stop and re-validate on SUSPEND.
A second route needs no backtest at all. It suits an Expert Advisor that already has a trusted period on the same account, such as a forward test or its first months of live trading. Set Baseline source to account history and choose the live start date. Deals closed before that date form the baseline, and deals closed after it form the live sample.
The first line of every report shows the version of the detector. After replacing the files, compile the scripts (F7) and check that line. If it shows an older version, the terminal is still running a previous build.
Applicability and Limitations
Every probability in the report is conditional on three assumptions. The live deals are independent, they come from a stationary distribution, and that distribution is the one the baseline represents. The tool checks what it can. It reports the precision of the baseline edge, the lag-1 autocorrelation of both samples, the symbol mix, the trade frequency, and the deviation per deal. It cannot check whether the baseline period represents the live market.
The quality of the baseline matters most. An in-sample optimization usually overstates the edge, and a discrepancy with such a baseline is expected even when the strategy works. A walk-forward or forward-test sample is the better baseline. A baseline taken from the account history has the opposite trade-off. It already includes real spreads, slippage, and commissions, but it usually contains fewer deals than a backtest. The bootstrap uses the baseline mean and deviation as if they were exact. The reported interval of the edge shows how far that is from the truth. With a short baseline, the threshold inherits that uncertainty.
Partial exits of one position count as separate observations. Strategies whose deals depend on each other, such as grids, martingales, or baskets closed together, violate the independence assumption even when the lag-1 autocorrelation looks small. For them, a block size of several deals is the safer setting, and the results should still be read with care. Per-lot normalization removes the effect of position size, but not the effect of changing risk per lot. A strategy whose stop distance varies strongly with volatility is better measured in R-multiples.
The tool complements walk-forward analysis and Monte Carlo testing rather than replacing them. Those methods validate a strategy before it goes live. The Edge Drift Detector checks, after the fact, whether the live account still behaves like the strategy that was validated.
Future Work
Four extensions would make the tool more complete:
- R-multiple normalization, dividing each outcome by the initial risk of the position instead of its volume.
- A variance CUSUM running alongside the mean detector, to monitor a change in risk over time instead of in a single look.
- An option to aggregate the partial exits of one position into a single observation.
- A live version inside an Expert Advisor that updates the statistic from OnTradeTransaction() and stops opening new positions when the alarm fires.
Conclusion
The question from the introduction was whether a live Expert Advisor is still the strategy that was tested. An equity curve cannot answer it, and neither can the last few deals. In the demonstration, the live account stayed ahead of the backtest expectation, and its last 20 deals looked better than the baseline. At the same time, the edge had been gone for more than 100 deals.
The Edge Drift Detector turns the question into a test against your own backtest, under stated assumptions. After each run, you get:
- A CUSUM alarm whose threshold is calibrated on your own deals, with a false-alarm rate that holds within the model.
- The estimated deal and date of the change point, the starting point for any re-validation.
- A description of the recent average in a short and a long window.
- A decomposition that shows whether the win rate, the size of wins, the size of losses, or breakeven deals changed the expectancy.
- Checks of the baseline precision, the independence of deals, the deviation, the trade frequency, and the symbol mix.
- The number of deals the detector needs when the mean shifts to zero, which tells you how long certainty will take.
- A heuristic Edge Health Score and a verdict of CONTINUE, WATCH, REDUCE, or SUSPEND.
Read the verdict as a decision rule, the change point as the date to re-validate from, and the detection delay as the patience your strategy requires. The source code is available in the MQL5 CodeBase: Edge Drift Detector in the MQL5 CodeBase.
The following table describes the source files that accompany the article.
| File Name | Description |
|---|---|
| EdgeDrift | The main script. It reads the baseline from a Strategy Tester report and the live deals from the account. It then runs the CUSUM detector, the window tests, and the expectancy decomposition, and prints the Edge Health Score with a verdict and recommendations. |
| DriftExport | The optional companion script. It exports the closing deals of a live account to the CSV format used by the main script, for example when the account runs on a VPS. |
| MQL5.zip | An archive whose root is the MQL5 folder, so it unpacks directly into the terminal data folder with every file in its correct place. Both scripts sit in the MQL5\Scripts\EdgeDrift folder, ready to compile without moving anything. |
- MetaQuotes, MQL5 Reference: File Functions, MQL5 Documentation;
- MetaQuotes, MQL5 Reference: Trade Functions, MQL5 Documentation;
- E. S. Page, Continuous Inspection Schemes, Biometrika, 41(1-2), 1954 (origin of the CUSUM statistic).
- B. Efron, Bootstrap Methods: Another Look at the Jackknife, The Annals of Statistics, 7(1), 1979 (origin of the bootstrap).
- H. R. Künsch, The Jackknife and the Bootstrap for General Stationary Observations, The Annals of Statistics, 17(3), 1989 (origin of the moving-block bootstrap).
- MQL5 forum, How do you detect when your EA stops matching its backtest?;
- Edge Drift Detector source code, MQL5 CodeBase: Edge Drift Detector in the MQL5 CodeBase
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.