ib_async icon indicating copy to clipboard operation
ib_async copied to clipboard

Remove incorrect code from contract details decoder

Open djkelleher opened this issue 1 year ago • 2 comments

Problem - The current contract details decoder contains a bit of code that should only be used in the bond contract details decoder.

Effect - The function first correctly assigns the timeZoneId, but then later in the function body overwrites it with the timezone TWS is set to use.

Solution - Delete the extra code.

For reference, here is the logic from the official API from Interactive Brokers:

def readLastTradeDate(self, fields, contract: ContractDetails, isBond: bool):
        lastTradeDateOrContractMonth = decode(str, fields)
        if lastTradeDateOrContractMonth is not None:
            if '-' in lastTradeDateOrContractMonth: 
                splitted = lastTradeDateOrContractMonth.split('-')
            else:
                splitted = lastTradeDateOrContractMonth.split()
                
            if len(splitted) > 0:
                if isBond:
                    contract.maturity = splitted[0]
                else:
                    contract.contract.lastTradeDateOrContractMonth = splitted[0]

            if len(splitted) > 1:
                contract.lastTradeTime = splitted[1]

            if isBond and len(splitted) > 2:
                contract.timeZoneId = splitted[2]

djkelleher avatar May 15 '24 17:05 djkelleher

Thanks for all the details! You're right this looks broken because we use the different function bondContractDetails() directly.

What contract was causing the problem? Always nice to be able to see the crash before the fix then verify it works after.

I'll probably refactor all those length checks too when adding this since we could just do a series of field = times.pop(0) instead of individual checks for each case.

mattsta avatar May 16 '24 00:05 mattsta

Update: I just tested on the latest TWS stable (10.19.2m) and this actually isn't an issue anymore (the timezone at index 2 is actually for the exchange rather than TWS).

This was an old PR (Nov 29, 2023) from ib_insync (https://github.com/erdewit/ib_insync/pull/664) that I just copied over here. The issues were originally encountered while pulling contract details for futures contracts on an earlier 10.19 stable release.

I just tested on all futures contracts IB has (I think) and it's never necessary to reassign the timezone since the original value is always correct. I think even though the reassigning works on latest release, it's probably still better to take it out for backwards compatibility reasons and better consistency with the official IB Python client.

djkelleher avatar May 16 '24 14:05 djkelleher