Investing in financial products involves risk to your capital.

Close Navigation
Learn more about IBKR accounts
Understanding Asynchronous Libraries with the TWS API

Understanding Asynchronous Libraries with the TWS API

Posted November 13, 2023 at 4:01 pm
Andrew Wise
Interactive Brokers

Working with many new users of the TWS API, we often see individuals attempt to set something like ‘x=reqMarketData()’ with the expectation of receiving data by calling the variable throughout their code. However, this is not the case, as the TWS API is built on an Asynchronous Framework to help reduce lag time and display the most up to date information possible. In such a fluid world like the trading industry, attempting to lock data into a sequential series is an uphill battle.

To understand the choice to use this system, it’s worth exploring the idea of synchronous and asynchronous. Synchronous systems imply that every action will have a reciprocated, sequential action. This is a standard system most users are familiar with in programming as asking for a variable typically results in that same variable being given back. Synchronous programming can be thought of like a professor instructing students in a classroom. There is a single point of conversation which will go back and forth in real time.

Conversely, asynchronous systems imply that after a call is made, some number of responses will be reciprocated. This can be anywhere from no responses to hundreds of responses. This is not the standard system most programmers are familiar with as most fields of computing do not require the user to receive more than a single value. However, in the aspect of market data where bid, ask, last, Greeks, and a plethora of other values need to be calculated, received, and updated as soon as possible, a synchronous system does not facilitate this data display. Back to our teaching example, one could consider this like homework assignments: some students will complete it on time alone, some in groups or with tutors, and some will not complete the assignment at all.

This asynchronicity has added value as well even beyond the formal standards of the market. Because traders will likely want market data for multiple instruments, all of the market data can have a single consolidated point for trade prices to return to. In the case of the TWS API, this would fall to tickPrice. This is far less resource intensive than having hundreds of values like x.tickPrice.Bid to be concurrently maintained by the machine. Instead, users can simply track a request identifier, and see that all resources displaying this value are linked back to our original market data request.

As such, developers can employ Interactive Brokers’ TWS API library to request market data for a range of instruments while also reviewing account data, sending out and tracking orders, and maintain their calculations in the background at the same time.

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

Throughout the lesson, please keep in mind that the examples discussed are purely for technical demonstration purposes, and do not constitute investment advice, an investment recommendation or investment research. Also, it is important to remember that placing trades in a paper account is recommended before any live trading.

Disclosure: Order Types / TWS

The order types available through Interactive Brokers Trader Workstation are designed to help you limit your loss and/or lock in a profit. Market conditions and other factors may affect execution. In general, orders guarantee a fill or guarantee a price, but not both. In extreme market conditions, an order may either be executed at a different price than anticipated or may not be filled in the marketplace.

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.