softwarespartan / IB4m

Interactive Brokers API for Matlab
GNU General Public License v2.0
62 stars 21 forks source link

error getting market index historical data #5

Closed NewbieAlex closed 7 years ago

NewbieAlex commented 7 years ago

First of all, great job, thanks! I was able to request historical stock data, but no success in market index. It doesn't seem to return any data. For example, I use the following code for SP500 index data, but "buf.size()" is 0, and later there will be a empty buffer error in "hde = buf.get();"


contract = com.tws.ContractFactory.GenericContract('SPX'); contract.m_secType = 'IND'; contract.m_primaryExch = 'CBOE'; contract.m_currency = 'USD';

% initialize session with TWS session = TWS.Session.getInstance();

% create local buffer for historical data events [buf,lh] = TWS.initBufferForEvent(TWS.Events.HISTORICALDATA);

% connect to TWS session.eClientSocket.eConnect('127.0.0.1',7496,0);

% request historical data for our contract for a year w/ 1-day bars session.eClientSocket.reqHistoricalData(1000001,contract,'20170505 16:00:00','20 D','1 day','TRADES',1,1,[]); pause(0.5);

buf.size() hde = buf.get();

softwarespartan commented 7 years ago

Hi Alex

Thanks for checking out IB4m!

Are you able to get contract details for that contract you have configured?

Sometimes if the contract is not right no data is returned — but you should see error.

Are you able to run the sample/example for historical data?

You can also ask TWS for contract details.

However, your “reqHistoricalData” does not look quite right to my eye. I think the “20 D” is not correct maybe.

Checkout

https://www.interactivebrokers.com/en/software/api/apiguide/java/reqhistoricaldata.htm <https://www.interactivebrokers.com/en/software/api/apiguide/java/reqhistoricaldata.htm>

and

https://www.interactivebrokers.com/en/software/api/apiguide/tables/historical_data_limitations.htm <https://www.interactivebrokers.com/en/software/api/apiguide/tables/historical_data_limitations.htm>

I have attached a short script that will help construct historical data queries

With this script do something like:

dt = now();

contract = com.tws.ContractFactory.GenericStockContract(symbol);

config.symbol = 'NFLX' ; config.duration = '1 Y' ; config.barsize = '1 day' ; config.whatToShow = 'TRADES'; config.useRTH = 1 ; config.delta = 365 ; config.N = 3 ;

and then call

[dat,bars] = historicDataQuery(contract,dt,config);

Let me know if this helps. Don’t hesitate to reach out if you need anything additional or have more questions.

Cheers -abel

On May 6, 2017, at 6:56 PM, NewbieAlex notifications@github.com wrote:

First of all, great job, thanks! I was able to request historical stock data, but no success in market index. It doesn't seem to return any data. For example, I use the following code for SP500 index data, but "buf.size()" is 0, and later there will be a empty buffer error in "hde = buf.get();"

contract = com.tws.ContractFactory.GenericContract('SPX'); contract.m_secType = 'IND'; contract.m_primaryExch = 'CBOE'; contract.m_currency = 'USD';

% initialize session with TWS session = TWS.Session.getInstance();

% create local buffer for historical data events [buf,lh] = TWS.initBufferForEvent(TWS.Events.HISTORICALDATA);

% connect to TWS session.eClientSocket.eConnect('127.0.0.1',7496,0);

% request historical data for our contract for a year w/ 1-day bars session.eClientSocket.reqHistoricalData(1000001,contract,'20170505 16:00:00','20 D','1 day','TRADES',1,1,[]); pause(0.5);

buf.size() hde = buf.get();

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/softwarespartan/IB4m/issues/5, or mute the thread https://github.com/notifications/unsubscribe-auth/AEWq1IaBXz_pT3EMdS9jNdcfhB8ijetSks5r3PqkgaJpZM4NS79A.

NewbieAlex commented 7 years ago

Hi Abel, Thanks for comments! So I've been able to get contract details and historical data only for stocks and ETF (eg. SPY). The sample/example code worked very well on stocks for me. The problem is that i couldn't get market index data, such as SPX for the sp500.

Also, I tried to run the above script, but got an error in Matlab: Undefined function or variable 'historicDataQuery'.

Again, it feels weird that I could get historical data with stocks but not market indices, with basically the same code. And I think I defined the product information correctly, since there's no other error reported.

Thanks, Alex

softwarespartan commented 7 years ago

Hi Alex

You need to have the “historicDataQuery.m” in your Matlab path

Also, were you able to get contract details for SPX?

I have had problems in the past with IB API and Index data for VIX etc.

-abel

On May 6, 2017, at 11:31 PM, NewbieAlex notifications@github.com wrote:

Hi Abel, Thanks for comments! So I've been able to get contract details and historical data only for stocks and ETF (eg. SPY). The sample/example code worked very well on stocks for me. The problem is that i couldn't get market index data, such as SPX for the sp500.

Also, I tried to run the above script, but got an error in Matlab: Undefined function or variable 'historicDataQuery'.

Again, it feels weird that I could get historical data with stocks but not market indices, with basically the same code. And I think I defined the product information correctly, since there's no other error reported.

Thanks, Alex

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/softwarespartan/IB4m/issues/5#issuecomment-299680063, or mute the thread https://github.com/notifications/unsubscribe-auth/AEWq1D_AIP2NPfOeT5G78WsuHx6DmcJbks5r3TsLgaJpZM4NS79A.

NewbieAlex commented 7 years ago

Thanks Abel. I was able to get details of SPX including exchange, IB symbol, currency, etc. Interestingly, it turns out that there's no such file named “historicDataQuery.m” in my IB4m download. Is it something that was included in previous IB4m version but not in the latest version?

softwarespartan commented 7 years ago

Hi Alex

That’s great. Apologies for the confusion. I had attached historicalDataQuery.m to an earlier email.

historical data query file attached here.

Do you mind to share your code snippet for getting SPX?

Cheers -abel

On May 7, 2017, at 8:24 AM, NewbieAlex notifications@github.com wrote:

Thanks Abel. I was able to get details of SPX including exchange, IB symbol, currency, etc. Interestingly, it turns out that there's no such file named “historicDataQuery.m” in my IB4m download. Is it something that was included in previous IB4m version but not in the latest version?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/softwarespartan/IB4m/issues/5#issuecomment-299702935, or mute the thread https://github.com/notifications/unsubscribe-auth/AEWq1Aterk2dNS1TOLWPW5jUop8i1wxlks5r3bgDgaJpZM4NS79A.

NewbieAlex commented 7 years ago

Hi Abel,

I'm sorry, the only script that I could see in your earlier replies, is the following: dt = now(); contract = com.tws.ContractFactory.GenericStockContract(symbol); config.symbol = 'NFLX' ; config.duration = '1 Y' ; config.barsize = '1 day' ; config.whatToShow = 'TRADES'; config.useRTH = 1 ; config.delta = 365 ; config.N = 3 ; [dat,bars] = historicDataQuery(contract,dt,config);

Unfortunately there seemed to be no "historicDataQuery.m" file attached. Do you mind to send it one more time?

Additionally, since .m file type is not supported, I'm attaching the script for getting SPX as text here.

Thanks, Alex


.m file as text:

contract = com.tws.ContractFactory.GenericContract('SPX'); contract.m_secType = 'IND'; contract.m_primaryExch = 'CBOE'; contract.m_currency = 'USD';

session = TWS.Session.getInstance();

[buf,lh] = TWS.initBufferForEvent(TWS.Events.HISTORICALDATA);

session.eClientSocket.eConnect('127.0.0.1',7496,0);

session.eClientSocket.reqHistoricalData(1000001,contract,'20170505 16:00:00','20 D','1 day','TRADES',1,1,[]); pause(0.5);

buf.size() hde = buf.get(); bars = collection2cell(hde.data);

open = cellfun(@(b)b.open,bars); high = cellfun(@(b)b.high,bars); low = cellfun(@(b)b.low,bars); close = cellfun(@(b)b.close,bars);
vol = cellfun(@(b)b.volume,bars); count = cellfun(@(b)b.count,bars);


NewbieAlex commented 7 years ago

Hey Abel, Thanks again for the great work here. While GitHub doesn't allow .m format as attachment, would you share something like a Dropbox link or add the file to IB4m (which would make it even better)? Thank you in advance!

-Alex

softwarespartan commented 7 years ago

Hi Alex

see if you can access this link:

https://www.dropbox.com/s/qdxzhyneg734auz/historicDataQuery.m?dl=0 https://www.dropbox.com/s/qdxzhyneg734auz/historicDataQuery.m?dl=0

Cheers -abel

On May 9, 2017, at 8:55 AM, NewbieAlex notifications@github.com wrote:

Hey Abel, Thanks again for the great work here. While GitHub doesn't allow .m format as attachment, would you share something like a Dropbox link or add the file to IB4m (which would make it even better)? Thank you in advance!

-Alex

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/softwarespartan/IB4m/issues/5#issuecomment-300210067, or mute the thread https://github.com/notifications/unsubscribe-auth/AEWq1Jg4HgwaKnLWuRithT_Bcy48WYHPks5r4Ix8gaJpZM4NS79A.

NewbieAlex commented 7 years ago

Awesome, it works! Thank you, Abel!