Home » Technical Analysis Functions » Understanding the ta.correlation() Function in Pine Script

Understanding the ta.correlation() Function in Pine Script

Photo of author
Published on

The ta.correlation() function is a powerful tool in Pine Script for traders and analysts looking to measure how two data series move in relation to each other. This function calculates the correlation coefficient, a statistical measure that quantifies the degree to which two variables tend to deviate from their simple moving averages (SMA) over a specified period. Let’s delve into its syntax, arguments, and practical applications to understand how it can be leveraged in trading strategies.


ta.correlation(sourceA, sourceB, periodLength) → series float


  • sourceA (series int/float): This is the first data series, often representing a financial instrument’s price or an indicator’s values.
  • sourceB (series int/float): The second data series, which could also be a price series or another indicator’s values.
  • periodLength (series int): This argument specifies the length or the number of bars to consider for the correlation calculation.


  • The function returns a floating-point series that represents the correlation coefficient between the two provided series. The return value ranges from -1 to 1, where:
  • 1 indicates a perfect positive correlation (as sourceA increases, sourceB increases at a proportional rate).
  • -1 signifies a perfect negative correlation (as sourceA increases, sourceB decreases at a proportional rate).
  • 0 suggests no correlation; the movements between the two series are completely random and independent.

Practical Example

indicator("Correlation between Closing Prices and Volume", overlay = false)

// Define the source series for correlation
closingPrices = close // Closing prices of the stock
tradingVolume = volume // Trading volume of the stock

// Set the period length for the correlation calculation
timeFrame = 20

// Calculate the correlation between closing prices and volume
priceVolumeCorrelation = ta.correlation(closingPrices, tradingVolume, timeFrame)

// Plot the correlation coefficient
plot(priceVolumeCorrelation, title="Price-Volume Correlation Coefficient", color=color.purple)

Walkthrough of Code

  1. Script Declaration:
    • //@version=5: Specifies the version of Pine Script being used. Version 5 is the latest, offering the most recent features and syntax improvements.
    • indicator("Correlation between Closing Prices and Volume", overlay = false): This line declares the script as an indicator with the title “Correlation between Closing Prices and Volume” and specifies that the indicator should not be overlaid on the main price chart (overlay = false).
  2. Defining Source Series for Correlation:
    • closingPrices = close: Defines a variable closingPrices to store the closing prices of the stock. The close built-in variable represents the closing price of the current bar.
    • tradingVolume = volume: Defines a variable tradingVolume to store the trading volume of the stock. The volume built-in variable holds the trading volume for the current bar.
  3. Setting the Period Length for Correlation Calculation:
    • timeFrame = 20: Sets the variable timeFrame to 20, indicating the number of bars (or period) over which the correlation between the closing prices and trading volume will be calculated. This value can be adjusted based on the desired analysis timeframe.
  4. Calculating the Correlation:
    • priceVolumeCorrelation = ta.correlation(closingPrices, tradingVolume, timeFrame): Utilizes the ta.correlation() function to calculate the correlation coefficient between closingPrices and tradingVolume over the timeFrame specified (20 bars in this case). The result is stored in the variable priceVolumeCorrelation. This function returns a series of floating-point numbers ranging between -1 and 1, where -1 indicates a perfect negative correlation, 0 indicates no correlation, and 1 indicates a perfect positive correlation.
  5. Plotting the Correlation Coefficient:
    • plot(priceVolumeCorrelation, title="Price-Volume Correlation Coefficient", color=color.purple): This line plots the calculated correlation coefficient on the chart. The plot is titled “Price-Volume Correlation Coefficient” and uses a purple color for the line. Since overlay = false was specified in the indicator declaration, this plot will appear in a separate pane below the main price chart.

Key Features and Takeaways

  • Function Usability: The ta.correlation() function is versatile, allowing users to compare any two data series, not just price data. This can include volume, indicators, or even external data series imported into the script.
  • Syntax and Application: Remember that sourceA and sourceB need to be of the same length and type (either integer or float). The length of the period over which the correlation is calculated can significantly affect the outcome, highlighting the importance of choosing a period length that aligns with your trading strategy or analysis goals.
  • Interpreting Results: A high positive correlation might indicate that the assets move together and could be used to hedge or double down on a particular market view. Conversely, a high negative correlation might suggest diversification opportunities.

Leave a Comment