Loading…

Contingency Table Analytics

A trader who splits closed trades by regime label or session and notices the win rate looks different across groups is asking a real statistical question - is the regime label related to the outcome - but rarely tests it properly. This library runs the standard contingency-table tools that answer exactly that, over any rows-by-columns count table: signal category on one axis, outcome category on the other, cell values are trade counts. CT_ChiSquare computes Pearson's chi-square statistic for independence, sum((observed-expected)^2/expected) over every cell, with the standard (rows-1)*(cols-1) degrees of freedom, and CT_ChiSquarePValueApprox converts it to an approximate significance reading via the same Wilson-Hilferty cube-root normal approximation used elsewhere in this catalogue where a full incomplete-gamma routine is not available. Chi-square answers "is there a relationship at all", but its magnitude also grows with sample size even when the underlying association strength is unchanged - a large enough trade count makes almost any non-zero relationship "significant", which makes the raw statistic a poor measure of how STRONG the relationship actually is. CT_CramersV normalises the chi-square statistic into a bounded 0..1 scale that is comparable across tables of different sizes and sample counts. CT_MutualInformationBits answers a related but distinct question: how many bits of the outcome's uncertainty does knowing the signal actually remove, on average. Unlike chi-square and Cramer's V, mutual information is not restricted to detecting monotonic or otherwise "orderly" relationships - any statistical dependence between signal and outcome, of any shape, contributes to it. The table itself is a flat row-major array rather than a true 2D array - MQL4 only allows the leftmost dimension of a dynamic array to vary, so a genuinely resizable rows x cols table has no true dynamic 2D form. Every function here works over that flat layout via CT_Get/CT_Set, so callers never need to write the r*cols+c indexing arithmetic themselves. Notes and limitations - CT_ChiSquare (and therefore CT_CramersV) returns false if any expected cell count is non-positive - an entirely empty row or column - since the statistic divides by the expected count there. This can happen with a sparse table even when every observed cell is non-negative. - The p-value is a Wilson-Hilferty approximation, not an exact chi-square CDF - accurate to roughly two significant figures for degrees of freedom >= 2, adequate to answer "significant at 5%?" but not precise enough to quote as an exact figure near a decision boundary. - Mutual information has no fixed upper bound in general; comparing it against log2(min(rows, cols)) gives a rough sense of scale, but it is not itself normalised to 0..1 the way Cramer's V is. - All statistics are computed from the counts as given - if the table is built from overlapping or non-independent trade groupings, the independence assumptions behind chi-square and its derivatives no longer strictly hold, though the numbers still describe the table as observed.

Script · MT5 · v1.0 · Free

About Contingency Table Analytics

Contingency Table Analytics is a script for MetaTrader 5. MQL source code included.

Files in the download: ContingencyTableAnalytics.mq5.mqh, ContingencyTableAnalytics.txt, ContingencyTableAnalyticsDemo.ex5, ContingencyTableAnalyticsDemo.mq5.

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: Buy/Sell Arrows · Trading Sessions · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria