示例#1
0
    async def listen_for_order_book_diffs(self, ev_loop: asyncio.BaseEventLoop, output: asyncio.Queue):
        while True:
            try:
                ws_message: str = await self.get_ws_subscription_message("book")
                async with websockets.connect(DIFF_STREAM_URL) as ws:
                    ws: websockets.WebSocketClientProtocol = ws
                    await ws.send(ws_message)
                    async for raw_msg in self._inner_messages(ws):
                        msg = ujson.loads(raw_msg)

                        msg_dict = {"trading_pair": convert_from_exchange_trading_pair(msg[-1]),
                                    "asks": msg[1].get("a", []) or msg[1].get("as", []) or [],
                                    "bids": msg[1].get("b", []) or msg[1].get("bs", []) or []}
                        msg_dict["update_id"] = max([*map(lambda x: float(x[2]), msg_dict["bids"] + msg_dict["asks"])],
                                                    default=0.)
                        if "as" in msg[1] and "bs" in msg[1]:
                            order_book_message: OrderBookMessage = KrakenOrderBook.snapshot_ws_message_from_exchange(
                                msg_dict, time.time())
                        else:
                            order_book_message: OrderBookMessage = KrakenOrderBook.diff_message_from_exchange(
                                msg_dict, time.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)
示例#2
0
    async def listen_for_trades(self, ev_loop: asyncio.BaseEventLoop,
                                output: asyncio.Queue):
        while True:
            try:
                ws_message: str = await self.get_ws_subscription_message(
                    "trade")

                async with websockets.connect(DIFF_STREAM_URL) as ws:
                    ws: websockets.WebSocketClientProtocol = ws
                    await ws.send(ws_message)
                    async for raw_msg in self._inner_messages(ws):
                        msg: List[Any] = ujson.loads(raw_msg)
                        trades: List[Dict[str, Any]] = [{
                            "pair":
                            convert_from_exchange_trading_pair(msg[-1]),
                            "trade":
                            trade
                        } for trade in msg[1]]
                        for trade in trades:
                            trade_msg: OrderBookMessage = KrakenOrderBook.trade_message_from_exchange(
                                trade)
                            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.0)
示例#3
0
 async def listen_for_order_book_snapshots(self, ev_loop: asyncio.BaseEventLoop, output: asyncio.Queue):
     while True:
         try:
             async with aiohttp.ClientSession() as client:
                 for trading_pair in self._trading_pairs:
                     try:
                         snapshot: Dict[str, Any] = await self.get_snapshot(client, trading_pair)
                         snapshot_timestamp: float = time.time()
                         snapshot_msg: OrderBookMessage = KrakenOrderBook.snapshot_message_from_exchange(
                             snapshot,
                             snapshot_timestamp,
                             metadata={"trading_pair": 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)
    async def listen_for_trades(self, ev_loop: asyncio.AbstractEventLoop,
                                output: asyncio.Queue):
        while True:
            try:
                ws_message: str = await self.get_ws_subscription_message(
                    "trade")

                async with self._throttler.execute_task(
                        CONSTANTS.WS_CONNECTION_LIMIT_ID):
                    ws = await websockets.connect(CONSTANTS.WS_URL)
                    await ws.send(ws_message)
                    async for raw_msg in self._inner_messages(ws):
                        msg = ujson.loads(raw_msg)
                        trades = [{
                            "pair":
                            convert_from_exchange_trading_pair(msg[-1]),
                            "trade":
                            trade
                        } for trade in msg[1]]
                        for trade in trades:
                            trade_msg: OrderBookMessage = KrakenOrderBook.trade_message_from_exchange(
                                trade)
                            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.0)
示例#5
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 = KrakenOrderBook.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
示例#6
0
    async def listen_for_order_book_diffs(self, ev_loop: asyncio.BaseEventLoop,
                                          output: asyncio.Queue):
        while True:
            try:
                ws_message: str = await self.get_ws_subscription_message("book"
                                                                         )
                async with self._throttler.execute_task(
                        CONSTANTS.WS_CONNECTION_LIMIT_ID):
                    ws: WSAssistant = await self._api_factory.get_ws_assistant(
                    )
                    await ws.connect(ws_url=CONSTANTS.WS_URL,
                                     ping_timeout=self.PING_TIMEOUT)

                    await ws.send(ws_message)
                    async for ws_response in ws.iter_messages():
                        msg = ws_response.data
                        if not (type(msg) is dict and "event" in msg.keys()
                                and msg["event"] in [
                                    "heartbeat", "systemStatus",
                                    "subscriptionStatus"
                                ]):
                            msg_dict = {
                                "trading_pair":
                                convert_from_exchange_trading_pair(msg[-1]),
                                "asks":
                                msg[1].get("a", []) or msg[1].get("as", [])
                                or [],
                                "bids":
                                msg[1].get("b", []) or msg[1].get("bs", [])
                                or []
                            }
                            msg_dict["update_id"] = max([
                                *map(lambda x: float(x[2]),
                                     msg_dict["bids"] + msg_dict["asks"])
                            ],
                                                        default=0.)
                            if "as" in msg[1] and "bs" in msg[1]:
                                order_book_message: OrderBookMessage = (
                                    KrakenOrderBook.
                                    snapshot_ws_message_from_exchange(
                                        msg_dict, time.time()))
                            else:
                                order_book_message: OrderBookMessage = KrakenOrderBook.diff_message_from_exchange(
                                    msg_dict, time.time())
                            output.put_nowait(order_book_message)
                    ws.disconnect()
            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)
示例#7
0
 async def get_new_order_book(self, trading_pair: str) -> OrderBook:
     rest_assistant = await self._get_rest_assistant()
     snapshot: Dict[str, Any] = await self.get_snapshot(
         rest_assistant,
         trading_pair,
         limit=1000,
         throttler=self._throttler)
     snapshot_timestamp: float = time.time()
     snapshot_msg: OrderBookMessage = KrakenOrderBook.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
示例#8
0
    async def listen_for_trades(self, ev_loop: asyncio.AbstractEventLoop,
                                output: asyncio.Queue):
        while True:
            try:
                ws_message: str = await self.get_ws_subscription_message(
                    "trade")

                async with self._throttler.execute_task(
                        CONSTANTS.WS_CONNECTION_LIMIT_ID):
                    ws: WSAssistant = await self._api_factory.get_ws_assistant(
                    )
                    await ws.connect(ws_url=CONSTANTS.WS_URL,
                                     ping_timeout=self.PING_TIMEOUT)

                    await ws.send(ws_message)
                    async for ws_response in ws.iter_messages():
                        msg = ws_response.data
                        if not (type(msg) is dict and "event" in msg.keys()
                                and msg["event"] in [
                                    "heartbeat", "systemStatus",
                                    "subscriptionStatus"
                                ]):
                            trades = [{
                                "pair":
                                convert_from_exchange_trading_pair(msg[-1]),
                                "trade":
                                trade
                            } for trade in msg[1]]
                            for trade in trades:
                                trade_msg: OrderBookMessage = KrakenOrderBook.trade_message_from_exchange(
                                    trade)
                                output.put_nowait(trade_msg)
                    ws.disconnect()
            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)