TradingView Correlation Indicator & Forex Pairs
Measure linear price relationships, avoid accidental risk doubling in forex pairs, and spot cross-market divergences. This Pine Script v6 correlation indicator combines an oscillator subwindow with an on-chart matrix table for real-time portfolio oversight.
Understanding Asset Correlation in Trading
Correlation measures the degree to which two asset prices move in relation to each other. Calculated between -1.0 and +1.0, correlation gives traders a mathematical foundation to manage systemic risk, hedge market exposure, and execute pairs trading strategies.
Assets move in lockstep. Example: EURUSD and GBPUSD. Buying both simultaneously increases directional exposure rather than diversifying risk.
Assets move independently. True portfolio diversification occurs when combining assets with low or near-zero correlation.
Assets move in opposite directions. Example: US Dollar Index (DXY) vs Gold (XAUUSD). Ideal for hedging and pairs trading.
Pine Script v6 Correlation Matrix Code
Copy and paste this script into TradingView. It computes the correlation between your current chart asset and up to 4 custom symbols, rendering both a historical oscillator and a real-time color-coded matrix table.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// (c) Pineify
//@version=6
indicator(title="Multi-Asset Correlation Indicator [Pineify]", shorttitle="Correlation Matrix", overlay=false)
// ---------------- Inputs ----------------
int len = input.int(20, "Correlation Length", minval=2)
string sym_comp1 = input.symbol("FX:EURUSD", "Comparison Symbol 1")
string sym_comp2 = input.symbol("FX:GBPUSD", "Comparison Symbol 2")
string sym_comp3 = input.symbol("FX:USDJPY", "Comparison Symbol 3")
string sym_comp4 = input.symbol("OANDA:AUDUSD", "Comparison Symbol 4")
bool show_table = input.bool(true, "Show Correlation Table on Chart")
string table_pos = input.string("top_right", "Table Position", options=["top_right", "bottom_right", "top_left", "bottom_left"])
// ---------------- Security Data Requests ----------------
c_main = close
c_comp1 = request.security(sym_comp1, timeframe.period, close)
c_comp2 = request.security(sym_comp2, timeframe.period, close)
c_comp3 = request.security(sym_comp3, timeframe.period, close)
c_comp4 = request.security(sym_comp4, timeframe.period, close)
// ---------------- Pearson Correlation Calculation ----------------
corr1 = ta.correlation(c_main, c_comp1, len)
corr2 = ta.correlation(c_main, c_comp2, len)
corr3 = ta.correlation(c_main, c_comp3, len)
corr4 = ta.correlation(c_main, c_comp4, len)
// ---------------- Oscillator Plot (Primary Comparison) ----------------
plot(corr1, title="Correlation with Symbol 1", color=color.blue, linewidth=2)
hline(1.0, "Perfect Positive (+1.0)", color=color.new(color.green, 50), linestyle=hline.style_dotted)
hline(0.7, "Strong Positive (+0.7)", color=color.new(color.green, 70), linestyle=hline.style_dashed)
hline(0.0, "Zero Correlation (0.0)", color=color.gray, linestyle=hline.style_solid)
hline(-0.7, "Strong Negative (-0.7)", color=color.new(color.red, 70), linestyle=hline.style_dashed)
hline(-1.0, "Perfect Negative (-1.0)", color=color.new(color.red, 50), linestyle=hline.style_dotted)
// ---------------- Visual Matrix Table ----------------
f_color(float val) =>
val >= 0.7 ? color.new(color.green, 20) :
val >= 0.3 ? color.new(color.green, 60) :
val <= -0.7 ? color.new(color.red, 20) :
val <= -0.3 ? color.new(color.red, 60) :
color.new(color.gray, 60)
var table_pos_enum = position.top_right
if table_pos == "bottom_right"
table_pos_enum := position.bottom_right
else if table_pos == "top_left"
table_pos_enum := position.top_left
else if table_pos == "bottom_left"
table_pos_enum := position.bottom_left
var table corr_tbl = table.new(table_pos_enum, 2, 5, bgcolor=color.new(color.black, 20), border_color=color.gray, border_width=1)
if barstate.islast and show_table
table.cell(corr_tbl, 0, 0, "Pair / Asset", bgcolor=color.slate, text_color=color.white, text_size=size.small)
table.cell(corr_tbl, 1, 0, "Corr (" + str.tostring(len) + ")", bgcolor=color.slate, text_color=color.white, text_size=size.small)
table.cell(corr_tbl, 0, 1, syminfo.ticker + " vs " + sym_comp1, text_color=color.white, text_size=size.small)
table.cell(corr_tbl, 1, 1, str.tostring(corr1, "#.##"), bgcolor=f_color(corr1), text_color=color.white, text_size=size.small)
table.cell(corr_tbl, 0, 2, syminfo.ticker + " vs " + sym_comp2, text_color=color.white, text_size=size.small)
table.cell(corr_tbl, 1, 2, str.tostring(corr2, "#.##"), bgcolor=f_color(corr2), text_color=color.white, text_size=size.small)
table.cell(corr_tbl, 0, 3, syminfo.ticker + " vs " + sym_comp3, text_color=color.white, text_size=size.small)
table.cell(corr_tbl, 1, 3, str.tostring(corr3, "#.##"), bgcolor=f_color(corr3), text_color=color.white, text_size=size.small)
table.cell(corr_tbl, 0, 4, syminfo.ticker + " vs " + sym_comp4, text_color=color.white, text_size=size.small)
table.cell(corr_tbl, 1, 4, str.tostring(corr4, "#.##"), bgcolor=f_color(corr4), text_color=color.white, text_size=size.small)
Key Features of this Pine Script v6 Indicator:
- Native V6 Calculation: Uses
ta.correlation()with dynamic security queries. - Live On-Chart Table: Displays color-coded correlation coefficients right on your chart layout.
- Multi-Market Support: Swap symbols in the settings to analyze Forex pairs, Crypto pairs, or Equity indices.
- Resource-Friendly: Respects TradingView security limits and stays fast and lightweight.
Common Forex Correlation Pairs
Understanding typical forex correlation tendencies helps prevent over-leveraging on correlated currencies.
| Currency Pair Combo | Typical Correlation | Trading Implication |
|---|---|---|
| EUR/USD & GBP/USD | Strong Positive (+0.80) | European currencies against USD. Simultaneous longs equal double USD short exposure. |
| EUR/USD & USD/CHF | Strong Negative (-0.90) | Swiss Franc closely mirrors Euro. Buying EURUSD and USDCHF at the same time cancels out profits. |
| AUD/USD & NZD/USD | Strong Positive (+0.85) | Commodity currencies heavily influenced by Asia-Pacific trade and metals demand. |
| USD/CAD & Crude Oil (WTI) | Strong Negative (-0.75) | Canada is a major oil exporter. Oil price rises strengthen CAD, pushing USD/CAD down. |
Need Custom Multi-Asset Analysis?
Use Pineify to build custom correlation screeners, pairs trading alerts, or automated Pine Script strategies with AI.