3 ways to get historical market data from IEX Cloud

IEX Cloud is a service offering financial data. They offer a wide variety of data sets, both for historical and real-time data. Those of us interested in financial data, whether for trading or research, are always looking for good data sources, especially for clean and complete data. This post will show you three ways to grab one very common type of financial data from IEX Cloud, historical prices.

First, you will need to setup a free account in order to get an API key. IEX Cloud offers some of their data for free to individual users, so you can get started and work on a trading model or do some research and only pay when you hit a data limit threshold. There are a variety of plans available to fit any data need. These demos will use a small amount of your monthly allotment if you choose to run them with your production API key.

Once you log in to the IEX Cloud console, you’ll see a link to get your API tokens. There are two tokens available, a secret token and a publishable token. You’ll also see that you can switch on ‘Sandbox Testing’. This will allow you to use a testing token against a sandbox instance of the APIs and not use up any of your free data for the purposes of exploring the APIs. Copy the publishable token and open a command prompt and run the following command, substituting your token for the one below.

$ curl "https://sandbox.iexapis.com/stable/stock/aapl/chart?token=Tpk_796XXXXXXXXXXXXXXXXabc"

This should return a json result of a month’s worth of daily historical price data for Apple common stock. Note that in the sandbox environment the data returned may be scrambled or random, so don’t use sandbox data for trading! Just switch to your production API token and use the https://cloud.iexapis.com server to get production data.

[...
{"date":"2019-11-08", "open":265.07,"close":261.86,"high":264.81,"low":262.03,"volume":17624513,"uOpen":269.89,"uClose":267.14,"uHigh":263.63,"uLow":259.87,"uVolume":17986609,"change":0.73,"changePercent":0.2807,"label":"Nov 8","changeOverTime":0.103373},
{"date":"2019-11-11", "open":262.4,"close":262.7,"high":267.54,"low":260.75,"volume":20750072,"uOpen":260.2,"uClose":271.1,"uHigh":270.08,"uLow":263.97,"uVolume":20668907,"change":2.08,"changePercent":0.8302,"label":"Nov 11","changeOverTime":0.111877}]

The IEX Cloud APIs are very well documented. They point out that this particular API is a carryover from an earlier version of their API and is being rewritten, so I won’t give too many specific examples. Instead let’s explore a few ways to get useful data from any of their APIs into your environment. Perhaps you want to use CSV format instead of JSON. To do this, add the format query parameter to your request. This parameter is accepted in many of the APIs.

$ curl "https://sandbox.iexapis.com/stable/stock/aapl/chart?format=csv&token= Tpk_796XXXXXXXXXXXXXXXXabc"
date,open,close,high,low,volume,uOpen,uClose,uHigh,uLow,uVolume,change,changePercent,label,changeOverTime
2019-10-14,240.5,243.08,243.83,242.38,24996712,240.6,237.5,245.52,237.91,25146316,0,0,Oct 14,0
2019-10-15,242.95,242.87,240.48,238.38,23774892,237.02,240.28,246.42,236.07,23331255,-0.56,-0.2398,Oct 15,-0.002369
2019-10-16,238.78,238.07,241.74,236,19340822,237.38,234.47,242.12,243.7,19957825,-0.99,-0.4092,Oct 16,-0.006663
2019-10-17,238.49,237.14,240.44,244.63,17397306,242.55,245.84,242.94,235.53,17303847,0.93,0.3929,Oct 17,-0.002563
...

Let’s look at three different ways you can easily get more data.

Shell scripts

For quick one-off requests you can run curl to pull down the data you need, storing it locally in your preferred format, and then later using it with your favorite analysis tool or programming language.

Google Sheets or Microsoft Excel

A second way to fetch the data is to use a spreadsheet to invoke the web service. This will pull the data down into your spreadsheet just below the formula and is a great way to prototype and explore data. First, here’s the example in Google Sheets.

=IMPORTDATA("https://sandbox.iexapis.com/stable/stock/aapl/chart?format=csv&token=Tpk_7dXXXXXXXXXXXXXXaa")

For Microsoft Excel (Windows only, sorry Mac users), the formula is a bit different.

=WEBSERVICE("https://sandbox.iexapis.com/stable/stock/aapl/chart?format=csv&token=Tpk_7dXXXXXXXXXXaa")

Python

A third way to fetch the data is to directly fetch it in Python. I’ll show a few examples here. To initialize a Python environment, setup a virtualenv using your favorite tool, then install the libraries we’ll use here. I’m using Python 3.7 in the notebook below, make sure you’re running 3.6 or above to ensure compatibility.

pip install requests pandas iexfinance jupyter matplotlib

In the notebook below, you can see how to use requests, pandas, and iexfinance to fetch this data easily.

Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
view raw iex_cloud.ipynb hosted with ❤ by GitHub

Leave a Reply

Your email address will not be published. Required fields are marked *