Analyzing Correlations with Python: Correlation Grid

1. First steps

Setting up a Python development environment

Pipenv automatically creates and manages a virtualenv for your projects, as well as adds/removes packages as you install/uninstall packages. More info about pipenv here.

$ pip install pipenv
$ pipenv shell

Installing packages

$ pipenv install pandas matplotlib quandl

Creating a Quandl account

Go to and create a free account. You can do this tutorial without this step although it’s worth it:

2. Creating the database to store our data

Creating the database

The following function will create one table per future. To check everything is correct, uncomment the lines as specified in the code below:

n tables:  [(19,)]
n futures: 19

Inserting the data into each table

Now it’s time to insert data for each instrument into the database. I have downloaded 20 years of data. From each future, I’m storing: Date, Open, High, Low, Close(Settle).

3. Compiling DataFrames

Read data from the database into a pandas DataFrame. The correlations will be run over the Close of each instrument so discard the remaining columns and add each close to the compiled DataFrame that will be used to visualize the correlations.

4. Visualizing Data

Take the compiled DataFrame and run the correlations of the % Change. Then format the title and the labels.

Run the code

If it’s the first time you execute the code, uncomment the first two lines to create the database and insert data.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store