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.
- PyEX by Tim Paine
- API Tokens
- Making API calls
- Sample Calls: Real-Time Stock Prices
- Sample Calls: Historical Stock Prices
- Sample Calls: Dividends
- Sample Calls: News
- Next steps and Acknowledgements
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:
Note that there are many other third-party client libraries available. You can view a full list in our API documentation.
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_."
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.
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ʼ.
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.
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.
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 →
Note that third-party libraries are not directly maintained by IEX Cloud.
Working with Data in Motion: A Guide to Data Streaming
Learn about types, challenges, best practices, and trends for the continuous and real-time flow of data from various sources.
Building on the Blockchain: Best Tools to Get Started
In this article, you'll learn about some of the different tools you can use when working with blockchain. You'll also learn about some of the various things you can do with it, such as implementing it on your own sites and apps, developing original tools with it, and participating in the crypto economy.
Have question about our platform and how to get started?