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.
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 612.55
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 620.05 124.49
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.002548 0.002988
2019-05-10 09:32:00 2019-05-10 09:32 AM ... 0.001333 0.002214
2019-05-10 09:33:00 2019-05-10 09:33 AM ... 0.003518 0.004512
2019-05-10 09:34:00 2019-05-10 09:34 AM ... 0.002432 0.003597
... ... ... ... ... ...
2019-05-10 15:55:00 2019-05-10 3:55 PM ... -0.005115 -0.004383
2019-05-10 15:56:00 2019-05-10 3:56 PM ... -0.005078 -0.003751
2019-05-10 15:57:00 2019-05-10 3:57 PM ... -0.005577 -0.004436
2019-05-10 15:58:00 2019-05-10 3:58 PM ... -0.005582 -0.004464
2019-05-10 15:59:00 2019-05-10 3:59 PM ... -0.003705 -0.002590
[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
2020-12-11 09:30:00 2020-12-11 09:30 AM ... 0.000000 0.000000
2020-12-11 09:31:00 2020-12-11 09:31 AM ... -0.000262 -0.000371
2020-12-11 09:32:00 2020-12-11 09:32 AM ... -0.004117 -0.003431
2020-12-11 09:33:00 2020-12-11 09:33 AM ... -0.005482 -0.004875
2020-12-11 09:34:00 2020-12-11 09:34 AM ... -0.005097 -0.004452
[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 0 0 ... 87070447 1677292939816 90130466
[1 rows x 26 columns]
For a detailed list of the endpoint methods, see here.
Fields¶
To obtain individual fields from an endpoint, select Field Methods are also provided.
Examples get_open()
, get_name()
Single Symbol
In [20]: aapl = Stock("AAPL")
In [21]: aapl.get_open()
Out[21]: 123.94
In [22]: aapl.get_price()
Out[22]:
AAPL
price 127.04
Multiple Symbols
In [23]: b = Stock(["AAPL", "TSLA"])
In [24]: b.get_open()
Out[24]:
open
AAPL 125.26
TSLA 634.73
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 for
more information.
Reference Data¶
See also
Investor’s Exchange Data¶
See also
API System Metadata¶
See also
Caching¶
iexfinance supports the caching of HTTP requests to IEX using the requests-cache package.
See also