Problem getting data from ibtws

Hi guys,
I was testing out the trading toolbox with interactive brokers. I established the connection and tried to run the following code:
ib = ibtws('',7496)
ibContract = ib.Handle.createContract;
ibContract.symbol = 'AAPL';
ibContract.secType = 'STK';
ibContract.exchange = 'SMART';
ibContract.primaryExchange = 'NASDAQ';
ibContract.currency = 'USD';
startdate = floor(now) - 5;
enddate = floor(now);
d = history(ib,ibContract,startdate,enddate)
It returned:
ib =
ibtws with properties:
ClientId: 0
Handle: [1×1 COM.TWS_TwsCtrl]
Host: ''
Port: 7496
d =
'Unable to read beyond the end of the stream.'
Does anyone know why?

4 comentarios

MN
MN el 19 de Mzo. de 2019
Same problem here!
Bartlomiej Winter
Bartlomiej Winter el 30 de Jun. de 2019
Same problem, hope we get an answer!
Rosanna Campagna
Rosanna Campagna el 28 de Jul. de 2019
Same problem, could you help me?
Oliver Rubicon
Oliver Rubicon el 28 de Dic. de 2019
Same problem, can someone help?

Iniciar sesión para comentar.

Respuestas (2)

Annie Leonhart
Annie Leonhart el 1 de Dic. de 2019
Please set the primaryExchange to 'SMART', they cannot be different from my testing. In some cases you need to run the command twice, becasue Matlab isn't accounting for the time needed for the API to connect to the historical data feed.
I am using Matlab 2019a: Trading Toolbox 3.5.1 & TWS Api 9.76
ib = ibtws('',4001, 0)
ibContract = ib.Handle.createContract;
ibContract.symbol = 'AAPL';
ibContract.secType = 'STK';
ibContract.exchange = 'SMART';
ibContract.primaryExchange = 'SMART';
ibContract.currency = 'USD';
startdate = floor(now) - 5;
enddate = floor(now);
d = history(ib,ibContract,startdate,enddate)
Screenshot_4.jpg

9 comentarios

Oliver Rubicon
Oliver Rubicon el 28 de Dic. de 2019
No it does not work. Still got the same error: 'Unable to read beyond the end of the stream.'
Annie Leonhart
Annie Leonhart el 28 de Dic. de 2019
Editada: Annie Leonhart el 28 de Dic. de 2019
It works 100%.
proof it runs.... ran just now. https://i.imgur.com/mky2wbu.jpg
You also need to be subscribed to realtime data on IB. If you are not, it will NOT work.
You need these subscriptions on IBKR:
CBOE Realtime: $1/m
US Equity and Options Add-On Streaming Bundle: $4.50/m
US Securities Snapshot and Futures Value Bundle: $10/m
if you are missing any of those subscriptions, it will not work. IB does not allow you to pull historical data without a current realtime subscription. You cannot pull historical data using the snapshot bundle alone as it does not allow streaming.
John Quattrone
John Quattrone el 31 de En. de 2020
Matlab 2019b: & TWS Api 9.76
I was having the same issue. I tried your example Annie and I got the same error as the others. I have all the required subscriptions. I had to use ib = ibtws('127.0.0.1',7496, 0) to get data
John Quattrone
John Quattrone el 31 de En. de 2020
Now it does not work so ignore my message
John Quattrone
John Quattrone el 31 de En. de 2020
What I have found is I have to restart MATLAB every time I want to run the script above. After the restart I can pull data. If I run it a second time I get
d =
'Unable to read beyond the end of the stream.'
Annie Leonhart
Annie Leonhart el 8 de Feb. de 2020
Editada: Annie Leonhart el 8 de Feb. de 2020
It’s because you need to close the connection at the end of the script.
Close(ib)
If you want to connect a second time without closing the connection, you must change the clientID
Sandro Hantke
Sandro Hantke el 21 de Mzo. de 2020
Hi Annie,
i still have the same problem. The connection to the TWS is established and verified. I also subscribed to market data from the German spot market (Spot Market Germany / Frankfurt). Maybe you can help me again?
Output: 'Unable to read beyond the end of the stream.'
ib = ibtws('',7496)
ibContract = ib.Handle.createContract;
ibContract.symbol = 'WDI';
ibContract.secType = 'STK';
ibContract.exchange = 'SMART';
ibContract.primaryExchange = 'IBIS';
ibContract.currency = 'EUR';
startdate = floor(now) - 5;
enddate = floor(now);
d = history(ib,ibContract,startdate,enddate)
close(ib)
Sandro Hantke
Sandro Hantke el 21 de Mzo. de 2020
Annie,
I just solved the problem! IBContract.exchange=IBIS
Annie Leonhart
Annie Leonhart el 21 de Mzo. de 2020
That’s right. SMART only works for stocks. Futures, bonds, etc you must specify the exact exchange.

Iniciar sesión para comentar.

Categorías

Preguntada:

el 21 de Dic. de 2018

Comentada:

el 21 de Mzo. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by