q/kdb+ api for getting market and financial data from IEX

Few months ago, I wrote an api for getting market and financial data from IEX in python. As discussed earlier, IEX makes a lot of its data available to the public through its webservice api (link).

In this post, I will show you how to use the api I wrote in q/kdb+. Let’s get started.

You can find the code here.

get_last_trade

Use this function to get last trade data (price and size) for one or more securities.

q)get_last_trade`aapl`ibm
sym  price  size time
---------------------------------------------
AAPL 174.66 100  2017.11.10D20:59:58.008999936
IBM  149.18 300  2017.11.10D20:59:59.724999936

get_prev_day_summary

Use this function to get summary data for a security for previous trading day. The summary includes high, open, close, volume, vwap, change etc

q)get_prev_day_summary`aapl
sym  date       open   high   low    close  volume      unadjustedVolume change changePercent vwap
------------------------------------------------------------------------------------------------------
AAPL 2017.11.10 175.11 175.38 174.27 174.67 2.51455e+07 2.51455e+07      -1.21  -0.688        174.8423

get_historical_summary

Use this function to get bucketed historical data for a security. Available buckets are: 1d, 1m, 3m, 6m, ytd, 1y, 2y and 5y.

q)get_historical_summary[`aapl;`1d]
sym  minute label      high    low     average volume notional numberOfTrades changeOverTime
--------------------------------------------------------------------------------------------
AAPL 09:30  "09:30 AM" 175.3   175.145 175.236 1651   289314   24             0
AAPL 09:31  "09:31 AM" 175.34  175.01  175.265 1762   308816.3 7              0.0001654911
AAPL 09:32  "09:32 AM" 174.82  174.545 174.697 1028   179588.3 11             -0.003075852
AAPL 09:33  "09:33 AM" 174.81  174.54  174.684 4750   829747   24             -0.003150038
AAPL 09:34  "09:34 AM" 174.81  174.64  174.713 5678   992023   35             -0.002984547
AAPL 09:35  "09:35 AM" 174.755 174.27  174.4   23624  4120017  76             -0.004770709
AAPL 09:36  "09:36 AM" 174.59  174.3   174.452 9903   1727598  35             -0.004473967
......

get_minutely_summary

Similar to the function above, you can get minutely bucketed data for a specific date.

q)get_minutely_summary[`aapl;`20171103]
sym  date       minute label      high    low     average volume notional     numberOfTrades changeOverTime
-----------------------------------------------------------------------------------------------------------
AAPL 2017.11.03 09:30  "09:30 AM" 174.26  173.77  174.043 12340  2147686      84             0
AAPL 2017.11.03 09:31  "09:31 AM" 173.9   173.4   173.622 8424   1462594      73             -0.002418942
AAPL 2017.11.03 09:32  "09:32 AM" 173.5   173.14  173.321 9401   1629396      88             -0.0041484
AAPL 2017.11.03 09:33  "09:33 AM" 173.53  173.12  173.368 18171  3150272      106            -0.003878352
AAPL 2017.11.03 09:34  "09:34 AM" 173.5   173.26  173.35  20372  3531493      108            -0.003981775
AAPL 2017.11.03 09:35  "09:35 AM" 173.47  173.1   173.308 7745   1342272      62             -0.004223094

get_company_info

Use this function to get information about a company such as exchange, industry, website, description, CEO etc.

q)get_company_info[`aapl]
sym  companyName  exchange               industry            website                description                                                    ..
---------------------------------------------------------------------------------------------------------------------------------------------------..
AAPL "Apple Inc." "Nasdaq Global Select" "Computer Hardware" "http://www.apple.com" "Apple Inc is designs, manufactures and markets mobile communic..

get_key_stats

Use this function to get key statistics about a security such as market cap, beta, revenue, debt etc.

q)get_key_stats`aapl
sym  companyName  marketcap    beta     week52high week52low week52change shortInterest shortDate  dividendRate dividendYield exDividendDate latest..
---------------------------------------------------------------------------------------------------------------------------------------------------..
AAPL "Apple Inc." 8.968103e+11 1.214983 176.24     104.08    60.6634      3.424262e+07  2017.10.13 2.52         1.429868                     9.2   ..
So the need to have a bigger penis tops every man’s list of desires since https://unica-web.com/archive/1999/1999-may-presidents-newsletter.html levitra uk time immemorial. Males continuously unica-web.com viagra mastercard suffer from impotence without even sharing anything to their partner because they may be incapable of satisfying their partner. The first case study is unica-web.com cialis viagra australia found in the simple. This medicine will become effective 1 hour after taking generic no prescription viagra it but the effect is noticeable after 45 mins.

get_company_news

Use this function to get latest news relating to a security.

q)get_company_news`aapl
sym  datetime                      headline                                                                       source          url              ..
---------------------------------------------------------------------------------------------------------------------------------------------------..
AAPL 2017.11.10D16:06:00.000000000 "Netflix dropped 5 percent this week, after rallying all year"                 "CNBC"          "https://api.iext..
AAPL 2017.11.10D14:16:00.000000000 "Apple will issue a fix for iPhone X devices that seize up in the cold"        "CNBC"          "https://api.iext..
AAPL 2017.11.10D12:04:00.000000000 "Apple acknowledges iPhone X issue in some devices, plans fix"                 "SeekingAlpha"  "https://api.iext..
AAPL 2017.11.10D11:01:00.000000000 "Your first trade for Friday, November 10"                                     "CNBC"          "https://api.iext..
AAPL 2017.11.10D08:30:00.000000000 "Apple's chart points to trillion dollar valuation by year-end: technician"    "CNBC"          "https://api.iext..
AAPL 2017.11.10D07:39:00.000000000 "Veterans And The Wall Street - Cramer's Mad Money (11/9/17)"                  "SeekingAlpha"  "https://api.iext..
AAPL 2017.11.10D01:30:00.000000000 "Cirrus Logic's Long Conference: A Watershed Moment"                           "SeekingAlpha"  "https://api.iext..
AAPL 2017.11.09D21:14:00.000000000 "My 3 Favorite Stocks After Q3 Earnings"                                       "SeekingAlpha"  "https://api.iext..
AAPL 2017.11.09D18:12:00.000000000 "Cramer: Successful CEOs and military leaders have these 5 traits in common"   "CNBC"          "https://api.iext..
AAPL 2017.11.09D16:35:00.000000000 "Intel AMD Graphics Partnership Agreement Has Negative Implications To Nvidia" "SeekingAlpha"  "https://api.iext..
AAPL 2017.11.09D13:00:00.000000000 "Clips Introduces Selfie Scenes for Immersive, 360-Degree Selfies on iPhone X" "Business Wire" "https://api.iext..

get_company_financials

Use this function to get financial information about a security.

q)get_company_financials`aapl
sym  reportDate grossProfit costOfRevenue operatingRevenue totalRevenue operatingIncome netIncome  researchAndDevelopment operatingExpense currentA..
---------------------------------------------------------------------------------------------------------------------------------------------------..
AAPL 2017.09.30 1.9931e+10  3.2648e+10    5.2579e+10       5.2579e+10   1.312e+10       1.0714e+10 2.997e+09              6.811e+09        1.28645e..
AAPL 2017.06.30 1.7488e+10  2.792e+10     4.5408e+10       4.5408e+10   1.0768e+10      8.717e+09  2.937e+09              6.72e+09         1.12875e..
AAPL 2017.03.31 2.0591e+10  3.2305e+10    5.2896e+10       5.2896e+10   1.4097e+10      1.1029e+10 2.776e+09              6.494e+09        1.0199e+..
AAPL 2016.12.31 3.0176e+10  4.8175e+10    7.8351e+10       7.8351e+10   2.3359e+10      1.7891e+10 2.871e+09              6.817e+09        1.03332e..

get_company_earnings

Use this function to get earnings related information about a security.

q)get_company_earnings`aapl
sym  actualEPS consensusEPS estimatedEPS announceTime numberOfEstimates EPSSurpriseDollar EPSReportDate fiscalPeriod fiscalEndDate
----------------------------------------------------------------------------------------------------------------------------------
AAPL 1.67      1.57         1.57         "AMC"        13                0.1               2017.08.01    "Q3 2017"    2017.06.30
AAPL 2.1       2.02         2.02         "AMC"        14                0.08              2017.05.02    "Q2 2017"    2017.03.31
AAPL 3.36      3.22         3.22         "AMC"        15                0.14              2017.01.31    "Q1 2017"    2016.12.31
AAPL 1.67      1.66         1.66         "AMC"        14                0.01              2016.10.25    "Q4 2016"    2016.09.30

get_most_active_stocks

Use this function to get most ‘active’ stocks for last trade date with additional info such as close price, open price etc.

q)get_most_active_stocks[]
sym   companyName                                primaryExchange           sector                   calculationPrice open  openTime                ..
---------------------------------------------------------------------------------------------------------------------------------------------------..
BAC   "Bank of America Corporation"              "New York Stock Exchange" "Financial Services"     "close"          26.57 2017.11.10D14:30:07.7440..
GE    "General Electric Company"                 "New York Stock Exchange" "Industrials"            "close"          19.98 2017.11.10D14:30:21.3809..
CMCSA "Comcast Corporation Class A Common Stock" "Nasdaq Global Select"    "Consumer Cyclical"      "close"          36.5  2017.11.10D14:30:00.0259..
T     "AT&T Inc."                                "New York Stock Exchange" "Communication Services" "close"          34.14 2017.11.10D14:30:19.0700..

get_most_gainers_stocks

Use this function to get stocks with highest gains for last trade date with additional info such as close price, open price etc.

q)get_most_gainers_stocks[]
sym  companyName                                   primaryExchange           sector              calculationPrice open  openTime                   ..
---------------------------------------------------------------------------------------------------------------------------------------------------..
NLNK "NewLink Genetics Corporation"                "NASDAQ Global Market"    "Healthcare"        "close"          8.75  2017.11.10D14:30:00.7500001..
RH   "RH"                                          "New York Stock Exchange" "Consumer Cyclical" "close"          82.15 2017.11.10D14:30:00.6030000..
EGAN "eGain Corporation"                           "NASDAQ Capital Market"   "Technology"        "close"          3.3   2017.11.10D14:30:00.3160000..
FRTA "Forterra Inc."                               "Nasdaq Global Select"    "Basic Materials"   "close"          8.08  2017.11.10D14:30:00.6209999..

get_most_losers_stocks

Use this function to get stocks with most loss for last trade date with additional info such as close price, open price etc.

q)get_most_losers_stocks[]
sym  companyName                              primaryExchange           sector               calculationPrice open  openTime                      c..
---------------------------------------------------------------------------------------------------------------------------------------------------..
NCS  "NCI Building Systems Inc."              "New York Stock Exchange" "Industrials"        "close"          15.8  2017.11.10D14:30:00.740000000 1..
GURE "Gulf Resources Inc."                    "Nasdaq Global Select"    "Basic Materials"    "close"          1.41  2017.11.10D14:30:00.312999936 1..
ALNY "Alnylam Pharmaceuticals Inc."           "Nasdaq Global Select"    "Healthcare"         "close"          137.6 2017.11.10D14:30:00.599000064 1..
FNHC "Federated National Holding Company"     "NASDAQ Global Market"    "Financial Services" "close"          14.13 2017.11.10D14:30:00.676000000 1..
AIRG "Airgain Inc."                           "NASDAQ Capital Market"   "Technology"         "close"          7.7   2017.11.10D14:30:00.652000000 8..

You can find the code here.

Join the Conversation

20 Comments

  1. Are you aware of such information that might be publicly available for credit? TRACE prints, bond quotes etc. I doubt it but thought worth asking….

    1. You’re correct that there is not much available for credit. However, there is a great site called Quandl which provides all sorts of financial and alternative data. Some datasets cost while others are free. I saw some free fixed income/credit data there. They also have data from FINRA so you might be able to find TRACE prints there.

  2. i’m able to get json results via http post on a chrome browser. But I hit an issue with exception “‘api.iextrading.com. OS reports: No such host is known.” when running any of the functions above in a q session. any idea how to get it to resolve the host api.iextrading.com?Thanks.

    1. I haven’t seen that error before. Maybe IEX api was undergoing some maintenance when you last tried. Try again and see if it works. I just tried running one of the functions and it worked.

      If it still doesn’t work, can you post all the commands you are running (how you are loading the library, how you are running the functions etc).

      For example, here is what I get when I run it:

      MacBook-Pro:iex_q $ q iex.q
      q)get_last_trade`aapl`ibm
      sym  price  size time
      ----------------------------------------------
      AAPL 172.91 100  2017.12.15D15:03:26.316000000
      IBM  152.52 100  2017.12.15D15:03:31.004999936
      
      1. q)get_last_trade`aapl`ibm
        {[main_url;suffix;prefix;char_delta;identifier]
        result: (`$”:https://“,main_url) suffix,” “,prefix;
        (char_delta + first result ss identifier) _ result
        }
        ‘type
        @
        `:https://api.iextrading.com
        “GET /1.0/tops/last?symbols=AAPL,IBM HTTP/1.0\r\nhost:www.api.iextrading.com\..
        q))\\

        1. I tried pinging from my PC,

          C:\>ping https://api.iextrading.com
          Ping request could not find host https://api.iextrading.com. Please check the name and try again.

          On Chrome browers, https://api.iextrading.com/ returns below
          [an error occurred while processing the directive]

          But going to https://api.iextrading.com/1.0/stock/aapl/company, returns a json:
          {“symbol”:”AAPL”,”companyName”:”Apple Inc.”,”exchange”:”Nasdaq Global Select”,”industry”:”Computer Hardware”,”website”:”http://www.apple.com”,”description”:”Apple Inc is designs, manufactures and markets mobile communication and media devices and personal computers, and sells a variety of related software, services, accessories, networking solutions and third-party digital content and applications.”,”CEO”:”Timothy D. Cook”,”issueType”:”cs”,”sector”:”Technology”}

          Note sure if there’s a change in the api causing the issue or PC terminal is not able to ping that domain name https://api.iextrading.com

          1. I don’t believe that URL is accessible by itself. If you try accessing it via a browser, you get ‘[an error occurred while processing the directive]’ message. Just try accessing one of the endpoints that IEX has made available to get the data.

        2. Hmm never seen this error before.

          Try running the actual code line by line and let me know the output of ‘result’.

          q)main_url: "api.iextrading.com";
          q)prefix: "HTTP/1.0\r\nhost:www.",main_url,"\r\n\r\n";
          q)convert_epoch:{"p"$1970.01.01D+1000000j*x};
          q)syms:`AAPL`IBM
          q)  syms:$[1<count syms;"," sv string(upper syms);string(upper syms)];
          q)syms
          "AAPL,IBM"
          q)  suffix: "GET /1.0/tops/last?symbols=",syms;
          q)  result: (`$":https://",main_url) suffix," ",prefix;
          
  3. Looks like SSL needs to be setup on the machine. I tried using the latest 32-bit kdb with debugging.
    It works for http but not https.

    q)(`$”:https://“,main_url) suffix,” “,prefix;
    ‘conn. OS reports: The requested protocol has not been configured into the system, or no implementation for it exists.
    [0] (`$”:https://“,main_url) suffix,” “,prefix;
    ^
    q)(`$”:http://“,main_url) suffix,” “,prefix;
    q)(`$”:http://“,main_url) suffix,” “,prefix
    “HTTP/1.1 301 Moved Permanently\r\nServer: nginx\r\nDate: Sun, 17 Dec 2017 08..

  4. Hello Himanshu,

    Thanks for this nice work.

    First of all, I am new member in KDB side, so my questions may be very basic sometime.
    I am trying to run the function from iex_q.q file and getting error. I am on my personal laptop with windows 10 OS. Kindly help me out as error shown below.

    
    q)get_last_trade[`IBM]
    'conn. OS reports: The requested protocol has not been configured into the system, or no implementation for it exists.
    [2] C:\q\w32\iex_q.q:19: get_data:{[main_url;suffix;prefix;char_delta;identifier]
    result: (`$":https://",main_url) suffix," ",prefix;
    ^
    (char_delta + first result ss identifier) _ result
    q))\
    
    
    1. Error

      
      q)get_last_trade[`IBM]
      'conn. OS reports: The requested protocol has not been configured into the system, or no implementation for it exists.
      [2] C:\q\w32\iex_q.q:19: get_data:{[main_url;suffix;prefix;char_delta;identifier]
      result: (`$":https://",main_url) suffix," ",prefix;
      ^
      (char_delta + first result ss identifier) _ result
      q))\
      
      
  5. Quandl also provides a python api to return json data. So, the above API for IEX is very useful as a reference point when getting data from Quandl.

    1. Nice! I looked at Quandl a while ago so will need to check it out again, especially after they got bought by NASDAQ.

Leave a comment

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