How to link Matlab to Interactive Brokers TWS

Interactive Brokers TWS is becoming more and more popular thanks to its unique, fast and inexpensive technology. Because of this, there is an increasing interest to combine the classic Interactive Brokers TWS Trading Tool with other software that would provide more flexibility and advantages in terms of personalisation, automatisation, strategy building and so on. Today, I decided to share with you a correct and working procedure of linking Matlab to IB TWS.

Requirements:

Step 1: Download and Install Matlab 2013b or later (including Trading Toolbox 2.0 or later)

Step 2: Download and Install Java Runtime Environment (required to run IB TWS)

Step 3: Download and Install Interactive Brokers TWS Standalone 954 or later

Step 4: Download and Install Interactive Brokers TWS API Install 970.06

Step 5: Configure IB TWS

  • From TWS Classic go on: Edit – Global Configuration – API
  • From TWS Mosaic go on: File – Global Configuration – API

In “API – Settings” check:

  1. Enable ActiveX and Socket Clients
  2. Include FX positions when sending portfolio
  3. Create API message log file

Make sure the “Socket Port” is set to 7496

Set “Logging level” to “Detail”

Uncheck “Allow connections from local host only”, hit on “Create” and “Enter trusted API address” 127.0.0.1

Configure TWS API Settings

In “API – Precautions” check all the boxes.

Configure TWS API Precautions

Once all this is set, re-launch both, Matlab and TWS, and simply type in the Matlab Command Window the following command:

ib = ibtws('',7496)

The return should be:

Matlab IBTWS

Meaning that Matlab is successfully connected to your TWS local session.

The connection can be closed by using the IB Trader Workstation connection object ib:

close(ib)

Note: Some of you, when trying to connect, may encounter the following error:

>> ib = ibtws('',7496); Error using feval Server Creation Failed: The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more details.

Error in actxserver (line 86) h=feval(['COM.' convertedProgID], 'server', machinename, interface);

Error in ibtws (line 38)

This can be solved by installing the 32 bit Version of “Microsoft Visual C++ 2005 Redistributable”.

4 thoughts on “How to link Matlab to Interactive Brokers TWS

  1. The reason IB-matlab exist was because there was no trading toolbox, or API for matlab, but now that problem is solved! No need for IB-matlab at least to get the data.

  2. Mine is 64bit windows. I installed vis C++ 2005, and still get the error:

    ib = ibtws(”,7496);

    Error in actxserver (line 86)
    h=feval([‘COM.’ convertedProgID], ‘server’, machinename, interface);

    Error in ibtws

    Do I need to do something else which I am missing here?

  3. As an alternative to the Matlab Trading Toolbox, consider using the IB-Matlab connector, which connects IB and Matlab:

    IB-Matlab works on all Matlab releases since 2007, on all platforms that support Matlab (including Linux and Mac), is extensively documented and super-robust, is in active use by many hundreds of traders and hedge funds worldwide, is one of the highest-rated products on IB’s Marketplace listing, is actively maintained with new functionality on an ongoing basis, and is much less expensive than a commercial license of Trading Toolbox. Multiple users who tested both products claimed that IB-Matlab is superior. You can find a detailed comparison between the products here: http://UndocumentedMatlab.com/ib-matlab/product-comparison

    Additional information: http://UndocumentedMatlab.com/ib-matlab

Leave a Reply