Loading…

Dependence Measures Toolkit

Two instruments can show an entirely ordinary, moderate Pearson correlation across most of history and still crash together on exactly the days that matter most - the tails, where a diversification or hedge assumption is actually being relied on. Pearson correlation is computed once, across the whole sample, and has no way to describe "these two only really move together during the worst 5% of days" - that co-crash behaviour needs a measure computed specifically in the tails, not a bulk-sample linear statistic, and this toolkit adds exactly that alongside the more familiar rank correlations. SpearmanCorrelation and KendallTau both capture monotonic (not just linear) dependence, at different robustness trade-offs: Spearman is Pearson's formula applied to ranks; Kendall's tau-b counts concordant versus discordant pairs directly and depends only on the ORDERING of pairwise comparisons, which typically makes it the most resistant of the three to a handful of wild outliers, since one extreme point still only participates in n-1 pairwise comparisons rather than dominating a sum of products the way it can under Pearson. UpperTailDependence and LowerTailDependence answer the co-crash question directly and empirically: convert both series to their own percentile rank, then ask what fraction of the points where x is in its own top (or bottom) tailFraction ALSO have y in its own top (or bottom) tailFraction. This is model-free - no distributional assumption is fitted, it is a direct count within the observed tail. GaussianCopulaRho gives the simplest usable copula fit for a "Lite" toolkit: it converts Spearman's rho to the equivalent Gaussian linear correlation via the closed-form identity rho_pearson = 2*sin(pi/6 * rho_spearman), the standard method-of-moments shortcut that avoids numerically inverting a bivariate normal CDF, which MQL4 has no built-in function for at all. This is a real, named result - not a full maximum- likelihood copula fit - and it inherits the Gaussian copula's own well- known limitation: a Gaussian copula's tail dependence is exactly zero by construction, for any value of rho. Use UpperTailDependence/ LowerTailDependence for actual tail behaviour; GaussianCopulaRho only ever describes the bulk, central dependence shape. Notes and limitations - Ranking is O(n^2) (insertion sort by value with tie-averaging), and KendallTau's pairwise scan is also O(n^2). Fine for the tens-to-low- hundreds sample sizes typical of trade or bar statistics; not intended for thousands-of-points series per call. - UpperTailDependence/LowerTailDependence return 0.0 when fewer than 2 points fall in the tail being examined - too few points to estimate a fraction from meaningfully. A very small tailFraction on a short sample will hit this guard; widen tailFraction or use a longer sample. - GaussianCopulaRho is a method-of-moments approximation via Spearman's rho, not a maximum-likelihood copula fit, and a Gaussian copula has zero tail dependence by construction regardless of rho - it describes central, not tail, dependence. Read the full description above before using this function to justify a tail-risk conclusion; it cannot support one. - These are all pairwise (two-series) measures. A portfolio of more than two instruments needs this toolkit called once per pair, or a separate matrix-oriented library - not in scope here.

Script · MT5 · v1.0 · Free

About Dependence Measures Toolkit

Dependence Measures Toolkit is a script for MetaTrader 5. It has 1 setting, such as Sample size. MQL source code included.

Files in the download: DependenceMeasures.mq5.mqh, DependenceMeasures.txt, DependenceMeasuresDemo.ex5, DependenceMeasuresDemo.mq5.

Settings

SettingDefaultWhat it does
Sample size100Number of synthetic points per pair

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: Hedging · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria