How to create an inter-asset correlation matrix with Google Sheets

[Last Updated: 1/8/2025]

This article shows you, step by step, how to create a dynamic inter-asset correlation matrix using Google Sheets. Then, we’ll look into making it more understandable using Conditional Formatting and Bar Charts.

If you’d like access to the spreadsheet, please sign up for the Quantastic Research newsletter and send a subsequent email to quantasticresearch.blog@gmail.com.

Step 1: Setting Up the Sheets

The first step is to set up the spreadsheets. To do this, we’ll use the GOOGLEFINANCE function provided by Google Sheets.

We’re going to create three sheets. The first is called Correlation, the second is called Data, and the third is called Parameters.

The Correlation sheet will contain the list of Tickers we want to run a correlation on.

Figure 1: Ticker list in Correlation spreadsheet.
Figure 1: Ticker list in Correlation spreadsheet.

Cell C2 has the following formula. To apply to the rest, simply click and drag, or double click the blue dot that appears in the bottom right corner of the cell.

=IFNA(GOOGLEFINANCE(B2, "name"), "")

The Parameters sheet will hold the following parameters for data collection:

  • End Date
  • Number of Years

The end date is the date up to which the data will be imported into Google Sheets. The number of years is the number of years since that ending date to import.

For example, if today is January 8th, 2025 and I want to run a correlation on the last year of stock data, then I would put 1/8/2025 as the End Date and 1 for Number of Years.

Figure 2: Parameters
Figure 2: Parameters

Step 2: Import the Data

Now we’re going to import the data. For now, we’re going to focus on importing the data from the first stock ticker in the list (for us, that’s SPY).

Navigate to the Data spreadsheet. In cell A2, use this formula to extract the ticker from the Correlations sheet:

=Correlations!B2

To add the name of the equity, populate B2 with the following:

=Correlations!C2

You should have:

Figure 3: Correlations sheet, ticker #1
Figure 3: Correlations sheet, ticker #1

Now, we’re going to skip a row to separate the data and populate cell A4 with the following formula:

=GOOGLEFINANCE(A2,"close",Parameters!$B$3-(365*Parameters!$B$2),Parameters!$B$3, "DAILY")

The formula above does the following:

  1. Refers to the ticker symbol in cell A2.
  2. Finds all the closing prices (see the “close” keyword in the second argument)
  3. Computes the starting date from the parameters given in the Parameters sheet. That computation involves taking the end date and subtracting the number of days (365 * number of years).
  4. The ending date is given in cell B3 of the Parameters sheet.
  5. We are only considering “DAILY” price data.

It should populate the following:

Figure 4: Historical stock data imported into the Data spreadsheet.
Figure 4: Historical stock data imported into the Data spreadsheet.

Step 3: Building the Correlation Matrix

Now it’s time to build the correlation matrix!

Navigate to the Correlations sheet and create the skeleton for the correlation matrix. Column E is just basic text (Ticker 1, Ticker 2, etc.). Row 1 is the same.

Column F has locked references to the Ticker list in column B ($B$2, $B$3, etc.). This allows you to copy and paste the contents using CTRL+C and, in cell G3, right click -> Paste Special -> Transpose.

Figure 8: Correlation matrix, unpopulated.
Figure 8: Correlation matrix, unpopulated.

We know that the diagonals should be all ones, so let’s do that now. I also added some borders to better view the correlation coefficients.

Figure 9: Populating the diagonal.
Figure 9: Populating the diagonal.

Using Named Ranges

Now, this goes a lot smoother if we use Named Ranges. Navigate to the Data spreadsheet. Then, from the top menu, select Named Ranges.

Figure 10: Named Ranges in the Data menu in Google Sheets.
Figure 10: Named Ranges in the Data menu.

We’re going to name the Close price data in column B “TICKER1”. The range starts at B5 and extends all the way to the bottom of the spreadsheet.

Click Add a Range. Then name it and specify the range.

Figure 11: Specifying a Named Range in Google Sheets.
Figure 11: Specifying a Named Range in Google Sheets.

Do this for the second ticker, third ticker, and so on.

When you’re finished naming the ranges, go back to the Correlations sheet.

Computing the Correlations

In cell G4, use the following formula:

=IFERROR(CORREL(TICKER1,TICKER2),"")

The CORREL function computes the correlation coefficient between two datasets. In this case, TICKER1 (SPY closing data) and TICKER2 (UVXY closing data) are being fed into the correlation function.

The IFERROR part makes it so nothing is displayed if there is an error computing the correlation between the data sets. I find that it makes for a cleaner spreadsheet.

You should get something similar to the following:

Figure 12: Computing correlations for the matrix.
Figure 12: Computing correlations for the matrix.

As you may know, the upper triangle is just a reflection of the bottom half. For example, the correlation between Ticker 1 vs. Ticker 2 is the same as the correlation between Ticker 2 vs. Ticker 1. You can choose whether you’d like to populate those cell with references to the corresponding cells in the lower triangle.

Here’s what it looks like with those cells populated. I’ve added a few more stocks to hit the point home:

Figure 13: Completed Correlation Matrix
Figure 13: Completed Correlation Matrix

Step 4: Visualizing the Matrix

When you use a correlation matrix, you usually are searching for how much a given asset is correlated with the others. You can use the correlation matrix above as-is, but it’s quite hard to visualize at glance.

There are a couple methods we can use to get a better sense of the data.

Conditional Formatting

Highlight the matrix cells (F3:P12).

Figure 14: Highlighted cells slated for conditional formatting.
Figure 14: Highlighted cells slated for conditional formatting.

Then, in the top menu, go to Format > Conditional Formatting:

Figure 15: Conditional Formatting in the top menu, Google Sheets.
Figure 15: Conditional Formatting in the top menu.

The Conditional Formatting tool should appear on the right. Click on Color Scale. You should notice the cells changed their color to various shades of green:

Figure 16: Applying the Color Scale to the correlation matrix.
Figure 16: Applying the Color Scale to the correlation matrix.

For some, this is good enough. Personally, I like to style the correlation matrix using red for negative values and green for positive values.

To do this, click on the Preview and select the “Red to white to green” option. Then, change Minpoint to “Number” and set it to -1. Change Midpoint to “Number” and set it to 0. Change Maxpoint to “Number and set it to 1.

Figure 17: Setting the color bounds and styling the conditional formatting.
Figure 17: Setting the color bounds and styling the conditional formatting.

The result is much easier to navigate:

Figure 18: Conditionally formatted correlation matrix.
Figure 18: Conditionally formatted correlation matrix.

Bar Charts

Let’s say you built the matrix above with the intention to research the correlations in comparison a specific stock (say, SPY).

We can build a bar chart that only shows the correlations with the ticker of interest. To do this, we’re going to add a feature to the Correlation sheet.

Define a cell that will hold the Ticker of Interest, which is a ticker that already exists in the original Ticker list.

Figure 19: Define a cell that will hold a "Ticker of Interest".
Figure 19: Define a cell that will hold a “Ticker of Interest”.

Then, create a table underneath the Ticker of Interest. The cells from A19 to A28 are referencing cells A2 through A11, while the cells from B19 to B28 reference the cells B2 through B11:

Figure 20: Ticker of interest correlation table.
Figure 20: Ticker of interest correlation table.

Using VLOOKUP to Extract the Inter-Asset Correlations

Now pay attention. Cells C19 through C28 will hold the correlations that compare only the ticker of interest with the tickers in column B.

Figure 21: Correlations for the Ticker of Interest.
Figure 21: Correlations for the Ticker of Interest.

The formula in C19 is:

=IFNA(VLOOKUP($C$15,$F$3:$P$12,A19+1,false))

The core part of this line is the VLOOKUP function. The first argument is the “lookup” value, which is the ticker of interest (in this case, SPY).

The second argument is the range from which we are extracting a value. This includes the correlation matrix with the addition of the tickers in column F. It’s important to include the tickers in column F because that is what the VLOOKUP function is using to extract the corresponding correlation values.

Figure 22: VLOOKUP range bounded in RED.
Figure 22: VLOOKUP range bounded in RED.

The third argument is the column we are extracting from within the range (Figure 22, bounded in red). The first column is always column 1, so the correlation values are held in columns 2 through 11. This is why the third argument is A19+1.

Creating the Bar Chart

To create the Bar Chart, highlight cells B18:C28. Then, in the top menu, go to Insert > Chart. The default chart should be something like a bar chart. You can change the name, style, and other aspects of the chart in the chart settings:

Figure 23: Inter-Asset Correlations for SPY compared to the rest of the stocks in the list.
Figure 23: Inter-Asset Correlations for SPY compared to the rest of the stocks in the list.

Now, you can change the “Ticker of Interest” and the chart will update automatically with the corresponding correlations. Here it is for Micron (MU):

Figure 24: Correlations for Micron vs. the rest of the list.
Figure 24: Correlations for Micron vs. the rest of the list.

Creating a Bar Chart with Sorted Correlations

You can also sort them using the SORT function:

Figure 25: Sorting the ticker of interest's correlation coefficients.
Figure 25: Sorting the ticker of interest’s correlation coefficients.

And build a bar chart based on range B31:C41 instead:

Figure 26: Sorted inter-asset correlations for Micron compared to others in the list.
Figure 26: Sorted inter-asset correlations for Micron compared to others in the list.

Author

quantasticresearch.blog@gmail.com

Hi, I'm Dom and I'm a graduate of Electrical Engineering & Computer Science, as well as a long-time user of the Python programming language. With Quantastic Research, I'm aiming to evolve my understanding of data science and machine learning techniques by sharing my experience through blog articles. Anything you find on this website is purely informational and should not be construed as financial or professional advice.

Probabilities of Up and Down Days feature image

Probabilities of Up and Down Days in the S&P500

[Last Updated: 11/24/2024] In this post we’ll be calculating the probabilities and statistics of up days and down days. First, I’ll use...

Read out all
In

Automated Stock Alerts Using the Notion API and Python

I recently wrote an article on using Windows Task Manager to periodically run Python scripts. I currently have a couple scripts automated...

Read out all
In

Automating Python Scripts using Windows Task Scheduler

If you landed here, you’re probably interested in having a script run automatically at specified times on your PC. Specifically, a Python...

Read out all
In

A Comprehensive Guide for Creating NumPy Arrays

NumPy (short for numerical Python) is a useful library for mathematics and data science, specifically for working with arrays of data. In...

Read out all
In

Working with TOML Files in Python

TOML files (Tom’s Obvious Minimal Language) are the brain-child of Mr. Tom Preston-Werner. They were developed as an alternative configuration file format...

Read out all