4 Things to keep in mind before you use IB TWS API to automate your trading strategy

Blog Single

Introduction

Do you have trading strategy that has an edge in the market and are you planning to automate your strategy using Interactive Brokers TWS API? If yes, then this article will help you decide if IB TWS API is right for you.

Interactive Brokers TWS API

Interactive Brokers (IB) is one of most popular brokers among institutional traders, professional traders and serious retail traders. IB was one of the first few brokers to provide retail traders an option to automate their trading processes using their TWS API. IB TWS API allows the traders/programmers the ability to automate functionality within TWS using external scripts/program. For example:

  • Order placement, modification & cancellation
  • Checking status of open orders and positions
  • Receiving historical and live market data feed
  • Receiving portfolio data and account values
  • Querying financial instrument details

Now, let discuss the various things you need to consider before you decide to you use IB TWS API for automating your strategy.

1. Time frame of the trading strategy

Streaming market feed provided by IB API is not true tick data but rather a snapshot data taken at intra-second intervals which differs depending on the type of instrument:

  • Forex pairs: 5 milliseconds
  • US options: 100 milliseconds
  • Stocks, futures and others: 250 milliseconds

Also, order updates on IB API happen on the order of 10s to 100s of milliseconds. As a result, IB API is suitable for automating low and medium frequency strategies that are not impacted by latency of a few hundred milliseconds. IB API is not at all suitable for doing HFT since it requires sending large number of orders into the market at high speed, with round trip execution times measured in microseconds. Latency of even a few milliseconds will significantly degrade the performance of a HFT strategy. Please refer to this link for more details.

2. Historical data request limitations

Most trading strategies need to make use of historical price data of securities for computing indicators and/or variables that help in generating buy and sell signals. If your strategy also falls in this category then you should know that IB places restrictions on the number of historical data requests you can make over a time interval. If you make more historical requests than the allowed limit, then IB can disconnect your API client for some time. This is because IB doesn't consider itself as a specialized market data provider and therefore it places restrictions to limit traffic which is not directly associated with trading. IB also advises it's clients to subscribe to market data from other data providers. IB employs following pacing violations for small bars (30 sec or less):

  • Making identical historical data requests within 15 seconds.
  • Making six or more historical data requests for the same Contract, Exchange and Tick Type within two seconds.
  • Making more than 60 requests within any ten minute period.

Recently IB has lifted historical data limitations for 1 minute bars and higher. Though IB has lifted the 'hard' limit, it still implements a 'soft' control on the historical requests. Please refer to this link for details.

3. Market data lines limitation

IB defines a market data line as a stream of live market data (also some times called at tick data) for an instrument. If a user requests real time streaming market data for 10 instruments either from TWS or it's API, it would mean that 10 market data lines are being used. These market data lines are computed as sum total of market data lines for TWS and TWS API. So suppose you request live stream market data for the same 5 instruments via TWS and also via their API, then it would be counted as 5 market data lines and not 10. By default, every user gets a total of 100 market data lines. However, if you need to access real time data for more than 100 instruments simultaneously, then you would need to purchase booster packs or by increasing the equity and or commission of the user's account. Please refer to this link for more details.

4. IB TWS API Architecture

TWS API doesn't provide simple RESTful API but rather has a bit complex asynchronous architecture which requires API users to run either the TWS application or IB Gateway application on local machine to access API. This architecture has both it's advantages and disadvantages.

Advantage of asynchronous architecture is that it helps to reduce delays in request/response operations and makes your application fast as you don't need to wait for API response before making another request to API. However, the disadvantage is that your program can become a bit complex, since your application needs to handle request and response from API separately.

You can also explore 3rd party python API like IB_INSYNC which try to reduce this complexity of native IB API. Please refer to this link for more information.

Share this Post: