async def fetch_trading_pairs() -> List[str]: try: async with aiohttp.ClientSession() as client: async with client.get( "https://api-pub.bitfinex.com/v2/conf/pub:list:pair:exchange", timeout=10) as response: if response.status == 200: data = await response.json() trading_pair_list: List[str] = [] for trading_pair in data[0]: # change the following line accordingly converted_trading_pair: Optional[str] = \ convert_from_exchange_trading_pair(trading_pair) if converted_trading_pair is not None: trading_pair_list.append( converted_trading_pair) else: logging.getLogger(__name__).info( f"Could not parse the trading pair " f"{trading_pair}, skipping it...") return trading_pair_list except Exception: # Do nothing if the request fails -- there will be no autocomplete available pass return []
def itemToTicker(item: Any) -> Ticker: try: item[0] = convert_from_exchange_trading_pair(item[0]) return Ticker(*item) except Exception: return None
async def get_active_exchange_markets(cls) -> pd.DataFrame: async with aiohttp.ClientSession() as client: tickers_response, exchange_conf_response, symbol_details_response = await safe_gather( client.get(f"{BITFINEX_REST_URL}/tickers?symbols=ALL"), client.get(f"{BITFINEX_REST_URL}/conf/pub:info:pair"), client.get(f"{BITFINEX_REST_URL_V1}/symbols_details"), ) tickers_response: aiohttp.ClientResponse = tickers_response exchange_conf_response: aiohttp.ClientResponse = exchange_conf_response symbol_details_response: aiohttp.ClientResponse = symbol_details_response if tickers_response.status != 200: raise IOError(f"Error fetching Bitfinex markets information. " f"HTTP status is {tickers_response.status}.") if exchange_conf_response.status != 200: raise IOError( f"Error fetching Bitfinex exchange information. " f"HTTP status is {exchange_conf_response.status}.") if symbol_details_response.status != 200: raise IOError( f"Error fetching Bitfinex symbol details. " f"HTTP status is {symbol_details_response.status}.") tickers_raw: List[Any] = await tickers_response.json() exchange_confs_raw: List[Any] = await exchange_conf_response.json() symbol_details_raw: List[Any] = await symbol_details_response.json( ) def itemToTicker(item: Any) -> Ticker: try: item[0] = convert_from_exchange_trading_pair(item[0]) return Ticker(*item) except Exception: return None tickers: List[Ticker] = list( filter( lambda ticker: ticker is not None, map( itemToTicker, filter( lambda item: item[0].startswith("t") and item[0]. isalpha() and item[0][1].isupper(), tickers_raw)))) exchange_confs = dict((convert_from_exchange_trading_pair(item[0]), ConfStructure._make(item[1])) for item in filter( lambda item: item[0].isalpha() and valid_exchange_trading_pair(item[0]), exchange_confs_raw[0])) symbol_details = dict( (convert_from_exchange_trading_pair(item["pair"].upper()), item) for item in filter( lambda item: item["pair"].isalpha( ) and valid_exchange_trading_pair(item["pair"].upper()), symbol_details_raw)) def getTickerPrices(ticker: Ticker) -> Dict[Any, Any]: base, quote = split_trading_pair(ticker.tradingPair) return { "symbol": ticker.tradingPair, "baseAsset": base, "base_increment": get_precision( symbol_details[ticker.tradingPair]["price_precision"]), "base_max_size": exchange_confs[ticker.tradingPair].max, "base_min_size": exchange_confs[ticker.tradingPair].min, "display_name": ticker.tradingPair, "quoteAsset": quote, "quote_increment": get_precision( symbol_details[ticker.tradingPair]["price_precision"]), "volume": ticker.volume, "price": ticker.last_price, } raw_prices = { ticker.tradingPair: getTickerPrices(ticker) for ticker in tickers } prices = cls._convert_volume(raw_prices) all_markets: pd.DataFrame = pd.DataFrame.from_records( data=prices, index="symbol") return all_markets.sort_values("USDVolume", ascending=False)