async def listen_for_order_book_diffs(self, ev_loop: asyncio.BaseEventLoop, output: asyncio.Queue): # Orderbooks Deltas and Snapshots are handled by listen_for_order_book_stream() # pass while True: connection, hub = await self.websocket_connection() try: async for raw_message in self._socket_stream(): decoded: Dict[str, Any] = self._transform_raw_message( raw_message) # Processes diff messages if decoded["type"] == "delta": diff: Dict[str, any] = decoded diff_timestamp = diff["nonce"] diff_msg: OrderBookMessage = BittrexOrderBook.diff_message_from_exchange( diff["results"], diff_timestamp) output.put_nowait(diff_msg) except Exception: self.logger().error( "Unexpected error when listening on socket stream.", exc_info=True) finally: connection.close() self._websocket_connection = self._websocket_hub = None
async def listen_for_trades(self, ev_loop: asyncio.BaseEventLoop, output: asyncio.Queue): # Trade messages are received as Orderbook Deltas and handled by listen_for_order_book_stream() # passa while True: connection, hub = await self.websocket_connection() try: async for raw_message in self._socket_stream(): decoded: Dict[str, Any] = self._transform_raw_message( raw_message) # Processes snapshot messages if decoded["type"] == "trade": trades: Dict[str, any] = decoded for trade in trades["results"]["deltas"]: trade_msg: OrderBookMessage = BittrexOrderBook.trade_message_from_exchange( trade, metadata={ "trading_pair": trades["results"]["marketSymbol"], "sequence": trades["results"]["sequence"] }, timestamp=trades["nonce"]) output.put_nowait(trade_msg) except Exception: self.logger().error( "Unexpected error when listening on socket stream.", exc_info=True) finally: connection.close() self._websocket_connection = self._websocket_hub = None
async def listen_for_order_book_snapshots(self, ev_loop: asyncio.BaseEventLoop, output: asyncio.Queue): # Technically this does not listen for snapshot, Instead it periodically queries for snapshots. while True: try: async with aiohttp.ClientSession() as client: for trading_pair in self._trading_pairs: try: snapshot: Dict[str, Any] = await self.get_snapshot(client, trading_pair) snapshot_timestamp: float = time.time() snapshot_msg: OrderBookMessage = BittrexOrderBook.snapshot_message_from_exchange( snapshot, snapshot_timestamp, metadata={"marketSymbol": trading_pair} ) output.put_nowait(snapshot_msg) self.logger().info(f"Saved {trading_pair} snapshots.") await asyncio.sleep(5.0) except asyncio.CancelledError: raise except Exception: self.logger().error("Unexpected error.", exc_info=True) await asyncio.sleep(5.0) # Waits for delta amount of time before getting new snapshots this_hour: pd.Timestamp = pd.Timestamp.utcnow().replace(minute=0, second=0, microsecond=0) next_hour: pd.Timestamp = this_hour + pd.Timedelta(hours=1) delta: float = next_hour.timestamp() - time.time() await asyncio.sleep(delta) except Exception: self.logger().error("Unexpected error occurred invoking queryExchangeState", exc_info=True) await asyncio.sleep(5.0)
async def listen_for_order_book_diffs(self, ev_loop: asyncio.AbstractEventLoop, output: asyncio.Queue): msg_queue = self._message_queues["delta"] while True: try: diff = await msg_queue.get() diff_timestamp = diff["nonce"] diff_msg: OrderBookMessage = BittrexOrderBook.diff_message_from_exchange( diff["results"], diff_timestamp ) output.put_nowait(diff_msg) except Exception: self.logger().error("Unexpected error when listening on socket stream.", exc_info=True)
async def listen_for_trades(self, ev_loop: asyncio.AbstractEventLoop, output: asyncio.Queue): msg_queue = self._message_queues["trade"] while True: try: trades = await msg_queue.get() for trade in trades["results"]["deltas"]: trade_msg: OrderBookMessage = BittrexOrderBook.trade_message_from_exchange( trade, metadata={"trading_pair": trades["results"]["marketSymbol"], "sequence": trades["results"]["sequence"]}, timestamp=trades["nonce"] ) output.put_nowait(trade_msg) except Exception: self.logger().error("Unexpected error when listening on socket stream.", exc_info=True)
async def get_new_order_book(self, trading_pair: str) -> OrderBook: async with aiohttp.ClientSession() as client: snapshot: Dict[str, Any] = await self.get_snapshot(client, trading_pair) snapshot_timestamp: float = time.time() snapshot_msg: OrderBookMessage = BittrexOrderBook.snapshot_message_from_exchange( snapshot, snapshot_timestamp, metadata={"marketSymbol": trading_pair} ) order_book: OrderBook = self.order_book_create_function() active_order_tracker: BittrexActiveOrderTracker = BittrexActiveOrderTracker() bids, asks = active_order_tracker.convert_snapshot_message_to_order_book_row(snapshot_msg) order_book.apply_snapshot(bids, asks, snapshot_msg.update_id) return order_book