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()
             async with aiohttp.ClientSession() as client:
                 for trading_pair in trading_pairs:
                     try:
                         snapshot: Dict[str, Any] = await self.get_snapshot(
                             client, trading_pair)
                         snapshot_message: OrderBookMessage = HuobiOrderBook.snapshot_message_from_exchange(
                             snapshot, metadata={"symbol": trading_pair})
                         output.put_nowait(snapshot_message)
                         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)
    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_msg: OrderBookMessage = HuobiOrderBook.snapshot_message_from_exchange(
                        snapshot, metadata={"symbol": trading_pair})
                    order_book: OrderBook = self.order_book_create_function()
                    order_book.apply_snapshot(snapshot_msg.bids,
                                              snapshot_msg.asks,
                                              snapshot_msg.update_id)
                    retval[trading_pair] = OrderBookTrackerEntry(
                        trading_pair, snapshot_msg.timestamp, order_book)
                    self.logger().info(
                        f"Initialized order book for {trading_pair}. "
                        f"{index + 1}/{number_of_pairs} completed.")
                    # Huobi rate limit is 100 https requests per 10 seconds
                    await asyncio.sleep(0.4)
                except Exception:
                    self.logger().error(
                        f"Error getting snapshot for {trading_pair}. ",
                        exc_info=True)
                    await asyncio.sleep(5)
            return retval
Beispiel #3
0
    async def listen_for_order_book_diffs(self, ev_loop: asyncio.BaseEventLoop, output: asyncio.Queue):
        while True:
            try:
                trading_pairs: List[str] = self._trading_pairs
                async with websockets.connect(HUOBI_WS_URI) as ws:
                    ws: websockets.WebSocketClientProtocol = ws
                    for trading_pair in trading_pairs:
                        subscribe_request: Dict[str, Any] = {
                            "sub": f"market.{trading_pair}.depth.step0",
                            "id": trading_pair
                        }
                        await ws.send(json.dumps(subscribe_request))

                    async for raw_msg in self._inner_messages(ws):
                        # Huobi compresses their ws data
                        encoded_msg: bytes = gzip.decompress(raw_msg)
                        # Huobi's data value for id is a large int too big for ujson to parse
                        msg: Dict[str, Any] = json.loads(encoded_msg.decode('utf-8'))
                        if "ping" in msg:
                            await ws.send(f'{{"op":"pong","ts": {str(msg["ping"])}}}')
                        elif "subbed" in msg:
                            pass
                        elif "ch" in msg:
                            order_book_message: OrderBookMessage = HuobiOrderBook.diff_message_from_exchange(msg)
                            output.put_nowait(order_book_message)
                        else:
                            self.logger().debug(f"Unrecognized message received from Huobi websocket: {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)
Beispiel #4
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)
         snapshot_msg: OrderBookMessage = HuobiOrderBook.snapshot_message_from_exchange(
             snapshot,
             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