Shilpa Santosh Engineer and Aspiring Python Coder at Marketcalls. Love to explore the complexity of the Financial Markets.

A Quick Start Guide to Compute Correlation Matrix in Python using NSEpy & Pandas

1 min read

Here is a quick tutorial in python to compute Correlation Matrix between multiple stock instruments using python packages like NSEpy & Pandas. Generally Correlation Coefficient is a statistical measure that reflects the correlation between two stocks/financial instruments. Determining the relationship between two securities is useful for analyzing intermarket relationships, sector/stock relationships and sector/market relationships.




Here are some of the essential python libraries required for Correlation Matrix Data Visualization

IPython (Interactive Python)
Pandas (Python Library to handle time-series data )
NSEpy (Fetch Historical data from NSEindia – NSEpy 0.3 ver or higher)
Matplotlib (Python library to handle 2D plotting)

Import the required python modules

i)from nsepy.archives we need to import the get_price_history:-for fetching the stock pricing details
ii) from datetime import we need to import date object :- for giving the date limit for the required stocks
iii) import pandas :- for the creation of dataframe
iv)import matplotlib for plotting the correlation heatmap

Create a list of stocks

Fetch Historical Data
Now fetch the price history of each stock based on a specific time limit and appending the last close value into an empty pandas data frame

Compute and Plot Correlation Matrix
Now compute the percentage change and Pearson correlation using the pandas dataframe functions pct_change() , corr() and plot the correlation matrix using
matplotlib as shown below.

Note : %pylab inline is used as a display variable on ipython notebook.

Sample IPython Notebook to generate Stock Correlation Matrix Map as shown below :

Shilpa Santosh Engineer and Aspiring Python Coder at Marketcalls. Love to explore the complexity of the Financial Markets.

How to Retrieve Opening Balance, Total Turnover, Realized and…

This tutorial explores how to access the limits API functionality in Algomojo using python to retrieve the Account Balance, Total Turnover, Realized, UnRealized PNL,...
Rajandran R
1 min read

Introduction to Backtrader – Creating your First Trading Strategy…

Backtrader is an open-source python framework for backtesting, optimizing, and deploying live algorithmic trading strategies.
Rajandran R
54 sec read

Getting Started with Google Colab

Google Colab also known as Google Colaboratory is a product from Google Research which allows user to run their python code from their browser...
Rajandran R
1 min read

Get Notifications, Alerts on Market Updates, Trading Tools, Automation & More