MATLAB Answers

Error with " Request Interactive Brokers Historical Data" example

42 views (last 30 days)
Mike A
Mike A on 27 Jul 2018
Commented: terry dylan on 13 Jan 2019
I am receiving an error when I follow the help example Request Interactive Brokers Historical Data.
ib = ibtws('',7496) %
ibContract = ib.Handle.createContract;
ibContract.symbol = 'IBM';
ibContract.secType = 'STK';
ibContract.exchange = 'SMART';
ibContract.primaryExchange = 'NYSE';
ibContract.currency = 'USD';
getdata(ib,ibContract)
startdate = floor(now) - 5;
enddate = floor(now);
d = history(ib,ibContract,startdate,enddate)
the output:
ans =
struct with fields:
LAST_PRICE: 145.19
LAST_SIZE: 1.00
VOLUME: 23572.00
BID_PRICE: 145.19
BID_SIZE: 5.00
ASK_PRICE: 145.20
ASK_SIZE: 3.00
d =
'No historical data query found for ticker id:975'
However the API error logs:
11:25:22:998 <- 20-975-0-IBM-STK--0---SMART-NYSE-USD---0-20180728 00:00:00-1 day-6 D-0-TRADES-1-0--
11:25:23:143 -> --17-975-20180722 00:00:00-20180728 00:00:00-6-20180720-149.11-149.50-146.10-146.25-56435-147.146-31676-20180723-146.26-146.70-145.01-145.85-31681-145.653-17672-20180724-145.73-147.04-145.73-146.05-33764-146.483-18819-20180725-145.80-146.91-145.50-145.80-30822-146.152-19893-20180726-146.74-149.27-146.34-147.48-42520-147.469-23727-20180727-146.93-147.36-144.66-145.19-19673-145.926-13840-
11:25:23:147 <- 25-1-975-
11:25:23:148 -> ---=4-2-975-366-No historical data query found for ticker id:975-
11:25:23:148 <- 25-1-975-
11:25:23:148 -> ---=4-2-975-366-No historical data query found for ticker id:975-
using 2018a with API 9.73.07
It doesn't matter the dates(1 day 10 days ago) or securities I try is still get the same error. Also Timeseries does not work a well, the API logs shows the data but the no historical data error.

  0 Comments

Sign in to comment.

Answers (3)

John Pries
John Pries on 25 Oct 2018
Anyone solve this issue. I've got it too. It seems like 'historicalDataEnd' event is happening before 'historicalData' and closing event listeners and clearing persistent variables ...

Scott Meier
Scott Meier on 9 Nov 2018
I believe the second line of code shown below is missing from the timeseries.m file. I'm using 2018b. I copied this line from the 2017a version.
case 'historicalDataEnd'
assignin('base','ibBuiltInIntraDayData',ibBuiltInIntraDayData);
% Clear persistent variables and event listeners
clear ibBuiltInIntraDayDataCounter ibBuiltInIntraDayData

  5 Comments

Show 2 older comments
terry dylan
terry dylan on 10 Jan 2019
Hi Scott,
i added a simillar line to the history.m file, it works now (maybe it works without this line as well) .
case 'historicalDataEnd'
assignin('base','ibBuiltInHistoricalData',ibBuiltInHistoricalData);
%Historical data end event
% Clear persistent variables and event listeners
clear ibBuiltInHistoricalDataCounter ibBuiltInHistoricalData
Sadly i noticed a new problem with IB connection, sometimes when i request data (using either getdata history or timeseries ), the code stuck in the while loop:
while c.DataRequest
drawnow
end
i suspect it happend due to using " ib = ibtws('',7496)" , i may have connect + disconnect from TWS too often ?? the only solution to make data request work again, is to quit Matlab, and re-launch Matlab
have you experienced the same problem before ?
Thanks
Terry
Scott Meier
Scott Meier on 11 Jan 2019
Yes, I experienced this often in the beginning, possibly from making too many history requests in a short time. If you use the IB Gateway instead of TWS, you can view the API messages and this can give a clue as to what's happening. It may be possible in TWS, but I'm not sure how to do that.
terry dylan
terry dylan on 13 Jan 2019
Hi Scott,
you can view the TWS log book, in TWS, go to Classic TWS view, then account->Diagnostics -> view TWS log book,
then you can export today's log book, in the file, there are details of messages, although i cant' understand very much.
the problem i have now, is still data request freeze, when i reconnect and reconnect from TWS, this doesnt happend with API 971, later version of API, stuck all the time................... this is such a big headache to me
best regards
Terry

Sign in to comment.


Yair Altman
Yair Altman on 4 Jan 2019
Edited: Yair Altman on 4 Jan 2019
Here's how you can fetch IB's historical data using the IB-Matlab connector:
>> data = IBMatlab('action','history', 'symbol','IBM', 'DurationValue',5, 'BarSize','1 hour')
data =
struct with fields:
dateNum: [1×78 double]
dateTime: {1×78 cell}
open: [1×78 double]
high: [1×78 double]
low: [1×78 double]
close: [1×78 double]
volume: [1×78 double]
count: [1×78 double]
WAP: [1×78 double]
hasGaps: [1×78 logical]
>> data.dateTime'
ans =
78×1 cell array
{'20181227 11:00:00'}
{'20181227 12:00:00'}
{'20181227 13:00:00'}
{'20181227 14:00:00'}
{'20181227 15:00:00'}
{'20181227 16:00:00'}
...
For additional details, see https://undocumentedmatlab.com/ib-matlab

  1 Comment

Mike A
Mike A on 10 Jan 2019
It is sad that you pay for a toolbox with the expectations that it will work and the only real solution that matlab has is to spen $300 a year with a third party vendor.

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