Skip to content

Commit e88ffd0

Browse files
committed
Add another check for "no price/size" empty values
Also adds some historical debug details for trying to figure out why the IBKR APIs return bad SPX values after hours (it always does this and even their official apps/sites report the bad values). As far as I can tell, there's a bug in the IBKR data APIs where, after hours, it returns the _correct_ value followed immediately by some incorrect value (maybe it's adjusted for something?) but we can't seem to capture the "good value" before the bad value replaces it because there's no indicator which value is which (other than the order they arrive in immediately when connecting).
1 parent d3d4c09 commit e88ffd0

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

ib_async/wrapper.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -925,14 +925,29 @@ def priceSizeTick(self, reqId: int, tickType: int, price: float, size: float):
925925
ticker.askSize = size
926926
elif tickType in {4, 68}:
927927
# for 'last' values, price can be valid with size=0 for updates like 'last SPX price' since SPX doesn't trade
928-
if price == -1 and size == 0:
928+
# Workaround: for TICK-NYSE, it is valid to have price=-1, size=0 because it can float between -10,000 and 10,000
929+
# and it also never reports a size. As a workaround, check if ticker.close exists as a proxy for "not TICK-NYSE"
930+
# because TICK-NYSE never has open/close values populated.
931+
if price == -1 and size == 0 and ticker.close > 0:
929932
price = self.defaultEmptyPrice
930933
size = self.defaultEmptySize
931934

935+
# BUG? IBKR is sometimes sending a GOOD VALUE followed by a PREVIOUS value all under tickType=4?
936+
# e.g. I get the SPX close delivered first with size=0, then I get another data point with size=1 priced one point lower,
937+
# but since the older price is delivered second, it replaces the "last" value with a wrong value? Not sure if it's
938+
# an IBKR data problem or a logic problem somewhere here?
939+
# More research: IBKR also shows the bad value in their own app, so there is a data bug in their own server logic somewhere.
940+
941+
# self._logger.error(f"[{tickType=}] updating last price size: {price=} {size=} :: BEFORE {ticker=}")
942+
# self._logger.error(f"[{tickType=}] SETTING {ticker.prevLast=} = {ticker.last=}; {ticker.prevLastSize=} = {ticker.lastSize=}")
943+
932944
ticker.prevLast = ticker.last
933945
ticker.prevLastSize = ticker.lastSize
934946
ticker.last = price
935947
ticker.lastSize = size
948+
949+
# self._logger.error(f"[{tickType=}] SET {ticker.prevLast=} = {ticker.last=}; {ticker.prevLastSize=} = {ticker.lastSize=}")
950+
# self._logger.error(f"[{tickType=}] updating last price size: {price=} {size=} :: AFTER {ticker=}")
936951
else:
937952
assert (
938953
tickType in PRICE_TICK_MAP

0 commit comments

Comments
 (0)