How to Import Historical Stock Prices Into A Python Script Using the IEX Cloud API

Python is one of the world’s most popular programming languages.

Specifically, Python for finance is arguably the world’s most popular language-application pair. This is because of the robust ecosystem of packages and libraries that makes it easy for developers to build robust financial applications.

In this tutorial, you will learn how to import historical stock prices from the IEX Cloud API and store them within your script in a pandas DataFrame.

new york stock exchange
  • Save

Table of Contents

You can skip to a specific section of this Python tutorial using the table of contents below:

Step #1: Create an IEX Cloud Account

I have spent my career building financial data infrastructure. This world is full of overpriced solutions that overpromise and underdeliver.

IEX Cloud is an exception to this. They are a robust provider of stock market data and are priced very affordably. Their pricing is only $9/month for their cheapest plan.

Once you create an IEX Cloud account, you will need to generate an API key. Storing that API key within a variable called `IEX_API_KEY` will allow you to proceed through the rest of this tutorial.

One last thing – although I am a big fan of IEX Cloud, I have no relationship with them whatsoever. I am only recommending their platform because I genuinely believe it is one of the best solutions for financial data infrastructure today.

Step #2: Import Pandas

The next thing you’ll need to do is import the Python library Pandas. Pandas is a portmanteau of “panel data” and is one of the most popular open-source libraries for working with tabular data (meaning it has columns and rows) in Python.

The first thing you’ll need to do is install pandas on your local machine. Doing this with the `pip` package manager is very easy. Run the following command from your terminal:

pip3 install pandasCode language: Bash (bash)

Next you’ll need to import pandas into your Python script. It is convention to import pandas under the alias `pd`, which makes it easier to reference the library later in your Python script.

Here’s how you do this:

import pandas as pdCode language: Python (python)

Step #3: Select Your API Endpoint

Now that our imports have been completed, we are ready to proceed with grabbing data from the IEX Cloud API.

Our next step is to select which endpoint of the IEX Cloud API we want to send our HTTP request to. The API is robust, with data on everything from technical analysis indicators to financial statement information.

Fortunately, we only need one of their most basic data points – historical prices. This makes our job relatively easy.

Below, I have taken the generalized format for an IEX Cloud API request and stored it in a string with four interpolated values:

  • `tickers`: the stock tickers of the companies that we are requesting data on
  • `endpoints`: the API endpoints we will be requesting data from
  • `data_range`: the amount of time that we would like to request data for
  • `IEX_API_KEY`: our IEX Cloud API key, which we handled earlier in this tutorial
HTTP_request = f'{tickers}&types={endpoints}&range={data_range}&token={IEX_API_KEY}'Code language: Python (python)

We will work through each of these interpolated variables step-by-step through the rest of this section.

First, let’s select our stock tickers. To add to the robustness of this tutorial, I have elected to request data on multiple tickers. We will be requesting data on the 5 largest companies in the United States, whose tickers are stored in a Python list below:

tickers = [
            ]Code language: Python (python)

We’ll need to serialize this list into a string of comma-separated-values so that we can include the data into our HTTP request. 

Here’s a quick Python statement that handles this:

ticker_string = ','.join(tickers)Code language: Python (python)

Next, we need to specify our `endpoints` variable. The endpoint we need is `chart`, so here’s the command to set the variable appropriately:

endpoints = 'chart'Code language: Python (python)

Lastly, we need to specify our `data_range` variable (we can’t simply use `range` since it is a reserved word in Python). A one-year range is sufficient for this tutorial, which is indicated by the shorthand `1y`. 

Here’s the Python statement we need:

data_range = '1y'Code language: Python (python)

Putting it all together, and we have:

import pandas as pd
tickers = [
tickers = ','.join(tickers)
endpoints = 'chart'
data_range = '1y'

HTTP_request = f'{tickers}&types={endpoints}&range={data_range}&token={IEX_API_KEY}'Code language: Python (python)

n the next section, we will learn how to ping this API and store the returned data in a pandas DataFrame.

Step #4: Ping the Endpoint and Store the Data in a Pandas DataFrame

The IEX API (and most APIs, to be frank) returns data in a format called JSON, which stands for JavaScript Object Notation. JSON files are similar to Python dictionaries.

The pandas library has an excellent built-in function called `read_json` that makes it easy to query JSON data and store it in a pandas DataFrame. 

As an example, here is how we could store the results of this API call in a DataFrame called `IEX_data` using the `read_json` method:

IEX_data = pd.read_json(HTTP_request)Code language: Python (python)

If you print this data in a Jupyter Notebook, here’s what the output looks like:

As you can see, each column of the DataFrame contains a JSON object with historical stock prices for the ticker represented by that column.

Final Thoughts

In this tutorial, you learned how to import historical stock prices into a Python script using the IEX Cloud API. 

Here is a brief summary of what was covered:

  • Why IEX Cloud is one of my preferred providers of financial data infrastructure
  • How to create an IEX Cloud account
  • How to install and import pandas
  • How to select your API endpoint on IEX Cloud
  • How to query the API and store the data in a pandas DataFrame

This tutorial is a guest contribution by Nick McCullum, who teaches people how to code on his website.

  • Save

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top
Share via
Copy link
Powered by Social Snap