# Cointegration

Rolling Engle-Granger cointegration testing on the chart: a 252-bar regression of log prices against a comparison symbol yields the hedge ratio, and the residual spread is z-scored and tested for stationarity every bar. Entries fire at z-score crosses of the 2 threshold while the pair tests cointegrated at the 5% level; a dashboard adds half-life and status. For pairs and relative-value traders.

- Indicator in the LuxAlgo Library, by LuxAlgo, published Apr 21, 2026.
- Access: free.
- Concepts: [Cointegration](https://www.luxalgo.com/library/concept/cointegration/) (primary)
- Open on Vela: https://vela.luxalgo.com/chart?pineScriptCodeId=mz91t65aqwfqchb27n8nowe9
- Page: https://www.luxalgo.com/library/indicator/cointegration/

**Cointegration** is a pairs-testing workstation in one pane, the definitive clean implementation of [cointegration](https://www.luxalgo.com/library/concept/cointegration/) for chart use. Every bar it regresses 252 bars of the chart symbol's log prices on a comparison leg (default SP:SPX) for a hedge ratio, rebuilds the residual spread, z-scores it, and runs the Engle-Granger step: the spread's Dickey-Fuller statistic must beat the MacKinnon critical value to test cointegrated. A dashboard reports hedge ratio, z-score, statistic, half-life and status.

## How to Trade the Cointegration?

- **Z-score beyond the entry levels**: a cross above +2 is a short-spread event, below -2 a long-spread event, by default only while the pair tests cointegrated.
- **Return to the mean**: exit events fire when the z-score crosses the exit threshold back toward zero.
- **Cointegration Established / Lost**: transitions carry alerts; a lost relationship stands entries down: a broken pair's deviations have no reason to revert.

## Cointegration Settings

- **Comparison Symbol** (default SP:SPX): second leg of the pair.
- **Source** (default close): price series for both legs.
- **Use Log Prices** (default on): regression and spread on log levels.
- **Formation Window** (default 252): bars behind regression, test and z-score.
- **Significance Level** (default 5%): test strictness; critical values -3.90 / -3.34 / -3.04.
- **Plot Test Statistic** (default off): plots the rolling statistic and its critical value.
- **Entry Threshold** (default 2): z-score arming entry events.
- **Exit Threshold** (default 0): reversion exit level; 0 waits for the full return.
- **Signals Require Cointegration** (default on): gate entries on a passing test.
- **Show Signal Markers** (default off): triangles on entries; alerts fire regardless.
- **Show Dashboard** (default on), with position and size options.
- Style inputs set the colors and the dimming of the spread while the pair fails the test.

## Frequently Asked Questions

### Is cointegration just strong correlation?

No. The [Correlation Coefficient](https://www.luxalgo.com/library/indicator/correlation-coefficient/) scores short-run co-movement of returns; this test interrogates levels: whether the hedge-ratio spread stays tied to a stable mean, the stationarity fading a spread depends on.

### What does the half-life reading mean?

It turns the fitted reversion speed into a bar count, the expected time for half of a deviation to bleed away. A half-life approaching the formation window warns the reversion is too slow to lean on.

### Which formation window should I use?

The default 252 approximates the classic one-year formation on daily charts. Stretching it buys stability at the price of clinging to broken pairs; shrinking it lets go sooner but false-alarms more.

## 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/cointegration/ (LuxAlgo Library). Free to use with attribution: https://www.luxalgo.com/library/license/
