async def get_open_orders(self) -> List[OpenOrder]: result = await self._api_request( "post", CONSTANTS.GET_OPEN_ORDERS_PATH_URL, {}, True ) ret_val = [] for order in result["result"]["order_list"]: if crypto_com_utils.HBOT_BROKER_ID not in order["client_oid"]: continue if order["type"] != "LIMIT": raise Exception(f"Unsupported order type {order['type']}") ret_val.append( OpenOrder( client_order_id=order["client_oid"], trading_pair=crypto_com_utils.convert_from_exchange_trading_pair(order["instrument_name"]), price=Decimal(str(order["price"])), amount=Decimal(str(order["quantity"])), executed_amount=Decimal(str(order["cumulative_quantity"])), status=order["status"], order_type=OrderType.LIMIT, is_buy=True if order["side"].lower() == "buy" else False, time=int(order["create_time"]), exchange_order_id=order["order_id"] ) ) return ret_val
async def listen_for_order_book_diffs(self, ev_loop: asyncio.BaseEventLoop, output: asyncio.Queue): """ Listen for orderbook diffs using websocket book channel """ msg_queue = self._message_queue[CryptoComWebsocket.DIFF_CHANNEL_ID] while True: try: payload = await msg_queue.get() trading_pair = crypto_com_utils.convert_from_exchange_trading_pair( payload["instrument_name"]) order_book_data = payload["data"][0] timestamp: float = order_book_data["t"] # data in this channel is not order book diff but the entire order book (up to depth 150). # so we need to convert it into a order book snapshot. # Crypto.com does not offer order book diff ws updates. orderbook_msg: OrderBookMessage = CryptoComOrderBook.snapshot_message_from_exchange( order_book_data, timestamp, metadata={"trading_pair": trading_pair}, ) output.put_nowait(orderbook_msg) except asyncio.CancelledError: raise except Exception: self.logger().error( f"Unexpected error parsing order book diff payload. Payload: {payload}", exc_info=True, ) await self._sleep(30.0)
def _format_trading_rules( self, instruments_info: Dict[str, Any]) -> Dict[str, TradingRule]: """ Converts json API response into a dictionary of trading rules. :param instruments_info: The json API response :return A dictionary of trading rules. Response Example: { "id": 11, "method": "public/get-instruments", "code": 0, "result": { "instruments": [ { "instrument_name": "ETH_CRO", "quote_currency": "CRO", "base_currency": "ETH", "price_decimals": 2, "quantity_decimals": 2 }, { "instrument_name": "CRO_BTC", "quote_currency": "BTC", "base_currency": "CRO", "price_decimals": 8, "quantity_decimals": 2 } ] } } """ result = {} for rule in instruments_info["result"]["instruments"]: try: trading_pair = crypto_com_utils.convert_from_exchange_trading_pair( rule["instrument_name"]) price_decimals = Decimal(str(rule["price_decimals"])) quantity_decimals = Decimal(str(rule["quantity_decimals"])) # E.g. a price decimal of 2 means 0.01 incremental. price_step = Decimal("1") / Decimal( str(math.pow(10, price_decimals))) quantity_step = Decimal("1") / Decimal( str(math.pow(10, quantity_decimals))) result[trading_pair] = TradingRule( trading_pair, min_price_increment=price_step, min_base_amount_increment=quantity_step) except Exception: self.logger().error( f"Error parsing the trading pair rule {rule}. Skipping.", exc_info=True) return result
async def fetch_trading_pairs() -> List[str]: async with aiohttp.ClientSession() as client: async with client.get(f"{constants.REST_URL}/public/get-ticker", timeout=10) as response: if response.status == 200: from hummingbot.connector.exchange.crypto_com.crypto_com_utils import \ convert_from_exchange_trading_pair try: data: Dict[str, Any] = await response.json() return [ convert_from_exchange_trading_pair(item["i"]) for item in data["result"]["data"] ] except Exception: pass # Do nothing if the request fails -- there will be no autocomplete for kucoin trading pairs return []