Correlation And Covariance
Correlation between two symbols, or between a symbol and an indicator, is a one-line formula until the input is not what you expected. A thin symbol during a quiet session, or a placeholder array that has not been filled yet, can easily be constant - every value identical, zero variance. Pearson's formula divides by the product of two standard deviations, and dividing by zero there does not throw an error on either platform, it produces NaN. NaN then survives silently through every downstream calculation until something far away and unrelated produces a nonsense number with no obvious cause. Every function in this library checks for that case and returns 0.0 instead - "no linear relationship could be measured" is the honest, safe answer, not a number that poisons everything built on top of it. PearsonCorrelation measures linear relationship only. Two series that move together in a curved, non-linear way can show a weak Pearson reading despite being tightly related in practice. SpearmanCorrelation exists for exactly that case: it applies Pearson's formula to the RANKS of the values instead of the values themselves, so it captures any monotonic relationship - linear or not - at the cost of losing the actual magnitude of the moves. Tied values are ranked by the standard convention, averaging the positions they would otherwise occupy. RollingCorrelation slides a fixed window across two series and returns the correlation for that window ending at a given index - the shape needed to watch two symbols drift in and out of sync bar by bar, such as a currency pair and its correlated cross, or a stock and its sector index. The header duplicates a small mean helper internally rather than including Statistics.mqh or Descriptive.mqh from elsewhere in this catalogue. Every CodeBase submission must stand alone when downloaded on its own, so this project never depends on another project's file being present. Notes and limitations - Arrays are treated as plain, chronologically ordered data (index 0 = oldest). Chart price series fetched as an MQL4 timeseries, or via CopyClose on MQL5 with the default series flag, can have index 0 as the newest bar - reverse them before passing them in, or the rolling window will read the wrong end. - PearsonCorrelation and SpearmanCorrelation return exactly 0.0, never NaN or a runtime error, when either input series has zero variance. Treat that 0.0 as "undefined, not zero relationship" when the context matters - a genuinely flat instrument and a genuinely uncorrelated one both read as 0.0 here, and only the caller knows which situation it is looking at. - SpearmanCorrelation's rank computation is O(n^2) (insertion sort by value). Fine for correlation windows of the size trading code normally uses (tens to low hundreds of points); not intended for tick-by-tick series with thousands of points per call. - Correlation measures a statistical relationship, not causation, and a high historical reading is not a guarantee it continues - recompute it on a rolling basis rather than trusting one static number indefinitely.
Script · MT4 · v1.0 · Free
About Correlation And Covariance
Correlation And Covariance is a script for MetaTrader 4. It has 1 setting, such as Series length. MQL source code included.
Files in the download: Correlation.mqh, Correlation.txt, CorrelationDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Series length | 20 | Length of the synthetic series used in the demo |
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: Trading Sessions · MT4 Script · MQL4 Source Code