Welcome to this lesson on requesting market data in the Trader Workstation API. In this video, we will be highlighting the requirements for requesting market data, how to request delayed data, how to request live market data, and how to request historical bars. Please note that these are the most popular methods of requesting market data; however, Interactive Brokers also offers tick data, histogram data, and market depth.
Discussing Market data subscriptions
Let’s begin by discussing market data subscriptions. In order for clients to subscribe to market data, users must have a funded IBKR account for at least $500 USD in most instances. There are some instances where this is not the case; however, for the average individual at Interactive Brokers, $500 is the minimum. This threshold must be maintained in addition to the cost of any subscriptions held by the account.
For those with a long-time IBKR PRO account, you may have observed that some instruments return market data to your Trader Workstation for free by default. That is because some market data can be provided to users for free while “on-platform”. On-platform simply means that users are observing data directly display through one of Interactive Brokers platforms. Exchanges consider API functionality to be considered off-platform, and as a result, typically have a cost affiliated with them. Some of the most popular market data subscriptions for API use are listed in the API Documentation for Market Data on IBKR Campus. Users can subscribe to market data through the Client Portal.
It is also worth clarifying that Market Data is affiliated on a per-user basis. Many clients will run a single Trader Workstation instance for monitoring trades; however, it is common to have a separate machine running your trading algorithm on IB Gateway hosted on a virtual machine elsewhere. In order for both of these platforms to retrieve market data, each user consuming market data would need to subscribe to data separately.
Requesting streaming data
With the subscription discussion out of the way, we can start to dive into the actual API requests. Please note that we will be using the same framework from our Essential Components video, so if there are any questions on the initial structure in this video, please be sure to review that lesson first.
The most popular way of requesting and viewing data in the API would be with EClient.reqMktData method, which requests the same data available in the TWS Watchlists.
Requesting delayed data
Clients that do not have a market data subscription for instruments can often request 15 minute delayed data. This is only a single extra step compared to standard market data subscriptions, so I will include it before moving forward.
To clarify if your requests will be Live or delayed, users simply need to call the app.reqMarketDataType function. The only argument this takes is the type of data to retrieve, which can be 1,2,3,4 or Live, Frozen, Delayed, or Delayed Frozen respectively. Frozen data will refer to market data from the most recent close, while delayed frozen data will return yesterday’s closing values. And then as we’ve mentioned, standard delayed data will return 15-minute delayed data.
If I am subscribed to market data on a given instrument, but request delayed market data, live data will still be returned. Interactive Brokers will always try to provide the most up-to-date market data where possible.
Requesting live data
Now, let’s start building out our request for streaming data. We will be focused on requesting price and size data; however, the reqMktData request can also return string, news, generic and even Greek values depending on the tick types provided.
From within our TestApp class, let’s start defining one of our tick functions, tickPrice. This will handle all returning values related to price values. tickPrice takes self, reqId, tickType, price, and attrib as arguments. While we’re already familiar with the first two, and the last two are rather self-explanatory, the tickType argument is used to indicate what kind of data is coming in.
Each tickType is an integer value that correlates to a specific value, be it bid price, last size, closing price, or otherwise. For a full list of all of these tick values, we can look at ticktype.py inside the ibapi source files and see exactly what everything is relating to. Users are welcome to reference the returned integer values directly; however, the enumerator contains a toStr method that converts our tick type integers into the values we see before us. In our file, we can add an import for from ibapi.ticktype import TickTypeEnum. This will allow us to reference the TicKTypeEnum.toStr() method and print out our value directly in a moment.
I will print out all of these values in an f-string, including our reference of TickTypeEnum.toStr(). As we discussed before, this would be perfectly fine to print out our price values; however, I also want to see the quantities of our trades effected by this. To do this, we will also add the EWrapper.tickSize function to our TestApp class as well. This function only takes the arguments: self, reqId, tickType, and size. The sizes returned here will relate to the prices returned in our tickPrice function and allow us to create a clearer picture of the trades taking place.
Now that we have everything in place to receive the data, let’s build out a contract object and a request for market data. Leaping off of our prior video, I’ll make a request for AAPL market data using the symbol, security type, currency, and exchange values.
With a contract now set, I can call app.reqMktData to start requesting my streaming data. For arguments, we’ll need to pass the reqId, which we’ll use our app.nextId() function for. I can pass mycontract for the contract object. For our next argument, the generic tick list, I will pass “232” as a string so I can retrieve the mark price from my request. For users looking to request multiple generic ticks, you would simply comma-separate the values within the string. So maybe you would pass “232, 233, 234” as an example.
The next argument defines if we are requesting a Snapshot value.
This is a single return instance aggregating the last 11 seconds of trading. If no trading has taken place, this will not return any values. And if we do see trades in the last 11 seconds, we will see those values returned in aggregate. Similarly, the next argument determines if we are requesting a regulatory snapshot. This is a snapshot used to determine the price of an instrument before executing a trade.
Regulatory snapshots will cost approximately $0.01 per request, until we reach the cost of the affiliated subscription. If I request market data for AAPL repeatedly, Interactive Brokers will eventually add the subscription to your account, as the cost of the regulatory snapshots equate to the value of the subscription anyway. The final argument takes market data options, which is an argument used only for internal purposes.
If we run this script, we’ll find an initial flood of data depicting the most recent values for several tick types, then over time we will receive all the live data prices and sizes as they come through.
Requesting Historical Data
Requesting Historical Data follows a similar pattern to the live market data requests. The one caveat to this is that market data cannot be retrieved if you do not have a valid market data subscription. Before we begin to dig into historical data, I’d like to first find how far back we can request market data. I’ll start finding this value using a new python file.
Requesting the headTimeStamp Value
In our new file, I will create a new function in the TestApp class to define the headTimeStamp function. This takes three arguments, self, a request ID, and the headTimeStamp string. Within my new function, I will print out my headTimeStamp value. I will also make a request for self.cancelHeadTimeStamp to terminate the request now that I’m done with it, and we can just pass the requestId we received as an argument. With the EWrapper piece out of the way, I will move out of the TestApp class to create my headTimeStamp request. I will copy over my same AAPL contract I used from the Live Data script, because I want to validate how far back I can find AAPL market data.
Next, I will make a call to the app.reqHeadTimeStamp function. This takes an argument for a request ID, which can use our nextId function; and a contract reference, which will take my mycontract object. After these two, I’m now encountering something known as the “whatToShow” value. This same value is used to denote what data is displayed in your TWS bar charts. In my case, I will use the value for Trades, though the full list of whatToShow values are available in our documentation.
The next argument relates to using regular trading hours. A 1 will indicate that we want the earliest date within trading hours, while a 0 will request the earliest date outside of trading hours. Finally, we have the formatDate parameter. This will indicate whether we want 1, a UTC timestamp in a string format, or 2, an Epoch timestamp. The latter is an integer representation of the same timestamp. You can consider the former better for human consumption, while the latter is best utilized in a programmatic request structure. I will show these off in just a moment by making two requests.
If we run this script using ‘1’ as the date format, we’ll see 19801212-14:30:00. Meaning AAPL’s “Trades” market data can go as far back as December 12, 1980 at 9:30 AM Eastern. Before we move on, I’ll quickly add another print statement to my headTimeStamp method for the datetime.datetime.fromtimestamp function, taking in the integer version of our headTimeStamp. If I change my original request to use 2 as my date format, I’ll print out the original epoch value as well as the python translated datetime, which is automatically converted to my pc’s local time in US/Central time.
Requesting historical bars
Now that we know our historical data range, we can start making a request for historical data. You are welcome to use the same file, but for my demonstration, I’ll be starting from a fresh example of our standard layout but add in our AAPL contract again. As always, we’ll define the EWrapper function inside TestApp using def historicalData. This function takes an argument for self, reqId, and bar. We will finish the function by printing the reqId and bar values.
I will note that we are printing out the full bar object; however, the bar object can be split out, so you may print bar.open for the opening price, bar.close for the closing price, and so on. But just for our presentation here, I’ll print the whole thing.
Each bar size is returned separately, so for us to know we’re done we should reference the EWrapper function for historicalDataEnd. This function takes an argument for self and reqID and is just meant to indicate that all available data has been returned to the user.
With the wrapper functions set, we’ll start our EClient request. To make the request, we’ll call the app.reqHistoricalData function. This takes 10 total arguments, starting with reqid, contract.
The next argument is endDateTime, which takes the value we’d like to end our historical data at. If we leave this as an empty string, the system will assume the current time. Otherwise, we would make a format for year, month, day followed by the 24 hour timestamp, and a timezone. You must pass the timezone for the exchange, available through a Contract Details request, the exact timezone used for your TWS, which is set prior to the homescreen, or using UTC. I will send my request for “20240523 16:00:00 US/Eastern”.
Then, we’ll pass in a duration value, which corresponds to the full interval over which data will be returned. So, if I specify “1 D”, I will receive however many bars in a single day. On the topic of bars, the next argument will take the bar size. In my case, I can pass “1 hour” to receive an hour.
This means I will receive 7 bars for my day, a bit more on that later.
Moving forward in our arguments, we’ll find more familiar values, like the whatToShow value we used before, which I’ll use “TRADES” for once again, then useRth and formatDate, again using 1 in both cases. Now we have the option for “keepUpToDate”, which allows us to build bars as data is available and return these new bars to the historicalDataUpdate function. I’m not interested in this data at the moment, so I’ll go ahead and leave this as False for now. Finally, we end with market data options, which I’ll again leave as an empty list.
Now if we run this script, I will see my request ID, and all of my bar’s values. While most of this is self-explanatory, there are a few points I’d like to mention from the programmatic standpoint. You might notice that we sent the request using US/Eastern, but my data shows America/Chicago. That’s because I’m choosing to print out my “Operator Timezone” even though I made the request with the “Instrument Timezone”. You can modify the time zone returned in TWS by opening the Global Configuration page and opening the API Settings. You’ll notice a section for “Send instrument-specific attributes for dual-mode API Client in”. Specifying Operator Timezone returns your TWS time zone, Instrument time zone returns the time zone for the contract, and UTC is obviously the UTC time zone.
The other piece I’d like to mention is the 7 bars I had referenced earlier. The Date value in the bar references the starting time for the bar. In my case, we can see my bar started at 08:30:00 America/Chicago, which is when NASDAQ opens for trading AAPL. But then we’ll see 09:00 as our next bar, meaning our first bar is only 30 minutes long, before turning into a series of 1-hour bars. This is the same behavior as Trader Workstation, though it may not be as commonly understood when pulling data programmatically. Therefore, it’s best practice to use the next bar as an indicator of approximate bar size.
This concludes our lesson on market data in the TWS API. Thank you for watching. If you have any questions, please be sure to review our documentation or leave a comment below this video. We look forward to having you in the next lesson of our TWS API series.
Code Snippet – headTimeStamp
from ibapi.client import * from ibapi.wrapper import * import datetime import time import threading port = 7497 class TestApp(EClient, EWrapper): def __init__(self): EClient.__init__(self, self) def nextValidId(self, orderId: OrderId): self.orderId = orderId def nextId(self): self.orderId += 1 return self.orderId def error(self, reqId, errorCode, errorString, advancedOrderReject=""): print(f"reqId: {reqId}, errorCode: {errorCode}, errorString: {errorString}, orderReject: {advancedOrderReject}") def headTimestamp(self, reqId, headTimeStamp): print(headTimeStamp) print(datetime.datetime.fromtimestamp(int(headTimeStamp))) self.cancelHeadTimeStamp(reqId) app = TestApp() app.connect("127.0.0.1", port, 0) threading.Thread(target=app.run).start() time.sleep(1) mycontract = Contract() mycontract.symbol = "AAPL" mycontract.secType = "STK" mycontract.exchange = "SMART" mycontract.currency = "USD" app.reqHeadTimeStamp(app.nextId(), mycontract, "TRADES", 1, 2)
Code Snippet – historical_market_data.py
from ibapi.client import * from ibapi.wrapper import * import datetime import time import threading port = 7497 class TestApp(EClient, EWrapper): def __init__(self): EClient.__init__(self, self) def nextValidId(self, orderId: OrderId): self.orderId = orderId def nextId(self): self.orderId += 1 return self.orderId def error(self, reqId, errorCode, errorString, advancedOrderReject=""): print(f"reqId: {reqId}, errorCode: {errorCode}, errorString: {errorString}, orderReject: {advancedOrderReject}") def historicalData(self, reqId, bar): print(reqId, bar) def historicalDataEnd(self, reqId, start, end): print(f"Historical Data Ended for {reqId}. Started at {start}, ending at {end}") self.cancelHistoricalData(reqId) app = TestApp() app.connect("127.0.0.1", port, 0) threading.Thread(target=app.run).start() time.sleep(1) mycontract = Contract() mycontract.symbol = "AAPL" mycontract.secType = "STK" mycontract.exchange = "SMART" mycontract.currency = "USD" app.reqHistoricalData(app.nextId(), mycontract, "20240523 16:00:00 US/Eastern", "1 D", "1 hour", "TRADES", 1, 1, False, [])
Code Snippet – LiveData.py
from ibapi.client import * from ibapi.wrapper import * import datetime import time import threading from ibapi.ticktype import TickTypeEnum port = 7497 class TestApp(EClient, EWrapper): def __init__(self): EClient.__init__(self, self) def nextValidId(self, orderId: OrderId): self.orderId = orderId def nextId(self): self.orderId += 1 return self.orderId def error(self, reqId, errorCode, errorString, advancedOrderReject=""): print(f"reqId: {reqId}, errorCode: {errorCode}, errorString: {errorString}, orderReject: {advancedOrderReject}") def tickPrice(self, reqId, tickType, price, attrib): print(f"reqId: {reqId}, tickType: {TickTypeEnum.toStr(tickType)}, price: {price}, attrib: {attrib}") def tickSize(self, reqId, tickType, size): print(f"reqId: {reqId}, tickType: {TickTypeEnum.toStr(tickType)}, size: {size}") app = TestApp() app.connect("127.0.0.1", port, 0) threading.Thread(target=app.run).start() time.sleep(1) mycontract = Contract() mycontract.symbol = "AAPL" mycontract.secType = "STK" mycontract.exchange = "SMART" mycontract.currency = "USD" app.reqMarketDataType(3) app.reqMktData(app.nextId(), mycontract, "232", False, False, [])
A big thanks!
Love!
Do you have any sample code for streaming the deep data(Level II)?
Hi Ali, thank you for asking. You can find sample code for market depth (level II) in our API user guide: https://interactivebrokers.github.io/tws-api/market_depth.html. We hope this helps!
First code is working fine. Code Snippet – Request Market Data But while running
Code Snippet – Request Historical Market Data
I am getting below errror
(ib_venv) akotak@Ankitkumars-MacBook-Pro IBJts % /Users/akotak/Downloads/AlgoTrading/IBJts/source/pythonclient/ib_venv/b
in/python /Users/akotak/Downloads/AlgoTrading/IBJts/source/pythonclient/my_work/rqst_hist_data.py
ERROR -1 2104 Market data farm connection is OK:usfarm.nj
ERROR -1 2104 Market data farm connection is OK:usfuture
ERROR -1 2104 Market data farm connection is OK:cashfarm
ERROR -1 2104 Market data farm connection is OK:usfarm
ERROR -1 2106 HMDS data farm connection is OK:ushmds.nj
ERROR -1 2106 HMDS data farm connection is OK:ushmds
ERROR -1 2158 Sec-def data farm connection is OK:secdefil
ERROR 1 162 Historical Market Data Service error message:No market data permissions for ISLAND STK. Requested market data requires additional subscription for API. See link in ‘Market Data Connections’ dialog for more details.
Hello Ankit, thank you for reaching out. All supported API programming languages offer call back functions to receive error messages sent from the TWS for troubleshooting purposes. Socket-based APIs (e.g., C#, Java, VB.Net, C++, Python and ActiveX) handle error messages within the error() call back function in the EWrapper interface. For additional information, including a list of message codes, please review our API documentation. We hope this helps!
If your query is still not answered after reviewing the documentation, please create a web ticket in Client Portal with the category “API.” Our experts will be glad to help you out!
Greetings,
I am trying to download the continous futures historical data for ES. I have set the contract as follows:
contract = Contract()
contract.symbol = “ES”
contract.secType = “CONTFUT”
contract.exchange = “GLOBEX”
contract.currency = “USD”
client.reqHistoricalData(
Fails with 200 contract specification not found. Can you tell me what is wrong here. Here is there any code sample available for getting continous futures.
Hello Arun, thank you for reaching out. Please review our TWS API user guide for sample code: https://interactivebrokers.github.io/tws-api/historical_time_and_sales.html. We hope it helps!
Hello, when I use “self.reqHistoricalData(orderId, mycontract, “20230908 09:20:00 Asia/Kolkata”, “2 D”, “5 mins”, “TRADES”, 0, 1, 0, [])” why the last of the bar is missing ? The output will be like “Historical Data: Date: 20230907 15:25:00, Open: 250.750000, High: 251.000000, Low: 250.400000, Close: 251.000000, Volume: 3814, Average: 250.765000, BarCount: 56
Historical Data: Date: 20230908 09:15:00, Open: 252.650000, High: 254.000000, Low: 252.500000, Close: 253.250000, Volume: 7373, Average: 252.950000, BarCount: 45
”
As you can see its ended on 15:25 not 15:30 I’m unable to end of the day data for 1min (which ends at 15:29) or 2min which ends at 15:28 – I can’t find the days last bar in any timeframe. What am i missing here? thanks!
Hello Kamal. For this inquiry, please open a web ticket in Client Portal (click the “Help?” in the upper right menu, then “Secure Message Center”). The best category to choose is “API.” Our API experts will be able to help you out from there!
Hi, I have a separate question related to this. For the output like in the original post (copied below), what is the meaning of the last item, BarCount? In addition, is Average the simple average of all trades or some weighted average (weighted by vol)? I tried to look up the IBKR website, but was not able to find out. I would appreciate your help or pointer to relevant resources. Thanks! Historical Data: Date: 20230908 09:15:00, Open: 252.650000, High: 254.000000, Low: 252.500000, Close: 253.250000, Volume: 7373, Average: 252.950000, BarCount: 45
Hello, thank you for asking. The BarCount field for the bar object is in reference to the number of trades during the bar’s timespan (only available for TRADES). The Average is a volume weighted average for the bar’s timeframe. We hope this helps answer your question!
Isn’t the times only the start of the interval: hence 15:29 has all trades 15:29-15:30 (and same for 15:25 has all trades 15:25-15:30).
Hi, the thing I don’t understand is when you define class TestApp, and start app.run(), why all the def functions inside the class TestApp are automatically executed? The basic python example tell people that we should explicitly call the function, for example, app.historicalData().
Hello, thank you for commenting. Our API experts would be glad to help you out! Please create a web ticket for this inquiry and select “API” as the category: https://www.interactivebrokers.com/sso/resolver?action=NEW_TICKET. You may also find the study notes below this video lesson helpful, and we have a TWS API user guide available on our website: https://interactivebrokers.github.io/tws-api/
First snippet worked fine, but the second one didn’t. I got the following error message:
ERROR -1 2104 Market data farm connection is OK:usopt
ERROR -1 2104 Market data farm connection is OK:usfarm
ERROR -1 2106 HMDS data farm connection is OK:euhmds
ERROR -1 2106 HMDS data farm connection is OK:cashhmds
ERROR -1 2106 HMDS data farm connection is OK:fundfarm
ERROR -1 2106 HMDS data farm connection is OK:ushmds
ERROR -1 2158 Sec-def data farm connection is OK:secdefil
ERROR 1 10314 End Date/Time: The date, time, or time-zone entered is invalid. The correct format is yyyymmdd hh:mm:ss xx/xxxx where yyyymmdd and xx/xxxx are optional. E.g.: 20031126 15:59:00 US/Eastern Note that there is a space between the date and time, and between the time and time-zone. If no date is specified, current date is assumed. If no time-zone is specified, local time-zone is assumed(deprecated). You can also provide yyyymmddd-hh:mm:ss time is in UTC. Note that there is a dash between the date and time in UTC notation.
When I tried to fix the time (from ’15:00′ to ’15:00:00′) as per the instructions in the error message, the code either hang or said that I didn’t have market data permissions for ISLAND STK. If the previous snippet worked fine for an identical contract, doesn’t this mean that I do have the required permissions? Moreover real-time quotes for AAPL seem to be available normally at TWS.
is there a way to get complete options chain for a security for any expiry ?
Hello, thank you for reaching out. You can use TWS Options Trader to modify the expiration date on a selected underlying asset. You can open it from the New Window button in Mosaic or the Trading Tools menu in Classic TWS. Once open, you can enter or change the underlying symbol in the top left. Next, hit enter to load the option chains. You can modify the expiration dates using the settings along the top. We hope this helps!
Hello,
Curious to know if there is a link that offers a sample for delayed data without a subscription or a subscription that offers this at a basic entry ? Thanks for any feedback or support. Python
Hi,
I tried the snippet of reqMktData in a paper account and get the error message:
“ERROR 1 10197 No market data during competing live session”
This only happens when calling reqMktData. The other two (reqTickByTickData/reqHistoricalData) works fine.
Hi Hao, thank you for reaching out. This error message indicates that the user is logged into the paper account and live account simultaneously trying to request live market data using both accounts. In such a scenario preference would be given to the live account. For more details please refer: https://ibkr.info/node/1719 We hope this helps!
I am looking to get complete optios chain for SPX for a given expiry using API
Hello, thank you for reaching out. Please view the linked FAQ below for instructions to create an options chain using API:
https://interactivebrokers.github.io/tws-api/options.html
We hope this helps!
I can’t get market data (5s bars, IBUS500 CFD, SMART) older than one month. According to the “Historical Data Limitations” page it should be 6 month, not one. What could I possibly have done wrong? Or have the limitations changed without notice?
Hello, thank you for reaching out. 5 second bars are not supported for period sizes larger than 1 hour (or 3600 S). 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!
I am trying to use IB Gateway with a Mac. All of the docs say there should be a checkbox “Enable ActiveX and Socket EClient” which does not exist in my config tab!?? Also, in the example above, there is this line of code:
app.connect(“127.0.0.1”, 7497, 1000)
What should this be using IB Gateway?
Finally, I have pip installed ibapi version 10.19.2. My IDE says there is no module ibapi.client or ibapi.wrapper?
Thx
Hello, thank you for reaching out. For IB Gateway, the “Enable ActiveX and Socket Clients” is always enabled as the platform was built specifically for API use. As discussed in the Essential Components of TWS API, the host, port, and client ID can vary based on system. At a minimum, the Socket Port (7497) would likely need to be changed to accommodate the default of IB Gateway Paper accounts (4002).
With respect to the inquiry of a pip install, please be aware that Interactive Brokers does not support network pip installation. See “Download the TWS API” for more information, here: https://www.interactivebrokers.com/campus/ibkr-api-page/trader-workstation-api/#find-the-api. After downloading the appropriate file, please see the Python-specific installation instructions here: https://www.interactivebrokers.com/campus/ibkr-api-page/trader-workstation-api/#setup-python.
Please reach back out with any more questions. We are here to help!
Hello, I’m just starting to explore the API and when trying the last example (app.reqMktData), I am getting “Requested market data is not subscribed.” What needs to be done to access real-time pricing via the API? This is a personal account. Thanks
Hello Alexander, thank you for asking. Please view this FAQ for instructions to subscribe to Market data in your live account: https://www.ibkr.com/faq?id=28239718
Some exchanges allow delayed data to be displayed without any market data subscription, free of charge. Customers receive free delayed market data (usually 10-20 minutes behind real-time quotes) for contracts to which they do not currently hold market data subscriptions when made available by the exchanges. To view other market data, please subscribe to the necessary market data subscriptions: https://spr.ly/IBKRMarketDataCampus
You can also use the Market Data Assistant to help find the best subscriptions for your needs:
https://ndcdyn.interactivebrokers.com/sso/Login?action=CS_MARKET_DATA_ASSISTANT
We hope this information is helpful!
Hello, How do I disconnect from the stream politely?
Hello, thank you for reaching out. Please view this FAQ to review or cancel your market data subscriptions:
https://www.ibkr.com/faq?id=28215152
We hope this helps!
Hello. Sorry, I meant when I have the stream of data is alive and then the program will need to disconnect at some point. It won’t need to be connected to the stream indefinitely. So when the program is ready to shut down, is there a system call to shut off the live data stream? Thank you
Hi again, we appreciate your question. Yes, there are a few ways to temporarily turn off live market data in your IBKR account:
If your account balance has recently dipped below the threshold for maintaining real time market data, your live data will be automatically turned off. You can re-subscribe once your balance is over the minimum threshold again.
You can manually pause your market data subscriptions for up to 60 days at a time in Account Management. This will turn off live data but allow you to easily resume the subscriptions later.
You can refresh your market data connection using the Ctrl + Alt + F keys which will temporarily disconnect the live data feed.
Logging out of TWS will disconnect the live data feed until you log back in again.
So in summary, you can either let the system automatically disable live data if your balance drops, manually pause the subscriptions temporarily, refresh the data connection, or log out of TWS to turn off live data.
We hope this helps!
how do we request historic market data function with what to show parameter set to TRADES getting volume bars, instead of time bars
Hello, thank you for reaching out. IBKR does not offer volume-based bars. However, it is possible to use EClient.reqHistoricalTicks request to construct the bars yourself. If you need additional assistance, 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! http://spr.ly/IBKR_ClientServicesCampus
I am getting the following error for the last close
2539336164736: Date: 20240923 12:50:00, Open: 116.550000, High: 116.570000, Low: 116.320000, Close: 116.370000, Volume: 8023, Average: 116.468000, BarCount: 2939, 2539336164784: Date: 20240923 12:52:00, Open: 116.360000, High: 116.430000, Low: 116.240000, Close: 116.250000, Volume: 7023, Average: 116.330000, BarCount: 2239, 2539336164832: Date: 20240923 12:54:00, Open: 116.250000, High: 116.380000, Low: 116.150000, Close: 116.200000, Volume: 9740, Average: 116.249000, BarCount: 3311, ]
The error comes for the bar at 12:56:00
Hi Eddie, thank you for reaching out. The “ETradeOnly” or “FirmQuoteOnly” flags are a legacy system used prior to our 9.83 release of the TWS API. This is a common indicator of a legacy installation using the pypi library, not affiliated with Interactive Brokers, or by skipping the setup.py installation step. Please follow the instructions described in this IBKR Campus course to ensure that you have properly installed TWS: https://www.interactivebrokers.com/campus/trading-lessons/accessing-the-tws-python-api-source-code/. After it is installed, please try the historical request again.
We hope this helps!
I am using
app.reqMarketDataType(3)
app.reqMktData(app.nextId(), mycontract, “232”, False, False, []).I can get the data, but they are always with 4 hours delay. I am in Vancouver Canada area.
Hi Pearson, 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! http://spr.ly/IBKR_ClientServicesCampus
Please advise how to request historical data of CONTFUT contracts to its earliest time point in increments. Please point me to sample code in python.
In the most updated documentation, it says ‘if I want to retrieve more than 1 day’s worth of 5 second bars, I will then need to request data in increments of D (days).’ But when putting reqHistoricalData into a for loop, I got identical data records each time.
Background: Follows API upgrading, I got error 10339 asking me to change end datetime to an empty string when calling reqHistoricalData. This force me to retrieve data from the most recent datetime, although I already have recent data. What I need is the data from an earlier time point (such as 1 year ago) to the earliest time point of a contract. Without the ability to specify end datetime, how to retrieve historical data of CONTFUT contract to its earliest time point in increments?
Thank you for reaching out. You can find the earliest data timestamp for a contract using the reqHeadTimeStamp function in TWS API. As mentioned in our Contracts documentation, Continuous Futures just present the front future contract. If you need to find earlier data than what is available as a Continuous Futures contract, you may instead specify an earlier date using the direct future contract. 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! http://spr.ly/IBKR_ClientServicesCampus
Hi, could you provide a simple code snippet to retrieve the current data (bid/ask, volume, Greeks, etc) for an option chain (given the underlying and an expiry date)? I have watched the ‘get market data’ video lesson, but this only discusses stock and futures market data. Many thanks!
Thank you for asking. The process for requesting data for Options and Stocks would be the same. The only adjustment would be to include the EWrapper.tickOptionComputation function to receive the Greek values. You can read more about this here: https://www.interactivebrokers.com/campus/ibkr-api-page/twsapi-doc/#request-option-greeks ). Please be aware there is no means to request the full option chain in a single request. You would need to request data for each given contract directly. 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! http://spr.ly/IBKR_ClientServicesCampus
Any chance you can point to the documentation page that describes this “232,233,234” values? I’ve looked everywhere and I can’t find it. Looking to learn more about what it means.
Hello, thank you for asking. Please view the documentation here: https://www.interactivebrokers.com/campus/ibkr-api-page/twsapi-doc/#available-tick-types
We hope this helps!
I’m trying to use reqHistoricalData with keepUpToDate = True, but I am getting only historical data, not update. Is there an example code for how use the keepUpToDate = True properly?
Hello, 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_TicketCampus
We also have a user guide for the APIs (and other trading platforms) on our website: https://spr.ly/IBKR_UserGuideCampus
What is “Please enter a valid security type” for Indian Markets?
Hi Anamak, thank you for reaching out. If you are experiencing a time-sensitive trading issue, please contact Client Services: http://spr.ly/IBKR_ClientServicesCampus
i’m getting the folllowing error when trying to get Options data with API. what can be the issue? thanks! “Sec-def data farm connection is OK:secdefil”
Hello Gabriel, thank you for reaching out. This means that everything is working as intended. You can review more information on this on this Traders’ Academy lesson: https://www.interactivebrokers.com/campus/trading-lessons/essential-components-of-tws-api-programs/
I get the error 10314 because I dont know the xx/xxx in endtime, US/Eastern will not fix it CET will not fix it. Please tell me the korrekt xx/xxx for CET (Central Europe Time) Thank you
Hi Torsten, thank you for reaching out. The timezone value of a contract may be requested using reqContractDetails and will be returned by the ContractDetails.TimeZoneId field.
Hi, I would like to know how to suppress the contract qualification errors from the API using ib_sync with Python. When I pull an option chain and process them using the qualifyContracts function, I receive that error when the specific strike price is not found. I try to use erorr handling (try, except, etc) without success. Is there a way to suppress these errors below?. Thanks Error 200, reqId 243: No security definition has been found for the request, contract: Option(symbol=’XSP’, lastTradeDateOrContractMonth=’20270115′, strike=594.0, right=’C’, multiplier=’100′, exchange=’CBOE’, currency=’USD’, tradingClass=’XSP’) Error 200, reqId 245: No security definition has been found for the request, contract: Option(symbol=’XSP’, lastTradeDateOrContractMonth=’20270115′, strike=596.0, right=’C’, multiplier=’100′, exchange=’CBOE’, currency=’USD’, tradingClass=’XSP’)
Hello, thank you for contacting us. The error mentioned implies requests for a contract that does not exist. Given that Interactive Brokers cannot provide support for third parties such as ib_insync, I would recommend using the reqSecDefOptParams or reqContractDetails request to find valid strikes for a given contract before requesting market data. 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. http://spr.ly/IBKR_ClientServicesCampus
Hi, I get some strange errors when using your original code ” historical_market_data.py”: reqId: 2, errorCode: 2176, errorString: Warning: Your API version does not support fractional share size rules. Please upgrade to a minimum version 163. Trimmed value 52980.31 to 52980, orderReject: 2 Date: 20240523 15:30:00, Open: 190.980000, High: 191.010000, Low: 189.050000, Close: 189.420000, Volume: 52980, Average: 189.938000, BarCount: 24557 … 2 Date: 20240523 21:00:00, Open: 187.270000, High: 187.830000, Low: 186.620000, Close: 186.910000, Volume: 64692, Average: 187.157000, BarCount: 35294 Historical Data Ended for 2. Started at 20240522 16:00:00, ending at 20240523 16:00:00 reqId: 2, errorCode: 366, errorString: No historical data query found for ticker id:2, orderReject: As I understand all shown prices are for reqId 2. So there are prices found, they look correct, but there is an error msg. Thank you Rainer
Hello Rainer, thank you for asking. This is a standard behavior when running an out-of-date API version, or not following the setup.py installation instructions noted in our Downloading & Installing the TWS API video. You can also find the steps for Updating the Python Interpreter for the TWSAPI in our Documentation: https://www.interactivebrokers.com/campus/ibkr-api-page/twsapi-doc/#setup-python
We hope this helps!
Hello, one more question: I get live and historical bid-ask for options, that are not expired. But using the same code for expired options leads to an error. What do I need to change here? As dataType I used either ‘BID’ or ‘ASK’. Thank you Best regards Rainer
Hello, thank you for reaching out. Expired options data is not available. To learn more, please visit our Unavailable Historical Data section of our documentation. We hope this helps!
I’m trying to request tickbytick data on eurusd using the code : mycontract = Contract() mycontract.symbol = “EUR” mycontract.secType = “CASH” mycontract.exchange = “IDEALPRO” mycontract.currency = “USD” app.reqTickByTickData(19001, mycontract, “Last”, 1000, True) and getting Failed to request tick-by-tick data.No historical market data for EUR/CASH@FXSUBPIP Last 0, orderReject:
Hi, I have a separate question related to one of the earlier posts. For the output like in the original post (copied below), what is the meaning of the last item, BarCount? In addition, is Average the simple average of all trades or some weighted average (weighted by vol)? I tried to look up the IBKR website, but was not able to find out. I would appreciate your help or pointer to relevant resources. Thanks! Historical Data: Date: 20230908 09:15:00, Open: 252.650000, High: 254.000000, Low: 252.500000, Close: 253.250000, Volume: 7373, Average: 252.950000, BarCount: 45
Hello, thank you for asking. The BarCount field for the bar object is in reference to the number of trades during the bar’s timespan (only available for TRADES). The Average is a volume weighted average for the bar’s timeframe. We hope this helps answer your question!
Thanks again for a wonderful lesson! FYI: tickType: {TickTypeEnum.tostr(tickType)} is now tickType: {TickTypeEnum.to_Str(tickType)}