Interactive Brokers Bridge

Interactive Brokers™ offers access to most major exchanges, a huge number of supported assets including options and futures, and low trading costs. The IB bridge gives Zorro algo trading strategies direct access the IB API, either through IB's Trader Workstation (TWS), or through the IB Gateway. The TWS is preferable for testing a script, the Gateway for automated trading. The TWS, a bloated java program, normally stops every 24 hours and interrupts the connection. The Gateway lasts a bit longer and usually disconnects every weekend. Thus, long-term algo trading with the IB API requires supervising the trading system and restarting the Gateway every Monday. When downloading TWS or Gateway from the IB website, get the release (stable) version, not the beta (unstable) version.

For trading with the TWS, open File/Global Configuration/API/Settings (see screenshot below) and select Enable ActiveX and Socket Clients. Make sure that the socket port is set to 7496. Deselect Read-Only API when you want to send orders. Make sure that Allow connections from localhost only is checked. Enter an individual number from 101..108 in Zorro's User field for connecting to the TWS via socket port 7496. The Password field can be left empty.

For trading with Gateway, select IB API (not FIX CTCI). Open Configuration/API/Settings and make sure that the socket port is set to 4002. Enter an individual number from 1..8 in Zorro's User field for connecting to the Gateway via socket port 4002. The Password field can be left empty.

Other socket port numbers than 7496 or 4002 can be set up in the Server field of the account list (see below). Up to 8 Zorros can connect to the same Gateway or to the same TWS, but each connection needs a separate IB.dll. Use DLL copies as described under Broker Arbitrage. Note that only a single TWS or a single Gateway per user is supported by IB. The connection will break down when a second Gateway or TWS instance with the same user is opened, even on a different PC.

Zorro login fields for IB:

User: 1 .. 8 for the Gateway, 101 .. 108 for the TWS
Password: Empty

Asset list example: AssetsIB.csv

Accounts.csv example entries:

Name Server Account User Pass Assets CCY Real NFA Plugin
IB-TWS-Demo   D4567890 101 0 AssetsIB USD 0 14 IB
IB-TWS-Real   U1234567 101 0 AssetsIB USD 1 14 IB
IB-Gateway 4002 U1234567 1 0 AssetsIB USD 1 14 IB
 

Trading with IB. Rebalancing portfolios

The IB plugin included with the free Zorro version supports paper trading accounts that begin with the letter "D". A plugin for real accounts is included in Zorro S. For opening an account with IB, the normal choice is a RegT Margin account. If you own Zorro S and trade high volumes, consider a Portfolio Margin account with its lower margin costs. At the time of this writing, IB paper trading accounts were only available after opening a real account.

Due to the high margin and lot size requirements, the Forex/CFD trading Z systems are not really suited for IB. They would require a large Capital setup for not skipping too many trades, and even then achieve less annual return due to the low leverage. The main advantage of IB is support of many exchanges, so that a large range of financial assets can be traded. This allows many new trade systems that exploit specific inefficiencies, f.i. volatility, or seasonal effects of particular stocks or treasuries.

Unadjusted historical price data for backtests can be downloaded from IB, with limits to the data resolution and maximum number of years. In second resolution only a few days of data are normally available. Unadjusted and adjusted EOD data is also available from many other Internet sources, f.i. from Yahoo, Stooq, or Quandl by using the Download script or the assetHistory or dataDownload functions. Assets symbols can be defined in a way that live data is loaded from IB and historical EOD data from another source. 

For rebalancing portfolios, the script can either adjust positions directly through enter/exit commands, or generate a CSV file containing the desired asset percentages. This file can then be manually imported in the TWS window Portfolio Tools / Rebalance Portfolio and will produce orders for adjusting the positions. The required CSV format changes from time to time, so export first an example file from the TWS and use it as template for the file generated by your script. An example file that rebalances a Z9 portfolio:

DES,XLU,STK,SMART/AMEX,,,,,,0.0
DES,SMH,STK,SMART/AMEX,,,,,,12.5
DES,XBI,STK,SMART/AMEX,,,,,,17.0
DES,XLI,STK,SMART/AMEX,,,,,,0.0
DES,XLV,STK,SMART/AMEX,,,,,,11.4
DES,VGK,STK,SMART/AMEX,,,,,,0.0
DES,VOO,STK,SMART/AMEX,,,,,,0.0
DES,UUP,STK,SMART/AMEX,,,,,,0.0
DES,VCSH,STK,SMART/AMEX,,,,,,0.0
DES,HYG,STK,SMART/AMEX,,,,,,0.0
DES,TLT,STK,SMART/AMEX,,,,,,20.0
DES,IAU,STK,SMART/AMEX,,,,,,19.0
DES,AGG,STK,SMART/AMEX,,,,,,0.0
DES,SPY,STK,SMART/AMEX,,,,,,0.0

For trading an asset, some prerequisites must be fulfilled:

Not all assets can be traded all the time, and not all price types are available for all assets. Index (IND) assets are read only, but you can trade indices with ETFs, Futures, or CFDs. Some assets, such as specific CFDs, are not available to US citizens, and require an UK or other account outside the US. European traders are not allowed, thanks to EU regulations, to trade US ETFs. The default minimum sizes for currency pairs are in the 20,000 contracts range, but entering a lower minimum size in the asset list is also possible. Orders are then marked as 'odd lot' and cause higher transaction costs. Some stocks (STK) and some other assets are not shortable, either permanently or dependent on market liquidity. Short trades for closing a long position are normally always accepted.

For setting up the IB asset list and for simulating IB trading in the backtest, margin requirements can be found on https://www.interactivebrokers.com/en/index.php?f=24176. The maintenance or end-of-day margin is often higher than the initial margin.

IB symbols

Any asset you want to trade must be represented by a line in the asset list. The asset symbols (Symbol column) are converted to IB contracts in the following way:

A example file AssetsIB.csv with currencies, CFDs, and stocks is included; we do however not guarantee the accuracy and correctness, so use it at your own risk.

Additional data and commands

The IB bridge supports the following additional data streams:

Multiple IB accounts are supported. The account can be set up in the account list.

The IB bridge supports the brokerCommand function with the following commands (extra commands can be implemented on user request):

Remarks

Typical IB API error messages

See also:

Brokers, Broker plugin, MT4 bridge

► latest version online