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.
- Matlab 2013b or later (including Trading Toolbox 2.0 or later)
- Interactive Brokers TWS Standalone 954 or later
- Interactive Brokers TWS API Install 970.06
- Java Runtime Environment
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:
- Enable ActiveX and Socket Clients
- Include FX positions when sending portfolio
- 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
In “API – Precautions” check all the boxes.
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:
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:
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”.