def _format_trading_rules( self, raw_trading_pair_info: List[Dict[str, Any]]) -> List[TradingRule]: trading_rules = [] for info in raw_trading_pair_info: try: trading_rules.append( TradingRule( trading_pair=convert_from_exchange_trading_pair( info['symbol']), # min_order_size=Decimal(info["min_amount"]), # max_order_size=Decimal(info["max_amount"]), min_price_increment=Decimal( num_to_increment(info["price_scale"])), min_base_amount_increment=Decimal( num_to_increment(info["quantity_scale"])), # min_quote_amount_increment=Decimal(info["1e-{info['value-precision']}"]), # min_notional_size=Decimal(info["min-order-value"]) min_notional_size=Decimal(info["min_amount"]), # max_notional_size=Decimal(info["max_amount"]), )) except Exception: self.logger().error( f"Error parsing the trading pair rule {info}. Skipping.", exc_info=True) return trading_rules
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)
async def fetch_trading_pairs() -> List[str]: async with aiohttp.ClientSession() as client: throttler = MexcAPIOrderBookDataSource._get_throttler_instance() async with throttler.execute_task(CONSTANTS.MEXC_SYMBOL_URL): url = CONSTANTS.MEXC_BASE_URL + CONSTANTS.MEXC_SYMBOL_URL async with client.get(url) as products_response: products_response: aiohttp.ClientResponse = products_response if products_response.status != 200: return [] # raise IOError(f"Error fetching active MEXC. HTTP status is {products_response.status}.") data = await products_response.json() data = data['data'] trading_pairs = [] for item in data: if item['state'] == "ENABLED": trading_pairs.append( convert_from_exchange_trading_pair( item["symbol"])) return trading_pairs