Loading…

Correlation Clustered Watchlist

A watchlist built by adding "everything that looks interesting" tends to accumulate near-duplicates: five JPY crosses that all move on the same USD or risk-sentiment driver, or a stock index CFD that tracks a currency pair almost tick for tick during certain sessions. None of that is obvious from the symbol names. It only shows up when the actual price history is compared. This script compares it. For every pair of symbols currently in Market Watch, it measures the Pearson correlation of their period-over-period close returns over InpCorrelationBars bars on InpCorrelationTimeframe. Any pair whose absolute correlation clears InpCorrelationThreshold is merged into the same cluster using a union-find structure, so a chain of relationships - A strongly correlated with B, B with C - lands all three in one cluster even though A and C were never compared directly above the threshold themselves. A simple pairwise filter without union- find would report that same chain as two separate overlapping pairs instead of one group, which is a much less useful watchlist. Clustering is purely statistical, not based on any hardcoded notion of "USD pairs" or "commodity currencies". A CFD index that happens to track a currency pair closely is caught exactly as reliably as two obviously related forex majors, because the test never looks at the symbol name - only at how the prices actually moved. With InpTrimToOnePerCluster switched on, the script goes one step further and edits Market Watch directly: every symbol in a cluster is hidden except one representative, which is the symbol on the current chart if it belongs to that cluster, or otherwise the first member found. The chart's own symbol is never hidden out from under itself, which matters because deselecting the symbol a chart is displaying would leave that chart showing a stale, unlinked quote. Notes and limitations - Correlation is measured on historical returns and can shift, sometimes quickly around a regime change or a news event. A cluster that was accurate last month is not guaranteed to still be accurate today - rerun periodically rather than trusting one snapshot indefinitely. - Symbols with fewer than ten overlapping bars of history are treated as uncorrelated (0.0) with everything, so a very new or very illiquid symbol will usually end up alone in its own cluster rather than grouped incorrectly. - The representative kept per cluster is a simple rule - current chart symbol first, otherwise the first member encountered - not a "best" symbol by any trading quality measure such as spread or liquidity. - Trimming only hides symbols from Market Watch with SymbolSelect(false). It does not delete, rename or otherwise modify anything about the symbols themselves, and every hidden symbol can be re-added at any time. - Runtime grows with the square of the number of watched symbols, since every pair is compared once. A very large Market Watch list will take noticeably longer than a small one.

Script · MT4 · v1.0 · Free

About Correlation Clustered Watchlist

Correlation Clustered Watchlist is a script for MetaTrader 4. It can alert you with a pop-up, sound, email or push message. It has 4 settings, such as Correlation bars, Correlation timeframe and Correlation threshold. MQL source code included.

Files in the download: CorrelationClusteredWatchlist.ex4, CorrelationClusteredWatchlist.mq4, CorrelationClusteredWatchlist.txt.

Settings

SettingDefaultWhat it does
Correlation bars100Bars used to measure each pairwise correlation
Correlation timeframePERIOD_D1Timeframe used to measure correlation
Correlation threshold0.80|correlation| at or above this merges two symbols into one cluster
Trim to one per clusterfalseHide every symbol in Market Watch except one representative per cluster

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 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: Trading Sessions · Alerts · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria