Webinar
August 6, 2020

How to Get Market Data in Python

A tutorial on how to get stock prices and other financial data while building in Python.

Cloud icon

Timothy Kou

https://iexcloud.io/community/blog/how-to-get-market-data-in-python
@iexcloud
https://iexcloud.io/community/blog/how-to-get-market-data-in-python
@iexcloud

Since Python is one of the most popular languages for building financial applications, we thought weʼd put together a Python-friendly introduction on how to access key financial data.

This tutorial walks through steps for integrating a range of datasets, and other tips you might need along the way.

Tutorial overview

  1. Installation
  2. PyEX by Tim Paine
  3. API Tokens
  4. Making API calls
  5. Sample Calls: Real-Time Stock Prices
  6. Sample Calls: Historical Stock Prices
  7. Sample Calls: Dividends
  8. Sample Calls: News
  9. Next steps and Acknowledgements

Installation: Dependencies

Part of what makes IEX Cloudʼs community great is the way it shares and insprires innovation. In this post, we will be featuring a third-party library created and maintained by one of our customers - pyEX. You can read more about pyEX in the Github repo.

In order to follow this tutorial, you will need to install the following libraries:

  • Python (>=3.7.4)
  • pyEX==0.2.1
  • PyEX by Tim Paine
  • Learn more about pyEX

    Note that there are many other third-party client libraries available. You can view a full list in our API documentation.

    See all libraries

    Finding your API Token

    To begin accessing data, youʼll need to get provisioned with an IEX Cloud API token. Get started by logging in or signing up for an IEX Cloud account. Your token can then be found in your Console under the API Tokens tab. To access live production data, you should use your public token, which begins with "pk_."

    Get your API Token

    If you want to start with unlimited free test data in our sandbox (deprecated), which we recommend for any newer users, use the token beginning with "Tpk_."

    Once you start using live production data, youʼll be charged credits against your subscriptionʼs credit allocation. If youʼre new to the platform or want to learn more about how pricing works with credits, we recommend first reading our Getting Started Guide.

    Learn about credits

    Making API Calls

    To begin using the functions defined in the pyEX library, simply import the library and create a Client. PyEX has a convenient Client object that can be used to make API calls using the same token. You can set the API token for the Client object in the constructor or in the IEX_TOKEN environment variable.

    In the next several sections, weʼll walk through how to access the top-requested data points using pyEX. Full documentation for pyEX, including instructions on how to make API calls to IEX Cloudʼs other datasets, can be found here.

    Real-time Stock Prices

    We provide both real-time stock prices and 15-minute delayed stock prices during market trading hours for U.S. stocks and ETFs. You can access this data via the Quote endpoint.

    The pyEX ʼquoteʼ and ʼquoteDFʼ methods make API calls to the Quote endpoint for a symbol. Below we see how to get real-time prices for Twitter (TWTR).

    You can save the ʼlatestPriceʼ field as the variable ʼpriceʼ.

    The Quote endpoint automatically returns several different fields. The ʼlatestPriceʼ field provides the real-time price of a symbol – the definitions for the other fields returned can be found in the documentation. You can also use our help article for more background on which field to use for specific use cases.

    Real-time prices are based on all trades that occur on the Investors Exchange (IEX), while 15-minute delayed stock prices reflect activity from all U.S. exchanges, and are provided by the Securities Information Processor / Consolidate Tape Association (SIP) . Accessing 15-minute delayed price data for Nasdaq-listed securities may require

    UTP authorization . Without UTP authorization, youʼll notice that certain fields for 15-minute delayed data are null for Nasdaq-listed stocks like AAPL (Apple) or GOOG (Google).

    Note that real-time prices available via the IEX Cloud API are different from real-time prices available via direct connection from IEX.

    Historical Stock Prices

    This example highlights how to retrieve end-of-day historical prices for Apple (AAPL) for the last five days. We offer up to 15 years of historical end-of-day prices for stocks, and the last 30 days of minute-by-minute data. You can read more about historical prices on IEX Cloud in our Help Center.

    The pyEX ʼchartʼ and ʼchartDFʼ methods make API calls to IEX Cloudʼs Historical Prices endpoint for a symbol. The available timeframes and definitions for returned columns/fields can be found in the documentation.

    Data is returned in a pandas DataFrame. By default, this DataFrame is indexed by date and returns end-of-day prices. Below, we retrieve prices for the last five days for symbol AAPL, then filter for ʼcloseʼ and ʼvolumeʼ.

    Dividends

    The pyEX ʼdividendsʼ and ʼdividendsDFʼ methods make API calls to our Dividends (basic) endpoint. The available timeframes and definitions for returned columns/fields can be found in the documentation.

    Using the ʼdividendsDFʼ method, data is returned in a pandas DataFrame. By default, this DataFrame is indexed by ex-dividend date. Below, we retrieve all dividends with ex-dates in the last six months for symbol HD.

    News

    The pyEX ʼnewsʼ and ʼnewsDFʼ methods make API calls to our News endpoint. The definitions for returned columns/fields can be found in the documentation.

    Using the ʼnewsDFʼ method, data is returned in a pandas DataFrame. By default, this DataFrame is indexed by datetime. The below retrieves the last 10 news articles for symbol GS and filters for the headline and source.

    Whatʼs next?

    This tutorial outlined how to get set up with IEX Cloud in Python and access commonly used financial data. If youʼre just getting started with the platform, or are looking to learn more best practices, we recommend the following resources:

    Watch our full getting started tutorial for the IEX Cloud API →What is a credit and how many do I need? →How to Use the IEX Cloud API →How to get real-time and 15-minute delayed stock prices →How to get historical stock prices →How to get international data →Other FAQs →

    Acknowledgements

    Note that third-party libraries are not directly maintained by IEX Cloud.

    Go to pyEX Github page

    See other third-party libraries