Rank Statistics Toolkit
Rank-based statistics are the right tool whenever two values are compared for a MONOTONIC relationship rather than a strictly linear one - does a higher indicator reading tend to go with a higher outcome, not "by how much exactly". Price-derived and outcome data is full of ties in practice: a quantised or rounded indicator reading, a discrete win/loss/breakeven outcome, a rounded P/L bucket. Ignoring ties biases both the correlation coefficient and the significance of the result, sometimes substantially. RS_Rank computes fractional (mid-)ranks: tied values all receive the mean of the ranks they jointly occupy, the standard convention. RS_SpearmanRho is then computed as the Pearson correlation of the two variables' mid- ranks - the EXACT tie-corrected definition of Spearman's rho, not the textbook sum-of-squared-rank-differences shortcut, which is only algebraically equivalent to this when there are no ties at all. RS_KendallTau computes tau-b: concordant pairs minus discordant pairs, divided by sqrt((n0-n1)*(n0-n2)) where n0 is the total pair count and n1/n2 are pairs tied on x and on y respectively. This stays correctly bounded in [-1, 1] even on heavily tied data; the simpler tau-a (dividing by the plain pair count) is biased toward zero as ties increase, understating the true association strength. RS_ConcordanceIndex is the two-sample generalisation used, for example, as an AUC-style score for a continuous signal against a binary or ordinal outcome: among every pair with a genuine outcome difference, the fraction where the score ranks them the same way, with a tied score counted as half a "win" - the standard C-index convention, reflecting that a tied score is neither evidence for nor against the ranking being correct. Every function here is an O(n^2) pairwise or ranking computation - the honest complexity for an exact, tie-correct result without extra structure, appropriate for the sample sizes (a bar window, a run of closed trades) this toolkit is built for. Notes and limitations - RS_KendallTau returns false when every pair is tied on x or on y (tau-b's denominator is zero) rather than dividing by zero. - RS_SpearmanRho returns false when either series has zero rank variance (every value identical) - correlation is undefined there, not zero. - RS_ConcordanceIndex only counts pairs with a genuine outcome difference as "comparable" - a pair tied on outcome contributes nothing, since it cannot be ranked correctly or incorrectly. - All three correlation/concordance functions are O(n^2); RS_Rank uses an O(n^2) insertion sort internally, appropriate for the bar-window/closed- trade sample sizes this toolkit targets, not intended for very large n.
Script · MT4 · v1.0 · Free
About Rank Statistics Toolkit
Rank Statistics Toolkit is a script for MetaTrader 4. MQL source code included.
Files in the download: RankStatisticsToolkit.mqh, RankStatisticsToolkit.txt, RankStatisticsToolkitDemo.mq4.
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: Trailing Stop & Break-even · MT4 Script · MQL4 Source Code