def get_all_time_low(self, currency: str = "usd") -> pd.DataFrame: """Get all time low data for given coin. [Source: CoinGecko] Returns ------- pandas.DataFrame All time low price data Metric,Value """ market_data = self.coin.get("market_data", {}) if market_data == {}: return pd.DataFrame() ath_columns = [ "current_price", "atl", "atl_date", "atl_change_percentage", ] results = {} for column in ath_columns: results[column] = market_data[column].get(currency) df = pd.Series(results).to_frame().reset_index() df.columns = ["Metric", "Value"] df["Metric"] = df["Metric"].apply( lambda x: lambda_replace_underscores_in_column_names(x) if isinstance(x, str) else x) df["Metric"] = df["Metric"].apply( lambda x: x.replace("Atl", "All Time Low")) df["Metric"] = df["Metric"] + f" {currency.upper()}" return df[df["Value"].notna()]
def get_holdings_overview(endpoint: str = "bitcoin") -> List[Any]: """Returns public companies that holds ethereum or bitcoin [Source: CoinGecko] Parameters ---------- endpoint : str "bitcoin" or "ethereum" Returns ------- List: - str: Overall statistics - pandas.DataFrame: Companies holding crypto """ cg = CoinGeckoAPI() data = cg.get_companies_public_treasury_by_coin_id(coin_id=endpoint) stats_str = f"""{len(data["companies"])} companies hold a total of {lambda_long_number_format_with_type_check(data["total_holdings"])} {endpoint} ({data["market_cap_dominance"]}% of market cap dominance) with the current value of {lambda_long_number_format_with_type_check(int(data["total_value_usd"]))} USD dollars""" # noqa df = pd.json_normalize(data, record_path=["companies"]) df.columns = list( map( lambda x: lambda_replace_underscores_in_column_names(x) if isinstance(x, str) else x, df.columns, )) return [stats_str, df]
def get_base_info(self) -> pd.DataFrame: """Get all the base information about given coin. [Source: CoinGecko] Returns ------- pandas.DataFrame Base information about coin """ regx = r'<a href="(.+?)">|</a>' results = {} for attr in BASE_INFO: info_obj = self.coin.get(attr, {}) if attr == "description": info_obj = info_obj.get("en") info_obj = re.sub(regx, "", info_obj) info_obj = re.sub(r"\r\n\r\n", " ", info_obj) results[attr] = info_obj results.update(self._get_base_market_data_info()) df = pd.Series(results).to_frame().reset_index() df.columns = ["Metric", "Value"] df["Metric"] = df["Metric"].apply( lambda x: lambda_replace_underscores_in_column_names(x) if isinstance(x, str) else x) return df[df["Value"].notna()]
def get_social_media(self) -> pd.DataFrame: """Get list of URLs to social media like twitter, facebook, reddit... [Source: CoinGecko] Returns ------- pandas.DataFrame Urls to social media Columns: Metric, Value """ social_dct = {} links = self._get_links() for (channel) in CHANNELS.keys(): # pylint: disable=consider-iterating-dictionary) if channel in links: value = links.get(channel, "") if channel == "twitter_screen_name": value = "https://twitter.com/" + value elif channel == "bitcointalk_thread_identifier" and value is not None: value = f"https://bitcointalk.org/index.php?topic={value}" social_dct[channel] = value social_dct["discord"] = find_discord(links.get("chat_url")) dct = rename_columns_in_dct(social_dct, CHANNELS) df = pd.Series(dct).to_frame().reset_index() df.columns = ["Metric", "Value"] df["Metric"] = df["Metric"].apply( lambda x: lambda_replace_underscores_in_column_names(x) if isinstance(x, str) else x) return df[df["Value"].notna()]
def get_global_info() -> pd.DataFrame: """Get global statistics about crypto from CoinGecko API like: - market cap change - number of markets - icos - number of active crypto [Source: CoinGecko] Returns ------- pandas.DataFrame Metric, Value """ client = CoinGeckoAPI() results = client.get_global() total_mcap = results.pop("market_cap_percentage") btc, eth = total_mcap.get("btc"), total_mcap.get("eth") for key in ["total_market_cap", "total_volume", "updated_at"]: del results[key] results["btc_market_cap_in_pct"] = btc results["eth_market_cap_in_pct"] = eth results["altcoin_market_cap_in_pct"] = 100 - (float(eth) + float(btc)) df = pd.Series(results).reset_index() df.columns = ["Metric", "Value"] df["Metric"] = df["Metric"].apply( lambda x: lambda_replace_underscores_in_column_names(x) if isinstance(x, str) else x) return df
def get_market_data(self) -> pd.DataFrame: """Get all the base market information about given coin. [Source: CoinGecko] Returns ------- pandas.DataFrame Base market information about coin Metric,Value """ market_data = self.coin.get("market_data", {}) market_columns_denominated = [ "market_cap", "fully_diluted_valuation", "total_volume", "high_24h", "low_24h", ] denominated_data = create_dictionary_with_prefixes( market_columns_denominated, market_data, DENOMINATION) market_single_columns = [ "market_cap_rank", "total_supply", "max_supply", "circulating_supply", "price_change_percentage_24h", "price_change_percentage_7d", "price_change_percentage_30d", "price_change_percentage_60d", "price_change_percentage_1y", "market_cap_change_24h", ] single_stats = {} for col in market_single_columns: single_stats[col] = market_data.get(col) single_stats.update(denominated_data) try: single_stats["circulating_supply_to_total_supply_ratio"] = ( single_stats["circulating_supply"] / single_stats["total_supply"]) except (ZeroDivisionError, TypeError) as e: logger.exception(str(e)) console.print(e) df = pd.Series(single_stats).to_frame().reset_index() df.columns = ["Metric", "Value"] df["Metric"] = df["Metric"].apply( lambda x: lambda_replace_underscores_in_column_names(x) if isinstance(x, str) else x) return df[df["Value"].notna()]
def get_blockchain_explorers(self) -> Union[pd.DataFrame, Any]: """Get list of URLs to blockchain explorers for given coin. [Source: CoinGecko] Returns ------- pandas.DataFrame Blockchain Explorers Columns: Metric, Value """ blockchain = self._get_links().get("blockchain_site") if blockchain: dct = filter_list(blockchain) df = pd.Series(dct).to_frame().reset_index() df.columns = ["Metric", "Value"] df["Metric"] = df["Metric"].apply( lambda x: lambda_replace_underscores_in_column_names(x) if isinstance(x, str) else x) return df[df["Value"].notna()] return None
def get_scores(self) -> pd.DataFrame: """Get different kind of scores for given coin. [Source: CoinGecko] Returns ------- pandas.DataFrame Social, community, sentiment scores for coin Metric,Value """ score_columns = [ "coingecko_rank", "coingecko_score", "developer_score", "community_score", "liquidity_score", "sentiment_votes_up_percentage", "sentiment_votes_down_percentage", "public_interest_score", "community_data", "public_interest_stats", ] single_stats = {col: self.coin.get(col) for col in score_columns[:-2]} nested_stats = {} for col in score_columns[-2:]: _dct = self.coin.get(col, {}) for k, _ in _dct.items(): nested_stats[k] = _dct.get(k, {}) single_stats.update(nested_stats) df = pd.Series(single_stats).reset_index() df.replace({0: ""}, inplace=True) df = df.fillna("") df.columns = ["Metric", "Value"] # pylint: disable=unsupported-assignment-operation df["Metric"] = df["Metric"].apply( lambda x: lambda_replace_underscores_in_column_names(x) if isinstance(x, str) else x) return df[df["Value"].notna()]
def get_websites(self) -> pd.DataFrame: """Get list of URLs to websites like homepage of coin, forum. [Source: CoinGecko] Returns ------- pandas.DataFrame Urls to website, homepage, forum Columns: Metric, Value """ websites_dct = {} links = self._get_links() sites = ["homepage", "official_forum_url", "announcement_url"] for site in sites: websites_dct[site] = filter_list(links.get(site)) df = pd.Series(websites_dct).to_frame().reset_index() df.columns = ["Metric", "Value"] df["Value"] = df["Value"].apply(lambda x: ",".join(x)) df["Metric"] = df["Metric"].apply( lambda x: lambda_replace_underscores_in_column_names(x) if isinstance(x, str) else x) return df[df["Value"].notna()]
def get_global_defi_info() -> pd.DataFrame: """Get global statistics about Decentralized Finances [Source: CoinGecko] Returns ------- pandas.DataFrame Metric, Value """ client = CoinGeckoAPI() results = client.get_global_decentralized_finance_defi() for key, value in results.items(): try: results[key] = round(float(value), 4) except (ValueError, TypeError): pass df = pd.Series(results).reset_index() df.columns = ["Metric", "Value"] df["Metric"] = df["Metric"].apply( lambda x: lambda_replace_underscores_in_column_names(x) if isinstance(x, str) else x) return df
def get_top_crypto_categories( sort_filter: str = SORT_VALUES[0]) -> pd.DataFrame: """Returns top crypto categories [Source: CoinGecko] Returns ------- pandas.DataFrame Rank, Name, Change_1h, Change_7d, Market_Cap, Volume_24h,Coins, Url """ if sort_filter in SORT_VALUES: client = CoinGeckoAPI() data = client.get_coins_categories() df = pd.DataFrame(data) del df["id"] del df["content"] del df["updated_at"] df["top_3_coins"] = df["top_3_coins"].apply(lambda_coin_formatter) df.columns = [ lambda_replace_underscores_in_column_names(col) if isinstance( col, str) else col for col in df.columns ] return df return pd.DataFrame()
def get_developers_data(self) -> pd.DataFrame: """Get coin development data from GitHub or BitBucket like: number of pull requests, contributor etc [Source: CoinGecko] Returns ------- pandas.DataFrame Developers Data Columns: Metric, Value """ dev = self.coin.get("developer_data", {}) useless_keys = ( "code_additions_deletions_4_weeks", "last_4_weeks_commit_activity_series", ) remove_keys(useless_keys, dev) df = pd.Series(dev).to_frame().reset_index() df.columns = ["Metric", "Value"] df["Metric"] = df["Metric"].apply( lambda x: lambda_replace_underscores_in_column_names(x) if isinstance(x, str) else x) return df[df["Value"].notna()]
def display_defi_protocols(top: int, sortby: str, descend: bool, description: bool, export: str = "") -> None: """Display information about listed DeFi protocols, their current TVL and changes to it in the last hour/day/week. [Source: https://docs.llama.fi/api] Parameters ---------- top: int Number of records to display sortby: str Key by which to sort data descend: bool Flag to sort data descending description: bool Flag to display description of protocol export : str Export dataframe data to csv,json,xlsx file """ df = llama_model.get_defi_protocols() df_data = df.copy() df = df.sort_values(by=sortby, ascending=descend) df = df.drop(columns="chain") df["tvl"] = df["tvl"].apply(lambda x: lambda_long_number_format(x)) if not description: df.drop(["description", "url"], axis=1, inplace=True) else: df = df[[ "name", "symbol", "category", "description", "url", ]] df.columns = [ lambda_replace_underscores_in_column_names(val) for val in df.columns ] df.rename( columns={ "Change 1H": "Change 1H (%)", "Change 1D": "Change 1D (%)", "Change 7D": "Change 7D (%)", "Tvl": "TVL ($)", }, inplace=True, ) print_rich_table(df.head(top), headers=list(df.columns), show_index=False) console.print("") export_data( export, os.path.dirname(os.path.abspath(__file__)), "ldapps", df_data, )