¿ªÔÆÌåÓý

ctrl + shift + ? for shortcuts
© 2025 Groups.io

[Java] [reqMktData()] Need help figuring out why reqMktData() isn't working


mchaney2
 

Hi all, I have the following code:
?? ????
??????? public void tickPrice(int tickerId, int field, double price, TickAttrib attrib) {
?? ???????? System.out.println(price);
?? ???? }

?? ???? private EReaderSignal signal = new EJavaSignal();
?? ???? private EClientSocket client = new EClientSocket(this, signal);
?? ????
?? ???? client.eConnect("127.0.0.1", 7496, 2);
?? ???? Thread.sleep(3000);
?????? ?
?????? ?
?????? ?
??????? reader = new EReader(client, signal);? ?
??????? reader.start();
?????? ?
??????? new Thread(() -> {
??????????? while (client.isConnected()) {
??????????????? signal.waitForSignal();
??????????????? try {
??????????????????? reader.processMsgs();
??????????????? } catch (Exception e) {
??????????????????? System.out.println("Exception: "+e.getMessage());
??????????????? }
??????????? }
??????? }).start();
?????? ?
?????? ?
??????? Contract contract = new Contract();
??????? contract.symbol("IGC");
??????? contract.secType("STK");
?????? ?
??????? Vector<TagValue> mktDataOptions = new Vector<TagValue>();

??????? client.reqCurrentTime();
??????? client.reqMktData(nextOrderID, contract, null, false, false, mktDataOptions);
???????
??????? Thread.sleep(1000);
??????? client.eDisconnect();

I would like to know what's going wrong here. When I run the program it successfully gives me the current time but it does nothing regarding the call to reqMktData(). I believe that calling reqMktData() should automatically cause the tickPrice() method to print out the current price. Am I correct in believing that this is automatic upon calling reqMktData()? Or am I missing an extra step somewhere?

I am doing this outside of market hours but I don't believe that is my problem.

I have searched around this site and can't find anything suggesting that my code shouldn't work.

Thank you for any help.


 

I have a few suggestions:

(1) have you specified nextOrderID?
(2) you could run reqContractDetails() before reqMktData() to ensure that your contract is sufficiently specified.
(3) After submitting reqMktData() you wait for only 1 second and then disconnect from IB. The disconnect could occur before you receive any market data, depending on how actively this stock ticker is being traded.
(4) before calling eDisconnect() you need to cancel your market data subscriptions. Otherwise you might have issues if you try to run the same program once more immediately afterwards.


mchaney2
 

Thank you for your reply!

Here are my responses to your questions:

1. Have you specified nextOrderID -
??????????? Yes, I defined it as zero elsewhere... Is there a programatic way to ensure it's always an appropriate number? I never intend to send any actual orders so I'm not sure if this matters.

2. ...Run reqContractDetails() before reqMktData() to ensure contract specified -
???????????? Ok thanks for the tip. Question : Does running reqContractDetails() auto fill the values into the parameters of the Contract object? So that I can then act on it as though the Contract object is "full"?

3. The disconnect could occur before you receive any market data, depending on how actively this stock ticker is being traded.
??????????? Ok so the reqMktData() function only will return data if any trades happen? i.e. will return nothing on zero volume day? I'm guessing to get the current "last traded price" we would go the historical data route? Or maybe set snapshot to true?

4.?
before calling eDisconnect() you need to cancel your market data subscriptions.
????????? I will look into how to do this and add it to my code, thank you.

I'm working at the moment but I'll be digging in deeper later. Thanks for your help, sorry if any questions are asinine.


mchaney2
 

With respect to number 4 and canceling my market data subscriptions... How do I do this? I've looked around the pages on the Github intro and don't see any way to do this with code.


 

1. reqMktData() requires you to specify a sequence number. You apparently decided to use parameter nextOrderID for this. But your code did not show whether that parameter was initialized somewhere. Which is why I asked. What number you select is not relevant (although I usually refrain from using zero as sequence number).
2. reqContractDetails() returns information in method contractDetails(). You could indeed pick up the returned contract and use that in the remainder of the code. Or simply print it to screen to verify that your contract definition was both sufficient and unique, resulting in precisely one returned contract.
3. method tickPrice() only gets updated if a price changes to what it was previously. This means that if there is no trading going on (and the best bid/ask prices don't change) you won't receive any data. If, during trading, multiple trades get executed at the same price you only receive a price update at the first trade, but not at the successive trades, because the price is not changing.
4. look for method cancelMktData(). In your case it would be cancelMktData(nextOrderID).


 

On Thu, Jan 21, 2021 at 08:35 PM, mchaney2 wrote:
With respect to number 4 and canceling my market data subscriptions... How do I do this? I've looked around the pages on the Github intro and don't see any way to do this with code.
See here:? http://interactivebrokers.github.io/tws-api/md_cancel.html