def _convert_ticker(self, instrument: str, data: dict) -> Ticker: return Ticker(timestamp=self._parse_time(data['timestamp']), instrument=instrument, ask=data['best_ask'], bid=data['best_bid'], last=data['ltp'], volume_24h=data['volume'], _data=data)
def _convert_ticker(self, instrument: str, data: dict) -> Ticker: _ = self return Ticker(timestamp=data['timestamp'] / 1000, instrument=instrument, ask=float(data['sell']), bid=float(data['buy']), last=float(data['last']), volume_24h=float(data['vol']), _data=data)
def _convert_ticker(self, timestamp: float, instrument: str, data: dict) -> Ticker: return Ticker(timestamp=timestamp, instrument=instrument, ask=float(data['market_ask']), bid=float(data['market_bid']), last=float(data['last_traded_price']), volume_24h=float(data['volume_24h']), _data=data)
def _convert_ticker(self, timestamp: float, instrument: str, data: list) -> Ticker: return Ticker(timestamp=timestamp, instrument=instrument, ask=float(data[2]), bid=float(data[0]), last=float(data[6]), volume_24h=float(data[7]), _data=data)
def get_ticker(self, instrument: str) -> Ticker: symbol = self.instruments[instrument].name_id res = self.public_get('/instrument', symbol=symbol) x = res[0] return Ticker(timestamp=self._parse_time(x['timestamp']), instrument=instrument, ask=float(x['askPrice']), bid=float(x['bidPrice']), last=float(x['lastPrice']), volume_24h=float(x['volume24h']), _data=res)
def get_ticker(self, instrument: str) -> Ticker: symbol = self.instruments[instrument].name_id res = self.public_get(f'/pubticker/{symbol}') return Ticker(timestamp=float(res['timestamp']), instrument=instrument, ask=float(res['ask']), bid=float(res['bid']), last=float(res['last_price']), volume_24h=float(res['volume']), _data=res)
def convert_raw_data(self, data: StreamData) -> Optional[StreamData]: key: Tuple[Hashable, ...] = data.key data: Any = self.MessageData(data.data) stream_type = key[0] if stream_type == StreamType.TICKER: cache = self._channel_data_cache[key[:2]] instrument: str = key[1] sub_type: str = key[2] symbol = self.rinstruments[instrument] time_str = None if sub_type == 'quote': if data.action == 'partial': cache['quote_partial_received'] = True if not cache.get('quote_partial_received'): return for x in sorted(data.data, key=lambda _: _['timestamp']): assert x['symbol'] == symbol, data.message cache['quote'] = x time_str = x['timestamp'] elif sub_type == 'trade': if data.action == 'partial': cache['trade_partial_received'] = True if not cache.get('trade_partial_received'): return for x in sorted(data.data, key=lambda _: _['timestamp']): assert x['symbol'] == symbol, data.message cache['trade'] = x time_str = x['timestamp'] else: logger.error(f'unknown route {data.message}') return quote = cache.get('quote') trade = cache.get('trade') if not quote or not trade or not time_str: return ticker = Ticker(timestamp=self._parse_time(time_str), instrument=instrument, ask=quote['askPrice'], bid=quote['bidPrice'], last=trade['price'], _data=None) return StreamData(key[:2], ticker) elif stream_type == StreamType.ORDER_BOOK: cache = self._channel_data_cache[key] instrument: str = key[1] symbol = self.rinstruments[instrument] if data.action == 'partial': cache['partial_received'] = True if not cache.get('partial_received'): return id_map = cache.setdefault('id_map', {}) if data.action in ('partial', 'insert'): for x in data.data: assert x['symbol'] == symbol, data.message id_map[x['id']] = x elif data.action == 'update': for x in data.data: assert x['symbol'] == symbol, data.message if x['id'] not in id_map: logger.warning(f'{x} not found') else: id_map[x['id']].update(x) elif data.action == 'delete': for x in data.data: assert x['symbol'] == symbol, data.message if x['id'] not in id_map: logger.warning(f'{x} not found') else: del id_map[x['id']] asks = [] bids = [] for x in id_map.values(): # {'symbol': 'XBTUSD', 'id': 1234567890, 'side': 'Sell', 'size': 100, 'price': 123456} side = x['side'].lower() if side == 'sell': asks.append((float(x['price']), float(x['size']), x['id'])) else: assert side == 'buy' bids.append((float(x['price']), float(x['size']), x['id'])) order_book = OrderBook(timestamp=time.time(), instrument=instrument, asks=asks, bids=bids, _data=None) return StreamData(key[:2], order_book) return None