MATLAB Answers

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

9 views (last 30 days)
Robin Szeto
Robin Szeto on 23 Oct 2018
Commented: Annie Leonhart on 14 Mar 2020
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 = '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)
Error in IBGateway_0001 (line 15)
histTradeData = history(ib,ibFutures,startDate,endDate);


Show 3 older comments
Annie Leonhart
Annie Leonhart on 9 Mar 2020
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.
Ryan Hendry
Ryan Hendry on 10 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
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.
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 ( 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:


Show 1 older comment
Yair Altman
Yair Altman on 24 Oct 2018
If you take a look at IB-Matlab's changelog ( ) you will see that it is continuously updated/improved. Give it a try...
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; = '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.


Steven Nilsen
Steven Nilsen on 9 Mar 2020
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.
Annie Leonhart
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by