Getting Started with Interactive Brokers

I recently started a project to build an automated trading platform for managing my personal investments. If you're like me and have said to yourself, "Hey, I like math, programming, and gambling", then you've probably thought about doing something similar at some point in time. After listening to a talk about building an at-home trading platform from a former developer and data scientist at a local quantitative trading firm (RGM), I was inspired to try my hand at building my own trading platform.

I know, there are a bunch of trading platforms available for day traders and even for automating trading (e.g., Tradestation, Interactive Brokers Trader Workstation, Ninja Trader), but what would be the fun in using something out of the box when you can build it yourself :-) . At a high-level, I hope to accomplish three basic tasks in developing this platform using the IB C++ API:

  1. collect market data
  2. simulate/evaluate algorithms against historical data
  3. run an automated agent to execute trades using custom algorithms

I'll make future posts to document my progress and the problems I encounter in working towards these goals. I am developing on a Linux OS (i.e., Ubuntu), but from the little that I've read on the subject it seems equally easy to work with the API on Windows and MAC.

Interactive Brokers API and Trader Workstation

While there are a number of brokers that provide APIs for their services, I decide to go with Interactive Brokers. I didn't do an in-depth investigation of the various features offered by other brokers' APIs, but after a couple hours of googling I was fairly convinced that Interactive Brokers (IB) (i) provided a reasonable fee structure, (ii) had a large user community, and (iii) IB's API would be sufficiently robust for what I wanted to accomplish. Check out the section below for further details on Interactive Brokers.

Before getting started with the IB C++ API, the first step was to download and install the Trader Workstation (TWS), which is a java-based trading platform from Interactive Brokers. The IB API acts as an interface to communicate with Trader Workstation (i.e., allowing you to do pretty much anything you could normally do through TWS) . The IB API allows you to write an application that can talk to TWS; programming in Java, C++, VB, or DDE (Excel). There are even a few Python ports of the IB API: PyTWS, IbPy, and swigibpy. There are also a couple of other opensource projects related to the IB API:

  • twsapi - a portable C++ version of the IB API using the GNU toolchain (i.e., Autotools)
  • twstools - command line tools built around the IB API

The Trader Workstation has a fairly ornate layout with all of the bells and whistles a day-trader would probably ever need. After you download TWS, you can start it up following the directions in the README and log in using the demo username and password (edemo/demouser). The java-based TWS application looks like this:

Download and Install API

Once you've got TWS up and running, go to the IB API download page and click on the "API Software" button. After you've agreed to the EULA, download the IB API by selecting the appropriate option for your OS. Linux users will get a jar file with the C++ and Java API (twsapi_unixmac_968.jar), and can extract it by following these instructions. The code for the C++ API is in the IBJts/cpp directory. You'll find three folders there:

  1. PosixSocketClient/ - socket interface classes
  2. Shared/ - helper functions and classes
  3. PosixSocketClientTest/ - example test application

To compile the example test application PosixSocketClientTest do the following:

$ cd IBJts/cpp/PosixSocketClientTest
## Fix broken header PosixTestClient.h
$ cp PosixTestClient.h PosixTestClient.h.old
$ sed 's/#include "EWrapper.h"/#include "EWrapper.h"\n#include <cstdio>/' PosixTestClient.h > /tmp/NewPosixTestClient.h
$ mv /tmp/NewPosixTestClient.h PosixTestClient.h
## Build PosixSocketClientTest
$ cp Makefile.linux Makefile
$ make

This will edit the broken header file (PosixTestClient.h) and build the example test application (PosixSocketClientTest).

Launching the Example Application - "Helloworld"

Before you launch PosixSocketClientTest, you'll need to configure TWS to allow it to interact with the API. The example test application is a simple program that places a limit order to buy 1000 shares of Microsoft (MSFT) and then after a short timeout cancels the order. Before you launch the example application, make sure that you are logged into TWS. Once you launch PosixSocketClientTest, you should see the following dialog box pop up in the TWS window:

Click "Accept" to allow the program to connect with TWS. Under the API tab in TWS, you should see the order for MSFT shares.

After 10 seconds, PosixSocketClientTest will cancel the order, so the limit order should go away. The example application will remain running as it waits for more signals, but nothing else is supposed to happen. Use Ctrl-c to stop the application. To learn how the example application does what it does, I suggest that you start by inspecting Main.cpp and PosixTestClient.{h,cpp} in IBJts/cpp/PosixSocketClientTest/.

Until Next Time ...

Congrats! Now you can run a simple application that uses the IB API to connect to TWS and do a few basic things. This example application seems like a great jumping off point for building much more complex and intricate programs. I'm currently working my way through the C++ API and hope to have a firm grasp on it in the coming weeks. Next time, I'll give some highlights of the IB API and hopefully be able to report some progress towards my first goal of collecting market data.

A Few Useful Links

Addition Details About Interactive Brokers

As I see it, the sentiment about Interactive Brokers is by and large positive. There are definitely some pros and cons to be aware of though.

On the pros side, IB has some of the lowest trading fees around. For stocks, the basic fee structure for IB works out to one dollar per 200 shares (i.e., 0.005 USD per share with a $1.00 minimum). This per share pricing model might seem foreign to those of us that have traded at other brokerage houses (such as Etrade or Fidelity) where you pay a fixed cost for each trade (up to some maximum number of shares). My average lot sizes are on the order of 100 shares, so this means that I'll be paying about a dollar per trade, which is far superior to my current rates at traditional brokerage firms. Options prices are also really small (70 cents per contract).

Aside from trading fees, Interactive Brokers also charges fees for connectivity and market data. I've copied a little snippet on some of the additional fees that IB charges.

To open an account with Interactive Brokers there is a $10,000 minimum. If client does not spend at least $10 in commissions per month they will be charged the difference. Additionally, there is a fee of $10 charged for real-time quotes each month which is waived if at least $30 in commissions is spent. Lastly, Interactive Brokers currently does not pay interest on any positive (free) cash balances for US ($) clients.

Some of the cons include the high minimum account balance ($10,000), which may be too much for the casual investor or for students, and what essentially amounts to an inactivity fee ($10 per month for commissions plus $10 per month for real-time quotes). For anybody who makes 3-5 trades a week, however, these commission minimums shouldn't be too difficult to meet. Before starting any sort of trading (through IB or any other broker), make sure you understand all of the fees associated with your trades. You might find that your trading style does not really fit well with the fee structure being offered. Remember, if you don't like what you find, look elsewhere.