1 Introduction
Interactive Brokers (IB, www.interactivebrokers.com) provides brokerage and financial data-feed services. IB customers can use its services using specialized applications (so-called “clients”) that can be installed on the user’s computer. These client applications provide a user-interface that enables the user to view portfolio and market information, as well as to issue trade orders to the IB server. The most widely-used IB client application is TWS (Trader Work Station).1
In addition to TWS, IB provides other means of communicating with its server. A lightweight client application called IB Gateway is installed together with TWS. IB Gateway has no fancy GUI like TWS but provides exactly the same API functionality as TWS, while using fewer system resources and running more efficiently.2
IB also publishes an interface (Application Programming Interface, or API) that enables user applications to connect to the IB server using one of its client applications (either IB Gateway or TWS). This API enables user trading models to interact with IB: send trade orders, receive market and portfolio information, process execution and tick events etc.
IB provides its API for three target platforms: Windows, Mac and Linux (Mac and Linux actually use the same API installation).3 The API has several variants, including C++, Java, DDE, and ActiveX (DDE and ActiveX only on Windows).
Matlab is a programming development platform that is widely-used in the financial sector. Matlab enables users to quickly analyze data, display results in graphs or interactive user interfaces, and to develop automated, semi-automated and decision-support trading models.
Unfortunately, IB does not provide an official Matlab API connector. While IB’s Java connector can be used directly in Matlab, setting up the event callbacks and data conversions between Matlab and the connector is definitely not easy. You need to be familiar with both Matlab and Java, at least to some degree.
This is the role of IB-Matlab (http://UndocumentedMatlab.com/IB-Matlab). IB-Matlab uses IB’s Java API to connect Matlab to IB, providing a seamless interface within Matlab to IB’s Java API functionality. Users can activate IB’s API using simple Matlab commands, without any need to know Java.
Java’s cross-platform compatibility means that exactly the same IB-Matlab code runs on all platforms supported by IB and Matlab, namely Windows (both 32 and 64 bit), Mac and Linux/Unix.
IB-Matlab consists of two parts that provide different ways of interacting with IB:
A Java package (IBMatlab.jar) that connects to the TWS/Gateway and provides full access to IB’s Java API functionality. Matlab users can use a special connector object in Matlab to invoke the Java API functions directly from within Matlab.
A Matlab wrapper (IBMatlab.p) that provides the most often-used API functionality in an easy-to-use Matlab function. This wrapper is a pure Matlab implementation that provides access IB’s most important functionalities, without needing to know anything about Java or the underlying connector.
Active trading actions (buy, sell, short, close, modify, cancel, exercise, lapse) and query actions (market, streaming quotes, open orders, historical, account and portfolio data) can be initiated with simple one-line Matlab code that uses the Matlab wrapper (IBMatlab.p). Additional trading features (the full range of IB’s Java API) can be accessed using the connector object that is provided by IB-Matlab.
Users can easily attach Matlab code (callbacks) to IB events. This enables special operations (e.g., adding an entry in an Excel file, sending an email or SMS) whenever an order executes, or a specified price is reached, for example.
Professional reviews of IB-Matlab were published in 20114 and 20125 in the Automated Trader magazine and can be downloaded from IB-Matlab’s web page.6
This document explains how to install and use IB-Matlab. Depending on the date that you have installed IB-Matlab, your version may be missing some features discussed in this document. Whenever you renew your annual license you will receive the latest IB-Matlab version, including all the functionality detailed here.