async def listen_for_order_book_diffs(self, ev_loop: asyncio.BaseEventLoop,
                                          output: asyncio.Queue):
        msg_queue = self._message_queue[MexcWebSocketAdaptor.DEPTH_CHANNEL_ID]
        while True:
            try:
                decoded_msg = await msg_queue.get()
                if decoded_msg['data'].get('asks'):
                    asks = [{
                        'price': ask['p'],
                        'quantity': ask['q']
                    } for ask in decoded_msg["data"]["asks"]]
                    decoded_msg['data']['asks'] = asks
                if decoded_msg['data'].get('bids'):
                    bids = [{
                        'price': bid['p'],
                        'quantity': bid['q']
                    } for bid in decoded_msg["data"]["bids"]]
                    decoded_msg['data']['bids'] = bids
                order_book_message: OrderBookMessage = MexcOrderBook.diff_message_from_exchange(
                    decoded_msg['data'],
                    microseconds(),
                    metadata={
                        "trading_pair":
                        convert_from_exchange_trading_pair(
                            decoded_msg['symbol'])
                    })
                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 self._sleep(30.0)
    async def listen_for_trades(self, ev_loop: asyncio.BaseEventLoop,
                                output: asyncio.Queue):
        msg_queue = self._message_queue[MexcWebSocketAdaptor.DEAL_CHANNEL_ID]
        while True:
            try:
                decoded_msg = await msg_queue.get()
                self.logger().debug(f"Recived new trade: {decoded_msg}")

                for data in decoded_msg['data']['deals']:
                    trading_pair = convert_from_exchange_trading_pair(
                        decoded_msg['symbol'])
                    trade_message: OrderBookMessage = MexcOrderBook.trade_message_from_exchange(
                        data,
                        data['t'],
                        metadata={"trading_pair": trading_pair})
                    self.logger().debug(
                        f'Putting msg in queue: {str(trade_message)}')
                    output.put_nowait(trade_message)
            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)
Exemplo n.º 3
0
    def setUp(self) -> None:
        super().setUp()
        throttler = AsyncThrottler(CONSTANTS.RATE_LIMITS)
        self.tracker: MexcOrderBookTracker = MexcOrderBookTracker(throttler=throttler,
                                                                  trading_pairs=[self.trading_pair])
        self.tracking_task = None

        # Simulate start()
        self.tracker._order_books[self.trading_pair] = MexcOrderBook()
        self.tracker._tracking_message_queues[self.trading_pair] = asyncio.Queue()
        self.tracker._order_books_initialized.set()
    async def get_new_order_book(self, trading_pair: str) -> OrderBook:
        snapshot: Dict[str,
                       Any] = await self.get_snapshot(self._shared_client,
                                                      trading_pair)

        snapshot_msg: OrderBookMessage = MexcOrderBook.snapshot_message_from_exchange(
            snapshot,
            trading_pair,
            timestamp=microseconds(),
            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
Exemplo n.º 5
0
    def test_track_single_book_apply_snapshot(self):
        snapshot_msg = MexcOrderBook.snapshot_message_from_exchange(
            msg=self.content,
            timestamp=1626788175000,
            trading_pair=self.trading_pair
        )
        self.simulate_queue_order_book_messages(snapshot_msg)

        with self.assertRaises(asyncio.TimeoutError):
            # Allow 5 seconds for tracker to process some messages.
            self.tracking_task = self.ev_loop.create_task(asyncio.wait_for(
                self.tracker._track_single_book(self.trading_pair),
                2.0
            ))
            self.async_run_with_timeout(self.tracking_task)

        self.assertEqual(1626788175000000, self.tracker.order_books[self.trading_pair].snapshot_uid)
 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()
             session = self._shared_client
             for trading_pair in trading_pairs:
                 try:
                     snapshot: Dict[str, Any] = await self.get_snapshot(
                         session, trading_pair)
                     snapshot_msg: OrderBookMessage = MexcOrderBook.snapshot_message_from_exchange(
                         snapshot,
                         trading_pair,
                         timestamp=microseconds(),
                         metadata={"trading_pair": trading_pair})
                     output.put_nowait(snapshot_msg)
                     self.logger().debug(
                         f"Saved order book snapshot for {trading_pair}")
                     await self._sleep(5.0)
                 except asyncio.CancelledError:
                     raise
                 except Exception as ex:
                     self.logger().error("Unexpected error." + repr(ex),
                                         exc_info=True)
                     await self._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 self._sleep(delta)
         except asyncio.CancelledError:
             raise
         except Exception as ex1:
             self.logger().error("Unexpected error." + repr(ex1),
                                 exc_info=True)
             await self._sleep(5.0)
 def test_get_order_book_for_valid_trading_pair(self):
     dummy_order_book = MexcOrderBook()
     self.exchange.order_book_tracker.order_books["BTC-USDT"] = dummy_order_book
     self.assertEqual(dummy_order_book, self.exchange.get_order_book("BTC-USDT"))