OPEN-SOURCE SCRIPT
Atualizado

Regime-Conditional Correlation [RC Tools]

123
RC Tools — Regime-Conditional Correlation
─────────────────────────────────────────────────────────────

█ OVERVIEW

Plain rolling correlation between two assets is well-covered ground. The angle here: correlation is not a constant — it changes with the market regime. This tool computes rolling correlation against a second symbol of your choice and buckets it by the same regime framework as the Regime Classifier, so you can see whether a correlation you're relying on actually holds up across market conditions, or only shows up in one of them.

█ WHAT IT DOES

Plots rolling correlation (Pearson, default 20-bar window) between the current chart's symbol and a compare symbol you choose. Colours the background by the current market regime (Trending — Expansion/Exhaustion, Ranging — Quiet/Volatile), using the identical directionality × volatility-percentile logic as the Regime Classifier. A table shows the current regime and correlation, plus the average correlation, its standard deviation, and the sample count for each of the four regimes historically.

█ THE THEORY BEHIND IT

A single "the correlation is 0.8" number hides a lot. Two assets can be tightly linked during calm trending markets and decouple completely during volatile chop — or vice versa. Regime-conditioning the correlation surfaces that structure instead of averaging it away. This matters directly for anything relying on a stable cross-asset relationship: hedges, pairs, or diversification assumptions that quietly break exactly when you need them most (in the volatile regime).

█ HOW IT IS CALCULATED

CORRELATION: standard Pearson correlation between the current symbol's close and the compare symbol's close (fetched via request.security on the same timeframe), over a rolling window (default 20 bars).

REGIME: identical to the Regime Classifier — Efficiency Ratio for directionality, percentile-ranked realised volatility for volatility state, crossed to give four states. Regime is measured on the CURRENT chart's own price action, not the compare symbol. See the Regime Classifier's description for the full methodology.

The correlation reading and the regime are both "as of now" — contemporaneous — so unlike the Regime Classifier's forward-return table, no forward-looking attribution is needed here: each confirmed bar's correlation is added directly to the running average for whichever regime was active on that same bar.

█ SETTINGS & CONFIGURATION

• Compare Symbol (default BTCUSD) — the second asset to correlate against
• Correlation Length (default 20 bars)
• Regime settings mirror the Regime Classifier exactly (Efficiency Ratio lookback, directionality threshold, realised vol lookback, percentile window, volatility percentile threshold) — keep these in sync if you run both indicators together
• Paint Main Chart Background — off by default; enable on only one of the two indicators if running both, to avoid overlapping backgrounds

█ HOW TO USE IT

Check whether a correlation you're relying on is regime-dependent before trusting it. Example: if a hedge shows strong negative correlation in Ranging — Quiet but the average correlation flips or weakens in Ranging — Volatile, that hedge may not protect you exactly when volatility spikes. Always check the sample count (N) per regime before drawing conclusions — a regime with few historical bars hasn't been tested enough to trust its average.

█ LIMITATIONS

• Correlation is measured over a short rolling window and is noisy by nature — it will swing even when the underlying relationship is stable.
• The compare symbol is fetched via request.security on the same timeframe; illiquid symbols, different exchange sessions, or timezone misalignment can introduce lag or missing values.
• Regime classification carries the same caveats as the Regime Classifier: it is backward-looking by construction, unstable near threshold boundaries, and needs substantial history to be reliable.
• Per-regime correlation statistics accumulate only from where the chart's loaded history begins — early sample counts are small and not yet statistically meaningful.
• This script does NOT repaint. All classification and correlation display values update on confirmed bar close only.

█ DISCLAIMER

For educational and informational purposes only. Nothing here is financial advice. Past correlation between any two assets does not indicate future results. Trade at your own risk.
Notas de Lançamento
Changed display format

Aviso legal

As informações e publicações não se destinam a ser, e não constituem, conselhos ou recomendações financeiras, de investimento, comerciais ou de outro tipo fornecidos ou endossados pela TradingView. Leia mais nos Termos de Uso.