In the previous part, Lamarcus discussed importing Python libraries
numpy as np
pandas as pd. Follow the series with today’s article, which will focus on how to build a heatmap.
Now that we have our libraries, let’s get our data.
#setting start and end dates
#importing Walmart and Target using pandas datareader
Before testing our two stocks for cointegration, let’s take a look at their performance over the period. We’ll create a plot of Walmart* and Target*.
#Creating a figure to plot on
#Creating WMT and TGT plots
plt.title(‘Walmart and Target Over 2014-2016’)
In the above plot, we can see a slight correlation at the beginning of 2014. But this doesn’t really give us a clear idea of the relationship between Walmart and Target. To get a definitive idea of the relationship between the two stocks, we’ll create a correlation heat-map.
To begin creating our correlation heatmap, we must first place Walmart* and Target* prices in the same dataframe. Let’s create a new dataframe for our stocks.
#initializing newDF as a pandas dataframe
#adding WMT closing prices as a column to the newDF
#adding TGT closing prices as a column to the newDF
Now that we have created a new dataframe to hold our Walmart and Target stock prices, let’s take a look at it.
We can see that we have the prices of both our stocks in one place. We are now ready to create a correlation heatmap of our stocks. To this, we will use Python’s Seaborn library. Recall that we imported
Seaborn earlier as sns.
#using seaborn as sns to create a correlation heatmap of WMT and TGT
In the above plot, we called the
corr() method on our
newDF and passed it into Seaborn’s heatmap object. From this visualization, we can see that our two stocks are not that correlated. Let’s create a final visualization to asses this relationship. We’ll use a scatter plot for this.
*Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.
Disclaimer: All investments and trading in the stock market involve risk. Any decisions to place trades in the financial markets, including trading in stock or options or other financial instruments is a personal decision that should only be made after thorough research, including a personal risk and financial assessment and the engagement of professional assistance to the extent you believe necessary. The trading strategies or related information mentioned in this article is for informational purposes only.
Disclosure: Interactive Brokers
Information posted on IBKR Traders’ Insight that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Traders’ Insight are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.
This material is from QuantInsti and is being posted with permission from QuantInsti. The views expressed in this material are solely those of the author and/or QuantInsti and IBKR is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.
In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.
Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.
Disclosure: Displaying Symbols on Video
Any stock, options or futures symbols displayed are for illustrative purposes only and are not intended to portray recommendations.