Close Navigation

Requesting Market Data

Lesson 4 of 11

Duration 7:29
Level Intermediate

To watch this video you must accept functional cookies.

Hello, and welcome to this lesson on requesting market data in the Interactive Brokers Client Portal API. In this lesson, we will be discussing how to find the snapshot fields, how to request live market data snapshots, and how to request historical market data.

Finding Market Data Fields

Let’s begin by first looking at the Interactive Brokers endpoints documentation page. If we scroll down, we will eventually find our iserver/marketdata/snapshot endpoint that we will be working with today. If we click on the banner, we will expand the snapshot section to reveal more information on the endpoint. The reason to point out this section is that we can find what data values we want to work with. Under the ‘Responses’ portion, I can see here where it shows ‘Model’. If we click that, we should find a new grey box.

I can click that right-pointed arrow to expand things, I will see a massive list of all available field values to use with the snapshot endpoint. Some of the most popular values are 31, 55, 84, and 86. As we can see by looking through this page, I will retrieve my last price, symbol, bid, and ask values respectively. There are many more values here that we encourage you to explore, but this will give us a great example to get started. With our fields found, we can start taking a closer look at the actual request.

Requesting Market Data

Jumping back to VS Code, I will create a new program with my typical framework, and a method named marketSnapshot(). I will create my endpoint for my market data request and set to “iserver/marketdata/snapshot”. The snapshot endpoint utilizes a GET request, so now we can start building the parameters list.

We can first create a conid variable, and set it equal to “conids=265598”. Some viewers may have picked up the fact that we stated “conids” instead of just “conid”. That is because this endpoint allows for multiple contract requests at once. You will just need to use a comma to separate each contract ID. Let’s do that by including IBM, or 8314.

With our contracts set, I will move on to our fields variable. I will set this variable equal to “fields=31,55,84,86” based on what we found before. That is all we need in order to get started with our requests. I will create a params variable, and concatenate my parameters. After that I can create my full url with the request_url set to “”.join([base_url, endpoint, “?”, params]).

Again, I can create a market_req variable and create a new GET request. I can use my json.dumps() method from before to help with readability. And finally, we will print both variables to see our response status and body. So if we make a few requests, we should see all of our fields returned.

Discussing the return

I would like to take a moment to discuss how data is requested and returned. The first request will typically only display the coindex, and the conid. This is just to signify that you have requested data for the given contract. You can think of it as instantiating the market data stream. Often times for stocks, you can make another follow up request right away, and receive data. However, it’s important to note that fields like 7310, or the Option’s Greek Theta, can take a few requests, or even a few moments to begin. That is because some values may not be readily available and must be calculated by Interactive Brokers before returning. Especially on less active strikes, this can take up to a minute before returning these fields.

Requesting Historical Data

            With our live market data returning as intended, we can now move on to a historical market data request. In addition to our standard framework, I will add an endpoint variable, and set this equal to “hmds/history” for my historical data request. This is yet another GET request, so this will be built with a set of parameters: conid, period, bar and optionally, outsideRth and barType.

            As opposed to live data, the historical data request will only allow a single conid at a time. As such, I will make a conid variable and set it equal to 265598 for AAPL. Now, I can make a period variable. This will determine the total range of data returned. It can scale from minutes, hours, days, weeks, months, or even years. But given our example today, I will set it equal to ‘1w’ for 1 week. Then I can create my bar variable and set it equal to ‘1d’, so I can see each day’s bar for the whole week.

            If I wanted to, I could make a request now and receive data without issue. However, I would like to move on to our optional parameters. I will start by setting outsideRth to ‘true’. By default, outsideRth is set to ‘false’ so we only see regular trading hours; however, we can choose to disable this. Next, I will create a barType variable and, in my case, set it equal to ‘midpoint’.

Available values:

  • Period: {Xmin, Xh, Xd, Xw, Xm, Xy}
  • Bar: {Xmin, Xh, Xd, Xw, Xm}
  • outsideRth: {true (for times outside the 9:30 – 16:00 trading hours), false (for those during regular trading hours only)}
  • barType:   {last, midprice, bid, ask, inventory}

Again, I will create a few join variables followed by my request, json, and print values. And so if I request this data, we will see 7 bars returned, for our requested 7 days, along with their OHLC values. It’s important to note that some values, such as the default ‘last’ value for the barType parameter will return Volume for the bar as well, notated by the ‘v’ field returned. Please feel free to explore our available values for both of these endpoints to find the best data for you.

Thank you for watching this lesson on requesting market data in the Client Portal API. If you find this lesson helpful, please check out our other lessons in the Client Portal API tutorial series.

Code Snippet – liveData.py

import requests
import json

# Disable SSL Warnings
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

def marketSnapshot():
    base_url = "https://localhost:5000/v1/api/"
    endpoint = "iserver/marketdata/snapshot"

    conid="conids=265598,8314"
    fields="fields=31,55,84,86"


    params = "&".join([conid, fields])
    request_url = "".join([base_url, endpoint, "?", params])

    md_req = requests.get(url=request_url, verify=False)
    md_json = json.dumps(md_req.json(), indent=2)

    print(md_req)
    print(md_json)

if __name__ == "__main__":
    marketSnapshot()

Code Snippet – histData.py

import requests
import json

# Disable SSL Warnings
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

def historicalData():
    base_url = "https://localhost:5000/v1/api/"
    endpoint = "hmds/history"

    conid="conid=265598"
    period="period=1w"
    bar="bar=1d"
    outsideRth="outsideRth=true"
    barType="barType=midpoint"

    params = "&".join([conid, period, bar,outsideRth, barType])
    request_url = "".join([base_url, endpoint, "?", params])

    hd_req = requests.get(url=request_url, verify=False)
    hd_json = json.dumps(hd_req.json(), indent=2)

    print(hd_req)
    print(hd_json)

if __name__ == "__main__":
    historicalData()

Join The Conversation

For specific platform feedback and suggestions, please submit it directly to our team using these instructions.

If you have an account-specific question or concern, please reach out to Client Services.

We encourage you to look through our FAQs before posting. Your question may already be covered!

45 thoughts on “Requesting Market Data”

  • Matt H

    I see exponential moving averages are available… what about RSI? It’s harder to calculate and would be a bigger help than emas… please?

  • zhineng

    I want to get the historical data of the specified “startTime”. How should this time format be transmitted? Can you give an example? I use java. Thank you.

    • Interactive Brokers

      Hello, thank you for reaching out. With the release of TWS 10.17 and TWS API 10.18 clients now can send date/time in different formats:

      API allows UTC format “yyyymmdd-hh:mm:ss” in date/time fields.
      Example: 20220930-15:00:00
      API allows date/time field format with instrument’s exchange timezone (for all non-operator fields) and operator’s time zone (for all fields).
      Example: IBM 20220930 15:00:00 US/Eastern
      We hope this helps!

  • Thomas

    Is it possible to get real time data (Open, High, Low, Close)
    Thank you.

    • Interactive Brokers

      Hi Thomas, yes, it is possible to get real-time market data through Interactive Brokers. You can follow these steps to subscribe to live market data:

      1. Log in to the Client Portal
      2. Click on the User menu (head and shoulders icon) in the top right corner, then select Settings
      3. Under User Settings, go to Trading Platform > Market Data Subscriptions
      We hope this helps!

  • Tomas

    Hi,
    to get “day” historical data with attributes below is OK.
    period=”period=1d”
    bar=”bar=15min”

    to get “hours” historical data with attributes below is NOT OK.
    period=”period=8h”
    bar=”bar=15min”

    Is this correct behavior of system? Thank you.

    • Interactive Brokers

      Hi Tomas, thank you for reaching out. That would be expected be expected behavior. Period supports S for seconds, d for days, w for weeks, and so on. ‘h’ or hours are not a supported unit for period values. Please see our documentation for additional insight on valid period units and their affiliated bar sizes: https://www.interactivebrokers.com/campus/ibkr-api-page/cpapi/#hmds-period-bar-size

      We hope this helps!

  • Matteo Prinetti

    Hi, how do I find out the conids for already expired options ? For example

    https://localhost:5000/v1/api/iserver/secdef/info?conid=756733&secType=OPT&month=JAN24&strike=470

    Will return all the “new” options on SPY for JAN 24.

    But if I try the ones which are already expired (in this example DEC23) it returns “no contracts retrieved”

    https://localhost:5000/v1/api/iserver/secdef/info?conid=756733&secType=OPT&month=DEC23&strike=470

    I would like to find out the conids of expired options so I can fetch the historical intraday data for a specific contract on a specific day (this is for 0DTE trading).

    Thank you

  • Ethan

    In which document I can find the field definition of the contract?
    thank you

  • Paolo

    How do I get all the securities in a specified index (e.g. Russell3k)? And the conid associated to them?
    Thank you!

  • Thomas

    During the trading day, it is possible to obtain historical candles and the current real-time candle. For example, 5:40 PM here I would like to complete 5 minutes candles in the past so e.g. 5:35, 5:30, 5:25 and so on. And the current candle at 5:40 PM.
    Ist this possible with one api call.
    Programming language is python.

    Thank you

    • Interactive Brokers

      Hello, thank you for reaching out. Each query for market data would require it’s own request. The WebAPI does not support updated historical market data through a single request. As a result, the /iserver/marketdata/history or /hmds/history endpoints should be used to request 5 minute bars as needed. I would note that the websocket endpoint does not support regularly streamed historical data either, and so the only method to receive bars through the webapi would be through regular queries.

      If the need for bar data is greater than the need for WebAPI usage, our TWS API does support historical bar data as well as building live bars with the keepUpToDate=True flag. Please see ( https://www.interactivebrokers.com/campus/ibkr-api-page/twsapi-doc/#hist-keepUp-date) for more information.”

  • P SHAH

    Hi there, thanks for the helpful videos. I’m trying to get historical options price for NVDA. First I got conid for NVDA running “iserver/secdef/search”. Then I found conid for 900 options for APR24 and use “hmds/history” to get 2w data with 5m bars. I’m getting the response but in the [‘data’] of the response, I get it with t, o, c, h, l. I don’t see other fields like volume or greeks. How do I get them ?

    • Interactive Brokers

      Hello, thank you for reaching out. The data received will be unique between barTypes. As we discussed in the video, “It’s important to note that some values, such as the default ‘last’ value for the barType parameter will return Volume for the bar as well, notated by the ‘v’ field returned.” Other barTypes, like what we saw with MidPoint, will only return the OHLCT values, as Bid and Ask do not maintain a trade volume. Other values, such as historical Greeks, are not available via Interactive Brokers in any of our barType values.

      We hope this answers your question.

  • Paolo

    Hi,

    Re: my previous question on the 30th of March:
    “How do I get all the securities in a specified index (e.g. Russell3k)? And the conid associated to them?”

    Through the app, I can see the breakdown of some ETFs (SPY, for example) into the holdings (stocks & weights). Is this information available via the API too?

    Thanks!

    • Interactive Brokers

      Hello, thank you for reaching out. At this time, this is not available via the API. Please reach back out with any more questions. We are here to help!

  • Bernard

    Hi i am new to IBKR. Id like to download daily historical OHLC for E-mini continuous futures contract into an excel.
    id appreciate if you would show me the steps. Sorry if this question has been asked before. Thanks very much in advance.

    • Interactive Brokers

      Hello, thank you for reaching out. Please follow these steps to download daily historical OHLC data for E-mini futures contracts into Excel using IBKR’s Real Time Data server:

      Open Excel and click on the “Data” tab.
      Click on “From Other Sources” and select “From Interactive Brokers”.
      In the RTD server login window, enter your IBKR account credentials and click ok.
      In the Configure Real-Time Data Connection window, select the contract you want data for from the dropdown (e.g. ESM9 for E-mini S&P 500 June 2019 contract).
      Set the fields you want to download, like Open, High, Low, Close.
      Set the time period, like start and end dates.
      Click Ok and the historical OHLC data will populate in the Excel sheet.
      You can refresh the data or add more contracts/fields as needed.

      Please view this FAQ for more information: https://www.ibkr.com/faq?id=28236663

      We hope this clarifies things!

  • Patrick

    Hello! When I try to subscribe on market data, I cannot, since on my paper trading account, there is no such option under user nor account settings. Why is there such limitations? I can only see communication and security options.
    Thank you!

    • Interactive Brokers

      Hello Patrick, thank you for reaching out. Real-time quotes are offered on a monthly fee-based subscription basis, and in order to pay for those subscriptions you must have a live, funded trading account. If you subscribe to data through your live trading account, those same subscriptions will be made available through your paper trading account. Please review this FAQ for more information: https://www.ibkr.com/faq?id=26561453

      We hope this helps!

  • Steve

    Hi, I am using my paper account (which shares my real account data subscription). I am able to get Market data, e.g. current stock price. However I wasn’t able to get historical data.

    The endpoint URL that I am using: `https://192.168.3.1:5000/v1/api/hmds/history?conid=4391&period=1w&bar=1d&outsideRth=true&barType=midpoint`

    I am using python. I would first get a Error 404: “, after that I will consistently get Error 500 “.
    I couldn’t figure out how to debug the issue.

    • Interactive Brokers

      Hello Steve, thank you for asking. The initial response object from the /hmds/history endpoint will always result in a 404. Given your parameters are all valid, the error is likely based on the IP hosting your client portal gateway. As mentioned in our Getting Started section of the Client Portal API documentation, the gateway must run on the same machine where commands are generated. If you experience any difficulties running your Client Portal Gateway on the same machine, please contact Client Services: https://spr.ly/IBKR_ClientServicesCampus

  • Anonymous

    Hi,
    given i would like to download historical data into excel, specifically OHLC for E-mini futures contract RTH, should I be using RTD or DDE? It is not clear from your video. Thank you very much.

    • Interactive Brokers

      Hello, thank you for reaching out. To request any data aside from Real Time Data, you would need to use ActiveX or DDE for Excel. RTD can only provide Real Time Data for users. We appreciate your question!

  • Clint

    Hi, is there any way to get a market data snapshot for complex position like a spread or combo? I see TWS shows the correct bid/ask for these type of orders but can’t seem to get it through an API call anyway.

  • Matt

    Hello, Thank you for this tutorial. I am using the other endpoint – ‘iserver/marketdata/history’ to get historical data. The values for volume seems to be wrong when using this endpoint. For example, for the symbol – AMGN having IB Contract ID – 266145, the volume returned for June 6th, when the bar is “d” is the following. The 11512.52 or 493.39 values for volume do not make sense. [{‘o’: 308.07, ‘c’: 306.96, ‘h’: 310.31, ‘l’: 306.16, ‘v’: 11512.52, ‘t’: 1717574400000}, {‘o’: 306.84, ‘c’: 305.64, ‘h’: 310.0, ‘l’: 304.36, ‘v’: 493.39, ‘t’: 1717660800000}] Can you please clarify how to interpret these values? Thank you!

    • Interactive Brokers

      Hi, thank you for reaching out. Please create a web ticket for this inquiry; we have a category specifically for “API.” One of our API experts will be happy to guide you! https://spr.ly/IBKR_ClientServicesCampus

  • Joachim

    How do I use hmds/history to get data for a specific date? Like in the TWS api. Below request will give a 503 which probably means I am requesting too much data. In TWS you would do the request with: endDateTime, The request’s end date and time (the empty string indicates current present moment). params = { ‘conid’: ‘272093’, ‘period’: ‘4y’, ‘bar’: ‘1min’ } # Send the GET request to the CP Web API response = requests.get(url, headers=headers, params=params, verify=False)

    • Interactive Brokers

      Hello, thank you for asking. Webapi historical data requests can only provide data with respect to the current time, but does not provide time relative to a specified date as the TWS API does. In situations where this is required, developers should look to utilize TWS API to resolve their data needs. If you have any additional questions, please create a web ticket for this inquiry; we have a category specifically for “API.” One of our API experts will be happy to guide you! https://spr.ly/IBKR_ClientServicesCampus

  • Jun

    Hello, I would like to know if the GET market data snapshot free or it is subjected to the 0.01 USD per call fee. Thank you.

    • Interactive Brokers

      Thank you for asking. IBKR offers eligible clients the option of receiving a real-time price quote for a single instrument on a request basis. This service, referred to as “Snapshot Quotes” differs from the traditional quote services which offer continuous streaming and updates of real-time prices. Snapshot Quotes are offered as a low-cost alternative to clients who do not trade regularly and require data from specific exchanges when submitting an order. To view the costs for Snapshot quotes on each exchange, please view this User Guide: https://www.ibkrguides.com/kb/snapshot-market-data.htm

      We hope this helps!

  • jagteshwar singh

    I want to get 5 minute data for Tsla, for the last 10 years how can I get it, if you can direct me that would be great

  • Bob Mah

    Hi IBKR Team. I would like to access the Real-time Live Market Data Level 1 as: bid, ask, lastPrice, through the Web API, but seems like I’m receiving the delayed data. I’m trying to understand why by using the same account in IBKR Desktop application, without any subscription I do see the real-time market data, but not possible to achieve the same result by API ? Thanks, best regards.

  • El Dorado

    Hi there, iserver/marketdata/snapshot works fine with my live account for IBM and AAPL (8314, 265598). However, on my paper account, I get: [ { “_updated”: 1756489472280, “conidEx”: “265598”, “conid”: 265598 }, { “_updated”: 1756489472281, “conidEx”: “8314”, “conid”: 8314 } ]

    • Interactive Brokers

      Hello, thank you for reaching out. Please create a web ticket for this inquiry; we have a category specifically for “API.” One of our API experts will be happy to guide you! https://spr.ly/IBKR_ClientServicesCampus

  • daniel

    Hello and thanks for these examples. I have success with the endpoint iserver/marketdata/history, but not with hmds/history: Using your example url = ‘https://localhost:5000/v1/api/hmds/history?conid=265598&period=1w&bar=1d&outsideRth=true&barType=Last’ resp = requests.get(url, verify=False) yields Response [500]: “There was an error processing the request. Please try again.” Same trouble with a conid for which I’m sure to have Market Data subscription. Thanks for helping out.

    • Joakim

      I had the same. Try outsideRth=”outsideRth=True”

  • daniel

    Response 500: login + logout on IB website (portal); is the real/paper account used on another machine ? (check TWS shows graphs, then close it again). Endpoint /iserver/marketdata/history: no bid/ask, no 5sec, can specify exchange, max 1000 rows output, last bar missing (eg: period=1d&bar=1min&startTime=20250904-14:00:00 misses the bar at 13:59:00 UTC). Endpoint hmds/history (beta): have bid/ask or 5sec, cannot specify exchange (default: SMART), get >1000 rows but large queries may not always give the same output (eg: period=1d&bar=5sec may truncate output), so stick to the rules in the table to be safe (eg: max 3600S for 5sec bars). Speed: not faster than TWS api (on avg 2 sec per day of 1min bars of EU stocks).

    • Interactive Brokers

      Hello, thank you for reaching out. Please create a web ticket for this inquiry; we have a category specifically for “API.” One of our API experts will be happy to guide you! https://spr.ly/IBKR_ClientServicesCampus

Leave a Reply

Disclosure: Interactive Brokers

The analysis in this material is provided for information only and is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad-based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation by IBKR to buy, sell or hold such investments. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

The views and opinions expressed herein are those of the author and do not necessarily reflect the views of Interactive Brokers, its affiliates, or its employees.

Disclosure: API Examples Discussed

Please keep in mind that the examples discussed in this material are purely for technical demonstration purposes, and do not constitute trading advice. Also, it is important to remember that placing trades in a paper account is recommended before any live trading.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.