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: RadarRelayOrderBookMessage = RadarRelayOrderBook.snapshot_message_from_exchange( snapshot, snapshot_timestamp, metadata={"trading_pair": trading_pair}) radar_relay_order_book: OrderBook = self.order_book_create_function( ) radar_relay_active_order_tracker: RadarRelayActiveOrderTracker = RadarRelayActiveOrderTracker( ) bids, asks = radar_relay_active_order_tracker.convert_snapshot_message_to_order_book_row( snapshot_msg) radar_relay_order_book.apply_snapshot(bids, asks, snapshot_msg.update_id) return radar_relay_order_book
async def get_tracking_pairs(self) -> Dict[str, OrderBookTrackerEntry]: # Get the currently active markets async with aiohttp.ClientSession() as client: trading_pairs: List[str] = await self.get_trading_pairs() retval: Dict[str, OrderBookTrackerEntry] = {} number_of_pairs: int = len(trading_pairs) for index, trading_pair in enumerate(trading_pairs): try: snapshot: Dict[str, any] = await self.get_snapshot( client, trading_pair) snapshot_timestamp: float = time.time() snapshot_msg: RadarRelayOrderBookMessage = RadarRelayOrderBook.snapshot_message_from_exchange( snapshot, snapshot_timestamp, metadata={"symbol": trading_pair}) radar_relay_order_book: OrderBook = self.order_book_create_function( ) radar_relay_active_order_tracker: RadarRelayActiveOrderTracker = RadarRelayActiveOrderTracker( ) bids, asks = radar_relay_active_order_tracker.convert_snapshot_message_to_order_book_row( snapshot_msg) radar_relay_order_book.apply_snapshot( bids, asks, snapshot_msg.update_id) retval[trading_pair] = RadarRelayOrderBookTrackerEntry( trading_pair, snapshot_timestamp, radar_relay_order_book, radar_relay_active_order_tracker) self.logger().info( f"Initialized order book for {trading_pair}. " f"{index+1}/{number_of_pairs} completed.") await asyncio.sleep(0.9) except Exception: self.logger().error( f"Error getting snapshot for {trading_pair}. ", exc_info=True) await asyncio.sleep(5.0) return retval
async def listen_for_order_book_snapshots(self, ev_loop: asyncio.BaseEventLoop, output: asyncio.Queue): while True: try: trading_pairs: List[str] = await self.get_trading_pairs() client: aiohttp.ClientSession = self.http_client() for trading_pair in trading_pairs: try: snapshot: Dict[str, any] = await self.get_snapshot( client, trading_pair) snapshot_timestamp: float = time.time() snapshot_msg: OrderBookMessage = RadarRelayOrderBook.snapshot_message_from_exchange( snapshot, snapshot_timestamp, metadata={"symbol": trading_pair}) output.put_nowait(snapshot_msg) self.logger().debug( f"Saved order book snapshot for {trading_pair}") await asyncio.sleep(5.0) except asyncio.CancelledError: raise except Exception: self.logger().error("Unexpected error.", exc_info=True) await asyncio.sleep(5.0) 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 asyncio.CancelledError: raise except Exception: self.logger().error("Unexpected error.", exc_info=True) await asyncio.sleep(5.0)