Loading…

Correlation Dendrogram Panel

A basket of ten symbols is not ten independent trades if eight of them move together. A flat correlation matrix shows every pairwise number but does not show the STRUCTURE - which symbols cluster together, and how tightly. This indicator computes that structure properly rather than approximating it: a pairwise Pearson correlation matrix built from each symbol's own log-return history, converted to a distance (1 minus correlation, so perfectly correlated symbols sit at distance zero), then merged step by step with average-linkage (UPGMA) agglomerative clustering - the same algorithm statistical software uses to build a dendrogram, not a cosmetic tree. Average linkage was chosen deliberately over single or complete linkage. Single linkage chains through whichever pair is nearest at each step and tends to produce one long straggling cluster on FX data, where almost every pair is at least loosely correlated. Complete linkage overreacts to the single worst-correlated pair inside an otherwise tight cluster. Average linkage, weighted by cluster size at every merge, gives the most stable and most commonly recognised dendrogram shape for a basket like a currency watchlist. The result is drawn as a real tree: each merge is a horizontal bracket at a height equal to that merge's distance, with the leaves (symbol names) along the bottom in the order the clustering itself produced - not alphabetical, not input order, but the order that keeps genuinely similar symbols adjacent. A basket that collapses into two or three clusters low on the tree (low merge height means high correlation) is really two or three independent bets wearing ten symbols' clothing, and the tree makes that visible in a way a matrix of numbers does not. MT4 has no native chart object space indexed by an arbitrary category axis, only time and price. The tree's horizontal leaf positions are mapped onto a block of the chart's own recent bar times purely as coordinate carriers - their real timestamps carry no meaning for the tree - and two invisible plotted values span the full merge-height range so the subwindow's vertical scale always fits the whole tree. Notes and limitations - Needs at least 3 usable symbols with at least 10 bars of shared history each; fewer than that and the panel prints a Comment() explaining why instead of drawing an empty or misleading tree. - Correlation is computed on log returns, not raw price, so it reflects co-movement rather than shared price level or trend direction alone. - The leaf x-axis rides on real chart bar times purely as a coordinate mechanism - do not read any meaning into which actual calendar date a leaf label lines up with. - A symbol with a flat (zero-variance) return series over the window is treated as uncorrelated with everything (correlation 0) rather than causing a division-by-zero failure. - Recomputing is timer-driven, not tick-driven, so the tree can lag the market by up to InpRefreshSecs on a fast-moving basket; this is intentional given how slowly a correlation structure genuinely changes.

Indicator · MT5 · v1.0 · Free

About Correlation Dendrogram Panel

Correlation Dendrogram Panel is an indicator for MetaTrader 5. It has 8 settings, such as Symbols, Max symbols and Timeframe. MQL source code included.

Files in the download: CorrelationDendrogram.ex5, CorrelationDendrogram.mq5, CorrelationDendrogram.txt.

Settings

SettingDefaultWhat it does
SymbolsComma-separated symbol list, e.g. EURUSD,GBPUSD,USDJPY - empty uses every symbol currently in Market
Max symbols14Cap on symbol count (keeps the tree readable and the O(n^3) clustering fast)
TimeframePERIOD_D1Timeframe the return series is built from
Bars120Number of bars of returns used for each pairwise correlation
Refresh secs30Recompute interval, in seconds (correlation on daily bars does not need per-tick recompute)
Tree colorclrDeepSkyBlueDendrogram line colour
Label colorclrWhiteLeaf symbol label colour
Line width2Dendrogram line width

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 → Indicators.
  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 indicator from the Navigator onto a chart and adjust the settings.

Download the PDF guide

Tags: MT5 Indicator · MQL5 Source Code

MetaTrader 4 & 5 indicators · PolyMaria