Outcome Dependency Tests
Win rate and average R tell you the shape of a strategy's outcomes, but not whether those outcomes are independent of each other. A strategy whose losses cluster - several bad trades in a row more often than chance predicts - carries a different real drawdown risk than one with an identical win rate whose outcomes are genuinely unrelated trade to trade, even though a simple win-rate or average-R summary cannot tell the two apart. This library runs two classical dependency checks over a caller- supplied sequence of outcomes (win/loss signs, or raw R-multiples/ profit figures - only the sign of each value is used by the tests). The Wald-Wolfowitz runs test counts how many "runs" (unbroken streaks of the same sign) the sequence actually has and compares that count against what independent draws with the same win/loss split would be expected to produce, on average, returning a z-score and a normal- approximation p-value: too few runs means results streak more than chance, too many means they alternate more than chance, and either one is evidence against independence. Lag-k serial correlation is the Pearson correlation of the sequence against itself shifted by k trades, a second, complementary view of the same question at a specific lag. The demo makes the difference concrete: a genuinely random 50/50 sequence and a deliberately streaky sequence built from repeating blocks of six wins then six losses, same length and same overall win rate, are run through both tests side by side - only the streaky one should trip the runs test's significance threshold, proving the test can actually distinguish the two rather than always reporting the same thing regardless of input. MQL4 has no MathErf/MathErfc built in, so the runs test's p-value uses the Abramowitz & Stegun polynomial approximation to the normal CDF. This is the same well-known approximation used elsewhere in this catalogue for the identical reason, but it is REIMPLEMENTED from scratch inside this file rather than included from any other project's header, per house style self-containment - every submission stands alone with no cross-project #include. Notes and limitations - A sequence holds at most 1000 values (OUTCOME_SEQ_MAX); OutcomeSequenceAdd returns false rather than overflow. - The runs test is undefined (returns false, not a misleading z=0) when the sequence is shorter than 2 values or every outcome is the same sign - both are genuinely not testable, not "no dependency found". - The p-value uses a normal approximation to the runs distribution, which is standard practice but is only accurate for a reasonably sized sample (tens of trades or more); treat a p-value from a very short sequence as indicative, not exact. - OutcomeDependencySerialCorrelation returns 0.0 both when there is genuinely no correlation AND when the correlation is mathematically undefined (fewer than 2 overlapping pairs, or zero variance in either half) - check seq.count against the requested lag before trusting a 0.0 result as a real "no correlation" finding. - The account-history half of the demo classifies each closed order by the sign of its own profit+swap+commission only; it does not weight by trade size or convert to R-multiples, so a big winner and a small winner both count as one "positive" outcome, identically. - No trading functions are used anywhere in this library.
Script · MT4 · v1.0 · Free
About Outcome Dependency Tests
Outcome Dependency Tests is a script for MetaTrader 4. It has 2 settings, such as Synthetic length and Max history to scan. MQL source code included.
Files in the download: OutcomeDependencyTests.mqh, OutcomeDependencyTests.txt, OutcomeDependencyTestsDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Synthetic length | 60 | length of each synthetic demo sequence |
| Max history to scan | 200 | most recent closed orders to scan from real account history |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Risk & Lot Size · MT4 Script · MQL4 Source Code