Calculate Correlation Between Bitcoin, Ethereum, S&P 500 and Nvidia Stocks using Python and YFinance

The Scraper Guy
2 min readSep 19, 2024

Easiest Method to see how related these assets are

Introduction

Correlation is a tool we can use to determine how correlated different types of asset classes are. It is a useful way we can identify highly correlated assets in order to create a portfolio that is well diversified.

Correlation can be calculate using the mathematical formula

We will be using python and pandas to calculate this for us so no need to be a math expert!

Code

Lets begin by importing the libraries

import yfinance as yf
import datetime as dt
import seaborn as sns

Now lets download all of our data using yfinance, feel free to download more or less data. I have elected to just download 5 minute period data over the past 5 days. Consider longer time frames to gather a more accurate idea of correlation long term.

btcData = yf.download('BTC-USD', period="5d", interval="5m")
btcData
spyData = yf.download('SPY', period="5d", interval="5m")
spyData
ethData = yf.download('ETH-USD', period="5d", interval="5m")
ethData
nvdaData = yf.download('NVDA', period="5d", interval="5m")
nvdaData

--

--

The Scraper Guy
The Scraper Guy

Written by The Scraper Guy

Teaching You How to Scrape Using Python LinkTree - https://linktr.ee/scrapingguy

Responses (1)