def load( coin: str, source: str, ) -> Tuple[Union[Optional[str], pycoingecko_model.Coin], Any]: """Load cryptocurrency from given source. Available sources are: CoinGecko, CoinPaprika, Coinbase and Binance. Loading coin from Binance and CoinPaprika means validation if given coins exists in chosen source, if yes then id of the coin is returned as a string. In case of CoinGecko load will return Coin object, if provided coin exists. Coin object has access to different coin information. Parameters ---------- coin: str Coin symbol or id which is checked if exists in chosen data source. source : str Source of the loaded data. CoinGecko, CoinPaprika, or Binance Returns ------- Tuple[Union[str, pycoingecko_model.Coin], Any] - str or Coin object for provided coin - str with source of the loaded data. CoinGecko, CoinPaprika, or Binance """ current_coin = "" # type: Optional[Any] if source == "cg": current_coin = pycoingecko_model.Coin(coin) return current_coin, source if source == "bin": parsed_coin = coin.upper() current_coin, pairs = show_available_pairs_for_given_symbol( parsed_coin) if len(pairs) > 0: print(f"Coin found : {current_coin}\n") else: print(f"Couldn't find coin with symbol {current_coin}\n") return current_coin, source if source == "cp": paprika_coins = get_list_of_coins() paprika_coins_dict = dict(zip(paprika_coins.id, paprika_coins.symbol)) current_coin, _ = coinpaprika_model.validate_coin( coin, paprika_coins_dict) return current_coin, source if source == "cb": coinbase_coin = coin.upper() current_coin, pairs = coinbase_model.show_available_pairs_for_given_symbol( coinbase_coin) if len(pairs) > 0: print(f"Coin found : {current_coin}\n") else: print(f"Couldn't find coin with symbol {current_coin}\n") return current_coin, source return current_coin, None
def load(other_args: List[str]): """Select coin from CoinPaprika Parameters ---------- other_args: List[str] Argparse arguments Returns ------- coin: str Coin that is defined on binance df_coin : pd.DataFrame Dataframe of prices for selected coin """ parser = argparse.ArgumentParser( add_help=False, formatter_class=argparse.ArgumentDefaultsHelpFormatter, prog="load", description="Define the coin to be used from CoinPaprika and get data", ) parser.add_argument( "-c", "--coin", help="Coin to get", dest="coin", type=str, required="-h" not in other_args, ) try: if other_args: if not other_args[0][0] == "-": other_args.insert(0, "-c") ns_parser = parse_known_args_and_warn(parser, other_args) if not ns_parser: return COINS = get_list_of_coins() COINS_DCT = dict(zip(COINS.id, COINS.symbol)) coin_id, _ = paprika.validate_coin(ns_parser.coin, COINS_DCT) if coin_id: return coin_id except SystemExit: print("") except Exception as e: print(e, "\n")
def load( coin: str, source: str = "cp", days: int = 60, vs: str = "usd", interval: str = "1day", should_load_ta_data: bool = False, ): """Load cryptocurrency from given source. Available sources are: CoinGecko, CoinPaprika, Coinbase, Binance and Yahoo Finance Loading coin from Binance and CoinPaprika means validation if given coins exists in chosen source, if yes then id of the coin is returned as a string. In case of CoinGecko load will return Coin object, if provided coin exists. Coin object has access to different coin information. Parameters ---------- coin: str Coin symbol or id which is checked if exists in chosen data source. source : str Source of the loaded data. CoinGecko, CoinPaprika, Yahoo Finance or Binance Returns ------- Tuple[Union[str, pycoingecko_model.Coin], str, str] - str or Coin object for provided coin - str with source of the loaded data. CoinGecko, CoinPaprika, Yahoo Finance or Binance - str with symbol - Dataframe with coin map to different sources """ if source in ("cg", "cp"): if vs not in ("USD", "BTC", "usd", "btc"): console.print( "You can only compare with usd or btc (e.g., --vs usd)\n") return None, None, None, None, None, None if interval != "1day": console.print( "Only daily data is supported for coingecko and coinpaprika (e.g., -i 1day)\n" ) return None, None, None, None, None, None current_coin = "" # type: Optional[Any] coins_map_df = prepare_all_coins_df().set_index("Symbol").dropna(thresh=2) if source == "cg": coingecko = pycoingecko_model.Coin(coin.lower(), True) if not coingecko.symbol: return None, None, None, None, None, None coin_map_df = coins_map_df.loc[coingecko.symbol] coin_map_df = ( coin_map_df.iloc[0] if isinstance(coin_map_df, pd.DataFrame) else coin_map_df ) # TODO: improve to choose the row that matches better; # if it is dataframe, it means that found more than 1 coin if should_load_ta_data: df_prices, currency = load_ta_data( coin_map_df=coin_map_df, source=source, currency=vs, days=days, limit=0, interval=interval, ) return ( str(coingecko), source, coingecko.symbol, coin_map_df, df_prices, currency, ) return ( str(coingecko), source, coingecko.symbol, coin_map_df, None, None, ) if source == "cp": paprika_coins = get_list_of_coins() paprika_coins_dict = dict(zip(paprika_coins.id, paprika_coins.symbol)) current_coin, symbol = coinpaprika_model.validate_coin( coin, paprika_coins_dict) if not symbol: return None, None, None, None, None, None coin_map_df = coins_map_df.loc[symbol.lower( ) if symbol is not None else symbol] coin_map_df = (coin_map_df.iloc[0] if isinstance( coin_map_df, pd.DataFrame) else coin_map_df) if should_load_ta_data: df_prices, currency = load_ta_data( coin_map_df=coin_map_df, source=source, currency=vs, days=days, limit=0, interval=interval, ) return (current_coin, source, symbol, coin_map_df, df_prices, currency) return (current_coin, source, symbol, coin_map_df, None, None) if source == "bin": if vs == "usd": vs = "USDT" if interval not in SOURCES_INTERVALS["bin"]: console.print( "Interval not available on binance. Run command again with one supported (e.g., -i 1day):\n", SOURCES_INTERVALS["bin"], ) return None, None, None, None, None, None # TODO: convert bitcoin to btc before searching pairs parsed_coin = coin.upper() current_coin, pairs = show_available_pairs_for_given_symbol( parsed_coin) if len(pairs) > 0: if vs not in pairs: console.print( "vs specified not supported by binance. Run command again with one supported (e.g., --vs USDT):\n", pairs, ) return None, None, None, None, None, None coin_map_df = coins_map_df.loc[parsed_coin.lower()] coin_map_df = (coin_map_df.iloc[0] if isinstance( coin_map_df, pd.DataFrame) else coin_map_df) # console.print(f"Coin found : {current_coin}\n") if should_load_ta_data: df_prices, currency = load_ta_data( coin_map_df=coin_map_df, source=source, currency=vs, days=0, limit=days, interval=interval, ) return ( current_coin, source, parsed_coin, coin_map_df, df_prices, currency, ) return (current_coin, source, parsed_coin, coin_map_df, None, None) return None, None, None, None, None, None if source == "cb": if vs == "usd": vs = "USDT" if interval not in SOURCES_INTERVALS["cb"]: console.print( "Interval not available on coinbase. Run command again with one supported (e.g., -i 1day):\n", SOURCES_INTERVALS["cb"], ) return None, None, None, None, None, None # TODO: convert bitcoin to btc before searching pairs coinbase_coin = coin.upper() current_coin, pairs = coinbase_model.show_available_pairs_for_given_symbol( coinbase_coin) if vs not in pairs: console.print( "vs specified not supported by coinbase. Run command again with one supported (e.g., --vs USDT):\n", pairs, ) return None, None, None, None, None, None if len(pairs) > 0: # console.print(f"Coin found : {current_coin}\n") coin_map_df = coins_map_df.loc[coin] coin_map_df = (coin_map_df.iloc[0] if isinstance( coin_map_df, pd.DataFrame) else coin_map_df) if should_load_ta_data: df_prices, currency = load_ta_data( coin_map_df=coin_map_df, source=source, currency=vs, days=0, limit=days, interval=interval, ) return (current_coin, source, coin, coin_map_df, df_prices, currency) return (current_coin, source, coin, coin_map_df, None, None) console.print(f"Couldn't find coin with symbol {current_coin}\n") return None, None, None, None, None, None if source == "yf": if vs.upper() not in YF_CURRENCY: console.print( "vs specified not supported by Yahoo Finance. Run command again with one supported (e.g., --vs USD):\n", YF_CURRENCY, ) return None, None, None, None, None, None if interval not in SOURCES_INTERVALS["yf"]: console.print( "Interval not available on YahooFinance. Run command again with one supported (e.g., -i 1day):\n", SOURCES_INTERVALS["yf"], ) return None, None, None, None, None, None # Search coin using crypto symbol coin_map_df_1 = coins_map_df.loc[coins_map_df.index == coin] # Search coin using yfinance id coin_map_df_2 = coins_map_df.loc[coins_map_df.YahooFinance == f"{coin.upper()}-{vs.upper()}"] # Search coin using crypto full name coin_map_df_3 = coins_map_df.loc[coins_map_df.CoinGecko == coin] for _df in [coin_map_df_1, coin_map_df_2, coin_map_df_3]: if not _df.empty: coin_map_df = _df break if coin_map_df.empty: console.print( f"Cannot find {coin} against {vs} on Yahoo finance. Try another source or currency.\n" ) return None, None, None, None, None, None if (coin_map_df["YahooFinance"]).isna().all(): console.print( f"Cannot find {coin} Yahoo finance. Try another source.\n") return None, None, None, None, None, None # Filter for the currency coin_map_df = coin_map_df[ coin_map_df["YahooFinance"].str.upper().str.contains(vs.upper())] coin_map_df = (coin_map_df.iloc[0] if isinstance( coin_map_df, pd.DataFrame) else coin_map_df) if should_load_ta_data: df_prices, currency = load_ta_data( coin_map_df=coin_map_df, source=source, currency=vs, days=days, limit=0, interval=interval, ) return ( coin_map_df["YahooFinance"], source, coin, coin_map_df, df_prices, currency, ) return ( coin_map_df["YahooFinance"], source, coin, coin_map_df, None, None, ) return None, None, None, None, None, None