The aforementioned error indicates that the Trading Toolbox in MATLAB cannot establish a connection to the IB Trader Workstation ActiveX-based API. This can result when:
- the ActiveX APIs is not enabled within the installation of the IB Trader Workstation,
- the input port number does not match the one used by TWS, or
- if you are running an incompatible version of MATLAB or Trader Workstation.
As of MATLAB R2014a, both 32-Bit and 64-Bit MATLAB work with the IB Trader Workstation. In addition, please note that the Interactive Brokers functionality in Trading Toolbox requires a licensed account for IB Trader Workstation.
First, please ensure that you have the latest version of TWS and the IB Trader Workstation API (both linked below) and that you have a licensed account for IB Trader Workstation:
TWS
Interactive Brokers API
Then, we can verify the TWS API configurations as follows:
- In the API settings (within Trader Workstation Configuration) please make sure that "Enable ActiveX and Socket Clients" is checked, and "Read-Ony API" is unchecked. You can access the API settings by navigating to 'File > Global Configuration > API > Settings' on the main window of TWS.
- In addition, please check that the Socket port number in the "ibtws" command is identical to one in the API settings.
If this does not resolve the issue, please verify whether you can use the COM interface outside of MATLAB (i.e. Visual Basic script):
- Open a new EXCEL file
- Enter 'ALT + F11' to open Visual Basic (VB) Editor
- Locate the "Project - VBAProject" block the top left corner in the popped out window
- Right click on your workbook name (e.g. Sheet1)
- Select 'Insert > Module'
Paste the VB script below to the module:
Sub IBconnect()
Dim ibtws As Object
Set ibtws = CreateObject("TWS.TwsCtrl")
MsgBox ("success")
End Sub
- Enter 'Crtl + S' to save the VB script. Select "Excel Macro-Enabled Workbook" from the "Save as type" drop-down list and click the "Save" button.
- Run the VB script by clicking the green arrow button
If the output from running the script is not "success" then likely there is something broken with the IB Trader API installation. It would be best to contact the IB support since the VB script is checking connection outside of MATLAB and the resulting error is not caused due to MATLAB.
Additionally, I would suggest to first (before reaching out to IB) try the suggestions in the MATLAB Answers post linked below as other users came across the same issue and have gotten next steps from IB:
The BaT file provided by IB helps establish connection with Excel using DDE connection and not ActiveX API connection which is used by MATLAB.
In order to instantiate the connection between IB and MATLAB, the system needs to recognize the ActiveX API program ID 'TWS.TwsCtrl'.
If the VB script won't run it's likely because it can't find the Trader Workstation program ID. This indicates that there is something going wrong with the IB installation files. If this is the case, I would recommend contacting the IB Support for help troubleshooting the IB installation files.