def get_historical_data(coin,vs_curr,start_date,end_date): """ Returns historical data from the coingecko API in dataframe form coin :lower_case name (e.g 'bitcoin') vs_curr : lower case ticker symbol (e.g 'btc') start_date : day/month/year date format (e.g 01/01/2021) end_date : day/month/year date format (e.g 01/01/2021) ex: get_historical_data(coin = 'bitcoin', vs_curr='usd', start_date='01/01/2020', end_date = '01,02,2021') """ import time import datetime import pandas as pd from pycoingecko import CoinGeckoAPI import numpy as np cg = CoinGeckoAPI() start = int(datetime.datetime.strptime(start_date, '%d/%m/%Y').strftime("%s")) end = int(datetime.datetime.strptime(end_date, '%d/%m/%Y').strftime("%s")) hist_data= cg.get_coin_market_chart_range_by_id(id=coin,vs_currency='usd',from_timestamp=start,to_timestamp=end) prices_dict = hist_data['prices'] data = pd.DataFrame.from_dict(prices_dict) data[0]=(pd.to_datetime(data[0],unit='ms')) data.columns = ['date', 'price'] data['returns'] = data['price'].pct_change() data['cum_returns'] = np.cumprod(1 + data['returns']) - 1 return data
def crypto_stat(crypto_name, currency, start1, end1): cg = CoinGeckoAPI() conv_from1 = time.mktime(time.strptime(start1, '%Y-%m-%d')) conv_to1 = time.mktime(time.strptime(end1, '%Y-%m-%d')) crypto = cg.get_coin_market_chart_range_by_id(crypto_name, 'usd', conv_from1, conv_to1) historical = crypto['prices'][:] return historical
class CoinGecko(Stock): PROVIDER_CURRENCY = 'USD' CACHE_HISTORICAL = True # Retrieving a large range, so use a daily cache def __init__(self, config: Config): super().__init__(config) self.PROVIDER_CURRENCY = self.config.main.currency # CoinGecko supports currency conversion natively self.cg = CoinGeckoAPI() def symbol_to_id(self): symbol = self.config.main.crypto.lower() coins = self.cg.get_coins_list() for coin in coins: if coin['symbol'] == symbol: return coin['id'] raise ValueError( f"Could not map {self.config.main.crypto} to a CoinGecko ID") def current(self) -> Point: if len(self.config.main.stock): raise NotImplementedError( "Stock not implemented for CoinGecko Provider") crypto = self.symbol_to_id() currency = self.config.main.currency.lower() price = self.cg.get_price(ids=crypto, vs_currencies=currency) return Point(timestamp=datetime.now(), data=self.currency_convert(price[crypto][currency])) def historical(self) -> Series: if len(self.config.main.stock): raise NotImplementedError( "Stock not implemented for CoinGecko Provider") start = datetime.today() - timedelta( days=100) # need to use >90 days to get daily granularity end = datetime.today() - timedelta(days=1) crypto = self.symbol_to_id() currency = self.config.main.currency.lower() prices = self.cg.get_coin_market_chart_range_by_id( crypto, currency, start.timestamp(), end.timestamp()) results = [] for ts, price in prices['prices']: day = datetime.fromtimestamp(ts // 1000) # millisecond timestamps p = Point(timestamp=day, data=self.currency_convert(price)) results.append(p) return Series(series=results)
class APIwrapper: def __init__(self): self.socket = CoinGeckoAPI() def coin_api(self, tries, delay, backoff, *args): for n in range(tries): try: return self.socket.get_coin_market_chart_range_by_id(*args) except (KeyboardInterrupt, SystemExit): raise except Exception as e: time.sleep(delay) delay *= backoff print(f"{e} occurred. New attempt in {delay} seconds") continue raise NameError(f"Failed within {tries} attempts")
def fetch_df(): cg = CoinGeckoAPI() top_n = 200 #n_days_ago = 50 time_now = int(time.time()) time_delta = 91 * 24 * 60 * 60 df_r = pd.DataFrame() for i, coin in enumerate(cg.get_coins_markets(vs_currency="usd")[:top_n]): coin_id = coin["id"] chart = cg.get_coin_market_chart_range_by_id( coin_id, vs_currency="usd", from_timestamp=time_now - time_delta, to_timestamp=time_now) df = pd.DataFrame(chart["prices"], columns=["time", "price"]) df['time'] = pd.to_datetime(df['time'],unit='ms') df["time_delta"] = df.time - df.iloc[0].time df.set_index("time_delta", inplace=True) df["%s" % coin_id] = np.log(df.price) - np.log(df.iloc[0].price) df.drop(columns=["time", "price"], inplace=True) df = df.iloc[1:] df = df.transpose() df_r = df_r.append(df) print(i, coin_id) time.sleep(.15) print(df_r) print() print(df_r.idxmax(axis = 0)) return df_r
#"s/statera": 5, #"synthetix-network-token": 0, #"aave": 0, #"yearn-finance": 5, #"kava": 5, } total_shares = sum(coins.values()) df = pd.DataFrame() for coin_id, position in coins.items(): print("Fetch:", coin_id) chart = cg.get_coin_market_chart_range_by_id(coin_id, vs_currency="usd", from_timestamp=time_now - time_delta, to_timestamp=time_now) dfp = pd.DataFrame(chart["prices"], columns=["time", coin_id]) dfp['time'] = pd.to_datetime(dfp['time'], unit='ms') dfp.set_index("time", inplace=True) start_price = dfp[coin_id].iloc[0] last_price = dfp[coin_id].iloc[-1] delta_price = last_price - start_price dfp[coin_id] -= start_price dfp[coin_id] /= delta_price df = pd.concat([df, dfp], axis=1)
current_USD, 2))) st.write("Which is a percentage change of ***{:,.2f}%***".format( round(perc_change, 2), )) if usd_diff == 0: st.write('''# You Broke Even''') elif usd_diff <= 0: st.write('''# You Would Have Lost''') else: st.write('''# You Missed Out On''') st.write('***${:,.2f}!!!***'.format(abs(round(usd_diff, 2)), )) now = datetime.now() historical_prices = cg.get_coin_market_chart_range_by_id( id='safemoon', vs_currency="usd", from_timestamp=HIST_DATE_datetime.timestamp(), to_timestamp=now.timestamp())['prices'] dates = [] prices = [] for x, y in historical_prices: dates.append(x) prices.append(y) dictionary = {"Prices": prices, "Dates": dates} df = pd.DataFrame(dictionary) df['Dates'] = pd.to_datetime(df['Dates'], unit='ms', origin='unix') st.line_chart(df.rename(columns={"Dates": "index"}).set_index("index"))
async def run(): cl = CryptoXLib.create_binance_client(os.environ['BINANCEAPIKEY'], os.environ['BINANCESECKEY']) coins = set() # download a list of coins supported by binance, the coins will be used as an input for a download from coin gecko exchange_info = (await cl.get_exchange_info())['response'] for symbol in exchange_info['symbols']: coins.add(symbol['baseAsset'].upper()) print(f"Number of coins: {len(coins)}") print(f"Coins: {sorted(coins)}") cg = CoinGeckoAPI() # maps symbol to coin gecko id coin_ids = {} for coin in cg.get_coins_list(): if coin['symbol'].upper() in coins: # filter major margin coins, stable coins and exchange coins if '3x-' not in coin['id'] and \ 'USDC' not in coin['symbol'].upper() and \ 'USDS' not in coin['symbol'].upper() and \ 'USDT' not in coin['symbol'].upper() and \ 'TUSD' not in coin['symbol'].upper() and \ 'BGBP' not in coin['symbol'].upper() and \ 'BUSD' not in coin['symbol'].upper() and \ 'PAX' not in coin['symbol'].upper() and \ 'BNB' not in coin['symbol'].upper(): coin_ids[coin['symbol'].upper()] = coin['id'] # output is of format {'YYYY-MM-DD': {'coin': {'price': ..., 'cap': ..., 'volume': ...}, ...} output = defaultdict(defaultdict) try: for coin in sorted(coin_ids.keys()): coin_id = coin_ids[coin] if STARTING_COIN is None or coin >= STARTING_COIN: print(f"Downloading {coin} ({coin_id})") # attempt to download the data max. 3 times (e.g. because of failures due to too many requests) for i in range(0, 3): try: data = cg.get_coin_market_chart_range_by_id(coin_id, 'usd', START_DT.timestamp(), END_DT.timestamp()) break except Exception as e: if i == 2: raise e else: print(e) print(f"Retrying after 30sec") time.sleep(30) for (price, cap, volume) in reversed(list(zip(data['prices'], data['market_caps'], data['total_volumes']))): date = datetime.datetime.fromtimestamp(price[0] / 1000).strftime("%Y-%m-%d") if coin not in output[date]: output[date][coin] = {} output[date][coin]['price'] = price[1] output[date][coin]['cap'] = cap[1] output[date][coin]['volume'] = volume[1] except Exception as e: print(e) # dump output into a file output_file_name = "data.json" if STARTING_COIN is None else f"{STARTING_COIN}.json" with open(output_file_name, 'w') as file: file.write(json.dumps(output)) await cl.close()
#print(token) if token in token_name: wallet_id.append(wallet) ids = token_id[token_name.index(token)] token_names.append(ids) #ids = token_file.loc[token_file[1]==token,0].item() #ids = token_file[token_file[1]==token][0].apply(lambda x: "'" + str(x) + "'") #ids = "'",ids, "'" print(ids) cg = CoinGeckoAPI() days_50 = 4881600 #56.5 days, just over 8 weeks entry = datetime.utcfromtimestamp(int(entry_date)).strftime('%d-%m-%Y') price_request = cg.get_coin_history_by_id(ids, str(entry)) entry_price = float(price_request.get('market_data')['current_price']['usd']) request = cg.get_coin_market_chart_range_by_id(ids,'usd', int(entry_date) - days_50, int(entry_date) + days_50) content = request.get('prices') token_price_data[ids] = content wk8b = content[0:7] wk7b = content[7:14] wk6b = content[14:21] wk5b = content[21:28] wk4b = content[28:35] wk3b = content[35:42] wk2b = content[42:49] wk1b = content[49:56] wk1a = content[56:63] wk2a = content[63:70] wk3a = content[70:77]
# set CoinGecko API onject cg = CoinGeckoAPI() # conditons for evaluation coin_list = cg.get_coins_list() base_currency = 'usd' crypto = ['ethereum', 'monero', 'bitcoin-cash'] # from_unix = time.mktime(datetime(2021, 5, 7, 12, 00).timetuple()) from_ts = tc.date_to_unix('2020-01-01 00:00:00') to_ts = tc.date_to_unix('2021-05-06 00:00:00') for i in range(len(crypto)): # get chart from CoinGecko API chart = cg.get_coin_market_chart_range_by_id(crypto[i], base_currency, from_ts, to_ts) coin_data = cg.get_coin_by_id(crypto[i]) coin_name, coin_symbol = coin_data['name'], coin_data['symbol'] # create list with prices and dates date = [(chart['prices'][ii][0]) for ii in range(len(chart['prices']))] prices = [(chart['prices'][ii][1]) for ii in range(len(chart['prices']))] date_str = [tc.unix_to_date(date[ii]) for ii in range(len(date))] dates_list = pd.to_datetime(date_str, format='%Y-%m-%d %H:%M:%S.%f') # plot results plt.figure('Price-time') plt.plot(dates_list, prices, linewidth=1, label=coin_name) plt.ylabel('Price [' + base_currency + ']') plt.xlabel('Date') plt.xticks(rotation=25)
import numpy as np import pandas as pd from pycoingecko import CoinGeckoAPI cg = CoinGeckoAPI() tether = cg.get_coin_market_chart_range_by_id( 'tether', 'usd', 1426377600, 1599523200) #tether data 15.3.2015-8.9.2020 tether_mcap = np.array(tether['market_caps'])[:, 1] #extract marketcap data tether_unix = list(range(1427673600, 1599523200, 86400)) #formatting unix, offset 15x86400 tether_mcap_pd = pd.DataFrame({ 'unix': tether_unix, 'tether_mcap': tether_mcap }) #repackage as pandas tether_mcap_pd.to_csv('tether_mcap.csv', sep=',', index=False) #save to csv tusd = cg.get_coin_market_chart_range_by_id( 'true-usd', 'usd', 1521936000, 1599523200) #tusd data 25.3.2018-8.9.2020 tusd_mcap = np.array(tusd['market_caps'])[:, 1] tusd_unix = list(range(1521849600, 1599523200, 86400)) #offset 1x86400 tusd_mcap_pd = pd.DataFrame({'unix': tusd_unix, 'tusd_mcap': tusd_mcap}) tusd_mcap_pd.to_csv('tusd_mcap.csv', sep=',', index=False) susd = cg.get_coin_market_chart_range_by_id( 'nusd', 'usd', 1534204800, 1599523200) #susd data 14.8.2018 -8.9.2020 susd_mcap = np.array(susd['market_caps'])[:, 1] susd_unix = list(range(1534204800, 1599523200, 86400)) susd_mcap_pd = pd.DataFrame({'unix': susd_unix, 'susd_mcap': susd_mcap}) susd_mcap_pd.to_csv('susd_mcap.csv', sep=',', index=False)
def get_projected_profit(df: pd.DataFrame) -> pd.DataFrame: """ calculate project profit for status==ACTIVE """ # for those I need to find a price (use coingecko) cg = CoinGeckoAPI() time_col = "timestamp_unix" currency = "usd" prices_btc = cg.get_coin_market_chart_range_by_id( id="bitcoin", vs_currency=currency, from_timestamp=df[df["symbol"] == "WBTC"][time_col].min(), to_timestamp=df[df["symbol"] == "WBTC"][time_col].max(), )["prices"] prices_eth = cg.get_coin_market_chart_range_by_id( id="ethereum", vs_currency=currency, from_timestamp=df[df["symbol"] == "ETH"][time_col].min(), to_timestamp=df[df["symbol"] == "ETH"][time_col].max(), )["prices"] time_col_cg = "timestamp_unix_gc" prices_btc = pd.DataFrame(prices_btc, columns=[time_col_cg, "price"]) prices_btc["symbol"] = "WBTC" prices_eth = pd.DataFrame(prices_eth, columns=[time_col_cg, "price"]) prices_eth["symbol"] = "ETH" df_prices = pd.concat([prices_btc, prices_eth]).reset_index(drop=True) # from millisecond timestamp to seconds df_prices[time_col_cg] //= 1000 # merge nearest historical prices to option creation timestamp df = pd.merge_asof( df.sort_values(time_col), df_prices.sort_values(time_col_cg), left_on=time_col, right_on=time_col_cg, by="symbol", allow_exact_matches=True, direction="nearest", ) # to calculate the break even price, I need the totalFee in USD # (the total usd costs which where paid) df["totalFeeUSD"] = df["totalFee"] * df["price"] df["premium_usd"] = df["premium"] * df["price"] df["breakeven"] = np.where( df["type"] == "CALL", df["strike"] + (df["totalFeeUSD"] / df["amount"]), # has to be scaled by amount size df["strike"] - (df["totalFeeUSD"] / df["amount"]), ) # there are some weird options (probably test cases) with very low/high strike prices # e.g. 1usd strike for 10 df_ put option (ID == WBTC-9). there breakeven price will be # negative based on the above calculation so I set the min value to 0 df["breakeven"] = np.where(df["breakeven"] < 0, 0, df["breakeven"]) # get latest prices current_price_wbtc = cg.get_price( ids="bitcoin", vs_currencies=currency)["bitcoin"][currency] current_price_eth = cg.get_price( ids="ethereum", vs_currencies=currency)["ethereum"][currency] df["current_price"] = np.where(df["symbol"] == "WBTC", current_price_wbtc, current_price_eth) # The projected profit is only relevant for options with status ACTIVE cause # the calculation of it is based on the current price which means that the projected # profit for exercised and expired options won't match the actual profit! (but thats ok, # as we only need it for ACTIVE anyways) # I) OTM: projected_profit is simply -premium df["projected_profit"] = np.where( (df["type"] == "CALL") & (df["current_price"] < df["strike"]), -df["premium"], np.nan, ) df["projected_profit"] = np.where( (df["type"] == "PUT") & (df["current_price"] > df["strike"]), -df["premium"], df["projected_profit"], ) # II) ITM df["projected_profit"] = np.where( (df["type"] == "CALL") & (df["current_price"] >= df["strike"]), ((df["current_price"] - df["breakeven"]) * df["amount"]) / df["current_price"], df["projected_profit"], ) df["projected_profit"] = np.where( (df["type"] == "PUT") & (df["current_price"] <= df["strike"]), ((df["breakeven"] - df["current_price"]) * df["amount"]) / df["current_price"], df["projected_profit"], ) df["profit"] = df["projected_profit"] df = df.drop(columns=["projected_profit"]) # NOTE(!)have to think about this, but I think overall its better the way it is not using this! # in some isolated cases (usually with large options) small differences in the calculated # break even price and the actual break even might be large enough to lead to some illogical # values for the projected profit. # I apply a filter on top, to make sure we don't see anything weird. # A profit can never be smaller than -premium (no matter what scenario) df["profit"] = np.where(df["profit"] < -df["premium"], -df["premium"], df["profit"]) # OTM is if the profit is simply the same as the negative premium # e.g. premium was 10eth -> if the profit equals -10 then the option is OTM # else ITM df["group"] = np.where(df["profit"] == -df["premium"], "OTM", "ITM") ################################### this section is for calculating offsets on the current price # to get an overview of how the pools P&L ranges with changes pct-changes (+/- 0-25 pct) in the spot price # the below values will be bonkers for no longer active options, so set them to Nan later on # we only need this for active stuff spread = 0.50 for i in np.arange(0, spread + 0.01, 0.01): i = round(i, 2) # if price increases (this will be good for the calls and bad for the puts) df[f"current_price_plus_{i}pct"] = df[ "current_price"] + df["current_price"] * i # if price decreases (will be bad for calls and good for puts) df[f"current_price_minus_{i}pct"] = (df["current_price"] - df["current_price"] * i) # OTM df[f"projected_profit_plus_{i}pct"] = np.where( (df["type"] == "CALL") & (df[f"current_price_plus_{i}pct"] < df["strike"]), -df["premium"], np.nan, ) df[f"projected_profit_plus_{i}pct"] = np.where( (df["type"] == "PUT") & (df[f"current_price_plus_{i}pct"] > df["strike"]), -df["premium"], df[f"projected_profit_plus_{i}pct"], ) df[f"projected_profit_minus_{i}pct"] = np.where( (df["type"] == "CALL") & (df[f"current_price_minus_{i}pct"] < df["strike"]), -df["premium"], np.nan, ) df[f"projected_profit_minus_{i}pct"] = np.where( (df["type"] == "PUT") & (df[f"current_price_minus_{i}pct"] > df["strike"]), -df["premium"], df[f"projected_profit_minus_{i}pct"], ) # II) ITM df[f"projected_profit_plus_{i}pct"] = np.where( (df["type"] == "CALL") & (df[f"current_price_plus_{i}pct"] >= df["strike"]), ((df[f"current_price_plus_{i}pct"] - df["breakeven"]) * df["amount"]) / df[f"current_price_plus_{i}pct"], df[f"projected_profit_plus_{i}pct"], ) df[f"projected_profit_plus_{i}pct"] = np.where( (df["type"] == "PUT") & (df[f"current_price_plus_{i}pct"] <= df["strike"]), ((df["breakeven"] - df[f"current_price_plus_{i}pct"]) * df["amount"]) / df[f"current_price_plus_{i}pct"], df[f"projected_profit_plus_{i}pct"], ) df[f"projected_profit_minus_{i}pct"] = np.where( (df["type"] == "CALL") & (df[f"current_price_minus_{i}pct"] >= df["strike"]), ((df[f"current_price_minus_{i}pct"] - df["breakeven"]) * df["amount"]) / df[f"current_price_minus_{i}pct"], df[f"projected_profit_minus_{i}pct"], ) df[f"projected_profit_minus_{i}pct"] = np.where( (df["type"] == "PUT") & (df[f"current_price_minus_{i}pct"] <= df["strike"]), ((df["breakeven"] - df[f"current_price_minus_{i}pct"]) * df["amount"]) / df[f"current_price_minus_{i}pct"], df[f"projected_profit_minus_{i}pct"], ) return df
print(len(topCoins)) writebaleList = [] counter = 0 counteri = 0 repeat = 0 dataInput = [] df = pd.read_csv("fileHist.csv") #for coin in topCoins: while counteri <= 600: coin = topCoins[counteri] time.sleep(1) try: get = cg.get_coin_market_chart_range_by_id(coin[0], 'usd', startdate, enddate) get = get['prices'] dataInput.clear() df[str(coin[0])] = "" for writein in get: price = str(writein[1]) dataInput.append(price) df.loc[df["TimeStamp"] == int(writein[0]), str(coin[0])] = price print(counteri) counteri = counteri + 1
import numpy as np import pandas as pd import time from pycoingecko import CoinGeckoAPI cg = CoinGeckoAPI() now = int(time.time()) #current unix time tether = pd.read_csv('tether_mcap.csv') #read old data tether_lastrow = np.array(tether.iloc[-1:]) #get last row [:,0] 0 = unix, 1 = mcap tether = cg.get_coin_market_chart_range_by_id(id='tether',vs_currency='usd',from_timestamp=int(tether_lastrow[:,0]),to_timestamp=now) #get new data #possible point of error if called too often? tether_mcap = np.array(tether['market_caps'])[:,1] #formatting new mcap ### tether_unix = list(range(int(tether_lastrow[:,0]),now,int((now-int(tether_lastrow[:,0]))/len(tether_mcap)+1))) #formatting new unix tether_mcap_pd = pd.DataFrame({'unix':tether_unix,'tether_mcap':tether_mcap}) #repackage as pandas tether_mcap_pd.to_csv('tether_mcap.csv',sep=',',index=False,header=False,mode='a') #mode = 'a' append to existing csv tusd = pd.read_csv('tusd_mcap.csv') tusd_lastrow = np.array(tusd.iloc[-1:]) # tusd = cg.get_coin_market_chart_range_by_id(id='true-usd',vs_currency='usd',from_timestamp=int(tusd_lastrow[:,0]),to_timestamp=now) tusd_mcap = np.array(tusd['market_caps'])[:,1] tusd_unix = list(range(int(tusd_lastrow[:,0]),now,int((now-int(tusd_lastrow[:,0]))/len(tusd_mcap)+1))) tusd_mcap_pd = pd.DataFrame({'unix':tusd_unix,'tusd_mcap':tusd_mcap}) tusd_mcap_pd.to_csv('tusd_mcap.csv',sep=',',index=False,header=False,mode='a') susd = pd.read_csv('susd_mcap.csv') susd_lastrow = np.array(susd.iloc[-1:]) # susd = cg.get_coin_market_chart_range_by_id(id='nusd',vs_currency='usd',from_timestamp=int(susd_lastrow[:,0]),to_timestamp=now)