Esempio n. 1
0
 async def listen_for_order_book_snapshots(self,
                                           ev_loop: asyncio.BaseEventLoop,
                                           output: asyncio.Queue):
     while True:
         try:
             async with websockets.connect(FTX_WS_FEED) as ws:
                 ws: websockets.WebSocketClientProtocol = ws
                 for pair in self._trading_pairs:
                     subscribe_request: Dict[str, Any] = {
                         "op": "subscribe",
                         "channel": "orderbook",
                         "market": convert_to_exchange_trading_pair(pair)
                     }
                     await ws.send(ujson.dumps(subscribe_request))
                 async for raw_msg in self._inner_messages(ws):
                     msg = simplejson.loads(raw_msg, parse_float=Decimal)
                     if "channel" in msg:
                         if msg["channel"] == "orderbook" and msg[
                                 "type"] == "partial":
                             order_book_message: OrderBookMessage = FtxOrderBook.snapshot_message_from_exchange(
                                 msg, msg["data"]["time"])
                             output.put_nowait(order_book_message)
         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 = FtxOrderBook.restful_snapshot_message_from_exchange(
             snapshot,
             snapshot_timestamp,
             metadata={"trading_pair": trading_pair})
         order_book: OrderBook = self.order_book_create_function()
         order_book.apply_snapshot(snapshot_msg.bids, snapshot_msg.asks,
                                   snapshot_msg.update_id)
         return order_book