Developing Investment Strategies in Python

Pedro Lealdino
August 24, 2020

Developing investment strategies based on data requires confidence and speed in data collection. Any failure in the collection process, normally caused by the providers, can have catastrophic consequences on your investments. In this blog, I will demonstrate a strong solution on how to collect data using the Intrinio API using Python.

Installing the Libraries

If you already have Python installed on your computer, open the terminal and install the necessary libraries with the command:


pip install intrinio_sdkpip
install pandaspip
install matplotlib

Collecting the Data

Now, we will start the development of our small data collection program. The first step is to import the necessary libraries:


import intrinio_sdk
import pandas as pd
import matplotlib.pyplot as plt

We define the symbols of the assets that we want to analyze in an array. The case is a fictitious scenario. Please don't consider it as advice.


symbols =
['AMZN','GOOGL','O','DIS','MCD','MAIN','APLE','NFLX','ATVI','AGNC','WW','TSLA','FB','JNJ','NVDA','LW']

data = pd.DataFrame()

For each symbol in the array, we collect the data defining the time of each bar and the quantity. Then, we feed the data frame with the closing prices of each request, defined by start date, end date, and the frequency:


for i in symbols:
   rates = security_api.get_security_stock_prices(identifier= i,
   start_date = '2010-12-31', end_date = '2020-08-10', frequency = 'daily')
   response_list = [x.to_dict() for x in rates.stock_prices]
   data[i] = [y['close'] for y in response_list]
   
data

Image by Author - Intrinio SDK

Calculating Returns

Calculating returns is quite easy. Just call the dataframe’s pct_change () method, and you’re good to go.


returns = data.pct_change()

Image by Author - Returns

Correlation Calculation

Like returns, correlations can also be easily calculated by calling the dataframe’s corr () method.


corr = data.corr()

Plotting the HeatMap

To build the heat graph, we will use the matplotlib library. So:


plt.figure(figsize=(15,15))
plt.imshow(corr, cmap = 'RdYlGn', interpolation='none', aspect='auto')
plt.colorbar()
plt.xticks(range(len(corr)), corr.columns, rotation = 'vertical')
plt.yticks(range(len(corr)), corr.columns)
plt.suptitle('HEATMAP - STOCKS', fontsize = 15, fontweight = 'bold')
plt.show()

Image by Author - Heatmap of selected stocks

Conclusion

In this post, we saw how to install the Intrinio SDK using Python, how to import the data of the assets we want to analyze through the Intrinio API, and how to create a heatmap of the correlations of the returns of these assets.

If you want to learn more about Intrinio, visit intrinio.com.

If you want to read more from me, visit my Medium blog.

Thanks for reading, see you next time!

Sorry, we no longer support Internet Explorer as a web browser.

Please download one of these alternatives and return for the full Intrinio Experience.

Google Chrome web browser icon
Chrome
Mozilla Firefox web browser icon
Firefox
Safari web browser icon
Safari
Microsoft Edge web browser icon
Microsoft Edge