Python api for getting market and financial data from IEX

Most of you have probably heard about IEX: The Investors Exchange. IEX is the exchange started by Brad Katsuyama who was the protagonist of Michael Lewis’s famous book Flash Boys (review). Just last year, IEX scored a major win when SEC approved its application to register as a national securities exchange. As time passes by, IEX continues to gain more and more market share.

Just like any other exchange, one of IEX’s most valuable asset is the market data generated by all the trading. However, unlike other exchanges, IEX makes its data available to public for free via web API. On February 22, 2017, IEX wrote a blog post announcing release of its web API. Since then, IEX has made quite a few enhancements and added support for newer datasets as well.

As of today, some of the data that IEX provides includes:

  • pricing data (latest trade and quote data as well as summary data going back up to 5 years),
  • reference data,
  • new data,
  • earnings data, and
  • financial data.

I started looking into their API docs and figured I would write a python wrapper which gets the data from IEX and returns it in a dataframe. It’s a simple wrapper and is in its early stages so it doesn’t do much besides what the API supports. Currently, it supports getting pricing, reference, news, earnings and financial data. It also leverages IEX’s reference data to first validate securities before requesting data for them.

You can find the code here.

Here are some examples:

Getting latest quote and trade data

The final dataframe contains some extra columns as well such as ‘sector’ and ‘securityType’. I decided to leave these columns in the final dataframe so that the users can decide whether them or not.

In [1]: import sys
In [2]: sys.path.append('/Users/himanshugupta/PycharmProjects/iex_data')
In [3]: import iex_data as IEX
In [4]:  iex = IEX.API()
In [5]: iex.get_latest_quote_and_trade(['AAPL', 'IBM'])
Out[5]:
        askPrice  askSize  bidPrice  bidSize  lastSalePrice  lastSaleSize            lastSaleTime             lastUpdated  marketPercent                          sector securityType  volume
symbol
AAPL      154.37     3100     149.2      100         150.51           100 2017-07-20 18:48:40.872 2017-07-20 18:49:08.068        0.00975  Technology Hardware & Equipmen  CommonStock  108396
IBM       147.60      100       0.0        0         147.66           100 2017-07-20 18:42:59.245 2017-07-20 18:49:13.563        0.01422             Software & Services  CommonStock   46842
# If all of the securities are not valid, you will get a message back saying stocks are invalid. 
In [6]: iex.get_latest_quote_and_trade(['AAPLE', 'IBMC'])
These stock(s) are invalid!
# If at least one security is valid, you will get results back.
In [7]: iex.get_latest_quote_and_trade(['AAPL', 'IBMC'])[['askPrice', 'askSize', 'bidPrice', 'bidSize']]
Out[7]:
 askPrice askSize bidPrice bidSize
symbol
AAPL 154.37 3100 149.2 100

 

Get latest trade data

If you only want latest trade data then use this method.

In [34]: iex.get_latest_trade(['AAPL', 'MSFT'])
Out[34]:
          price  size                    time
symbol
AAPL    150.585   100 2017-07-20 18:53:06.818
MSFT     73.800   100 2017-07-20 18:53:07.953

 

Get latest news

This is a great feature. IEX provides latest news stories via its API. Using this method, you can get latest news stories for securities.

In [36]: iex.get_latest_news(['SNAP'])
Out[36]:
  symbol                time                                                                                      headline                summary               source                                                                                            url            related
0   SNAP 2017-07-20 16:51:00  Elon Musk must be 'from the future' because he's so awesome at everything, says Dick Costolo  No summary available.  CNBC via QuoteMedia  http://app.quotemedia.com/quotetools/newsItem.htm?webmasterId=102699&storyId=8588191995690775  FB,SNAP,TSLA,TWTR

 

Get aggregated (bars) trade data

This method provides users with aggregated trade data. By default, it gets data for previous day. Users can get data for 1 day (1d), 1 month (1m), 6 months (6m), 1 year (1y), Year to date (ytd), 2 years (2y) and 5 years (5y).

In [57]: bars = iex.get_trade_bars_data(['APPLE', 'CSCO'], bucket='1m')
# Print out available columns
In [58]: bars.keys()
Out[58]: Index(['change', 'changePercent', 'close', 'date', 'high', 'low', 'open', 'unadjustedClose', 'unadjustedVolume', 'volume', 'vwap', 'symbol'], dtype='object')

In [62]: bars[['symbol', 'high', 'low', 'open', 'close', 'volume', 'vwap']].head()
Out[62]:
  symbol     high      low     open    close    volume     vwap
0   CSCO  31.7236  31.2887  31.4759  31.6939  18459210  31.5967
1   CSCO  31.8623  31.5453  31.6444  31.5552  18190130  31.6525
2   CSCO  31.5750  31.2183  31.4363  31.5453  20386074  31.4327
3   CSCO  31.7038  31.4066  31.6146  31.5552  19436616  31.5690
4   CSCO  32.0010  31.4561  31.5552  31.7930  25792153  31.7880

 

Get financial data

Use this method to get financial data for securities such as cash flow, debt, assets, gross profit, net income etc.

In [47]: financials = iex.get_financials(['AMD', 'INTC'])
# Print out available columns
In [48]: financials.keys()
Out[48]: Index(['cashChange', 'cashFlow', 'costOfRevenue', 'currentAssets', 'currentCash', 'currentDebt', 'grossProfit', 'netIncome', 'operatingExpense', 'operatingGainsLosses', 'operatingIncome', 'operatingRevenue', 'reportDate', 'researchAndDevelopment', 'shareholderEquity', 'totalAssets', 'totalCash', 'totalDebt', 'totalLiabilities', 'totalRevenue', 'symbol'], dtype='object')
n [50]: financials[['symbol', 'cashChange', 'cashFlow', 'costOfRevenue', 'currentAssets', 'currentCash', 'currentDebt', 'grossProfit', 'netIncome', 'operatingExpense']]
Out[50]:
  symbol  cashChange    cashFlow  costOfRevenue  currentAssets  currentCash currentDebt  grossProfit   netIncome  operatingExpense
0    AMD  -542000000  -299000000      653000000     2498000000    722000000         n/a    331000000   -73000000         360000000
1    AMD     6000000   188000000      755000000     2530000000   1264000000         n/a    351000000   -51000000         354000000
2    AMD   301000000    29000000     1248000000     2824000000   1258000000         n/a     59000000  -406000000         352000000
3    AMD   241000000   -85000000      708000000     2506000000    957000000   226000000    319000000    69000000         327000000
4   INTC  -626000000  3898000000     5649000000    36058000000   4934000000  5073000000   9147000000  2964000000        5548000000
5   INTC   808000000  8150000000     6269000000    35508000000   5560000000  4634000000  10105000000  3562000000        5579000000
6   INTC   867000000  5758000000     5795000000    36216000000   4752000000  3573000000   9983000000  3378000000        5521000000
7   INTC   824000000  3845000000     5560000000    31188000000   3885000000  4560000000   7973000000  1330000000        6655000000

 

Get earnings

Use this method to get earnings data for securities such as EPS report date, EPS, estimated EPS, fiscal end date etc.

In [51]: earnings = iex.get_earnings(['FB', 'VRX'])
# Print out available columns
In [52]: earnings.keys()
Out[52]: Index(['EPSReportDate', 'EPSSurpriseDollar', 'actualEPS', 'announceTime', 'consensusEPS', 'estimatedEPS', 'fiscalEndDate', 'fiscalPeriod', 'numberOfEstimates', 'symbol'], dtype='object')
In [53]: earnings[['symbol', 'EPSReportDate', 'EPSSurpriseDollar', 'actualEPS', 'announceTime', 'consensusEPS', 'estimatedEPS', 'fiscalEndDate']]
Out[53]:
  symbol EPSReportDate EPSSurpriseDollar actualEPS announceTime  consensusEPS  estimatedEPS fiscalEndDate
0     FB    2017-07-26               N/A       N/A          AMC          1.13          1.13    2017-06-30
1     FB    2017-05-03              0.16      1.04          AMC          0.88          0.88    2017-03-31
2     FB    2017-02-01              0.13      1.24          AMC          1.11          1.11    2016-12-31
3     FB    2016-11-02              0.11      0.88          AMC          0.77          0.77    2016-09-30
4    VRX    2017-08-08               N/A       N/A          BTO          0.96          0.96    2017-06-30
5    VRX    2017-05-09              1.84       2.8          BTO          0.96          0.96    2017-03-31
6    VRX    2017-02-28              0.02      1.26          BTO          1.24          1.24    2016-12-31
7    VRX    2016-11-08             -0.23      1.55          BTO          1.78          1.78    2016-09-30

As mentioned earlier, my api provides users with an easy way to get data from IEX in a python dataframe. The IEX API is still very new so I expect IEX to be making lots of changes and adding new datasets. I will try my best to add support for the new datasets.

Code can be found on my github.

Leave a Reply

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