OPEN-SOURCE SCRIPT

Correlation Thermal Matrix [ShigemiQuant]

471
What's hidden in your portfolio?

Two "diversified" positions can silently carry the same risk — but a standard correlation table won't show you the structure behind it. Correlation Thermal Matrix (CTM) goes further: it builds a live pairwise heatmap for up to 15 symbols, then applies K-Means clustering to automatically group symbols that share similar correlation behavior. The result is a block-diagonal matrix where hidden risk clusters become immediately visible — something no other public TradingView indicator currently offers.

━━ What It Does ━━

CTM calculates pairwise Pearson correlation coefficients for up to 15 symbols and renders them as a color-coded thermal matrix directly on your chart. Red cells indicate strong negative correlation (-1.0), white indicates near-zero, and blue indicates strong positive correlation (+1.0).

Beyond the heatmap, a K-Means algorithm analyzes each symbol's full row of correlations — its "correlation profile" — and groups symbols with similar profiles into clusters. The matrix is then reordered so that clustered symbols sit adjacent to each other, separated by clear visual dividers. This block-diagonal structure reveals the hidden architecture of your watchlist at a glance.

━━ Key Features ━━

  • K-Means Clustering (ML) — Automatically groups symbols whose correlation profiles are similar. Adjustable from K=2 to K=5 clusters (default K=3). Powered by MLMatrixLib.
  • Block-Diagonal Sorting — Clustered symbols are placed adjacent to each other with separator rows/columns at cluster boundaries, making structural patterns impossible to miss.
  • Live Correlation Heatmap — Pairwise correlations for up to 15 symbols, updated every bar. Manual Pearson calculation bypasses ta.correlation() limitations for full flexibility.
  • 3 Built-in Presets + Custom Mode — FX Majors (12 pairs including EURUSD, GBPUSD, USDJPY), US Stocks (AAPL, MSFT, GOOGL and more), Crypto (BTC, ETH, SOL and more), or define your own symbols entirely.
  • Auto Duplicate Skip — If your chart symbol already exists in the preset, CTM automatically removes the duplicate so no slot is wasted.
  • Flexible Display — Choose table position (9 locations), text size (Tiny / Small / Normal), and toggle numeric values on or off.
  • Open Source — Full source code available under Mozilla Public License 2.0. Read it, learn from it, build on it.


━━ How to Use ━━

  • Step 1 — Add CTM to any chart. Select a preset (FX Majors / US Stocks / Crypto) or switch to Custom and enter your own symbols.
  • Step 2 — Read the heatmap. Deep blue cells = highly correlated pairs. Deep red = inversely correlated. White = independent. If two positions you hold are deep blue, you are more concentrated than you think.
  • Step 3 — Check the cluster blocks. Symbols inside the same block share similar correlation behavior. Symbols in different blocks offer genuine diversification.
  • Step 4 — Adjust the correlation period (default 20 bars) to match your trading horizon. Shorter periods capture recent regime shifts; longer periods reveal structural relationships.
  • Step 5 — Watch for changes. When a pair that was previously uncorrelated suddenly turns blue, this often signals a regime change — reassess your exposure accordingly.


━━ Parameters ━━

[code]
Parameter Default Range / Options
Correlation Period 20 5 - 100 bars
Cluster Count (K) 3 2 - 5
Symbol Preset FX Majors FX Majors / US Stocks / Crypto / Custom
Table Position Top Right 9 chart positions
Text Size Tiny Tiny / Small / Normal
Show Values On On / Off
[/code]

━━ Practical Applications ━━

  • Concentration risk detection — Holding EURUSD and GBPUSD simultaneously? If their correlation reads +0.92, your effective exposure is nearly doubled. CTM makes this visible before the drawdown does.
  • Diversification discovery — Symbols in separate clusters behave independently. For example, if BTC and ETH land in Cluster 1 while SOL sits in Cluster 2, SOL may offer genuine diversification against the BTC/ETH group. Use cluster membership to build positions that don't collapse together.
  • Regime change early warning — A correlation that breaks from its historical pattern signals a structural shift in the market. Monitor cluster membership over time — when a symbol migrates from one cluster to another, it deserves attention.


━━ Suggested Workflow ━━

  • 1. Detect risk clusters (CTM) — Identify which of your positions share correlated behavior.
  • 2. Find independent momentum (Multi-Symbol Momentum Screener) — Screen for momentum signals, then cross-check with CTM to confirm those signals come from different clusters.
  • 3. Size by real exposure (Smart Position Sizer) — Allocate position size based on per-cluster exposure rather than per-symbol, so correlated groups don't silently dominate your book.


━━ About ShigemiQuant ━━

ShigemiQuant builds open-source quantitative tools focused on risk management and portfolio intelligence. Every script is designed to solve a real trading problem — no repainting, no black boxes, fully readable source code.

━━ Changelog ━━

  • v1.0 — Initial release. Live correlation heatmap with K-Means clustering, 3 presets, block-diagonal sorting, and auto duplicate skip.

Clause de non-responsabilité

Les informations et publications ne sont pas destinées à être, et ne constituent pas, des conseils ou recommandations financiers, d'investissement, de trading ou autres fournis ou approuvés par TradingView. Pour en savoir plus, consultez les Conditions d'utilisation.