# Lead-lag Detection

Lead-lag Detection is an indicator on [Vela](https://vela.luxalgo.com/chart), our next-generation charting platform, written in Pine Script®.

Sweeps the cross-correlation of two instruments' returns across lags of -20 to +20 bars over a rolling 100-observation window - SP:SPX as the default comparison - and plots the peak with 2/sqrt(n) significance bounds. A profile beside the last bar draws the full correlation function lag by lag, a dashboard names the leader, and alerts cover new leads, leader flips, lost relationships and peak-lag shifts.

- Indicator in the LuxAlgo Library, by LuxAlgo, published Jun 6, 2026.
- Access: free.
- Concepts: [Lead-lag Detection](https://www.luxalgo.com/library/concept/lead-lag-detection/) (primary)
- Open on Vela: https://vela.luxalgo.com/chart?pineScriptCodeId=dxyzafpidcxuupsw6lyzw0jt
- Page: https://www.luxalgo.com/library/indicator/lead-lag-detection/

**Lead-lag Detection** measures whether the chart symbol systematically moves before or after a related instrument, and by how many bars. The cross-correlation sweep behind [lead-lag detection](https://www.luxalgo.com/library/concept/lead-lag-detection/) reruns on both series' returns every bar, from -20 to +20 lags, and the peak value plots with approximate significance bounds at 2/sqrt(n). A relationship is only called while the peak sits at a nonzero lag beyond the bounds; the dashboard names the leader and its lag in plain language.

## How to Trade the Lead-lag Detection?

- **Peak at a positive lag**: the chart symbol leads the comparison by that many bars; at a negative lag, the comparison leads the chart.
- **Significance first**: inside the bounds the honest read is no significant lead - alerts mark the transitions in and out.
- **Compare against lag 0**: the Lag-0 Correlation plot shows the plain contemporaneous correlation - the value added by shifting one series in time, at a glance.

Peak Lag Shift and Leader Flip alerts round out the set - a relationship is only worth trusting while its lag and sign persist.

## Lead-lag Detection Settings

- **Comparison Symbol** (default SP:SPX): the second instrument of the pair.
- **Source** (default close) and **Returns** (default Log): the correlated series, sampled only on fresh comparison bars so session mismatches cannot fabricate lags.
- **Correlation Window** (default 100): overlapping observations n at every lag, and the bounds.
- **Max Lag** (default 20): the sweep spans -K to +K bars; wider sweeps multiply the odds of spurious peaks.
- **Show Profile** (on) with **Profile Offset** (default 5): the per-lag correlogram beside the last bar.
- **Show Dashboard** (on, Top Right, Small); style: **Lag-0 Correlation** (on), **Normalized Peak Lag** (off), **Significance Bounds** (on).

## Frequently Asked Questions

### How does this relate to Lead-lag Instrument Triggers?

This build is the measurement step: whether a lead exists, its direction and size. [Lead-lag Instrument Triggers](https://www.luxalgo.com/library/indicator/lead-lag-instrument-triggers/) is the execution layer built on it, arming confirmation windows on the lagger after leader events.

### Why correlate returns instead of prices?

Two trending series show high price correlation with no genuine linkage - the classic spurious result. Returns remove the shared trend, and sampling only on common fresh observations removes the other big artifact: stale prices from nonsynchronous sessions.

### Should I trust a peak that barely clears the bounds?

Cautiously. The bounds are approximate and optimistic when returns are serially dependent, and a 41-lag sweep gives many chances for luck. Persistence is the better filter, which the Normalized Peak Lag plot exists to monitor.

## Related indicators

- [Arbitrage Detector](https://www.luxalgo.com/library/indicator/arbitrage-detector/) (Correlation): markdown at https://www.luxalgo.com/library/indicator/arbitrage-detector.md
- [The Echo Forecast](https://www.luxalgo.com/library/indicator/the-echo-forecast/) (Correlation): markdown at https://www.luxalgo.com/library/indicator/the-echo-forecast.md
- [Intermarket Swing Projection](https://www.luxalgo.com/library/indicator/intermarket-swing-projection/) (Correlation): markdown at https://www.luxalgo.com/library/indicator/intermarket-swing-projection.md
- [Arbitrage Matrix](https://www.luxalgo.com/library/indicator/arbitrage-matrix/) (Correlation): markdown at https://www.luxalgo.com/library/indicator/arbitrage-matrix.md
- [Correlated Sine Oscillator](https://www.luxalgo.com/library/indicator/correlated-sine-oscillator/) (Correlation): markdown at https://www.luxalgo.com/library/indicator/correlated-sine-oscillator.md
- [Sector Correlation Matrix](https://www.luxalgo.com/library/indicator/sector-correlation-matrix/) (Correlation): markdown at https://www.luxalgo.com/library/indicator/sector-correlation-matrix.md
- [Correlation Clusters](https://www.luxalgo.com/library/indicator/correlation-clusters/) (Correlation): markdown at https://www.luxalgo.com/library/indicator/correlation-clusters.md
- [Historical Correlation](https://www.luxalgo.com/library/indicator/historical-correlation/) (Correlation): markdown at https://www.luxalgo.com/library/indicator/historical-correlation.md

---

Source: https://www.luxalgo.com/library/indicator/lead-lag-detection/ (LuxAlgo Library). Free to use with attribution: https://www.luxalgo.com/library/license/
