Skip to main content

Stock Correlation Analysis With Google Sheets

Correlation is a statistical relationship that measures how related the movement of one variable is compared to another variable. For example, stock prices fluctuate over time and are correlated accordingly or inversely to one another. Understanding stock correlation and being able to perform analysis are very helpful in managing a stock portfolio investment. In this post, I explain in details how to perform correlation analysis among stocks in Google Sheets.

How to use CORREL function and GOOGLEFINANCE function in Google Sheets

Understanding correlation and its applications in stock investing

The most familiar correlation measure is the Pearson product-moment correlation coefficient. The strength of the relationship between two variables is expressed numerically between -1 and 1. For example:

  • Two stocks are positively correlated when their prices always go up or go down together. Their coefficient is between (0, 1].
  • Two stocks are negatively correlated when one's price goes up while the other goes down or vice versa. Their coefficient is between [-1, 0).
  • Two stocks are uncorrelated if their prices fluctuate independently of each other. Their coefficient is then 0.

In stock investing, from the historical prices of stocks, investors can compute the correlations between them in the past and predict their relationships in the future. Such as:

  • Based on historical correlations of one stock with a market index or a commodity in terms of price, an investor might predict whether the stock will go up or go down in response to a change related to the index or the commodity.
  • Based on historical correlations of stocks, an investor might reduce risk by diversifying a portfolio with stocks that are not highly correlated.

Stock correlation analysis with Google Sheets

As suggested by the Pearson product-moment formula, to compute the correlation coefficient between two stocks, here are what I need to do:

  • Choose a date range to retrieve historical prices of the two stocks
  • Retrieve historical prices of the two stocks
  • Apply the dataset to the Pearson product-moment formula

Those steps are exactly what Google Sheets has to offer:

Finally, here is the step by step of how to use Google Sheets to perform stock correlation analysis:

  • Define a list of stocks
  • Define a time period for historical prices of stocks
  • Use the GOOGLEFINANCE function to fetch historical prices of the stocks during the defined time period
  • Use the CORREL function to compute the correlation coefficient for every two stocks in the list
  • Present all computed correlation coefficients in form of a matrix. The matrix is very good visualization support to present correlations between many stocks (more than two). It can enable the comparison of the strength between a pair of stocks relative to other pairs.
    • The header row and header column of the matrix contain symbols of stocks.
    • Each cell in the matrix is a coefficient between 2 stocks, the one in the header row and the one in the header column of that cell.
  • Apply color scale conditional formatting on the matrix to better visualize the strength of the relationships between the stocks
  • Sort the matrix by a selected stock to identify clusters that are highly correlated (positively or negatively)
  • Draw conclusions from the matrix visualization

Getting started

I have made available a spreadsheet in Google Sheets to perform stock correlation analysis. You can follow the steps below to getting started with it.

  • Make a copy of the spreadsheet
  • Create a sheet that contains stocks you want to perform correlations analysis. The sheet must contain 2 columns: Symbol and Name as same as the sample sheets CAC 40, Dow Jones, SP500 ETFs, and My Portfolio. You can name the sheet whatever you want. Please note that symbols must be recognizable by the GOOGLEFINANCE function.
  • Define parameters of the analysis in the Configuration sheet. The required parameters are:
    • Stocks Sheet Name
    • Historical Prices Start Date
    • Historical Prices End Date
    • Historical Prices Interval
    • Sort Correlation Matrix By Symbol

  • Through the items in the menu Stock Correlation Analysis, perform the steps below in order:
    • 0 Clear current analysis
    • 1 Generate Historical Prices Sheets
    • 2 Generate Correlation Price Data Sheet
    • 3 Generate Correlation Data Sheet
    • 4 Generate Correlation Matrix Sheet

User guide

  • The Configuration sheet contains all parameters for the analysis, such as time period, list of stocks, colors for conditional formatting the correlations matrix, etc. You can find directly in the sheet description for each parameter.
How to do correlation analysis in Google Sheets
  • The Correlation Price Data sheet contains all historical prices of all stocks during the defined time period.
  • The Correlation Data sheet contains all correlation coefficients that are results of applying the CORREL function on the dataset in the Correlation Price Data sheet.
  • The Correlation Matrix sheet visualizes correlation coefficients in form of a matrix with an eye-catching conditional format. The last column in the matrix is reserved for the average of correlation coefficients of stock in the header column vis-a-vis the other stocks. The visualization of the matrix is fully customizable via the parameters in the Configuration sheet.
    • You can choose different colors for the color scale of the matrix.
    • You can choose between Name or Symbol of stocks to display in the header row and the header column of the matrix.
    • You can choose how the matrix is sorted to reveal interesting clusters.

  • The ready-to-use script helps to do steps of correlation analysis efficiently. The script will add automatically the Stock Correlation Analysis menu to the spreadsheet on opening. It is convenient so that you can execute the commands directly from the spreadsheet user interface. The menu contains all the main steps necessary to perform an analysis.
    • 0. Clear current analysis: It will delete all the historical prices sheets that were generated as well as the 3 sheets: Correlation Price Data, Correlation Data, and Correlation Matrix. You should run this command only if you want to perform the correlations analysis on a new list of stocks.
    • 1. Generate Historical Prices Sheets: It will create each historical prices sheet for each stock. Those historical prices sheets are hidden automatically as I rarely need to look at them.
    • 2. Generate Correlation Price Data Sheet: It will aggregate all historical prices sheets above into the Correlation Price Data sheet. I need to do so to facilitate the calculation of correlation coefficients between many stocks.
    • 3. Generate Correlation Data Sheet: It will compute the correlation coefficient for each pair of stocks and put them all into the Correlation Data sheet.
    • 4. Generate Correlation Matrix Sheet: Il will present the coefficients in the Correlation Data sheet in form of a matrix. The matrix is formatting with parameters defined in the Configuration sheet.

Most of the time, you only need to define a list of stocks that you want to perform correlation analysis on them and change parameters in the Configuration to reveal insight between stocks.

Note: If the number of stocks is large, it might cause some delays on the spreadsheet. In this case, you can wait for the script finished or refresh the spreadsheet and retry.

Conclusion

Understanding stock correlation and being able to perform analysis are very helpful in managing a stock portfolio investment. This post explains in detail how to perform correlation analysis among stocks in Google Sheets.

Disclaimer

The post is only for informational purposes and not for trading purposes or financial advice.

Feedback

If you have any feedback, question, or request please:

Support this blog

If you value my work, please support me with as little as a cup of coffee! I appreciate it. Thank you!

Share with your friends

If you read it this far, I hope you have enjoyed the content of this post. If you like it, share it with your friends!

Comments

Popular posts

How to convert column index into letters with Google Sheets

How to convert column index into letters with Google Sheets

In Google Sheets, rows are indexed numerically, starting from 1, but columns are indexed alphabetically, starting from A. Hence, it is pretty straightforward to work with rows and trickier to work with columns as we need to convert between column index and corresponding letters. For example, what are the letters of column 999th in Google Sheets? In this post, we will look at how to convert a column index into its corresponding letters by using the built-in functions of Google Sheets. What are letters of the column 999th in a spreadsheet? Unfortunately, Google Sheets does not provide a ready-to-use function that takes a column index as an input and returns corresponding letters as output. However, there is a little trick of combining other available built-in functions to do the task. In Google Sheets, there is the ADDRESS function that returns the cell reference, according to the specified row index (first parameter) and column index (second parameter) in the input. For example,
Create a dividend income tracker with Google Sheets by simply using pivot tables

Create a dividend income tracker with Google Sheets by simply using pivot tables

As my investment strategy is to buy stocks that pay regular and stable dividends during a long-term period, I need to monitor my dividends income by stocks, by months, and by years, so that I can answer quickly and exactly the following questions: How much dividend did I receive on a given month and a given year? How much dividend did I receive for a given stock in a given year? Have a given stock's annual dividend per share kept increasing gradually over years? Have a given stock's annual dividend yield been stable over years? In this post, I explain how to create a dividend tracker for a stock investment portfolio with Google Sheets by simply using pivot tables.
Use SPARKLINE to create 52-week range price indicator chart for stocks in Google Sheets

Use SPARKLINE to create 52-week range price indicator chart for stocks in Google Sheets

The 52-week range price indicator chart shows the relative position of the current price compared to the 52-week low and the 52-week high price. It visualizes whether the current price is closer to the 52-week low or the 52-week high price. In this post, I explain how to create a 52-week range price indicator chart for stocks by using the SPARKLINE function and the GOOGLEFINANCE function in Google Sheets.
Compute daily evolution of a stock investment portfolio by using only built-in functions of Google Sheets

Compute daily evolution of a stock investment portfolio by using only built-in functions of Google Sheets

To effectively track a stock investment portfolio, it is necessary to know its evolution in the past. As I use Google Sheets to track my stock investment portfolio, I have researched and successfully implemented several solutions. In this post, I am happy to share in detail how to compute the daily evolution of a stock investment portfolio by simply using only the available built-in functions in Google Sheets.
How to convert column index into letters with Google Apps Script

How to convert column index into letters with Google Apps Script

Although Google Sheets does not provide a ready-to-use function that takes a column index as an input and returns corresponding letters as output, we can still do the task by leveraging other built-in functions ADDRESS , REGEXEXTRACT , INDEX , SPLIT as shown in the post . However, in form of a formula, that solution is not applicable for scripting with Google Apps Script. In this post, we look at how to write a utility function with Google Apps Script that converts column index into corresponding letters.
Create dividend income tracker with Google Data Studio

Create dividend income tracker with Google Data Studio

With transactions registered, it is easy to create a dividend income tracker with Google Sheets. However, a dividend income tracker in Google Sheets is not interactive. Instead of having different pivot tables and switching forth and back among them, I can create an interactive dividend income tracker with a single-page report on Google Data Studio. In this post, I explain how to create a dividend income tracker with Google Data Studio.
Slice array in Google Sheets

Slice array in Google Sheets

Many functions in Google Sheets return an array as the result. However, I find that there is a lack of built-in support functions in Google Sheets when working with an array. For example, the GOOGLEFINANCE function can return the historical prices of a stock as a table of two columns and the first-row being headers Date and Close. How can I ignore the headers or remove the headers from the results?
Compute cost basis of stocks with FIFO method in Google Sheets

Compute cost basis of stocks with FIFO method in Google Sheets

After selling a portion of my holdings in a stock, the cost basis for the remain shares of that stock in my portfolio is not simply the sum of all transactions. When selling, I need to decide which shares I want to sell. One of the most common accounting methods is FIFO (first in, first out), meaning that the shares I bought earliest will be the shares I sell first. As you might already know, I use Google Sheets extensively to manage my stock portfolio investment, but, at the moment of writing this post, I find that Google Sheets does not provide a built-in formula for FIFO. Luckily, with lots of effort, I succeeded in building my own FIFO solution in Google Sheets, and I want to share it on this blog. In this post, I explain how to implement FIFO method in Google Sheets to compute cost basis in stocks investing.
Compare stock portfolio to market indexes

Compare stock portfolio to market indexes

As investors, we always want to see our portfolio grows over time. If a portfolio made a 1000$ of gain in one year, is it good enough? Should we gauge the portfolio's performance against an alternative investment, for instance, a market index? Should we aim to beat that index? In this post, we will see how to compare a stock portfolio to market indexes by using Google Sheets, Apps Script, and Google Data Studio. Select market indexes to compare Method to compare Guides Prepare data with Google Sheets and Apps Script Visualize benchmark in Google Data Studio Demo Conclusion Note References Select market indexes to compare In the context of stock investment, investors can compare the performance of their portfolios over a period of time with an alternative investment such as a market index, a portfolio of another investor, or even a single stock if they don't want any diversification, etc. In general, a market index is a good benchmark because it represents t
How to copy data in Google Sheets as HTML table

How to copy data in Google Sheets as HTML table

I often need to extract some sample data in Google Sheets and present it in my blog as an HTML table. However, when copying a selected range in Google Sheets and paste it outside the Google Sheets, I only get plain text. In this post, I explain how to copy data in Google Sheets as an HTML table by writing a small Apps Script program.