The code in the post contains a simple correlation indicator that can be used as an alternative to Tradingview’s built-in “Correlation Coefficient” indicator. The indicator allows users to correlate up to 3 separate instruments on the same subplot. This allows you, for example, to easily see the correlation of your instrument with stocks, bonds and FX. Alternatively, a user can also see the correlation with sector, industry peers or any other data available in Tradingview.

### What is Correlation?

Before we jump ahead into the code, let’s not assume that all readers are familiar with correlation and how it can be applied to trading. The term correlation is used to describe a relationship between two variables, items, objects or things. Correlated variables/data move in a proportional manner to each other and as such, this is where we infer/assume that they have some sort of a relationship to each other. The relationship could be a “cause and effect” type relationship where one variable is driving the other or they might both be affected by the same external factor.

The following terms can be used to describe the type of correlation between variables:

• Postive Correlation: Is used when`variable A`increases and`variable B`also increases at the same (or similar time). For example, one could say that there is a correlation between the number of Big Mac’s a person eats per week and their body fat.
• Negative Correlation: Also known as an inverse correlation, describes the relationship where `variable A` increases whilst `variable B` decreases. Taking the Big Mac example a step further, one might say that there is an inverse correlation between Big Mac’s eaten and life expectancy.
• Spurious Correlation: This is the dangerous one. It is a term given to two items or datasets that appear to have a high correlation but are not actually related. This can easily happen with data sets which are both rising or falling by a proportional amount over a given measurement period. There is a great website that lists some amusing spurious correlations and will allow you to quickly realize that not all correlated items have a valid relationship to each other. You can find that here: http://www.tylervigen.com/spurious-correlations. I particularly enjoyed the correlation between the US cheese consumption per capita and the number of people who died by becoming tangled in their bedsheets. Stay away from cheese supper folks!

#### Correlation Coefficient

Now we know how to describe correlations, we need some way to measure it. This is where a correlation coefficient comes into play. It is a mathematical formula that results in a numerical measure of the correlation between  -1 and 1. A more detailed breakdown of how to read the values produced by the correlation coefficient shall be provided in the commentary section below.

### Use Cases

Having a grasp on correlation can help a trader anticipate price movements and understand the relationship between different instruments or asset classes. If there is a known correlation, the trader can look at the other asset to see if it is confirming a setup or perhaps get a better understanding on what is driving the current price action.

Correlation is also essential for a type of strategy known as pairs trading. In this strategy, a trader will monitor a pair of stocks which are highly correlated over the long term. E.g Coca-Cola and Pepsi or Ford and GM. If for some reason they become uncorrelated over the short term, the trader will simultaneously go long on one stock and short on the other until they become correlated once more. The hope is that that one of the stocks will have to move further before they are correlated again resulting in a profit.

Finally, for investors, understanding correlation can help to ensure that their portfolio is diversified and reduce volatility by investing in assets that are not highly correlated to each other.

## Commentary and Notes

Tradingview provides a handy `correlation()`function that made this task nice and simple. Because of this, the bulk of the code centres on importing data and plotting it nicely. As such, this code provides some useful level guides to allow a trader to easily see key areas. The levels were taken from a nice Dummies article on how to interpret correlation coefficient levels.

In statistics, the correlation coefficient r measures the strength and direction of a linear relationship between two variables on a scatterplot. The value of r is always between +1 and –1. To interpret its value, see which of the following values your correlation r is closest to:

• Exactly 1. A perfect downhill (negative) linear relationship

• 0.70. A strong downhill (negative) linear relationship

• 0.50. A moderate downhill (negative) relationship

• 0.30. A weak downhill (negative) linear relationship

• 0. No linear relationship

• +0.30. A weak uphill (positive) linear relationship

• +0.50. A moderate uphill (positive) relationship

• +0.70. A strong uphill (positive) linear relationship

• Exactly +1. A perfect uphill (positive) linear relationship

As you will see in the chart examples below, the key zones are colour coded on the indicator. Above 70 indicates a strong positive correlation and has a green background colour. Conversely, anything below -70 indicates a strong negative correlation. This has a red background colour in the charts. Finally, values between 30 and -30 indicate that the instruments are not correlated. This is equally valuable information and is therefore highlighted.

The code only allows for 3 instruments to be correlated to each other. I found that adding more instruments reduced the readability of the indicator significantly. However, if you want to add more, some simple copy and pasting will suffice.

To look at long term or short term correlations, the period should be changed. The script default is 200, which like a 200-day moving average provides a long-term view of the correlation without too much noise.

### On the Charts

Default Settings: National Grid correlations with FTSE100, UK Gov Bonds, and GBPUSD.

In this example we can see there has been a long-term correlation between National Grid and UK Government bonds.

Long Term vs Short Term Correlation:

Next, we have Ford vs GM and S&P500

In this example, GM is indicated as the green line. The GM closing price is also overlayed on the main chart. We can see that over the long run, Ford and GM are highly correlated. However, over the short term in December, GM dropped about 9% whilst Ford steadily rose. This left a delta of about 14% which eventually closed when the two stocks resumed their longer-term correlation.

## Find This Post Useful?

If this post saved you time and effort, please consider support the site! There are many ways to support us and some won’t even cost you a penny.