Last year, I wrote a q/kdb+ and python API for getting data from IEX. IEX, Investors Exchange, is an American exchange which provides a lot of financial data for free. You can get access to more data by getting a paid subscription.
Last year’s API was based on IEX’s v1 API which has now been retired. They have moved on to a new platform called IEX Cloud and started capping the amount of data you can access monthly in free tier and the type of data as well. On the plus side, IEX Cloud seems to be faster and more stable. There is also crypto data available now if you are interested. They have a nice management platform where you can see your usage and get access to other resources.
I decided to create another q/kdb+ wrapper around IEX Cloud’s REST API to make sure you are able to get data from IEX natively in kdb+. At first, I was thinking of using this great python API called iexfinance but decided to write something myself natively and mirror it as close to IEX’s REST API as possible.
You can find the new wrapper on github.
If you look at the code, it’s quite straightforward and much simpler than the earlier v1 version. For the most part, the code simply constructs the endpoint that IEX has specified for certain type of data and then uses .Q.hg
to issue a GET response. The data is returned in JSON format which is then converted into a kdb+ table using .j.k.
After that, I have mostly kept the datatypes unchanged except for date/datetime
columns which I have converted from strings
to appropriate types.
Getting started with IEX Cloud
Unlike before, you can’t just download my code and start getting data from IEX. With IEX Cloud, you have to first create an account with them and get your authentication token. You can sign up for an account here. Once you have signed up, go to your management portal and click on API Tokens
on the left sidebar.
There, you will find your Secret
Token which should start with sk_
. Store it somewhere because you will need it to use the API.
Using the API
Now that you have the token, you are ready to use the API. Download the code from my github, open iex_cloud.q
and assign token
variable to your secret key.
token:"sk_this_is_my_token";
You MUST do this or else the code won’t work!
Now, you can load the file iex_cloud.q
into a kdb+ session and start using the functions.
$ q iex_cloud.q
The code has all the functions documented with a brief summary of what type of data they return but to get the comprehensive documentation, you should check out IEX Cloud’s official docs.
Here are some examples:
Company news
q)company_news[`aapl;5] datetime headline .. -----------------------------------------------------------------------------.. 2019.10.05D13:18:28.000000000 "10 wireless earbud options that cost way less .. 2019.10.05D12:28:32.000000000 "Nike, Huami, Apple, Kemet: 'Mad Money' Lightni.. 2019.10.05D00:24:00.000000000 "The Tech Stock That Apple, AMD, and Nvidia Can.. 2019.10.04D22:03:00.000000000 "Inside one of the most mysterious mansions in .. 2019.10.04D20:40:33.000000000 "Apple to release smaller, cheaper iPhone SE2 i.. 2019.10.05D12:28:32.000000000 "Nike, Huami, Apple, Kemet: 'Mad Money' Lightni.. 2019.10.05D00:24:00.000000000 "The Tech Stock That Apple, AMD, and Nvidia Can.. 2019.10.04D22:03:00.000000000 "Inside one of the most mysterious mansions in .. 2019.10.04D20:40:33.000000000 "Apple to release smaller, cheaper iPhone SE2 i..
Company info
q)company_info[`aapl] symbol companyName exchange industry website description .. -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------.. "AAPL" "Apple, Inc." "NASDAQ" "Telecommunications Equipment" "http://www.apple.com" "Apple, Inc. engages in designing, manufacturing, and marketing of mobile communication, media devices, personal ..
Company’s key stats
This function provides key stats such as market cap, number of employees, shares outstanding, 52 week high, 52 week change, moving averages etc for a given company.
q)key_stats[`aapl] week52change week52high week52low marketcap employees day200MovingAvg day50MovingAvg float avg10Volume avg30Volume ttmEPS ttmDividendRate companyName sharesOutstanding maxChangePercen.. -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------.. 0.012127 228.41 142 1.025899e+12 132000 190.73 211.99 4.512383e+09 2.811659e+07 2.804818e+07 11.83 2.96 "Apple, Inc." 4.51918e+09 223.7624 ..
Dividends
q)dividends[`aapl;"1y"] exDate paymentDate recordDate declaredDate amount flag currency description frequency date ------------------------------------------------------------------------------------------------------------------------------------------------------------- 2018.11.09 2018.11.15 2018.11.12 2018.11.01 0.73 "No Change QoQ" "USD" "Apple Declares Quarterly Dividend of $0.73 Per Share " "Quarterly" 2019.10.05
Earnings
Use this function to get the company’s latest earnings. With the free plan, you can only get last earnings but with a paid plan you can get n last earnings.
q)earnings[`aapl;2] actualEPS consensusEPS announceTime numberOfEstimates EPSSurpriseDollar EPSReportDate fiscalPeriod fiscalEndDate yearAgo yearAgoChangePercent --------------------------------------------------------------------------------------------------------------------------------------------- 2.18 2.09 "AMC" 37 0.09 2019.07.30 "Q2 2019" 2019.06.30 2.34 -0.0684
Historical prices
This is a very useful function which gives you high, low, open, close, average, volume etc per minute. You can specify how far you want to go back by specifying a range. Available ranges are: 1d,
5d
, 5dm
(10 min intervals) 1m
, 1mm
(30 min intervals), 3m
, 6m
, ytd
, 1y
, 2y
, 5y
, max
q)historical_prices_range[`aapl;"1d"] date minute label high low open close average volume notional numberOfTrades --------------------------------------------------------------------------------------------------- 2019.10.04 09:30 "09:30 AM" 225.83 225.05 225.56 225.27 225.418 11410 2572021 94 2019.10.04 09:31 "09:31 AM" 225.31 224.92 225.225 225.23 225.171 6880 1549173 60 2019.10.04 09:32 "09:32 AM" 225.23 224.76 225.23 224.85 224.936 4216 948329.4 45 2019.10.04 09:33 "09:33 AM" 224.87 224.295 224.85 224.525 224.549 4692 1053582 48 2019.10.04 09:34 "09:34 AM" 224.65 224.31 224.54 224.43 224.513 3245 728545.7 31 2019.10.04 09:35 "09:35 AM" 224.37 224.26 224.37 224.325 224.313 2410 540594.7 23 2019.10.04 09:36 "09:36 AM" 224.69 224.45 224.45 224.64 224.623 2327 522697.7 18 2019.10.04 09:37 "09:37 AM" 224.95 224.67 224.675 224.945 224.815 3859 867563.4 34 2019.10.04 09:38 "09:38 AM" 225.235 224.93 224.95 225.235 225.147 3574 804675.9 40 2019.10.04 09:39 "09:39 AM" 225.44 225.205 225.25 225.41 225.333 1809 407627.5 15
Similarly, to get historical prices for a specific day (instead of a range), you can use this function:
q)historical_prices_day[`aapl;"20191003"] date minute label high low average volume notional numberOfTrades marketHigh marketLow marketAverage marketVolume marketNotional marketNumberOfTrades open close marketOpen ma.. -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------.. 2019.10.03 09:30 "09:30 AM" 218.45 218 218.281 1698 370641.7 26 218.56 217.96 218.213 373050 8.140423e+07 973 218.34 218.17 218.43 21.. 2019.10.03 09:31 "09:31 AM" 218.96 218.24 218.651 3143 687220.3 26 219.2 218.14 218.683 229229 5.012841e+07 957 218.27 218.95 218.17 21.. 2019.10.03 09:32 "09:32 AM" 219.22 218.91 219.024 2506 548874 20 219.273 218.88 219.048 146935 3.218585e+07 804 218.945 219.215 218.96 21.. 2019.10.03 09:33 "09:33 AM" 219.28 218.97 219.123 1622 355417.7 19 219.34 218.95 219.167 98607 2.161146e+07 489 219.28 218.99 219.26 21.. 2019.10.03 09:34 "09:34 AM" 219.15 218.89 218.976 2811 615543.1 24 219.18 218.83 219.015 67692 1.482559e+07 403 218.99 218.93 218.99 21.. 2019.10.03 09:35 "09:35 AM" 218.85 218.74 218.786 1926 421382.8 16 218.9 218.689 218.79 102186 2.235732e+07 474 218.74 218.85 218.88 21.. 2019.10.03 09:36 "09:36 AM" 218.98 218.63 218.772 932 203895.7 13 219 218.63 218.812 74120 1.621834e+07 395 218.76 218.77 218.83 21..
Intraday prices
This is another useful function that gets you prices for the current day.
q)intraday_prices[`aapl] date minute label high low open close average volume notional numberOfTrades --------------------------------------------------------------------------------------------------- 2019.10.04 09:30 "09:30 AM" 225.83 225.05 225.56 225.27 225.418 11410 2572021 94 2019.10.04 09:31 "09:31 AM" 225.31 224.92 225.225 225.23 225.171 6880 1549173 60 2019.10.04 09:32 "09:32 AM" 225.23 224.76 225.23 224.85 224.936 4216 948329.4 45 2019.10.04 09:33 "09:33 AM" 224.87 224.295 224.85 224.525 224.549 4692 1053582 48 2019.10.04 09:34 "09:34 AM" 224.65 224.31 224.54 224.43 224.513 3245 728545.7 31 2019.10.04 09:35 "09:35 AM" 224.37 224.26 224.37 224.325 224.313 2410 540594.7 23 2019.10.04 09:36 "09:36 AM" 224.69 224.45 224.45 224.64 224.623 2327 522697.7 18 2019.10.04 09:37 "09:37 AM" 224.95 224.67 224.675 224.945 224.815 3859 867563.4 34
Data for most active securities
This function returns data for the most active securities of the day.
q)most_active[] symbol companyName primaryExchange calculationPrice open openTime close closeTime high low latestPrice latestSource lat.. -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------.. "GE" "General Electric Co." "New York Stock Exchange" "close" 8.61 2019.10.04D13:30:38.788000000 8.57 2019.10.04D20:00:13.726000128 8.7 8.54 8.57 "Close" "Oc.. "CHK" "Chesapeake Energy Corp." "New York Stock Exchange" "close" 1.36 2019.10.04D13:30:00.479000064 1.36 2019.10.04D20:00:23.196000000 1.39 1.31 1.36 "Close" "Oc.. "BAC" "Bank of America Corp." "New York Stock Exchange" "close" 27.84 2019.10.04D13:30:13.097999872 28.37 2019.10.04D20:00:24.379000064 28.395 27.84 28.37 "Close" "Oc..
Similarly, you have two more functions that return data for:
- top gainers of the day –
gainers[]
- top losers of the day –
losers[]
Crypto prices
You can also get latest crypto prices now from IEX Cloud.
q).crypto.quote[`btcusd] symbol primaryExchange sector calculationPrice high low latestPrice latestSource latestUpdate latestVolume previousClose ---------------------------------------------------------------------------------------------------------------------------------------------------------- "BTCUSD" "" "cryptocurrency" "realtime" "8072.89" "Real time price" 2019.10.05D15:48:44.225999872 "0.00949184"
Reference data
IEX Cloud also makes available reference data for cryptos and stocks. There are two functions that provide you with info on available cryptos and stocks:
.ref.crypto_syms[]
.ref.syms[]
q).ref.crypto_syms[] symbol name exchange date type region currency isEnabled ------------------------------------------------------------------------------------------ "BTCUSD" "Bitcoin to USD" 2020.06.09 "crypto" "US" "USD" 1 "ETHUSD" "Ethereum to USD" 2020.06.09 "crypto" "US" "USD" 1 "ETHBTC" "Ethereum to Bitcoin" 2020.06.09 "crypto" "US" "BTC" 1 "ZECUSD" "Zcash to USD" 2020.06.09 "crypto" "US" "USD" 1 "ZECBTC" "Zcash to Bitcoin" 2020.06.09 "crypto" "US" "BTC" 1 "ZECETH" "Zcash to Ethereum" 2020.06.09 "crypto" "US" "ETH" 1 "ZECBCH" "Zcash to Bitcoin Cash" 2020.06.09 "crypto" "US" "BCH" 1 "ZECLTC" "Zcash to Litecoin" 2020.06.09 "crypto" "US" "LTC" 1 "BCHUSD" "Bitcoin Cash to USD" 2020.06.09 "crypto" "US" "USD" 1 "BCHBTC" "Bitcoin Cash to Bitcoin" 2020.06.09 "crypto" "US" "BTC" 1 "BCHETH" "Bitcoin Cash to Ethereum" 2020.06.09 "crypto" "US" "ETH" 1 "LTCUSD" "Litecoin to USD" 2020.06.09 "crypto" "US" "USD" 1 "LTCBTC" "Litecoin to Bitcoin" 2020.06.09 "crypto" "US" "BTC" 1 "LTCETH" "Litecoin to Ethereum" 2020.06.09 "crypto" "US" "ETH" 1 "LTCBCH" "Litecoin to Bitcoin Cash" 2020.06.09 "crypto" "US" "BCH" 1 q).ref.syms[] symbol exchange name date type iexId region currency isEnabled figi cik ------------------------------------------------------------------------------------------------------------------------------------------------------ ,"A" "NYS" "Agilent Technologies Inc." 2020.06.09 "cs" "IEX_46574843354B2D52" "US" "USD" 1 "BBG000C2V3D6" "1090872" "AA" "NYS" "Alcoa Corp." 2020.06.09 "cs" "IEX_4238333734532D52" "US" "USD" 1 "BBG00B3T3HD3" "1675149" "AAAU" "PSE" "Perth Mint Physical Gold ETF" 2020.06.09 "et" "IEX_474B433136332D52" "US" "USD" 1 "BBG00LPXX872" 0n "AACG" "NAS" "ATA Creativity Global Sponsored ADR" 2020.06.09 "ad" "IEX_44595A4C53392D52" "US" "USD" 1 "BBG000V2S3P6" 0n "AADR" "PSE" "AdvisorShares Dorsey Wright ADR ETF" 2020.06.09 "et" "IEX_5253355435362D52" "US" "USD" 1 "BBG000BDYRW6" 0n "AAL" "NAS" "American Airlines Group Inc." 2020.06.09 "cs" "IEX_4353464A535A2D52" "US" "USD" 1 "BBG005P7Q881" "6201" "AAMC" "ASE" "Altisource Asset Management Corp." 2020.06.09 "cs" "IEX_5442323844432D52" "US" "USD" 1 "BBG003PNL136" "1555074"
I hope you find the API useful. Let me know if you have any feedback.