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 :