def display_losers(period: str, top: int, sortby: str, descend: bool, links: bool, export: str) -> None: """Shows Largest Losers - coins which lost the most in given period of time. [Source: CoinGecko] Parameters ---------- period: str Time period by which data is displayed. One from [1h, 24h, 7d, 14d, 30d, 60d, 1y] top: int Number of records to display sortby: str Key by which to sort data descend: bool Flag to sort data descending links: bool Flag to display urls export : str Export dataframe data to csv,json,xlsx file """ if sortby == "Change": sortby = f"%Change_{period}" df = pycoingecko_model.get_gainers_or_losers(period=period, typ="losers") if not df.empty: df = df.sort_values(by=sortby, ascending=descend) df_data = df.copy() if not links: df.drop("Url", axis=1, inplace=True) if gtff.USE_TABULATE_DF: print( tabulate( df.head(top), headers=df.columns, floatfmt=".4f", showindex=False, tablefmt="fancy_grid", ), "\n", ) else: print(df.to_string, "\n") export_data( export, os.path.dirname(os.path.abspath(__file__)), "losers", df_data, ) else: print("") print("Unable to retrieve data from CoinGecko.") print("")
def display_gainers(period: str = "1h", top: int = 20, sortby: str = "Symbol", export: str = "") -> None: """Shows Largest Gainers - coins which gain the most in given period. [Source: CoinGecko] Parameters ---------- period: str Time period by which data is displayed. One from [1h, 24h, 7d, 14d, 30d, 60d, 1y] top: int Number of records to display sortby: str Key to sort data export : str Export dataframe data to csv,json,xlsx file """ df = pycoingecko_model.get_gainers_or_losers(top=top, period=period, typ="gainers") if not df.empty: if sortby in COINS_COLUMNS: df = df[(df["Volume [$]"].notna()) & (df["Market Cap [$]"].notna())].sort_values( by=sortby, ascending=False) for col in ["Volume [$]", "Market Cap [$]"]: if col in df.columns: df[col] = df[col].apply( lambda x: lambda_very_long_number_formatter(x)) 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__)), "gainers", df, ) else: console.print("\nUnable to retrieve data from CoinGecko.\n")
def gainers(other_args: List[str]): """Shows Largest Gainers - coins which gain the most in given period from www.coingecko.com Parameters ---------- other_args: List[str] Arguments to pass to argparse """ parser = argparse.ArgumentParser( prog="gainers", add_help=False, formatter_class=argparse.ArgumentDefaultsHelpFormatter, description=""" Shows Largest Gainers - coins which gain the most in given period. You can use parameter --period to set which timeframe are you interested in. eg. 1h, 24h, 7d, 14d, 30d, 60d, 1y You can look on only top N number of records with --top, You can sort by Rank, Symbol, Name, Volume, Price, Change with --sort and also with --descend flag to set it to sort descending. There is --links flag, which will display one additional column you all urls for coins. """, ) parser.add_argument( "-p", "--period", dest="period", type=str, help="time period, one from [1h, 24h, 7d, 14d, 30d, 60d, 1y]", default="1h", choices=["1h", "24h", "7d", "14d", "30d", "60d", "1y"], ) parser.add_argument( "-t", "--top", dest="top", type=int, help="top N number records", default=15, ) parser.add_argument( "-s", "--sort", dest="sortby", type=str, help="Sort by given column. Default: Rank", default="Rank", choices=["Rank", "Symbol", "Name", "Volume", "Price", "Change"], ) parser.add_argument( "--descend", action="store_false", help="Flag to sort in descending order (lowest first)", dest="descend", default=True, ) parser.add_argument( "-l", "--links", dest="links", action="store_true", help="Flag to show urls. If you will use that flag you will additional column with urls", default=False, ) try: ns_parser = parse_known_args_and_warn(parser, other_args) if not ns_parser: return if ns_parser.sortby == "Change": sortby = f"%Change_{ns_parser.period}" else: sortby = ns_parser.sortby df = gecko.get_gainers_or_losers( period=ns_parser.period, typ="gainers" ).sort_values(by=sortby, ascending=ns_parser.descend) if not ns_parser.links: df.drop("Url", axis=1, inplace=True) print( tabulate( df.head(ns_parser.top), headers=df.columns, floatfmt=".4f", showindex=False, tablefmt="fancy_grid", ), "\n", ) except Exception as e: print(e, "\n")