async def listen_for_trades(self, ev_loop: asyncio.BaseEventLoop,
                                output: asyncio.Queue):
        ws = None
        while True:
            try:
                ws = await self._create_websocket_connection()
                payload = {
                    "method":
                    "SUBSCRIBE",
                    "params": [
                        f"{utils.convert_to_exchange_trading_pair(trading_pair).lower()}@aggTrade"
                        for trading_pair in self._trading_pairs
                    ],
                    "id":
                    self.TRADE_STREAM_ID
                }
                await ws.send_json(payload)

                async for json_msg in self._iter_messages(ws):
                    if "result" in json_msg:
                        continue
                    trade_msg: OrderBookMessage = BinancePerpetualOrderBook.trade_message_from_exchange(
                        json_msg)
                    output.put_nowait(trade_msg)
            except asyncio.CancelledError:
                raise
            except Exception:
                self.logger().error(
                    "Unexpected error with Websocket connection. Retrying after 30 seconds...",
                    exc_info=True)
                await self._sleep(30.0)
            finally:
                ws and await ws.close()
 async def listen_for_trades(self, ev_loop: asyncio.BaseEventLoop,
                             output: asyncio.Queue):
     while True:
         try:
             ws_subscription_path: str = "/".join([
                 f"{convert_to_exchange_trading_pair(trading_pair).lower()}@aggTrade"
                 for trading_pair in self._trading_pairs
             ])
             stream_url = f"{utils.wss_url(endpoint=CONSTANTS.PUBLIC_WS_ENDPOINT)}?streams={ws_subscription_path}"
             ws: websockets.WebSocketClientProtocol = await websockets.connect(
                 stream_url)
             async for raw_msg in self.ws_messages(ws):
                 msg_json = ujson.loads(raw_msg)
                 trade_msg: OrderBookMessage = BinancePerpetualOrderBook.trade_message_from_exchange(
                     msg_json)
                 output.put_nowait(trade_msg)
         except asyncio.CancelledError:
             raise
         except Exception:
             self.logger().error(
                 "Unexpected error with Websocket connection. Retrying after 30 seconds...",
                 exc_info=True)
             await self._sleep(30.0)
         finally:
             await ws.close()
Exemplo n.º 3
0
 async def listen_for_trades(self, ev_loop: asyncio.BaseEventLoop, output: asyncio.Queue):
     while True:
         msg = await self._message_queue[CONSTANTS.TRADE_STREAM_ID].get()
         msg.data["data"]["s"] = await self.convert_from_exchange_trading_pair(
             exchange_trading_pair=msg.data["data"]["s"],
             domain=self._domain,
             throttler=self._throttler)
         trade_message: OrderBookMessage = BinancePerpetualOrderBook.trade_message_from_exchange(msg.data)
         output.put_nowait(trade_message)
Exemplo n.º 4
0
 async def listen_for_trades(self, ev_loop: asyncio.BaseEventLoop,
                             output: asyncio.Queue):
     while True:
         try:
             # trading_pairs: List[str] = await self.get_trading_pairs()
             ws_subscription_path: str = "/".join([
                 f"{convert_to_exchange_trading_pair(trading_pair).lower()}@aggTrade"
                 for trading_pair in self._trading_pairs
             ])
             stream_url = f"{self._stream_url}?streams={ws_subscription_path}"
             async with websockets.connect(stream_url) as ws:
                 ws: websockets.WebSocketClientProtocol = ws
                 async for raw_msg in self.ws_messages(ws):
                     msg_json = ujson.loads(raw_msg)
                     trade_msg: OrderBookMessage = BinancePerpetualOrderBook.trade_message_from_exchange(
                         msg_json)
                     output.put_nowait(trade_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)