Esempio n. 1
0
 async def listen_for_order_book_diffs(self, ev_loop: asyncio.BaseEventLoop,
                                       output: asyncio.Queue):
     while True:
         try:
             async with websockets.connect(f"{WS_URL}") as ws:
                 ws: websockets.WebSocketClientProtocol = ws
                 for pair in self._trading_pairs:
                     subscribe_request: Dict[str, Any] = {
                         "type": "subscribe",
                         "channel": "orderbook",
                         "id": pair
                     }
                     await ws.send(ujson.dumps(subscribe_request))
                 async for raw_msg in self._inner_messages(ws):
                     msg = ujson.loads(raw_msg)
                     if "contents" in msg:
                         if "updates" in msg["contents"]:
                             ts = datetime.timestamp(datetime.now())
                             for item in msg["contents"]["updates"]:
                                 order_msg: OrderBookMessage = DydxOrderBook.diff_message_from_exchange(
                                     item, ts, msg)
                                 output.put_nowait(order_msg)
                         elif "bids" in msg["contents"]:
                             ts = datetime.timestamp(datetime.now())
                             order_msg: OrderBookMessage = DydxOrderBook.snapshot_message_from_exchange(
                                 msg["contents"], ts, msg)
                             output.put_nowait(order_msg)
         except asyncio.CancelledError:
             raise
         except Exception:
             self.logger().error(
                 "Unexpected error with WebSocket connection. Retrying after 30 seconds...",
                 exc_info=True)
             await asyncio.sleep(30.0)
Esempio n. 2
0
 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, 1000)
         snapshot_timestamp: float = time.time()
         snapshot_msg: OrderBookMessage = DydxOrderBook.snapshot_message_from_exchange(
             snapshot, snapshot_timestamp, metadata={"id": trading_pair})
         order_book: OrderBook = self.order_book_create_function()
         bids, asks = self.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