OPEN-SOURCE SCRIPT

Gold/DXY Correlation Oscillator

6 921
Gold/DXY Correlation Oscillator — Regime-Filtered RSI Signals

This indicator measures the rolling Pearson correlation between Gold (the chart instrument) and the US Dollar Index (DXY), and uses that correlation regime to gate RSI-based buy/sell signals.

The core premise is straightforward: Gold and the US Dollar tend to move inversely. When this inverse relationship breaks down — i.e., the correlation rises above a user-defined threshold — trade signals are suppressed, reducing false entries during regimes where the normal macro relationship is not in force.

─────────────────────────────────────
How it works
─────────────────────────────────────

1. Correlation Measurement
A rolling Pearson correlation (default: 20 bars) is calculated between Gold's close price and DXY's close price (fetched via request.security). The result ranges from -1.0 (perfect inverse) to +1.0 (perfect positive).

2. Regime Filter
The correlation is compared to a user-set threshold (default: -0.3). Only when the correlation is below this threshold — indicating the typical inverse relationship is active — are trade signals enabled.

3. Signal Generation
Buy Signal : Correlation below threshold AND RSI crosses above the oversold level (default: 30)
Sell Signal : Correlation below threshold AND RSI crosses below the overbought level (default: 70)

─────────────────────────────────────
How to read the sub-panel
─────────────────────────────────────

• Correlation line — turns green when the inverse regime is active (below threshold), red when it is not
• Yellow dashed line — the active threshold level
• Green shading — fills the zone between threshold and -1.0 only when the regime is active
• ▲ BUY / ▼ SELL labels — appear at the correlation value on signal bars, with a background colour pulse
• Zero line (dotted) — reference for neutral correlation

─────────────────────────────────────
Inputs
─────────────────────────────────────

• DXY Ticker Symbol — default CAPITALCOM:DXY; change to TVC:DXY if your data provider requires it
• Correlation Lookback Period — rolling window for Pearson calculation (default: 30)
• Correlation Threshold — cut-off below which the inverse regime is considered active (default: -0.3)
• RSI Period — standard RSI lookback (default: 14)
• RSI Oversold Level — buy crossover level (default: 30)
• RSI Overbought Level — sell crossunder level (default: 70)

─────────────────────────────────────
Alerts
─────────────────────────────────────

Two alert conditions are built in:
• "Gold Buy Signal" — fires on a confirmed buy condition
• "Gold Sell Signal" — fires on a confirmed sell condition

Both are compatible with TradingView webhook alerts.

─────────────────────────────────────
Notes & limitations
─────────────────────────────────────

• The correlation line will show as na for the first (lookback) bars on any chart. This is expected behaviour.
• DXY data availability depends on your TradingView data subscription. If signals do not appear, verify the DXY ticker resolves correctly on your account.
• This indicator is non-repainting. All calculations reference confirmed bar closes only (lookahead = barmerge.lookahead_off).

─────────────────────────────────────
Disclaimer
─────────────────────────────────────

This script is published for educational and analytical purposes only. It does not constitute investment advice and is not intended for use as the sole basis for any trading decision. Past signal behaviour does not guarantee future results. Use at your own risk.

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.