softwarespartan / IB4m

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

underConId not displayed correctly #75

Open dakr001 opened 4 years ago

dakr001 commented 4 years ago

Hi,

I need to trace back the underlying futures for FOPs. It appears the underlying contract ID is not displayed correctly. underConId appears to be identical to conid for all the options I checked (including equity options). See FOP example below.

I was expecting the underConID would refer to the underlying futures contract. I don't think it's possible to trace the futures from the information below since the FOP expiry can be different from the Futures expiry.

conid = 383564101 symbol = AUD secType = FOP expiry = 20200207 strike = 0.73 right = Call multiplier = 100000 exchange = GLOBEX primaryExch = null currency = USD localSymbol = ADUG0 C0730 tradingClass = ADU marketName = ADU minTick = 5.0E-5 price magnifier = 1 validExchanges = GLOBEX underConId = 383564101 longName = Australian dollar contractMonth = 202002

dakr001 commented 4 years ago

Hi Abel/Despair2000,

I've investigated this further with tech support from IB. I believe there is an issue with IB4m. The underConId returned using IB4m is 383564101, which is identical to conid.

When I asked IB to check, they got underConId =187062382, which matches the underlying futures contract that I see on TWS expiring in Mar2020.

Can you please check? Is there anything I can do myself to get this fixed?

Thanks very much.

Despair2000 commented 4 years ago

I'm afraid this is a job for @softwarespartan . I'm just a user like you and sadly I have no knowledge of Java.

dakr001 commented 4 years ago

Thanks @Despair2000 . @softwarespartan any chance you can have a look please. Thanks.

softwarespartan commented 4 years ago

Please read the documentation :)

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

You need to access the “underConId” field

On Oct 14, 2019, at 9:16 AM, dakr001 notifications@github.com wrote:

Thanks @Despair2000 https://github.com/Despair2000 . @softwarespartan https://github.com/softwarespartan any chance you can have a look please. Thanks.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/softwarespartan/IB4m/issues/75?email_source=notifications&email_token=ABC2VVGONWRFIJVSKQMOAVDQORWKVA5CNFSM4JACM54KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEBEUCGI#issuecomment-541671705, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABC2VVBBQEQQCZE5RY23TW3QORWKVANCNFSM4JACM54A.

dakr001 commented 4 years ago

Hi @softwarespartan

I did read the documentation and I used underConId.

The issue I see is that the conid and underConID numbers returned by IB4m are exactly the same (383564101) so I cannot trace back the underlying contract. The underConId is incorrect. The underConID that IB tech support got for this contract was different (187062382) but is correct.

My code to get the contract details are below: session = TWS.Session.getInstance(); session.eClientSocket.eConnect('127.0.0.1',7496,0); [detailsBuf , detailslh] = TWS.initBufferForEvent(TWS.Events.CONTRACTDETAILS);

contract = com.ib.client.Contract(); contract.secType('FOP') contract.symbol('AUD') contract.currency('USD') contract.exchange('GLOBEX') contract.strike(0.73) contract.right('Call') contract.lastTradeDateOrContractMonth('20200207');

session.eClientSocket.reqContractDetails(0,contract); while detailsBuf.size() <=0 pause(1); end

e = collection2cell(detailsBuf.get().data); disp(e{:})

Result below where conid=underConId=383564101

conid = 383564101 symbol = AUD secType = FOP expiry = 20200207 strike = 0.73 right = Call multiplier = 100000 exchange = GLOBEX primaryExch = null currency = USD localSymbol = ADUG0 C0730 tradingClass = ADU marketName = ADU minTick = 5.0E-5 validExchanges = GLOBEX underConId = 383564101 longName = Australian dollar contractMonth = 202002

softwarespartan commented 4 years ago

You’re still not actually accessing the field of the contract details object. You are just looking at a (likely buggy) string representation of the object that gets created when call display (i.e. toString)

Try something like:

e = buf.get().data()

e.contractDetails.underConId

To see all the fields/methods:

fields(e.contractDetails) 
methods(e.contractDetails)

Sent from my iPhone

On Oct 14, 2019, at 8:09 PM, dakr001 notifications@github.com wrote:

Hi @softwarespartan

I did read the documentation and I used underConId.

The issue I see is that the conid and underConID numbers returned by IB4m are exactly the same (383564101) so I cannot trace back the underlying contract. The underConID that IB tech support got was different (187062382) but is correct.

My code to get the contract details are below: session = TWS.Session.getInstance(); session.eClientSocket.eConnect('127.0.0.1',7496,0); [detailsBuf , detailslh] = TWS.initBufferForEvent(TWS.Events.CONTRACTDETAILS);

contract = com.ib.client.Contract(); contract.secType('FOP') contract.symbol('AUD') contract.currency('USD') contract.exchange('GLOBEX') contract.strike(0.73) contract.right('Call') contract.lastTradeDateOrContractMonth('20200207');

session.eClientSocket.reqContractDetails(0,contract); while detailsBuf.size() <=0 pause(1); end

e = collection2cell(detailsBuf.get().data); disp(e{:})

Result below where conid=underConId=383564101

conid = 383564101 symbol = AUD secType = FOP expiry = 20200207 strike = 0.73 right = Call multiplier = 100000 exchange = GLOBEX primaryExch = null currency = USD localSymbol = ADUG0 C0730 tradingClass = ADU marketName = ADU minTick = 5.0E-5 validExchanges = GLOBEX underConId = 383564101 longName = Australian dollar contractMonth = 202002

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

dakr001 commented 4 years ago

Hi @softwarespartan

Got the following. e is a 1x1 HashSet

e = detailsBuf.get().data(); e.contractDetails.underConId Undefined function 'contractDetails' for input arguments of type 'java.util.HashSet'.

fields(e.contractDetails) Undefined function 'contractDetails' for input arguments of type 'java.util.HashSet'.

Despair2000 commented 4 years ago

Try this one:

e = detailsBuf.get.data.iterator.next.contractDetails; e.underConid

dakr001 commented 4 years ago

Hey @Despair2000 I got a result which looks right - Thank you.

-If there are many contracts how do you get all the underConids? This only returns one... -What other contract specs can I get using this? Tried e.symbol, and e.currency - both didn't work...

Thanks again...

Despair2000 commented 4 years ago

You can only request contract details for 1 contract at a time.

You get all details in a similar way. The symbol for example you find in the contract. Issue

methods(e)

This will give you all methods for the object. There you see the contract method.

e.contract

And when you call this one you will finally find

e.contract.symbol

The currency you find also in the contract. It's all there, you just have to find it :-)

dakr001 commented 4 years ago

Cheers @Despair2000

Amazing, thank you very much. The reqContractDetails below returns close to 1800 contracts for a series of options chains. Is there a way to loop and inspect through the contract details? In the code below, they are stored in cells and the underConId are wrong. Would like to use your suggestion in using the underConID method.

[detailsBuf , detailslh] = TWS.initBufferForEvent(TWS.Events.CONTRACTDETAILS); contract = com.ib.client.Contract(); contract.secType('FOP') contract.symbol('AUD') contract.currency('USD') contract.exchange('GLOBEX') session.eClientSocket.reqContractDetails(0,contract); while detailsBuf.size() <=0 pause(1); end e = collection2cell(detailsBuf.get().data);

Despair2000 commented 4 years ago

Wow, OK. I never worked with options. I will try to sort this out for you...

Despair2000 commented 4 years ago

I got it. Will post it for you in a few minutes. Just have to debug the wall street opening for my algo now first...

Despair2000 commented 4 years ago

So here you go. The code below gives you a cell array of all contracts and outputs the underConid of the contract in cell 123 as example.

e = detailsBuf.get.data; it = e.iterator; out = cell(e.size,1); indx = 1; while hasNext(it); out{indx} = next(it); indx = indx + 1; end out{123}.contractDetails.underConid

dakr001 commented 4 years ago

@Despair2000 you're my hero for all time :)

softwarespartan commented 4 years ago

you can use collection2cell to convert the buffer contents into a cell array of events.

You can also use collection2cell to convert HashSet into a cell array of items.

ContractDetails can return many entries for single request depending on the asset type (e.g. options) which is why hash set (i.e. collection) is used to aggregate into single request response.

dakr001 commented 4 years ago

@softwarespartan Thank you for the pointers...