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

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

I would like to see a solution from Mathworks on this. I experienced the exact same set of challenges with Matlab 2018b and 2019b, using the latest IB API (9.76) and the latest "stable" IB API (9.72). When using the older IB API (9.71.06) it does connect, but then gives a similar error message:
>> bizDayConvention = 13;
currentdate = today;
startDate = daysadd(currentdate,-20,bizDayConvention);
endDate = daysadd(currentdate,-1,bizDayConvention);
>> histTradeData = history(ib,ibContract,startDate,endDate);
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)})
Was this problem ever solved? I am experiencing the same thing. It appears that I have a Catch-22. I can connect with the older API and not the newer version, but the older one seems to be lacking certain functionality.
I just checked the latest Interactive Brokers APIs again using both:
TWS API Stable for Windows Version: API 9.76 Release Date: May 08 2019
TWS API Stable for Windows Version:API 9.79 Release Date: Feb 05 2020
Neither of these will connect to Matlab R2020a, same problem as last year. The older IB API V9.71.06 will connect to Matlab 2020a, but gives the same error registering events described originally. I am using this script, per the Mathworks' "Getting Started" tutorial at https://www.mathworks.com/videos/get-started-with-trading-toolbox-connect-to-interactive-brokers-1-of-3-91839.html
ib = ibtws('',7496);
ibContract = ib.Handle.createContract;
ibContract.symbol = 'MSFT';
ibContract.secType = 'STK';
ibContract.exchange = 'SMART';
ibContract.currency = 'USD';
bizDayConvention = 13;
currentdate = today;
startDate = daysadd(currentdate, -20, bizDayConvention);
endDate = daysadd(currentdate, -1, bizDayConvention);
histTradeData = history(ib, ibContract, startDate, endDate);
When will Matlab update their system to work with the newest Interactive Brokers APIs? It is hard to debug when things go wrong, when you know you're using an obsolete API.
MATLAB works with the latest 9.76 api. Trust me. It does. I’m a programmer, MATLAB’s code will work on 9.7x and above. How do I know this? It’s clearly written in the source file.
Also, I view the even callbacks raw as I program my own event viewers. MATLABs code is written correctly to handle the latest API.
If it’s. It working for you, then your setup is wrong.
Maybe if you actually post your error we can help you.
But I will tell you with 100% certainty, MARLAB works with the latest IBKR api. I’m running 9.76 btw.
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.
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)

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

Thanks for your reply. I have prior experience on Trading Toolbox + IB around 1 year ago, so I am quite sure the ports/socks...etc I am using now is ok and can connect perfectly. What's wrong should be more complex issue caused by IB keep on updating their software but Matlab seems not quite aware of that. By looking at the latest release note of Matlab trading toolbox one can know that they have done very little recently to update their package that sold to me. Sad.
If you take a look at IB-Matlab's changelog ( https://undocumentedmatlab.com/files/IB-Matlab_User_Guide.pdf#changelog ) you will see that it is continuously updated/improved. Give it a try...
Thanks! Will give a try. Just a question. Do I need to uninstall Trading Toolbox before using IB-Matlab connector?
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.

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

It's interesting that you apparently got it to work. However, the latest stable API from IB is 9.76, not 9.72. Matlab still hasn't updated their system to work with the latest. When I use the latest stable IB API, it doesn't even make the connection between Matlab and IB.
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.
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.

Asked:

on 23 Oct 2018

Edited:

on 27 May 2021

Community Treasure Hunt

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

Start Hunting!