AssetCorrelationUtils

- Open source Library Used for Indicators that utilize correlation between assets for divergence calculations. It has no drawing elements.
ASSET CORRELATION UTILS
PineScript library for automatic detection of correlated asset pairs and triads for multi-asset analysis.
WHAT IT DOES
This library automatically identifies correlated assets based on the current chart symbol. It returns properly configured asset pairings for use in SMT divergence detection, inter-market analysis, and multi-asset comparison tools.
HOW IT WORKS
The library matches your chart symbol against known correlation groups:
- Index Futures: NQ/ES/YM/RTY triads (including micros)
- Metals: Gold/Silver/Copper triads (futures and CFD)
- Forex: EUR/GBP/DXY and USD/JPY/CHF triads
- Energy: Crude/Gasoline/Heating Oil triads
- Treasury: ZB/ZF/ZN bond triads
- Crypto: BTC/ETH/TOTAL3 and major altcoin pairings
Inversion flags are automatically computed for assets that move inversely (e.g., DXY vs EUR pairs).
HOW TO USE
EXPORTED FUNCTIONS
- resolveCurrentChart(): One-call auto-detection using chart syminfo
- resolveAssets(): Full detection with custom parameters
- resolveTriad() / resolveDyad(): Manual resolution with inversion logic
- detect*() functions: Category-specific detectors for custom workflows
TYPES
- AssetPairing: Core structure for primary/secondary/tertiary tickers with inversion flags
- AssetConfig: Full resolution result with detection status and asset category
DISCLAIMER
This library is a utility for building multi-asset indicators. Asset correlations are not guaranteed and may change over time. Always validate pairings for your specific trading context.
Full Default Function type and Field descriptions below.
Library "AssetCorrelationUtils"
detectIndicesFutures(ticker)
Detects Index Futures (NQ/ES/YM/RTY + micro variants)
Parameters:
ticker (string): The ticker string to check (typically syminfo.ticker)
Returns: AssetPairing with secondary and tertiary assets configured
detectMetalsFutures(ticker)
Detects Metal Futures (GC/SI/HG + micro variants)
Parameters:
ticker (string): The ticker string to check
Returns: AssetPairing with secondary and tertiary assets configured
detectForexFutures(ticker)
Detects Forex Futures (6E/6B + micro variants)
Parameters:
ticker (string): The ticker string to check
Returns: AssetPairing with secondary and tertiary assets configured
detectEnergyFutures(ticker)
Detects Energy Futures (CL/RB/HO + micro variants)
Parameters:
ticker (string): The ticker string to check
Returns: AssetPairing with secondary and tertiary assets configured
detectTreasuryFutures(ticker)
Detects Treasury Futures (ZB/ZF/ZN)
Parameters:
ticker (string): The ticker string to check
Returns: AssetPairing with secondary and tertiary assets configured
detectCryptoFutures(ticker)
Detects CME Crypto Futures (BTC/ETH + micro variants)
Parameters:
ticker (string): The ticker string to check
Returns: AssetPairing with secondary and tertiary assets configured
detectCADFutures(ticker)
Detects CAD Forex Futures (6C + micro variants)
Parameters:
ticker (string): The ticker string to check
Returns: AssetPairing with secondary and tertiary assets configured
detectForexCFD(ticker, tickerId)
Detects Forex CFD pairs (EUR/GBP/DXY, USD/JPY/CHF triads)
Parameters:
ticker (string): The ticker string to check
tickerId (string): The full ticker ID (syminfo.tickerid) for primary asset
Returns: AssetPairing with secondary and tertiary assets configured
detectCrypto(ticker, tickerId)
Detects major Crypto assets (BTC, ETH, SOL, XRP, alts)
Parameters:
ticker (string): The ticker string to check
tickerId (string): The full ticker ID for primary asset
Returns: AssetPairing with secondary and tertiary assets configured
detectMetalsCFD(ticker, tickerId)
Detects Metals CFD (XAU/XAG/Copper)
Parameters:
ticker (string): The ticker string to check
tickerId (string): The full ticker ID for primary asset
Returns: AssetPairing with secondary and tertiary assets configured
detectIndicesCFD(ticker, tickerId)
Detects Indices CFD (NAS100/SP500/DJ30)
Parameters:
ticker (string): The ticker string to check
tickerId (string): The full ticker ID for primary asset
Returns: AssetPairing with secondary and tertiary assets configured
detectEUStocks(ticker, tickerId)
Detects EU Stock Indices (GER40/EU50) - Dyad only
Parameters:
ticker (string): The ticker string to check
tickerId (string): The full ticker ID for primary asset
Returns: AssetPairing with secondary asset configured (tertiary empty for dyad)
getDefaultFallback(tickerId)
Returns default fallback assets (chart ticker only, no correlation)
Parameters:
tickerId (string): The full ticker ID for primary asset
Returns: AssetPairing with chart ticker as primary, empty secondary/tertiary (no correlation)
applySessionModifierWithBackadjust(tickerStr, sessionType)
Applies futures session modifier to ticker WITH back adjustment
Parameters:
tickerStr (string): The ticker to modify
sessionType (string): The session type (syminfo.session)
Returns: Modified ticker string with session and backadjustment.on applied
applySessionModifierNoBackadjust(tickerStr, sessionType)
Applies futures session modifier to ticker WITHOUT back adjustment
Parameters:
tickerStr (string): The ticker to modify
sessionType (string): The session type (syminfo.session)
Returns: Modified ticker string with session and backadjustment.off applied
isTriadMode(pairing)
Checks if a pairing represents a valid triad (3 assets)
Parameters:
pairing (AssetPairing): The AssetPairing to check
Returns: True if tertiary is non-empty (triad mode), false for dyad
getAssetTicker(tickerId)
Extracts clean ticker string from full ticker ID
Parameters:
tickerId (string): The full ticker ID (e.g., "BITGET:BTCUSDT.P")
Returns: Clean ticker string (e.g., "BTCUSDT.P")
resolveTriad(chartTickerId, pairing)
Resolves triad asset assignments with proper inversion flags
Parameters:
chartTickerId (string): The current chart's ticker ID (syminfo.tickerid)
pairing (AssetPairing): The detected AssetPairing
Returns: Tuple [primaryAsset, secondaryAsset, tertiaryAsset, invertSecondary, invertTertiary]
resolveDyad(chartTickerId, pairing)
Resolves dyad asset assignment with proper inversion flag
Parameters:
chartTickerId (string): The current chart's ticker ID
pairing (AssetPairing): The detected AssetPairing (dyad: tertiary is empty)
Returns: Tuple [primaryAsset, secondaryAsset, invertSecondary]
resolveAssets(ticker, tickerId, assetType, sessionType, useBackadjust)
Main auto-detection entry point. Detects asset category and returns fully resolved config.
Parameters:
ticker (string): The ticker string to check (typically syminfo.ticker)
tickerId (string): The full ticker ID (typically syminfo.tickerid)
assetType (string): The asset type (typically syminfo.type)
sessionType (string): The session type for futures (typically syminfo.session)
useBackadjust (bool): Whether to apply back adjustment for futures session alignment
Returns: AssetConfig with fully resolved assets, inversion flags, and detection status
resolveCurrentChart()
Simplified auto-detection using current chart's syminfo values
Returns: AssetConfig with fully resolved assets, inversion flags, and detection status
AssetPairing
Core asset pairing structure for triad/dyad configurations
Fields:
primary (series string): The primary (chart) asset ticker ID
secondary (series string): The secondary correlated asset ticker ID
tertiary (series string): The tertiary correlated asset ticker ID (empty for dyad)
invertSecondary (series bool): Whether secondary asset should be inverted for divergence calc
invertTertiary (series bool): Whether tertiary asset should be inverted for divergence calc
AssetConfig
Full asset resolution result with mode detection and computed values
Fields:
detected (series bool): Whether auto-detection succeeded
isTriadMode (series bool): True if triad (3 assets), false if dyad (2 assets)
primary (series string): The resolved primary asset ticker ID
secondary (series string): The resolved secondary asset ticker ID
tertiary (series string): The resolved tertiary asset ticker ID (empty for dyad)
invertSecondary (series bool): Computed inversion flag for secondary asset
invertTertiary (series bool): Computed inversion flag for tertiary asset
assetCategory (series string): String describing the detected asset category
Updated the Asset selection for Futures contracts to use other futures contracts instead of FOREXCOM-CFD
Updated the Asset correlation order to favor more popular assets.
Updated RTY Correlation to YM - ES (NQ - ES before)
- Fixed Buggy 'COMEX_MINI' Broker Prefix to just 'COMEX'
- Unfortunate typo using COMEX_MINI for Non-Micro variants of metals
Typo: CME_MINI instead of CBOT_MINI for YM with RTY correlations
- Fixed Bond Broker Prefixes from 'CBOT_MINI' to 'CBOT'
Added Implementation Help comments to the source code for devs
Added:
detectMetalsFuturesGxt(ticker)
Detects Metal Futures in GXT Mode (Gold→XAUEUR/XAUGBP, Silver→XAGEUR/XAGGBP)
Parameters:
ticker (string): The ticker string to check
Returns: AssetPairing with GXT secondary and tertiary assets (empty for copper — falls through to default)
detectMetalsCFDGxt(ticker, tickerId)
Detects Metals CFD in GXT Mode (XAUUSD→XAUEUR/XAUGBP, XAGUSD→XAGEUR/XAGGBP)
Parameters:
ticker (string): The ticker string to check
tickerId (string): The full ticker ID for primary asset
Returns: AssetPairing with GXT secondary and tertiary assets (empty for copper — falls through to default)
Updated:
resolveAssets(ticker, tickerId, assetType, sessionType, useBackadjust, gxtMode)
Main auto-detection entry point. Detects asset category and returns fully resolved config.
Parameters:
ticker (string): The ticker string to check (typically syminfo.ticker)
tickerId (string): The full ticker ID (typically syminfo.tickerid)
assetType (string): The asset type (typically syminfo.type)
sessionType (string): The session type for futures (typically syminfo.session)
useBackadjust (bool): Whether to apply back adjustment for futures session alignment
gxtMode (bool): When true, metals (Gold/Silver) use currency-cross triads (XAUEUR/XAUGBP, XAGEUR/XAGGBP) instead of standard metal correlations. Copper falls back to default.
Returns: AssetConfig with fully resolved assets, inversion flags, and detection status
resolveCurrentChart(gxtMode)
Simplified auto-detection using current chart's syminfo values
Parameters:
gxtMode (bool): When true, metals use currency-cross triads instead of standard metal correlations
Returns: AssetConfig with fully resolved assets, inversion flags, and detection status
Added:
resolveQuad(chartTickerId, pairing)
Resolves quad asset assignments with proper inversion flags (4 assets, primary-only divergences)
Parameters:
chartTickerId (string): The current chart's ticker ID (syminfo.tickerid)
pairing (AssetPairing): The detected AssetPairing with quaternary populated
Returns: Tuple [primaryAsset, secondaryAsset, tertiaryAsset, quaternaryAsset, invertSecondary, invertTertiary, invertQuaternary]
Updated:
AssetPairing
Core asset pairing structure for triad/dyad configurations
Fields:
primary (series string): The primary (chart) asset ticker ID
secondary (series string): The secondary correlated asset ticker ID
tertiary (series string): The tertiary correlated asset ticker ID (empty for dyad)
quaternary (series string)
invertSecondary (series bool): Whether secondary asset should be inverted for divergence calc
invertTertiary (series bool): Whether tertiary asset should be inverted for divergence calc
invertQuaternary (series bool)
AssetConfig
Full asset resolution result with mode detection and computed values
Fields:
detected (series bool): Whether auto-detection succeeded
isTriadMode (series bool): True if triad (3 assets), false if dyad (2 assets)
isQuadMode (series bool)
primary (series string): The resolved primary asset ticker ID
secondary (series string): The resolved secondary asset ticker ID
tertiary (series string): The resolved tertiary asset ticker ID (empty for dyad)
quaternary (series string)
invertSecondary (series bool): Computed inversion flag for secondary asset
invertTertiary (series bool): Computed inversion flag for tertiary asset
invertQuaternary (series bool)
assetCategory (series string): String describing the detected asset category
Added:
detectMetalsFuturesQuad(ticker)
Detects Metal Futures in Quad Mode (Gold→SI/XAUEUR/XAUGBP, Silver→GC/XAGEUR/XAGGBP)
Parameters:
ticker (string): The ticker string to check
Returns: AssetPairing with futures secondary + GXT cross-pairs as tertiary/quaternary (empty for copper — falls through to default)
detectMetalsCFDQuad(ticker, tickerId)
Detects Metals CFD in Quad Mode (XAUUSD→XAGUSD/XAUEUR/XAUGBP, XAGUSD→XAUUSD/XAGEUR/XAGGBP)
Parameters:
ticker (string): The ticker string to check
tickerId (string): The full ticker ID for primary asset
Returns: AssetPairing with quad assets (empty for copper/cross-pairs — falls through to default)
Updated:
resolveAssets(ticker, tickerId, assetType, sessionType, useBackadjust, gxtMode, quadMode)
Main auto-detection entry point. Detects asset category and returns fully resolved config.
Parameters:
ticker (string): The ticker string to check (typically syminfo.ticker)
tickerId (string): The full ticker ID (typically syminfo.tickerid)
assetType (string): The asset type (typically syminfo.type)
sessionType (string): The session type for futures (typically syminfo.session)
useBackadjust (bool): Whether to apply back adjustment for futures session alignment
gxtMode (bool): When true, metals (Gold/Silver) use currency-cross triads (XAUEUR/XAUGBP, XAGEUR/XAGGBP) instead of standard metal correlations. Copper falls back to default.
quadMode (bool): When true, metals use futures secondary + GXT cross-pairs as tertiary/quaternary (e.g., GC→SI/XAUEUR/XAUGBP). Takes priority over gxtMode for metals.
Returns: AssetConfig with fully resolved assets, inversion flags, and detection status
resolveCurrentChart(gxtMode, quadMode)
Simplified auto-detection using current chart's syminfo values
Parameters:
gxtMode (bool): When true, metals use currency-cross triads instead of standard metal correlations
quadMode (bool): When true, metals use futures secondary + GXT cross-pairs as tertiary/quaternary
Returns: AssetConfig with fully resolved assets, inversion flags, and detection status
Updated:
detectMetalsCFD(ticker, tickerId)
Detects Metals CFD (XAU/XAG/Copper + EUR/GBP cross-pairs)
Parameters:
ticker (string): The ticker string to check
tickerId (string): The full ticker ID for primary asset
Returns: AssetPairing with secondary and tertiary assets configured
detectMetalsCFDQuad(ticker, tickerId)
Detects Metals CFD in Quad Mode (XAUUSD→XAGUSD/XAUEUR/XAUGBP, XAGUSD→XAUUSD/XAGEUR/XAGGBP, cross-pairs→USD base/correlated metal/opposite cross)
Parameters:
ticker (string): The ticker string to check
tickerId (string): The full ticker ID for primary asset
Returns: AssetPairing with quad assets (empty for copper — falls through to default)
- GXT mode now works on index futures. Before this update, gxtMode changed
nothing on an index chart, so Triad (GXT) and Triad returned the same two legs. - New exported detector, detectIndicesFuturesGxt(). The GXT index triad is
NQ, ES and YM. Russell is excluded on purpose. - The source now carries a full auto mode matrix above resolveAssets(). It
lists what every mode returns on every supported symbol, with exchange prefixes.
Changed
- Only the YM and MYM branch returns different legs. Triad gives RTY and ES.
GXT gives ES and NQ. NQ, ES, MNQ and MES keep the same two legs in both modes. - RTY and M2K have no GXT branch. They return the Triad result.
- Tetrad is unchanged on index charts. quadMode has priority over gxtMode, so
the four-leg table still includes Russell. - Import path is now fstarcapital/AssetCorrelationUtils/13.
How a mode maps to the parameters
- Triad — resolveCurrentChart()
- Triad (GXT) — resolveCurrentChart(true)
- Tetrad — resolveCurrentChart(false, true)
- Dyad — not a library mode. Call the Triad branch, then set
config.tertiary to an empty string and config.isTriadMode to false yourself.
Metals read quadMode first, then gxtMode, then the default branch. Index futures
ignore gxtMode while quadMode is true. Every other asset class ignores both
flags and always returns the Triad branch.
Index futures
- NQ, MNQ — Triad and GXT: CME_MINI:ES1!, CBOT_MINI:YM1!.
Tetrad adds CME_MINI:RTY1!. - ES, MES — Triad and GXT: CME_MINI:NQ1!, CBOT_MINI:YM1!.
Tetrad adds CME_MINI:RTY1!. - YM, MYM — Triad: CME_MINI:RTY1!, CME_MINI:ES1!.
GXT: CME_MINI:ES1!, CME_MINI:NQ1!.
Tetrad: CME_MINI:RTY1!, CME_MINI:ES1!, CME_MINI:NQ1!. - RTY, M2K — Triad and GXT: CBOT_MINI:YM1!, CME_MINI:ES1!.
Tetrad adds CME_MINI:NQ1!.
Micro charts get full-size legs. An MNQ chart returns ES1!, not MES1!.
Metal futures
- GC — Triad: COMEX:SI1!, COMEX:HG1!.
GXT: FOREXCOM:XAUEUR, FOREXCOM:XAUGBP.
Tetrad: COMEX:SI1!, FOREXCOM:XAUEUR, FOREXCOM:XAUGBP. - SI — Triad: COMEX:GC1!, COMEX:HG1!.
GXT: OANDA:XAGEUR, OANDA:XAGGBP.
Tetrad: COMEX:GC1!, OANDA:XAGEUR, OANDA:XAGGBP. - HG — every mode: COMEX:GC1!, COMEX:SI1!.
- MGC — Triad: COMEX_MINI:SIL1!, COMEX_MINI:MHG1!.
GXT: FOREXCOM:XAUEUR, FOREXCOM:XAUGBP.
Tetrad: COMEX_MINI:SIL1!, FOREXCOM:XAUEUR, FOREXCOM:XAUGBP. - SIL — Triad: COMEX_MINI:MGC1!, COMEX_MINI:MHG1!.
GXT: OANDA:XAGEUR, OANDA:XAGGBP.
Tetrad: COMEX_MINI:MGC1!, OANDA:XAGEUR, OANDA:XAGGBP. - MHG — every mode: COMEX_MINI:MGC1!, COMEX_MINI:SIL1!.
Copper has no GXT branch and no Tetrad branch, so it returns the Triad result.
Metal CFD
- XAUUSD — Triad: FOREXCOM:XAGUSD, FOREXCOM:COPPER.
GXT: FOREXCOM:XAUEUR, FOREXCOM:XAUGBP.
Tetrad: FOREXCOM:XAGUSD, FOREXCOM:XAUEUR, FOREXCOM:XAUGBP. - XAGUSD — Triad: FOREXCOM:XAUUSD, FOREXCOM:COPPER.
GXT: OANDA:XAGEUR, OANDA:XAGGBP.
Tetrad: FOREXCOM:XAUUSD, OANDA:XAGEUR, OANDA:XAGGBP. - COPPER — every mode: FOREXCOM:XAUUSD, FOREXCOM:XAGUSD.
- XAUEUR — Triad and GXT: FOREXCOM:XAUUSD, FOREXCOM:XAUGBP.
Tetrad: FOREXCOM:XAUUSD, FOREXCOM:XAGUSD, FOREXCOM:XAUGBP. - XAUGBP — Triad and GXT: FOREXCOM:XAUUSD, FOREXCOM:XAUEUR.
Tetrad: FOREXCOM:XAUUSD, FOREXCOM:XAGUSD, FOREXCOM:XAUEUR. - XAGEUR — Triad and GXT: FOREXCOM:XAGUSD, OANDA:XAGGBP.
Tetrad: FOREXCOM:XAGUSD, FOREXCOM:XAUUSD, OANDA:XAGGBP. - XAGGBP — Triad and GXT: FOREXCOM:XAGUSD, OANDA:XAGEUR.
Tetrad: FOREXCOM:XAGUSD, FOREXCOM:XAUUSD, OANDA:XAGEUR.
A cross-pair chart returns the same Triad and GXT legs, because the chart symbol
is already a cross.
Asset classes with one branch in every mode
- 6E — CME:6B1!, TVC:DXY inverted.
- 6B — CME:6E1!, TVC:DXY inverted.
- M6E — CME:M6B1!, TVC:DXY inverted.
- M6B — CME:M6E1!, TVC:DXY inverted.
- 6C, M6C — FOREXCOM:USDCAD, TVC:DXY inverted.
- CL, MCL — NYMEX:RB1!, NYMEX:HO1!.
- RB, MRB — NYMEX:CL1!, NYMEX:HO1!.
- HO, MHO — NYMEX:CL1!, NYMEX:RB1!.
- ZB — CBOT:ZF1!, CBOT:ZN1!.
- ZF — CBOT:ZB1!, CBOT:ZN1!.
- ZN — CBOT:ZB1!, CBOT:ZF1!.
- BTC, MBT futures — CME:ETH1!, CRYPTOCAP:TOTAL3.
- ETH, MET futures — CME:BTC1!, CRYPTOCAP:TOTAL3.
- EURUSD — FOREXCOM:GBPUSD, TVC:DXY inverted.
- GBPUSD — FOREXCOM:EURUSD, TVC:DXY inverted.
- DXY — FOREXCOM:EURUSD inverted, FOREXCOM:GBPUSD inverted.
- USDJPY — FOREXCOM:USDCHF, TVC:DXY.
- USDCHF — FOREXCOM:USDJPY, TVC:DXY.
- USDCAD — TVC:DXY, CME:6C1! inverted.
- BTC spot — BITGET:ETHUSDT.P, CRYPTOCAP:TOTAL3.
- ETH spot — BITGET:BTCUSDT.P, CRYPTOCAP:TOTAL3.
- SOL — BITGET:XRPUSDT.P, CRYPTOCAP:TOTAL3.
- XRP — BITGET:SOLUSDT.P, CRYPTOCAP:TOTAL3.
- ZEC, DOGE, ADA, BNB, TAO — BITGET:BTCUSDT.P, BITGET:ETHUSDT.P.
- NAS100 — VANTAGE:SP500FT, VANTAGE:DJ30FT.
- SP500 — VANTAGE:NAS100FT, VANTAGE:DJ30FT.
- DJ30 — VANTAGE:NAS100FT, VANTAGE:SP500FT.
- GER40 — FOREXCOM:EU50. Dyad only, no third leg.
- EU50 — FOREXCOM:GER40. Dyad only, no third leg.
A DXY chart is read as the third leg of the EUR, GBP and DXY triad, so both of
its legs come back inverted. The alt coin rows pair to the majors, and the legs
stay on BITGET perpetual contracts on every exchange.
Behavior to code against
- config.primary is always syminfo.tickerid. No mode replaces the chart
symbol. - Futures legs pass through ticker.modify(). The returned string carries the
chart session and the backadjustment setting, so it is not equal to the plain
ticker in the lists above. - Tetrad on a symbol with no fourth leg returns the Triad result.
config.isQuadMode stays false. Test that field before you read
config.quaternary. - An unrecognized symbol returns config.detected false, an assetCategory of
"fallback", and empty second and third legs. Test config.detected before you
request data.
Known limits
- resolveTriad() and resolveQuad() read pairing.invertTertiary only. A
detector that sets invertSecondary, such as the 6C branch, does not carry that
flag into the result. Invert the second leg in your own script if your
calculation needs it. - The library resolves symbols. It requests no data and draws nothing, so the
lag and the repaint behavior belong to your request.security() call, not to
this update.
Refer to line 842 in the code to see the full list of resolutons
Perpustakaan Pine
Dalam semangat TradingView sebenar, penulis telah menerbitkan kod Pine ini sebagai perpustakaan sumber terbuka supaya pengaturcara Pine lain dari komuniti kami boleh menggunakannya semula. Sorakan kepada penulis! Anda boleh menggunakan perpustakaan ini secara peribadi atau dalam penerbitan sumber terbuka lain, tetapi penggunaan semula kod ini dalam penerbitan adalah dikawal selia oleh Peraturan Dalaman.
All my tutorials on Tradingview Tips / Tricks and my tools are uploaded on my youtube. I do not upload How-To videos on tradingview.
Penafian
Perpustakaan Pine
Dalam semangat TradingView sebenar, penulis telah menerbitkan kod Pine ini sebagai perpustakaan sumber terbuka supaya pengaturcara Pine lain dari komuniti kami boleh menggunakannya semula. Sorakan kepada penulis! Anda boleh menggunakan perpustakaan ini secara peribadi atau dalam penerbitan sumber terbuka lain, tetapi penggunaan semula kod ini dalam penerbitan adalah dikawal selia oleh Peraturan Dalaman.
All my tutorials on Tradingview Tips / Tricks and my tools are uploaded on my youtube. I do not upload How-To videos on tradingview.