Common Usage Examples

The iex-examples repository provides a number of detailed examples of iexfinance usage. Basic examples are also provided below.

Using iexfinance to access data from IEX is quite easy. The most commonly-used endpoints are the Stocks endpoints, which allow access to various information regarding equities, including quotes, historical prices, dividends, and much more.

The iexfinance codebase and documentation are structured in a way that emulates much of the IEX Cloud Documentation for readability and ease of use.

These modules provide classes and functions to execute queries to IEX Cloud.

Account

See also

Account

Stocks

See also

For more information, see Stocks.

Real-time Quotes

To obtain real-time quotes for one or more symbols, use the get_price method of the Stock object:

In [1]: from iexfinance.stocks import Stock

In [2]: tsla = Stock('TSLA')

In [3]: tsla.get_price()
Out[3]: 
         TSLA
price  619.27

or for multiple symbols, use a list or list-like object (Tuple, Pandas Series, etc.):

In [4]: batch = Stock(["TSLA", "AAPL"])

In [5]: batch.get_price()
Out[5]: 
         TSLA    AAPL
price  617.14  127.63

Historical Data

It’s possible to obtain historical data the get_historical_data and get_historical_intraday.

Daily

To obtain daily historical price data for one or more symbols, use the get_historical_data function. This will return a daily time-series of the ticker requested over the desired date range (start and end passed as datetime.datetime objects):

In [6]: from datetime import datetime

In [7]: from iexfinance.stocks import get_historical_data

In [8]: start = datetime(2017, 1, 1)

In [9]: end = datetime(2018, 1, 1)

In [10]: df = get_historical_data("TSLA", start, end)

For Pandas DataFrame output formatting, pass output_format:

In [11]: df = get_historical_data("TSLA", start, end, output_format='pandas')

Minutely (Intraday)

To obtain historical intraday data, use get_historical_intraday as follows. Pass an optional date to specify a date within three months prior to the current day (default is current date):

In [12]: from datetime import datetime

In [13]: from iexfinance.stocks import get_historical_intraday

In [14]: date = datetime(2019, 5, 10)

In [15]: get_historical_intraday("AAPL", date)
Out[15]: 
                           date     label  ...  changeOverTime  marketChangeOverTime
2019-05-10 09:30:00  2019-05-10  09:30 AM  ...        0.000000              0.000000
2019-05-10 09:31:00  2019-05-10  09:31 AM  ...        0.002492              0.002972
2019-05-10 09:32:00  2019-05-10  09:32 AM  ...        0.001320              0.002154
2019-05-10 09:33:00  2019-05-10  09:33 AM  ...        0.003390              0.004628
2019-05-10 09:34:00  2019-05-10  09:34 AM  ...        0.002408              0.003693
...                         ...       ...  ...             ...                   ...
2019-05-10 15:55:00  2019-05-10   3:55 PM  ...       -0.005317             -0.004417
2019-05-10 15:56:00  2019-05-10   3:56 PM  ...       -0.005034             -0.003904
2019-05-10 15:57:00  2019-05-10   3:57 PM  ...       -0.005668             -0.004356
2019-05-10 15:58:00  2019-05-10   3:58 PM  ...       -0.005603             -0.004508
2019-05-10 15:59:00  2019-05-10   3:59 PM  ...       -0.003724             -0.002580

[390 rows x 20 columns]

or for a pandas.DataFrame indexed by each minute:

In [16]: get_historical_intraday("AAPL", output_format='pandas').head()
Out[16]: 
                           date     label  ...  changeOverTime  marketChangeOverTime
2021-06-02 09:30:00  2021-06-02  09:30 AM  ...        0.000000              0.000000
2021-06-02 09:31:00  2021-06-02  09:31 AM  ...        0.000345              0.000650
2021-06-02 09:32:00  2021-06-02  09:32 AM  ...        0.001971              0.002330
2021-06-02 09:33:00  2021-06-02  09:33 AM  ...        0.002748              0.002693
2021-06-02 09:34:00  2021-06-02  09:34 AM  ...        0.001552              0.001725

[5 rows x 20 columns]

Endpoints

The Stock object can obtain each of these endpoints. Requests for single symbols will return the exact results from that endpoint as shown in the IEX API documentation (see below). Requests for multiple symbols will return a symbol-indexed dictionary of the endpoint requested.

Endpoint Method examples get_quote(), get_volume_by_venue()

In [17]: from iexfinance.stocks import Stock

In [18]: aapl = Stock("AAPL")

In [19]: aapl.get_previous_day_prices()
Out[19]: 
        change changeOverTime changePercent  ...   uVolume        updated    volume
AAPL  0.798927       0.006319        0.0066  ...  60725027  1633354614449  61349379

[1 rows x 26 columns]

For a detailed list of the endpoint methods, see here.

Fields

To obtain individual fields from an endpoint, select Additional Methods are also provided.

Examples get_open(), get_name()

Single Symbol

In [20]: aapl = Stock("AAPL")

In [21]: aapl.get_open()
Out[21]: 128.8

In [22]: aapl.get_price()
Out[22]: 
         AAPL
price  129.86

Multiple Symbols

In [23]: b = Stock(["AAPL", "TSLA"])

In [24]: b.get_open()
Out[24]: 
        open
AAPL  128.92
TSLA  646.43

For a detailed list of these functions, see here.

Endpoint-Specific Parameters

Top-level parameters may be passed to the Stock function, including output_format and request parameters (such as retry_count, and pause) - the latter of which will be used should any queries made by the object fail. These parameters are passed keyword arguments, and are entirely optional.

Certain endpoints (such as quote and chart), however, allow customizable parameters. To specify one of these parameters, merely pass it to an endpoint method as a keyword argument.

aapl = Stock("AAPL", output_format='pandas')
aapl.get_quote(displayPercent=True).loc["ytdChange"]

Note

The output_format from the initial call to the Stock function will be used (if the output format has not been change through change_output_format since) and cannot be changed through calls to endpoint methods. See Stocks/Equities for more information.

Reference Data

See also

Reference Data

Investor’s Exchange Data

API System Metadata

Caching

iexfinance supports the caching of HTTP requests to IEX using the requests-cache package.

See also

Caching Queries