Correlation Meter Indicator





This simple script calculates the covariance and correlation coefficient between two markets using arrays.

How It Works

It’s a simple indicator, but very useful for determining how correlated your preferred markets to trade are.

A correlation reading of +1.0 means the markets are perfectly positively correlated, a reading of -1.0 means they are perfectly negatively correlated.

If you’re not sure what correlation & covariance are then here are some useful definitions:

Covariance
Correlation Coefficient

For traders this can be useful for deciding how much risk to spread across two markets that have a high correlation, or how to hedge existing positions by trading a negatively correlated market.

For investors this can be useful for building a truly diversified portfolio.

If a market has a high positive correlation, the black line will stay above zero most of the time. If a market has a high negative correlation, the black line will stay below zero most of the time.

A market with no or little correlation will bounce between the two or hover around zero most of the time.

The example market above is comparing Apple’s weekly price action to the S&P500’s over the past 20 weeks. It has a high positive correlation as the black line is above zero most of the time.

Good luck with your trading!

Settings


Lookback: How many bars to perform the calculation on.

Source: Price source to calculate the correlation on.

Reference Market: The reference market to compare to the current market.


Pine Script Basics Course
 

Video Lesson

Here’s a YouTube video I recorded which takes you step-by-step through each line of code:


Source Code

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ZenAndTheArtOfTrading / www.PineScriptMastery.com
// This script calculates the covariance and correlation coefficient between two array data sets
// @version=4
study("Correlation Meter", overlay=false)

// Get user input 
lookback    = input(title="Lookback", type=input.integer, defval=20) 
source      = input(title="Source", type=input.source, defval=close)
reference   = input(title="Reference Market", type=input.string, defval="OANDA:SPX500USD")

// Get % change of reference data source
referenceData = security(symbol=reference, resolution=timeframe.period, expression=source)
referenceChange = ((referenceData - referenceData[1]) / referenceData[1]) * 100

// Get % change of current market
currentData = source
currentChange = ((currentData - currentData[1]) / currentData[1]) * 100

// Declare arrays
var referenceArray = array.new_float(lookback)
var currentArray = array.new_float(lookback)

// Shift (remove) the last (first entered) value from our arrays (FIFO)
array.shift(referenceArray)
array.shift(currentArray)

// Add the current values to our arrays
array.push(referenceArray, referenceChange)
array.push(currentArray, currentChange)

// Determine & plot our covariance relationship
covariance = array.covariance(currentArray, referenceArray)
plot(covariance, color=color.purple, style=plot.style_area, transp=0, display=display.none, title="Covariance")

// Plot our reference data
plot(referenceChange, color=color.red, style=plot.style_columns, transp=10, display=display.none, title="Reference Market")
plot(currentChange, color=color.blue, style=plot.style_histogram, linewidth=4, display=display.none, title="Current Market")

// Determine the standard deviation of our arrays
referenceDev = array.stdev(referenceArray)
currentDev = array.stdev(currentArray)
correlation = covariance / (referenceDev * currentDev)
plot(correlation, color=color.black, linewidth=2, style=plot.style_stepline, title="Correlation Strength")

// Plot reference lines
hline(price=1.0)
hline(price=-1.0)
hline(price=0.0)

Last Updated: 3rd March, 2021


Free Premium Charts!

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments