Unable to connect to Interactive Brokers after installed 2018a in new Laptop
1 view (last 30 days)
Show older comments
Hi Sir/Mdm,
I just re-install TWS and Matlab in my new computer. And I discover that I was unable to connect the trading tool box to my interactive broker TWS.
I have ensure I follow the istruction to installed TWS trading software and the TWS API.
I have also ensure "Enable Active X and Socket Clients" is checked.
I still encountered this error:
>> ib = ibtws('',7496)
Error using actxserver (line 90)
Server creation failed. Invalid ProgID 'TWS.TwsCtrl.1'.
Attached is the setting for API in TWS
![2019-03-11 23_31_52-Clipboard.jpg](https://www.mathworks.com/matlabcentral/answers/uploaded_files/207885/2019-03-11%2023_31_52-Clipboard.jpeg)
Error in ibtws
3 Comments
Annie Leonhart
on 29 Nov 2019
Edited: Annie Leonhart
on 1 Dec 2019
Please note this is for API version 9.72. If you are using 9.76 version of the API, it should be working with trading toolbox version 3.5.1 and above no problem. The solution for API 9.72 requires modifying the ibtws.m file located at:
C:\Program Files\MATLAB\R20xx\toolbox\trading\trading\@ibtws\ibtws.m
You can edit with Notepad++ as admin or Matlab ran as admin.
replace
x.Handle = actxserver('TWS.TwsCtrl.1');
With
%Connect to Trader Workstation
try
% Version 9.73+
x.Handle = actxserver('TWS.TwsCtrl');
catch
% Version 9.71-
x.Handle = actxserver('TWS.TwsCtrl.1');
end
With that you'll be able to connect on any API release from IBKR.
If you want realtime data, you will need to modify the realtime.m file located at:
C:\Program Files\MATLAB\R20xx\toolbox\trading\trading\@ibtws\realtime.m
replace
c.Handle.reqMktDataEx(tickerID(i),s{i},fldList,0)
with
% IB market data request API call
c.DataRequest = true;
for i = 1:numContracts
try
c.Handle.reqMktDataEx(tickerID(i),s{i},fldList,0,c.Handle.createTagValueList)
catch
%Version pre 9.7 API call
c.Handle.reqMktDataEx(tickerID(i),s{i},fldList,0)
end
end
and in the same file replace
ibBuiltInRealtimeData.id = varargin{9}.id;
switch varargin{9}.tickType
case 1
% BID PRICE
ibBuiltInRealtimeData.BID_PRICE = varargin{9}.price;
case 2
% ASK PRICE
ibBuiltInRealtimeData.ASK_PRICE = varargin{9}.price;
case 4
% LAST PRICE
ibBuiltInRealtimeData.LAST_PRICE = varargin{9}.price;
end
with
ibBuiltInRealtimeData.id = varargin{7}.id;
switch varargin{7}.tickType
case 1
% BID PRICE
ibBuiltInRealtimeData.BID_PRICE = varargin{7}.price;
case 2
% ASK PRICE
ibBuiltInRealtimeData.ASK_PRICE = varargin{7}.price;
case 4
% LAST PRICE
ibBuiltInRealtimeData.LAST_PRICE = varargin{7}.price;
end
You will also need to fix the ibExampleRealtimeEventHandler.m located at:
C:\Program Files\MATLAB\R20xx\toolbox\trading\trading\@ibtws\ibExampleRealtimeEventHandler.m
replace
case 'tickPrice'
% Get table index
tInd = find(varargin{9}.id == tickerID);
switch varargin{9}.tickType
case 1
% BID PRICE
data{tInd,3} = varargin{9}.price;
case 2
% ASK PRICE
data{tInd,5} = varargin{9}.price;
case 4
% LAST PRICE
data{tInd,1} = varargin{9}.price;
end
set(t,'Data',data)
with
case 'tickPrice'
% Get table index
tInd = find(varargin{7}.id == tickerID);
switch varargin{7}.tickType
case 1
% BID PRICE
data{tInd,3} = varargin{7}.price;
case 2
% ASK PRICE
data{tInd,5} = varargin{7}.price;
case 4
% LAST PRICE
data{tInd,1} = varargin{7}.price;
end
set(t,'Data',data)
You are not the only one who is fed up with Yair trying to sell paying Matlab customers a 3rd party solution. If you have any other questions about IBTWS feel free to ask, I've fixed every issue I've found so far myself and will help you out for free.
I may even create a far more extensive ActiveX based solution that includes IB Financial Advisor functions free on Github. This is rediculous.
Image Analyst
on 1 Dec 2019
Patrick, if the solution is already in the MATLAB (and toolboxes) that you already paid for, then use them. I often recommend toolboxes (usually sold by the Mathworks, though occasionally by others such as Eigenvector University) that can do things more conveniently than if you had to write it from lower level MATLAB functions. Sure, I could write regionprops() and bwmorph() myself, but it will cost me more in time than to just buy it already written. If your solution is not in something you paid for, you can either pay for a toolbox, write it yourself, or hope that someone else will eventually see your post and write it for you and give it to you for free. Annie has apparently done the latter for you, but it's not always that a poster can expect a generous donor like Annie to come along with turnkey free code right when the code is urgently needed. She should post it as an official "Answer" rather than a comment so Jeff can "Accept" her answer if it solves the problem. This was her first post, so you lucked out, though it sounds like in the future she will continue to supply code and toolboxes to people for free.
Yair is one of the worlds top publishers and authors and contributor of free MATLAB code (if you've ever looked at his web site which has mountains of free code that you will find nowhere else). I know him personally. He is on the Mathworks Advisory board and often knows more about the code than the Mathworks staff. He spends more time on MATLAB than even the people who work at the Mathworks. No one is obligated to buy his books, toolboxes, courses, or consulting time but the community is fortunate to have someone like Yair who is usually the one people turn to for the toughest MATLAB problems.
Answers (2)
Yair Altman
on 12 Mar 2019
Sometime in recent years, presumably around TWS API 9.70, IB changed their COM server's ProgID from 'TWS.TwsCtrl.1' to 'TWS.TwsCtrl'. This breaks the Trading Toolbox code, which still assumes a ProgId of 'TWS.TwsCtrl.1'. Since the Trading Toolbox's code is encrypted, you cannot modify its expected ProgId. The best you could do is to try to uninstall your IB API and install an old version (possibly with the assistance of IB). Note that this might break other things for you - the old IB API version might not be fully supported by IB today, but it's worth trying. And of course you could report this to MathWorks and hope that a fix or workaround will be provided in a future release.
As an alternative, you could try to use my IB-Matlab connector instead (http://undocumentedmatlab.com/IB-Matlab). IB-Matlab is based on IB's Java API (not COM), so it runs well on all platforms that support Matlab (Windows, Mac and Linux), and does not exhibit the problems that are common with the COM-based interface, such as the one that you encountered. It is by far the highest-rated product on IB's Marketplace.
0 Comments
Alessio Cianini
on 14 Jan 2020
enable DDE clients..that's all
1 Comment
Annie Leonhart
on 15 Jan 2020
Incorrect.
Server creation failed. Invalid ProgID 'TWS.TwsCtrl.1'. means he has a non-updated trading toolbox, or doesn't have the latest IB API.
DDE is excel...
See Also
Categories
Find more on Transaction Cost Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!