OPEN-SOURCE SCRIPT

Bitcoin vs. Gold correlation with lag

21
BTC vs Gold (Lag) + Correlation — multi-timeframe, publication notes

What it does

Plots Gold on the same chart as Bitcoin, with a configurable lead/lag.

Lets you choose how the series is displayed:

Gold shifted forward (+lag on chart) — shows gold ahead of BTC on the time axis (visual offset).

Gold aligned to BTC (gold lag) — standard alignment; gold is lagged for calculation and plotted in place.

BTC 200D Lag (BTC shifted forward) — visualizes BTC shifted forward (like popular “BTC 200D Lag” charts).

Computes Pearson correlations between BTC (no lag) and Gold (with lag) over multiple lookback windows equivalent to:

30d, 60d, 90d, 180d, 365d, 2y (730d), 3y (1095d), 5y (1825d).

Shows a table with the correlation values, automatically scaled to the current timeframe.

Why this is useful

A common macro claim is that BTC tends to follow Gold with a delay (e.g., ~200 trading days). This tool lets you:

Visually advance Gold (or BTC) to see that lead-lag relationship on the chart.

Quantify the relationship with rolling correlations.

Switch timeframes (D/W/M/…): everything automatically stays in sync.

Quick start

Open a BTC chart (any exchange).

Add the indicator.

Set Gold symbol (default TVC:GOLD; alternatives: OANDA:XAUUSD, COMEX:GC1!, etc.).

Choose Lag value and Lag unit (Days/Weeks/Months/Years/Bars).

Pick Visual Mode:

To mirror those “BTC 200D Lag” posts: choose “BTC 200D Lag (BTC shifted forward)” with 200 Days.

To view Gold 200D ahead of BTC: select “Gold shifted forward (+lag on chart)” with 200 Days.

Keep Rebase to 100 ON for an apples-to-apples visual scale. (You can move the study to the left price scale if needed.)

Inputs

Gold symbol: external series to pair with BTC.

Lag value: numeric value.

Lag unit: Days, Weeks, Months (≈30d), Years (≈365d), or direct Bars.

Visual mode:

Gold shifted forward (+lag on chart) → gold is offset to the right by the lag (visual only).

Gold aligned to BTC (gold lag) → standard plot (no visual offset); correlations still use lagged gold.

BTC 200D Lag (BTC shifted forward) → BTC is offset to the right by the lag (visual only).

Rebase to 100 (visual): rescales each series to 100 on its first valid bar for clearer comparison.

Show gold without lag (debug): optional reference line.

Show price tag for gold (lag): toggles the track price label.

Timeframe handling

The study uses the current chart timeframe for both BTC and Gold (timeframe.period).

Lag in time units (Days/Weeks/Months/Years) is internally converted to an integer number of bars of the active timeframe (using timeframe.in_seconds).

Example: on W (weekly), 200 days ≈ 29 bars.

On intraday timeframes, days are converted proportionally.

Correlation math

Correlation = ta.correlation(BTC, Gold_lagged, length_in_bars)

Lookback lengths are the bar-equivalents of 30/60/90/180/365/730/1095/1825 days in the active timeframe.

Important: correlations are computed on prices (not returns). If you prefer returns-based correlation (often more statistically robust), duplicate the script and replace price inputs with change(close) or ta.roc(close, 1).

Reading the table

Window: nominal day label (e.g., 30d, 1y, 5y).

Bars (TF): how many bars that window equals on the current timeframe.

Correlation: Pearson coefficient [-1, 1]. Background tint shows intensity and sign.

Tips & caveats

Visual offsets (offset=) move series on screen only; they don’t affect the math. The math always uses BTC (no lag) × Gold (lagged).

With large lags on high timeframes, early bars will be na (normal). Scroll forward / reduce lag.

If your Gold feed doesn’t load, try an alternative symbol that your plan supports.

Rebase to 100 helps visibility when BTC ($100k) and Gold ($2k) share a scale.

Months/Years use 30/365-day approximations. For exact control, use Days or Bars.

Correlations on very short lengths or sparse data can be unstable; consider the longer windows for sturdier signals.

This is a visual/analytical tool, not a trading signal. Always apply independent risk management.

Suggested setups

Replicate “BTC 200D Lag” charts:

Visual Mode: BTC 200D Lag (BTC shifted forward)

Lag: 200 Days

Rebase: ON

Gold leads BTC (Gold ahead):

Visual Mode: Gold shifted forward (+lag on chart)

Lag: 200 Days

Rebase: ON

Compatibility: Pine v6, overlay study.
Best with: BTCUSD (any exchange) + a reliable Gold feed.
Author’s note: Lead-lag relationships are not stable over time; treat correlations as descriptive, not predictive.

Penafian

Maklumat dan penerbitan adalah tidak dimaksudkan untuk menjadi, dan tidak membentuk, nasihat untuk kewangan, pelaburan, perdagangan dan jenis-jenis lain atau cadangan yang dibekalkan atau disahkan oleh TradingView. Baca dengan lebih lanjut di Terma Penggunaan.