Example #1
0
    async def listen_for_order_book_diffs(self, ev_loop: asyncio.AbstractEventLoop, output: asyncio.Queue):
        """
        Listen for orderbook diffs using websocket book channel
        """
        msg_queue = self._message_queue[self.DIFF_FILTER_ID]
        msg = None
        while True:
            try:
                msg = await msg_queue.get()
                msg_timestamp = int(time.time() * 1e3)

                if "reset" in msg and msg["reset"] is True:
                    # First response from websocket is a snapshot. This is only when reset = True
                    snapshot_msg: OrderBookMessage = ProbitOrderBook.snapshot_message_from_exchange(
                        msg=msg,
                        timestamp=msg_timestamp,
                    )
                    output.put_nowait(snapshot_msg)
                else:
                    diff_msg: OrderBookMessage = ProbitOrderBook.diff_message_from_exchange(
                        msg=msg,
                        timestamp=msg_timestamp,
                        metadata={"market_id": msg["market_id"]}
                    )
                    output.put_nowait(diff_msg)
            except asyncio.CancelledError:
                raise
            except Exception:
                self.logger().error(
                    f"Error while parsing OrderBookMessage from ws message {msg}", exc_info=True
                )
 async def listen_for_order_book_diffs(self, ev_loop: asyncio.BaseEventLoop,
                                       output: asyncio.Queue):
     """
     Listen for orderbook diffs using websocket book channel
     """
     while True:
         try:
             async with websockets.connect(
                     uri=CONSTANTS.WSS_URL.format(self._domain)) as ws:
                 ws: websockets.WebSocketClientProtocol = ws
                 for trading_pair in self._trading_pairs:
                     params: Dict[str, Any] = {
                         "channel": "marketdata",
                         "filter": ["order_books"],
                         "interval": 100,
                         "market_id": trading_pair,
                         "type": "subscribe"
                     }
                     await ws.send(ujson.dumps(params))
                 async for raw_msg in self._inner_messages(ws):
                     msg_timestamp: int = int(time.time() * 1e3)
                     msg: Dict[str, Any] = ujson.loads(raw_msg)
                     if "order_books" not in msg:
                         # Unrecognized response from "order_books" channel
                         continue
                     if "reset" in msg and msg["reset"] is True:
                         # First response from websocket is a snapshot. This is only when reset = True
                         snapshot_msg: OrderBookMessage = ProbitOrderBook.snapshot_message_from_exchange(
                             msg=msg,
                             timestamp=msg_timestamp,
                         )
                         output.put_nowait(snapshot_msg)
                     else:
                         diff_msg: OrderBookMessage = ProbitOrderBook.diff_message_from_exchange(
                             msg=msg,
                             timestamp=msg_timestamp,
                             metadata={"market_id": msg["market_id"]})
                         output.put_nowait(diff_msg)
         except asyncio.CancelledError:
             raise
         except Exception:
             self.logger().network(
                 "Unexpected error with WebSocket connection.",
                 exc_info=True,
                 app_warning_msg=
                 "Unexpected error with WebSocket connection. Retrying in 30 seconds. "
                 "Check network connection.")
             await asyncio.sleep(30.0)
         finally:
             await ws.close()
Example #3
0
 async def get_new_order_book(self, trading_pair: str) -> OrderBook:
     snapshot: Dict[str, Any] = await self.get_order_book_data(trading_pair)
     snapshot_timestamp: int = int(time.time() * 1e3)
     snapshot_msg: OrderBookMessage = ProbitOrderBook.snapshot_message_from_exchange(
         snapshot,
         snapshot_timestamp,
         metadata={"trading_pair": trading_pair})
     order_book = self.order_book_create_function()
     bids, asks = probit_utils.convert_snapshot_message_to_order_book_row(
         snapshot_msg)
     order_book.apply_snapshot(bids, asks, snapshot_msg.update_id)
     return order_book
 async def listen_for_order_book_snapshots(self,
                                           ev_loop: asyncio.BaseEventLoop,
                                           output: asyncio.Queue):
     """
     Listen for orderbook snapshots by fetching orderbook
     """
     while True:
         try:
             for trading_pair in self._trading_pairs:
                 try:
                     snapshot: Dict[str,
                                    any] = await self.get_order_book_data(
                                        trading_pair, domain=self._domain)
                     snapshot_timestamp: int = int(time.time() * 1e3)
                     snapshot_msg: OrderBookMessage = ProbitOrderBook.snapshot_message_from_exchange(
                         msg=snapshot,
                         timestamp=snapshot_timestamp,
                         metadata={
                             "market_id": trading_pair
                         }  # Manually insert trading_pair here since API response does include trading pair
                     )
                     output.put_nowait(snapshot_msg)
                     self.logger().debug(
                         f"Saved order book snapshot for {trading_pair}")
                     # Be careful not to go above API rate limits.
                     await asyncio.sleep(5.0)
                 except asyncio.CancelledError:
                     raise
                 except Exception:
                     self.logger().network(
                         "Unexpected error with WebSocket connection.",
                         exc_info=True,
                         app_warning_msg=
                         "Unexpected error with WebSocket connection. Retrying in 5 seconds. "
                         "Check network connection.")
                     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.BaseEventLoop,
                                output: asyncio.Queue):
        """
        Listen for trades using websocket trade channel
        """
        while True:
            try:
                async with websockets.connect(
                        uri=CONSTANTS.WSS_URL.format(self._domain)) as ws:
                    for trading_pair in self._trading_pairs:
                        params: Dict[str, Any] = {
                            "channel": "marketdata",
                            "filter": ["recent_trades"],
                            "interval": 100,
                            "market_id": trading_pair,
                            "type": "subscribe"
                        }
                        await ws.send(ujson.dumps(params))
                    async for raw_msg in self._inner_messages(ws):
                        msg_timestamp: int = int(time.time() * 1e3)
                        msg = ujson.loads(raw_msg)
                        if "recent_trades" not in msg:
                            # Unrecognized response from "recent_trades" channel
                            continue

                        if "reset" in msg and msg["reset"] is True:
                            # Ignores first response from "recent_trades" channel. This response details the last 100 trades.
                            continue
                        for trade_entry in msg["recent_trades"]:
                            trade_msg: OrderBookMessage = ProbitOrderBook.trade_message_from_exchange(
                                msg=trade_entry,
                                timestamp=msg_timestamp,
                                metadata={"market_id": msg["market_id"]})
                            output.put_nowait(trade_msg)
            except asyncio.CancelledError:
                raise
            except Exception:
                self.logger().error("Unexpected error.", exc_info=True)
                await asyncio.sleep(5.0)
            finally:
                await ws.close()
Example #6
0
    async def listen_for_trades(self, ev_loop: asyncio.AbstractEventLoop, output: asyncio.Queue):
        """
        Listen for trades using websocket trade channel
        """
        msg_queue = self._message_queue[self.TRADE_FILTER_ID]
        while True:
            try:
                msg = await msg_queue.get()
                msg_timestamp: int = int(time.time() * 1e3)

                if "reset" in msg and msg["reset"] is True:
                    # Ignores first response from "recent_trades" channel. This response details the last 100 trades.
                    continue
                for trade_entry in msg["recent_trades"]:
                    trade_msg: OrderBookMessage = ProbitOrderBook.trade_message_from_exchange(
                        msg=trade_entry,
                        timestamp=msg_timestamp,
                        metadata={"market_id": msg["market_id"]})
                    output.put_nowait(trade_msg)
            except asyncio.CancelledError:
                raise
            except Exception:
                self.logger().error("Unexpected error.", exc_info=True)