DEV Community

Amit Wani
Amit Wani

Posted on

Correlation Analysis Between Stocks using Python

Hello everyone, this article is about the tutorial on how to do the correlation analysis of Indian Bank stocks. We will compare major private and public banking stocks listed on the stock exchange.

I will be using Python for data analysis for this one.

Install Dependencies

First, we need to install the required dependencies

pip install pandas
pip install numpy
pip install yfinance
pip install seaborn
Enter fullscreen mode Exit fullscreen mode

Import Dependencies

Now, we will import these required dependencies into our code.

import pandas as pd
import numpy as np
import yfinance as yf
import seaborn as sns
Enter fullscreen mode Exit fullscreen mode

Fetching Stock Price Data

I have taken the below stocks for correlation analysis and stored them in a list. You can see I have added their ticker symbols ending with ".NS" these are picked up from Yahoo Finance since we will be fetching data from Yahoo Finance.

tickers = ['HDFCBANK.NS', 'ICICIBANK.NS', 'AXISBANK.NS', 'SBIN.NS', 'KOTAKBANK.NS', 'INDUSINDBK.NS', 'BANKBARODA.NS']
Enter fullscreen mode Exit fullscreen mode

Now we will fetch data for these stocks from Yahoo Finance using yfinance. I am fetching data of 10 years with auto_adjust=True for getting close price data adjusted for corporate actions. Then storing them into another list called data.

data = []

for ticker in tickers:
    ytick = yf.Ticker(ticker)
    df = ytick.history(period="10y", auto_adjust=True, threads = True)
    df = df[df['Close'] > 0]

    data.append(df)
Enter fullscreen mode Exit fullscreen mode

Manipulating Data

Now, we have a list with multiple panda data frames. For creating a correlation matrix, we need to merge them into a single data frame with only the closing price data of all the stocks.

For this to achieve, we will first use the zip function to zip all the data frames and then use a dictionary to associate the data frame with the stock using the dict function. Finally, we will use the concat function of pandas to concatenate all these data frames from dict with appropriate columns.

mergedDf = pd.concat(dict(zip(tickers, data)), axis=1)
Enter fullscreen mode Exit fullscreen mode

The new data frame should look something like this

image.png

Now since here we have multiple columns of data, but we don't need it, that's why we will only keep Close column data.
We will get the Level 1 values and filter only with the Close column.

closeDf = mergedDf.loc[:,mergedDf.columns.get_level_values(1).isin(['Close'])]
Enter fullscreen mode Exit fullscreen mode

It will now look like this

image.png

But we also don't need these two levels of headers now. So we can drop Level 1 and only keep symbol tickers as the column header.

closeDf.columns = closeDf.columns.droplevel(1)
Enter fullscreen mode Exit fullscreen mode

It will now look like this

image.png

Correlation Analysis

Now we have close price data for all stocks in a single data frame. We now need to calculate daily returns so that we can then create a correlation matrix from that data. But, simple pct_change returns won't work as every stock has a different base as well, so we will need to calculate daily log-returns of all stocks. We can do this by doing the following:

logretDf = np.log(closeDf.pct_change() + 1)
Enter fullscreen mode Exit fullscreen mode

It will now look like this

image.png

Now we have a data frame from which we can get various insights regarding correlation, standard variation etc.
We can show the correlation matrix by

logretDf.corr()
Enter fullscreen mode Exit fullscreen mode

You will see something like this

image.png

This shows us the correlation percentage of all stocks with each other over the past 10 years of data.
To visualize this better we can use seaborn to plot it in a graphical fashion like below

sns.heatmap(logretDf.corr())
Enter fullscreen mode Exit fullscreen mode

You will see something like this

image.png

This will show us a much easier view of the correlation between various banking stocks.

If you got this far, means now you can do correlation analysis of any number of stocks. If you like what I write, please do share it on social media. Till next time, peace.!

Top comments (1)

Collapse
 
jacquelinebuo profile image
Jacqueline-buo

I am a seasoned Crypto Investment Manager with a strong track record of guiding clients towards financial success through strategic investments in cryptocurrencies.
I am reaching out to you today because I believe you have the potential to benefit greatly from the world of digital currency and the opportunities it presents. Bitcoin, in particular, has emerged as a revolutionary asset class, and I want to offer my expertise to help you navigate this exciting and lucrative space.
Email::: jacquiline.buo@gmail.com
facebook.com/jacquelinebu0