Ticker Correlation Matrix Table and Heatmap [SS]

Hello everyone,

I am in the process of releasing some of my own utility indicators/things I use to reference and perform analyses.
I do a lot of quantitative/math based analyses, including correlation assessments that I traditionally would need to export data from Tradingview and perform in SPSS, Excel or R. I have been slowly building a repertoire of Excel/R functionality right on pinescript so I do not need to constantly export data and can perform the assessments right on Tradingview.

This is an example of such an indicator.

About the Indicator:
It is a correlation table/matrix indicator. It will allow up to 10 ticker inputs, which can be stocks, economic data, anything available on Tradingview, and it will perform a correlation assessment in a matrix / heatmap style.

The indicator will show the various correlations among all of the selected ticker inputs and will colour them based on correlation strength and type.

Strong negative correlations will appear bright red.
Strong positive correlations will appear bright green.
Complete absence of correlation (i.e. 0) will show bright orange.
The rest will show a darker shade to indicate less strength/correlation.

Calculation Functions

In addition to outputting a correlation matrix, the indicator is also able to express the relationship between tickers in a linear expression using the y = mx + b formula.
If we look at table, we can see that MSFT and AAPL have a significantly strong correlation of 0.82.

If we wanted to express this relationship mathmatically, we can ask the indicator to represent the linear relationship in our y = mx + b format. We simply toggle to our menu and select the Convert From MSFT (Ticker 2) and convert to APPL (Ticker 3):

When we select this, a new table will populate below and give you the expression as well as the amount of error associated with it:

In this case, we can see that the equation is y = 0.553x + 0.626 with a range of around 10 points in either direction.
This means that, to convert MSFT to AAPL, we would multiply the MSFT price by 0.553 and then add 0.626. So if we try it, MSFT closed at 328.41. So we substitute:

AAPL price = 0.553(328.41) + 0.626
AAPL price = 181.61 + 0.626
AAPL Price = 182.24 +/- 10

AAPL actually closed at 184.12. So pretty good. If we try another, let's do SPY to XLF:

So we substitute, SPY closed at 449.16.

XLF Price = 449.16(0.077) + 0.084
XLF price = 34.59 + 0.084
XLF price = 34.67

XLF actually closed at 34.49.

This is handy if you want to see how one stock price may affect another. If you are long on one stock and short on another, you can use this to determine what the likely outcome may be for the alternative stock. However, I recommend only performing this on tickers that have a relationship of 0.7 or higher, or a relationship of -0.7 or lower.

I always had to use SPSS to do this, so being able to do this right in Pinescript for me is a huge convenience!

Some other uses:

As I tend to post educational stuff on Tradingview and I frequently use correlation matrices, I have formatted the indicator to be more aesthetically pleasing for these purposes. Thus, you can unselect extra ticker slots that you do not need. IF I only need to display 3 tickers, I can unselect tickers 4 - 10. The end result is a cleaner table:

Essential Functions:

The assessment length is defaulted to 75 candles on the daily timeframe. Be sure to have the daily timeframe opened when you are viewing the indicator.
You can increase or decrease the assessment length as you desire.
You can also specify the source. The source is defaulted to close, but if you want to see the direct correlation of ticker's highs and/or lows, you can modify the source input in the settings menu to look at this.

Just remember to have the chart opened to whatever timeframe you are looking at.

And that's the indicator! Hopefully you find it helpful. Its more of an academic indicator, but it is performing a function that I personally use frequently in analyses, so I hope you may also benefit from it as well!

Thanks for checking it out! Safe trades everyone!

Script open-source

Dans le véritable esprit de TradingView, l'auteur de ce script l'a publié en open-source, afin que les traders puissent le comprendre et le vérifier. Bravo à l'auteur! Vous pouvez l'utiliser gratuitement, mais la réutilisation de ce code dans une publication est régie par le règlement. Vous pouvez le mettre en favori pour l'utiliser sur un graphique.

Clause de non-responsabilité

Les informations et les publications ne sont pas destinées à être, et ne constituent pas, des conseils ou des recommandations en matière de finance, d'investissement, de trading ou d'autres types de conseils fournis ou approuvés par TradingView. Pour en savoir plus, consultez les Conditions d'utilisation.

Vous voulez utiliser ce script sur un graphique ?