def normalize(ticker: Ticker, calc_price: bool = False) -> dict: # convert named tuples to dicts so we send usable keys new_ticks = [] for tick in ticker.ticks: td = tick._asdict() if td['tickType'] in (48, 77): td['type'] = 'trade' new_ticks.append(td) ticker.ticks = new_ticks # some contracts don't have volume so we may want to calculate # a midpoint price based on data we can acquire (such as bid / ask) if calc_price: ticker.ticks.append({'type': 'trade', 'price': ticker.marketPrice()}) # serialize for transport data = asdict(ticker) # add time stamps for downstream latency measurements data['brokerd_ts'] = time.time() if ticker.rtTime: data['broker_ts'] = data['rtTime_s'] = float(ticker.rtTime) / 1000. return data
def normalize(ticker: Ticker, calc_price: bool = False) -> dict: # convert named tuples to dicts so we send usable keys new_ticks = [] for tick in ticker.ticks: td = tick._asdict() td['type'] = tick_types.get(td['tickType'], 'n/a') new_ticks.append(td) ticker.ticks = new_ticks # some contracts don't have volume so we may want to calculate # a midpoint price based on data we can acquire (such as bid / ask) if calc_price: ticker.ticks.append({'type': 'trade', 'price': ticker.marketPrice()}) # serialize for transport data = asdict(ticker) # add time stamps for downstream latency measurements data['brokerd_ts'] = time.time() # stupid stupid shit...don't even care any more.. # leave it until we do a proper latency study # if ticker.rtTime is not None: # data['broker_ts'] = data['rtTime_s'] = float( # ticker.rtTime.timestamp) / 1000. data.pop('rtTime') return data
def startReqMktData(self, reqId, contract): """ Start a snapshot or tick subscription that has the reqId associated with the contract. """ self._contractIdentity2ReqId[id(contract)] = reqId self._reqId2Contract[reqId] = contract ticker = self.getTicker(contract) if not ticker: ticker = Ticker(contract=contract, ticks=[]) self._reqId2Ticker[reqId] = ticker
def startTicker(self, reqId, contract, tickType): """ Start a tick request that has the reqId associated with the contract. Return the ticker. """ ticker = self.tickers.get(id(contract)) if not ticker: ticker = Ticker(contract=contract, ticks=[], tickByTicks=[], domBids=[], domAsks=[], domTicks=[]) self.tickers[id(contract)] = ticker self.reqId2Ticker[reqId] = ticker self.ticker2ReqId[tickType][ticker] = reqId return ticker
def startTicker(self, reqId, contract, isMktDepth=False): """ Start a snapshot, tick stream or market depth stream that has the reqId associated with the contract. Return the ticker. """ ticker = self.tickers.get(id(contract)) if not ticker: ticker = Ticker(contract=contract, ticks=[], domBids=[], domAsks=[], domTicks=[]) self.tickers[id(contract)] = ticker self.reqId2Ticker[reqId] = ticker if isMktDepth: self.ticker2MktDepthReqId[ticker] = reqId else: self.ticker2MktDataReqId[ticker] = reqId return ticker
def price_update(ticker: _IBTicker): contract_ = self._from_ib_contract(ib_contract=ticker.contract) assert contract_ in self._price_subscriptions con_subs_ = self._price_subscriptions[contract_] for func_, func_dict_ in con_subs_.items(): price_type_ = func_dict_["price_type"] fn_kwargs_ = func_dict_["kwargs"] if price_type_ == PriceType.MARKET: price_ = ticker.midpoint() elif price_type_ == PriceType.ASK: price_ = ticker.ask elif price_type_ == PriceType.BID: price_ = ticker.bid else: raise TypeError(f"Unknown price type {price_type_}.") func_(contract_, price_, **fn_kwargs_)