# Correlation Coefficient

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

Computes the Pearson correlation coefficient between the chart symbol and a second user-selected symbol over a rolling window, plotted as an oscillator between -1 and +1. Defaults are a comparison symbol input and a 20-bar length, with an option to correlate log returns instead of raw prices. This is the standard raw correlation coefficient calculation.

- Indicator in the LuxAlgo Library, by LuxAlgo, published Apr 13, 2020.
- Access: free.
- Concepts: [Correlation](https://www.luxalgo.com/library/concept/correlation/) (primary)
- Open on Vela: https://vela.luxalgo.com/chart?pineScriptCodeId=aybzz0lppqg8ni9lddt1r3pf
- Page: https://www.luxalgo.com/library/indicator/correlation-coefficient/

The **Correlation Coefficient** measures how consistently the chart symbol moves with a second, user-selected symbol, plotting the Pearson correlation over a rolling window as an oscillator bounded between -1 and +1. A reading near +1 says the two series rise and fall together; near -1, they move opposite ways; near zero, no stable linear relationship exists.

This build is the standard raw calculation: the chart symbol is compared against a comparison symbol input (the S&P 500 by default) over a 20-bar window, with an option to correlate log returns instead of raw prices. The Correlation line is drawn with a soft area fill, framed by dotted levels at +1, zero, and -1.

## How to Trade the Correlation Coefficient?

- **Near +1**: the two symbols move in near-lockstep; holding both adds exposure, not diversification.
- **Near -1**: consistent opposite movement, the profile a hedge is supposed to have.
- **Around zero**: no dependable relationship; treat the pair as independent.
- **Zero-line crosses**: the sign has flipped; alert conditions cover crosses above and below zero.
- **Drift from an extreme toward zero**: a once-tight relationship is decoupling.

Correlation frames trades rather than triggering them: use it to size, filter, or veto ideas generated elsewhere.

## Correlation Coefficient Settings

- **Symbol** (default SP:SPX): the instrument the chart symbol is measured against.
- **Length** (default 20): bars in the rolling window. Shorter reacts fast but whipsaws; longer is steadier but slow to register a real break.
- **Source** (default close): the price series used for both symbols.
- **Use Log Returns** (default off): correlates bar-to-bar log returns instead of raw values, stripping the trend effect that inflates raw-price readings.

## Frequently Asked Questions

### Should I correlate raw prices or log returns?

Log returns, for most decisions. Two instruments in long uptrends can post high raw-price correlation regardless of how their individual bars behave; Use Log Returns measures how the bars themselves move together instead.

### How is the Correlation Coefficient different from beta?

Correlation grades the consistency of co-movement on a fixed -1 to +1 scale, but says nothing about size. Beta scales it: a stock can track an index tightly while moving twice as far on every swing. Correlation captures the first fact and misses the second.

### Does a high correlation make two symbols a good pairs trade?

Not by itself. Tightly correlated instruments can still drift apart indefinitely, so pairs traders use the coefficient as a first screen and test the spread separately. And the rolling 20-bar sample can swing hard, so confirm on a longer Length before trusting it.

## Related indicators

- [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
- [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
- [Arbitrage Detector](https://www.luxalgo.com/library/indicator/arbitrage-detector/) (Correlation): markdown at https://www.luxalgo.com/library/indicator/arbitrage-detector.md
- [Arbitrage Matrix](https://www.luxalgo.com/library/indicator/arbitrage-matrix/) (Correlation): markdown at https://www.luxalgo.com/library/indicator/arbitrage-matrix.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
- [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
- [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

---

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