Trading Toolbox (V3.5) NOT working with latest IB API

1 view (last 30 days)
System setting: Win10, Matlab 2018b, Trading Toolbox 3.5, IB Gateway 973.21 (newest)
1) Latest stable IB API now is V9.72, this version cannot successfully connect IB and Matlab by the following code: ib = ibtws('',7496);
2) Tried IB API V9.71.06 (an older version) instead, the above code can connect IB and Matlab, BUT..... other problem arise. Error message will pops up after execution of the following codes:
ibFutures = ib.Handle.createContract;
ibFutures.symbol = 'ES';
ibFutures.secType = 'FUT';
ibFutures.expiry = '201812'; % Dec 2018
ibFutures.exchange = 'SMART';
ibFutures.currency = 'USD';
bizDayConvention = 13; % i.e. BUS/252
currentdate = today;
startDate = daysadd(currentdate,-20,bizDayConvention);
endDate = daysadd(currentdate,-1,bizDayConvention);
histTradeData = history(ib,ibFutures,startDate,endDate);
Error message as below:
Error using registerevent (line 72)
Error registering events. 'historicalDataEnd' is not a valid event name.
Error in ibtws/history (line 54)
c.Handle.registerevent({eventNames{i},@(varargin)ibBuiltInHistoryEventHandler(varargin{:},c,tickerID)})
Error in IBGateway_0001 (line 15)
histTradeData = history(ib,ibFutures,startDate,endDate);
  6 Comments
Ryan Hendry
Ryan Hendry on 10 Mar 2020
Edited: Ryan Hendry on 11 Mar 2020
Hey friend, I appreicate your post here. I am relatively new to using matlab for these purposes. I have the following:
(TWS has API settings enables, socket etc.)
%% IB TWS Connection
clc
clear
% PORT 7497 IS FOR PAPER-BACKTEST, 7496 IS FOR LIVE
ib = ibtws('', 7497);
The error I recieve is:
Error in actxserver (line 89)
h=feval(['COM.' convertedProgID], 'server', machinename, interface);
Error in ibtws (line 37)
x.Handle = actxserver('TWS.TwsCtrl.1');
Error in test_book (line 9)
ib = ibtws('', 7497);
Not sure where I am going wrong, i've been trying to follow online solutions but they don't seem to work. If you have any suggestions let me know. I am running Trading Toolbox 3.6 and Matlab R2019b.
Annie Leonhart
Annie Leonhart on 14 Mar 2020
You’re not using the latest api. Uninstall any api you have, delete the folder at C:/TWS API.
Reboot
Then install 9.76+
This is an API problem.
It’s looking for the API jar file for 9.71 and below named TWS.TwsCTRL.1 when it’s named TWS.TwsCtrl in the new APIS

Sign in to comment.

Answers (2)

Yair Altman
Yair Altman on 24 Oct 2018
Check whether you have correctly set the relevant settings on your TWS/Gateway's API configuration panel, specifically "Enable ActiveX clients", socket port 7496 (not the standard 4001/4002), and localhost (127.0.0.1) added as a trusted IP. Without these settings, no Matlab client can connect to IB. The port number that you specify in your ibtws() command has to match the port number that you specified in the TWS/Gateway's API configuration.
Also consider trying the Java-based IB-Matlab connector: https://undocumentedmatlab.com/IB-Matlab
  4 Comments
Robin Szeto
Robin Szeto on 25 Oct 2018
Thanks! Will give a try. Just a question. Do I need to uninstall Trading Toolbox before using IB-Matlab connector?
Yair Altman
Yair Altman on 25 Oct 2018
There is no need to uninstall anything, they work independently of each other (although you probably won't be able to connect both of them at the same time to IB).

Sign in to comment.


Annie Leonhart
Annie Leonhart on 15 Jan 2020
The guy's contract is not valid.
I'm not subscribed to GLOBEX so I cannot request ES. Also, you cannot use SMART as an exchange for Futures, Futures Options, or Commodities, Bonds, etc... SMART is only for stocks and options.
Here is a proper contract for a futures security. Also, you must set the contract "include Expired" because it's 2019 and you're requesting an expired contract.
ibFutures = ib.Handle.createContract;
ibFutures.symbol = 'CL';
ibFutures.secType = 'FUT';
ibFutures.lastTradeDateOrContractMonth = '201812'; % Dec 2018
ibFutures.includeExpired = true;
ibFutures.exchange = 'NYMEX';
ibFutures.primaryExchange = 'NYMEX';
ibFutures.currency = 'USD';
Now that you have a proper contract, you need to setup the proper request days, you're requesting 20 days of history from today, but again, it's 2019. Either set the current day to Dec 2018 or do as I did -500 days.
currentdate = today;
startDate = daysadd(currentdate,-500,bizDayConvention);
endDate = daysadd(currentdate,-1,bizDayConvention);
Finally run the code
histTradeData = history(ib,ibFutures,startDate,endDate)
This is using the latest stable API 9.72 from IB on R2019a.
  3 Comments
Annie Leonhart
Annie Leonhart on 9 Mar 2020
Edited: Annie Leonhart on 9 Mar 2020
Wrong. It works on the latest api.
You have it setup wrong. The problem is on your end, not matlabs.
If you can’t get a connection at all your port is wrong.
Please post whatever error you’re getting. I can make it work on any API. I’m currently running it on 9.76. Also note at the time of posting, 9.72 was the latest. It’ll work on 9.79 as well.
Gilles Desvilles
Gilles Desvilles on 25 May 2021
Edited: Gilles Desvilles on 27 May 2021
Dear Annie
You give a new hope to matlab users, in 2019 I had asked tens of times Mathworks to correct their bug, maybe they have done it in 2020 ...
I have been forced to use matlab 32 bits 2015a and api 971.06 and it worked as a temporary solution
Can you tell which version of matlab you use, which port (7496 ?), which operating system (linux, windows etc) ?
Thank you

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!