Skip to content

Instantly share code, notes, and snippets.

@addisonlynch
Created December 31, 2017 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save addisonlynch/43fbc008ea1d38e6308032ae691457d3 to your computer and use it in GitHub Desktop.
Save addisonlynch/43fbc008ea1d38e6308032ae691457d3 to your computer and use it in GitHub Desktop.

Setting up a new environment

Ideally, before installing or using iexfinance, we'll create a new virtual environment using virtualenv. This will ensure that our packages are isolated from other projects and configured correctly.

https://gist.github.com/0ef89b4afba054c38dd78e1074b553ce

Getting started

Once our environment is created, we can now install iexfinance. We do so by the following from iexfinance's pypl repository.

https://gist.github.com/55d1115157987ca9a1b666fbde257e0f

This will install the latest stable release of iexfinance that is ready for use. Once installed, we can import the library and begin downloading data!

Retrieving Data

iexfinance uses two internal objects, Share and Batch to retrieve equities data. Share is used for single symbols and uses the /stock/<symbolname> endpoint from IEX. Batch, however, uses the market endpoint from Batch Requests to conduct the retrieval for multiple symbols.

Single Symbol

We'll first work with the following instantiation:

https://gist.github.com/6e5905d86686e1d248008ad9880435d5

So, we've called the IexFinance function and passed it the symbol "aapl", for Apple Inc. We've receieved a Share object in return whose symbol is "aapl." Notice that we have not passed any parameters at instantiation, so our object has used its defaults (see Share for more information). Should we attempt to pass a symbol not contained in the available symbol list (see Utilities), an IEXSymbolError will be raised:

https://gist.github.com/08edfd354f64581d74f6766b553dced0

At this point, the wrapper has downloaded all avaiable data for Apple Inc., and we can quickly access certain endpoints and datapoints without the overhead of making multiple API calls for the information. We'll first work with the Quote endpoint. The IEX Docs for quote:

We see on the right an exact representation of the Quote endpoint's response, in this case a JSON of various datapoints including "symbol" and "companyName", among others. To retrieve the endpoint as represented, we use the provided endpoint method get_quote:

https://gist.github.com/190ceed37fad834af4108d60ce7d90ed

We see that get_quote returns the same as the IEX docs! But what if we don't want the entire endpoint? iexfinance provides a number of datapoint methods which allow access to specific items within certain endpoints. For instance, we could use get_open or get_company_name to obtain the relevant information:

https://gist.github.com/2eeb09efd8316e5d4d7a1bfbd3964e0c

A full list of the avaiable datapoint methods is provided in the Share documentation. In addition to these methods, it is possible to obtain one or more datapoints from a specific endpoint, using the get_select_datapoints method, passing the desired endpoint and a string or list of desired datapoint(s) as below:

https://gist.github.com/673bbcdd9931f7b7e8a60d5a2c7f6d22

We see that get_select_datapoints returns a dict of these datapoints, indexed by the keys provided. Note: the datapoint names must be entered in the exact formatting as the IEX documentation. If we attempt to select an invalid datapoint, an IEXDatapointError will be raised:

https://gist.github.com/af04031b839fd8d7bb8cd2de177a8c68

Multiple Symbols

For batch requests, IexFinance returns a Batch object, which contains many of the same methods as Share, but returns data in a dict indexed by each symbol provided.

https://gist.github.com/84e604184854e67515dfce9d13321980

We can see that the entire dataset, indexed by "AAPL" and "TSLA", contains each endpoint. To obtain an individual endpoint, we use an endpoint method as we would with single symbols:

https://gist.github.com/de0cfec81eaab8f1f4c0adc7408f9290

We see that the response of an endpoint method is also symbol-indexed. This remains true for all methods in Batch, including datapoint methods:

https://gist.github.com/e288ef085ac932e1bd5d51fdbf4e0b66

Obtaining multiple endpoints or multiple datapoints from a certain endpoint is easy for multiple symbols:

https://gist.github.com/236e5d3dc2451d7018266f0af20169a1

Updating Data

When we call the IexFinance function, the resulting object calls the refresh method at instantiation. This method downloads and obtains the latest market data from IEX. Realtime data is updated to the latest 15 minutes, per the IEX documentation.

https://gist.github.com/eaca12d8d3b75d876f84af910263ea24

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment