def tickString(self, reqId: int, tickType: int, value: str): ticker = self.reqId2Ticker.get(reqId) if not ticker: return try: if tickType == 47: # https://interactivebrokers.github.io/tws-api/fundamental_ratios_tags.html d = dict( t.split('=') # type: ignore for t in value.split(';') if t) # type: ignore for k, v in d.items(): with suppress(ValueError): if v == '-99999.99': v = 'nan' d[k] = float(v) d[k] = int(v) ticker.fundamentalRatios = FundamentalRatios(**d) elif tickType in (48, 77): # RT Volume or RT Trade Volume string format: # price;size;ms since epoch;total volume;VWAP;single trade # example: # 701.28;1;1348075471534;67854;701.46918464;true priceStr, sizeStr, rtTime, volume, vwap, _ = value.split(';') if volume: if tickType == 48: ticker.rtVolume = int(volume) elif tickType == 77: ticker.rtTradeVolume = int(volume) if vwap: ticker.vwap = float(vwap) if rtTime: ticker.rtTime = datetime.fromtimestamp( int(rtTime) / 1000, timezone.utc) if priceStr == '': return price = float(priceStr) size = int(sizeStr) if price and size: if ticker.prevLast != ticker.last: ticker.prevLast = ticker.last ticker.last = price if ticker.prevLastSize != ticker.lastSize: ticker.prevLastSize = ticker.lastSize ticker.lastSize = size tick = TickData(self.lastTime, tickType, price, size) ticker.ticks.append(tick) elif tickType == 59: # Dividend tick: # https://interactivebrokers.github.io/tws-api/tick_types.html#ib_dividends # example value: '0.83,0.92,20130219,0.23' past12, next12, nextDate, nextAmount = value.split(',') ticker.dividends = Dividends( float(past12) if past12 else None, float(next12) if next12 else None, parseIBDatetime(nextDate) if nextDate else None, float(nextAmount) if nextAmount else None) self.pendingTickers.add(ticker) except ValueError: self._logger.error(f'tickString with tickType {tickType}: ' f'malformed value: {value!r}')
def tickString(self, reqId, tickType, value): ticker = self.reqId2Ticker.get(reqId) if not ticker: return try: if tickType == 48: # RTVolume string format: # price;size;ms since epoch;total volume;VWAP;single trade # example: # 701.28;1;1348075471534;67854;701.46918464;true price, size, _, rtVolume, vwap, _ = value.split(';') if rtVolume: ticker.rtVolume = int(rtVolume) if vwap: ticker.vwap = float(vwap) if price == '': return price = float(price) size = float(size) if price and size: if ticker.prevLast != ticker.last: ticker.prevLast = ticker.last ticker.last = price if ticker.prevLastSize != ticker.lastSize: ticker.prevLastSize = ticker.lastSize ticker.lastSize = size tick = TickData(self.lastTime, tickType, price, size) ticker.ticks.append(tick) self.pendingTickers.add(ticker) elif tickType == 59: # https://interactivebrokers.github.io/tws-api/tick_types.html#ib_dividends if value == ",,,": # no dividend ticker.dividends = Dividends(0, 0, None, 0) else: # example value: '0.83,0.92,20130219,0.23' past12, next12, date, amount = value.split(',') ticker.dividends = Dividends( float(past12), float(next12), util.parseIBDatetime(date), float(amount)) except ValueError: self._logger.error( f'tickString with tickType {tickType}: ' f'malformed value: {value!r}')