softwarespartan / IB4m

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

Problem retrieving real time / streaming market data #66

Open ytorvi opened 4 years ago

ytorvi commented 4 years ago

Hello,

I am trying to develop a algorithm using matlab(2018b, on windows machine) and your java API 9.73 version. The requirement is to retrieve real time data from the IB database, using the TWS java API. I have gone through your example programs (Thank you for that) and also referred to the issue threads on this topic on the github repository. but i am not able to get the program working for realtime data. i could successfully execute it with historicaldata.

what i am trying to achieve? --> log into the IB system, use the java API, retrieve real time data for one or more contract symbols, process the data and plot them as needed, and then keep repeating this to generate real time streaming data and real time streaming charts.

what i have tried.

  1. pls find the attached file, where you have given a example of testMkDataCallbacks3() function at the github issues link https://github.com/softwarespartan/IB4m/issues/8 .

I get errors as below..

No method 'reqMktData' with matching signature found for class 'com.ib.client.EClientSocket'. Error in testMkDataCallbacks3 (line 52) session.eClientSocket.reqMktData(reqIdSPY, contractSPY, genericTickList, 0, []);

Error in testMkDataCallbacks3>callback (line 67) function callback(~,e); if e.event.data.reqId == reqIdSPY; buf.add(e.event); end; end Error in TWS.processNotification (line 145) notify( ...

i am using the callback function in the event listener, as suggested by you. i also tried out the [buf,lh] = TWS.initBufferForEvent(TWS.Events.REALTIMEBAR); method. the buffer is usually empty.

i could get the historicaldata TWS event working, but i am stuck with the real time streaming market data, where you need some form of callback to the TWS event.

Your help is much appreciated. Regards, Yogesh G. Torvi

testMkDataCallbacks3.txt

softwarespartan commented 4 years ago

Thanks for reaching out.

The first thing to verify is that you are able to get the MarketDataExample working as provided in the docs folder.

The second thing to verify is that you have read the associated documentation from the IB API

http://interactivebrokers.github.io/tws-api/md_request.html

Indeed, from your error below it looks like you are not making a valid function call for market data

From the docs:

client.reqMktData(1004, ContractSamples.USStockAtSmart(), "233,236,258", false, false, null);

From the example in IB4m/docs/MarketDataExample.m:

session.eClientSocket.reqMktData(reqId, contract, genericTickList, false, false, []);

From your error:

session.eClientSocket.reqMktData(reqIdSPY, contractSPY, genericTickList, 0, []);

Your function call does not match the required inputs.

Cheers -abel

On Sep 18, 2019, at 2:02 PM, ytorvi notifications@github.com wrote:

Hello,

I am trying to develop a algorithm using matlab(2018b, on windows machine) and your java API 9.73 version. The requirement is to retrieve real time data from the IB database, using the TWS java API. I have gone through your example programs (Thank you for that) and also referred to the issue threads on this topic on the github repository. but i am not able to get the program working for realtime data. i could successfully execute it with historicaldata.

what i am trying to achieve? --> log into the IB system, use the java API, retrieve real time data for one or more contract symbols, process the data and plot them as needed, and then keep repeating this to generate real time streaming data and real time streaming charts.

what i have tried.

pls find the attached file, where you have given a example of testMkDataCallbacks3() function at the github issues link #8 https://github.com/softwarespartan/IB4m/issues/8 . I get errors as below..

No method 'reqMktData' with matching signature found for class 'com.ib.client.EClientSocket'. Error in testMkDataCallbacks3 (line 52) session.eClientSocket.reqMktData(reqIdSPY, contractSPY, genericTickList, 0, []);

Error in testMkDataCallbacks3>callback (line 67) function callback(~,e); if e.event.data.reqId == reqIdSPY; buf.add(e.event); end; end Error in TWS.processNotification (line 145) notify( ...

i am using the callback function in the event listener, as suggested by you. i also tried out the [buf,lh] = TWS.initBufferForEvent(TWS.Events.REALTIMEBAR); method. the buffer is usually empty.

i could get the historicaldata TWS event working, but i am stuck with the real time streaming market data, where you need some form of callback to the TWS event.

Your help is much appreciated. Regards, Yogesh G. Torvi

testMkDataCallbacks3.txt https://github.com/softwarespartan/IB4m/files/3626222/testMkDataCallbacks3.txt — 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/66?email_source=notifications&email_token=ABC2VVE2TYOAEQTS5QSNPN3QKIKEHA5CNFSM4IX5WGC2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HMD32NA, or mute the thread https://github.com/notifications/unsubscribe-auth/ABC2VVADKXZYI5BJP2LYD5LQKIKEHANCNFSM4IX5WGCQ.

ytorvi commented 4 years ago

Hi Abel,

Thank you very much for an immediate reply. i ran the marketdata example and one false was missing. i fixed that latest event call, which works is as below.. session.eClientSocket.reqMktData(reqId,contract,genericTickList,false,false,[]);

i also changed the contract settings, since i am now using API 9.73. for API 9.73, the following contract settings work for me.

% create a contract for SPY
contract = com.ib.client.Contract();
contract.symbol ('SPY' );
contract.exchange ('ARCA');
contract.primaryExch ('ARCA');
contract.secType ('STK' );
contract.currency ('USD' );

now the reqMktData call works without any issue. but i am still getting error running the callback function, as a part of event listener.

following is the error..

Warning: Error occurred while executing the listener callback for event TWS_ERROR defined for class TWS.Events: Undefined function 'reqId' for input arguments of type 'com.tws.Error'. Error in my_MarketDataExample>callback (line 217) if (e.event.data.reqId == reqId ) Error in TWS.processNotification (line 145) notify(

reqId is defined in the m file. i even tried to hard code the reqId check, but with same result.

it also seems that i can have an event listener with only one function call. the sample m file was calling the display function to display event data. with the callback function, i had to comment it out and only keep the callback function call.

%% commented this out @(s,e)disp(e.event.data) ,... % create a callback to print error messages to the command window %% added callback function as part of event listener lherr = event.listener( ... TWS.Events.getInstance ,... TWS.Events.ERROR ,... @callback ... );

please find attached my modified m file.

Regards, Yogesh

my_MarketDataExample.txt

ytorvi commented 4 years ago

when i try to extract real time data using realtimebars method, following is the error / non-data that i receive at matlab prompt ..

session.eClientSocket.reqRealTimeBars(0,contract,5,"MIDPOINT",true,[]) 0 102 Duplicate ticker id

session.eClientSocket.reqRealTimeBars(0,contract,5,"TRADES",true,[]) 0 102 Duplicate ticker id

my reqId is 0.

softwarespartan commented 4 years ago

not sure what the question is here.

Your file does not define a callback for the error. You can comment this out/ignore since TWS.Session now automatically defines an error listener and callback.

-abel

On Sep 18, 2019, at 3:11 PM, ytorvi notifications@github.com wrote:

Hi Abel,

Thank you very much for an immediate reply. i ran the marketdata example and one false was missing. i fixed that latest event call, which works is as below.. session.eClientSocket.reqMktData(reqId,contract,genericTickList,false,false,[]);

i also changed the contract settings, since i am now using API 9.73. for API 9.73, the following contract settings work for me.

% create a contract for SPY contract = com.ib.client.Contract(); contract.symbol ('SPY' ); contract.exchange ('ARCA'); contract.primaryExch ('ARCA'); contract.secType ('STK' ); contract.currency ('USD' );

now the reqMktData call works without any issue. but i am still getting error running the callback function, as a part of event listener.

following is the error..

Warning: Error occurred while executing the listener callback for event TWS_ERROR defined for class TWS.Events: Undefined function 'reqId' for input arguments of type 'com.tws.Error'. Error in my_MarketDataExample>callback (line 217) if (e.event.data.reqId == reqId ) Error in TWS.processNotification (line 145) notify(

reqId is defined in the m file. i even tried to hard code the reqId check, but with same result.

it also seems that i can have an event listener with only one function call. the sample m file was calling the display function to display event data. with the callback function, i had to comment it out and only keep the callback function call.

%% commented this out @(s,e)disp(e.event.data) ,... % create a callback to print error messages to the command window %% added callback function as part of event listener lherr = event.listener( ... TWS.Events.getInstance ,... TWS.Events.ERROR ,... @callback https://github.com/callback ... );

please find attached my modified m file.

Regards, Yogesh

my_MarketDataExample.txt https://github.com/softwarespartan/IB4m/files/3626537/my_MarketDataExample.txt — You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/softwarespartan/IB4m/issues/66?email_source=notifications&email_token=ABC2VVHGTTCIKUCSJPOD2OTQKISGXA5CNFSM4IX5WGC2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD7AAESI#issuecomment-532677193, or mute the thread https://github.com/notifications/unsubscribe-auth/ABC2VVHFXBS42Z45YG3IQV3QKISGXANCNFSM4IX5WGCQ.

softwarespartan commented 4 years ago

Yep, you have a duplicate request id. Try using different/increment reqId

On Sep 18, 2019, at 3:28 PM, Yogesh Torvi notifications@github.com wrote:

when i try to extract real time data using realtimebars method, following is the error / non-data that i receive at matlab prompt ..

session.eClientSocket.reqRealTimeBars(0,contract,5,"MIDPOINT",true,[]) 0 102 Duplicate ticker id

session.eClientSocket.reqRealTimeBars(0,contract,5,"TRADES",true,[]) 0 102 Duplicate ticker id

my reqId is 0.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/softwarespartan/IB4m/issues/66?email_source=notifications&email_token=ABC2VVAEVBUSLMNCBK5ADWTQKIUJDA5CNFSM4IX5WGC2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD7AB3TA#issuecomment-532684236, or mute the thread https://github.com/notifications/unsubscribe-auth/ABC2VVA3ZAV6D6IBWU3OJELQKIUJDANCNFSM4IX5WGCQ.

ytorvi commented 4 years ago

not sure what the question is here. Your file does not define a callback for the error. You can comment this out/ignore since TWS.Session now automatically defines an error listener and callback. -abel

in that case i dont need to have a separate event listener and callback function for the marketdata event?

my original problem still continues. how can i keep getting the live market data streaming into matlab? can you please share a example for live market data streaming using the latest API?

Regards, Yogesh

softwarespartan commented 4 years ago

IB4m/docs/MarketDataExample.m

On Sep 18, 2019, at 3:54 PM, Yogesh Torvi notifications@github.com wrote:

not sure what the question is here. Your file does not define a callback for the error. You can comment this out/ignore since TWS.Session now automatically defines an error listener and callback. -abel

in that case i dont need to have a separate event listener and callback function for the marketdata event?

my original problem still continues. how can i keep getting the live market data streaming into matlab? can you please share a example for live market data streaming using the latest API?

Regards, Yogesh

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/softwarespartan/IB4m/issues/66?email_source=notifications&email_token=ABC2VVDFO7RA54JP4PIPFTLQKIXJJA5CNFSM4IX5WGC2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD7AERQA#issuecomment-532695232, or mute the thread https://github.com/notifications/unsubscribe-auth/ABC2VVGIXZIPQDDS5X7H3UTQKIXJJANCNFSM4IX5WGCQ.

ytorvi commented 4 years ago

Okay, but executing IB4m/docs/MarketDataExample.m program gives me the marketdata once for the symbol, even though i have set the parameter for streaming data. % request market data subscription. Note that arg4 is read as "snapshot=false" session.eClientSocket.reqMktData(reqId,contract,genericTickList,false,false,[]); ^ |- false should give me data stream

as per the current program, the mktDataEvents are extracted and displayed for the current dataset or snapshot.

can you please guide me on how do i go about getting the updated market data values, say every 5 seconds or some X timeframe and how do i keep displaying the updated values from within matlab?

thank you very much for your help and your time.

Regards, Yogesh