def index_board_logic_op(crypto_asset, logic_one_arr, daily_ewma, curr_reb_start, next_reb_start, curr_board_eve): # turn the first logic row into a dataframe and add the 'Time' column # the first logic row will be used for the next quarter weights computation first_logic_row = pd.DataFrame(logic_one_arr, columns=crypto_asset) # computing the new second logic row that is used to compute the # weights relative to the next rebalance period ewma_fraction = daily_ewma_fraction(daily_ewma, first_logic_row, curr_reb_start, curr_board_eve) print("ewma_fraction") print(ewma_fraction) daily_ewma_double_row = index_norm_logic_op(crypto_asset, daily_ewma) print(daily_ewma_double_row) second_logic_row = (ewma_fraction >= 0.02) * 1 double_checked_ewma = daily_double_log_check(first_logic_row, second_logic_row, daily_ewma, curr_reb_start, curr_board_eve) # adding the Time columns to the double checked ewma human_curr_start = timestamp_to_human([curr_reb_start], date_format="%m-%d-%y") human_curr_board = timestamp_to_human([curr_board_eve], date_format="%m-%d-%y") period_date_list = date_gen(human_curr_start[0], human_curr_board[0], EoD="N") double_checked_ewma["Time"] = period_date_list print(double_checked_ewma) # giving "Time" and "Date" columns to the df containing the logic rows human_next_start = timestamp_to_human([next_reb_start], date_format="%m-%d-%y") first_logic_row["Time"] = next_reb_start first_logic_row["Date"] = human_next_start second_logic_row["Time"] = next_reb_start second_logic_row["Date"] = human_next_start # computing the new weights that will be used starting from the # next rebalance date weights_for_board = quarter_weights(double_checked_ewma, [int(curr_board_eve)], crypto_asset) weights_for_board["Time"] = next_reb_start weights_for_board["Date"] = human_next_start return first_logic_row, second_logic_row, weights_for_board, daily_ewma_double_row
def test_timestamp_to_human(): # date in format yyyy-mm-dd check_list = [ '2020-01-01', '2020-01-02', '2020-01-03', '2020-01-04', '2020-01-05' ] # array of dates in timestamp format ts_array = np.array( [1577836800, 1577923200, 1578009600, 1578096000, 1578182400]) date = timestamp_to_human(ts_array) assert date == check_list
collection_index_raw = db.index_level_raw # collection for index level base 1000 collection_index_1000 = db.index_level_1000 # ################ DATE SETTINGS ######################## # define the start date as MM-DD-YYYY start_date = "01-01-2016" # define today in various format today_str = datetime.now().strftime("%Y-%m-%d") today = datetime.strptime(today_str, "%Y-%m-%d") today_TS = int(today.replace(tzinfo=timezone.utc).timestamp()) y_TS = today_TS - 86400 two_before_TS = y_TS - 86400 today_human = data_setup.timestamp_to_human([today_TS]) yesterday_human = data_setup.timestamp_to_human([y_TS]) two_before_human = data_setup.timestamp_to_human([two_before_TS]) # define the variable containing all the date from start_date to today. # the date are displayed as timestamp and each day refers to 12:00 am UTC reference_date_vector = data_setup.date_gen(start_date) # define all the useful arrays containing the rebalance start # date, stop date, board meeting date rebalance_start_date = calc.start_q("01-01-2016") rebalance_stop_date = calc.stop_q(rebalance_start_date) board_date = calc.board_meeting_day() board_date_eve = calc.day_before_board() next_rebalance_date = calc.next_start()
last_five_days_mongo = date_list[(len(date_list) - 5):len(date_list)] last_five_days_mongo = [ str(single_date) for single_date in last_five_days_mongo ] print(last_five_days_mongo) # finding the date to download as difference between # complete array of date and date now stored on MongoDB date_to_add = Diff(last_five_days, last_five_days_mongo) if date_to_add != []: if len(date_to_add) > 1: date_to_add.sort() start_date = timestamp_to_human([date_to_add[0]], date_format="%m-%d-%Y") start_date = start_date[0] end_date = timestamp_to_human([date_to_add[len(date_to_add) - 1]], date_format="%m-%d-%Y") end_date = end_date[0] else: start_date = datetime.fromtimestamp(int(date_to_add[0])) start_date = start_date.strftime("%m-%d-%Y") end_date = start_date # #### data download part ##### for Crypto in CRYPTO_ASSET:
from cryptoindex.index_hist import hist_time_single_date from cryptoindex.data_setup import (timestamp_to_human) from cryptoindex.index_func import days_variable day = None start_q_list = next_start() board_eve_list = day_before_board() board_day = board_meeting_day() start_q = start_q() print(start_q_list) print(board_eve_list) print(board_day) print(start_q) print(timestamp_to_human(start_q_list[1:len(start_q_list)])) day_TS, _ = days_variable(day) # day_TS = day_TS + 86400 print(day_TS) if day_TS in start_q_list: print("First day of the quarter") elif day_TS in board_eve_list: print("Board day") else:
def index_hist_uploader(crypto_asset_price, crypto_asset_vol, exc_vol_tot, price_ret, weights_for_board, first_logic_matrix_df, second_logic_matrix_df, ewma_df, double_checked_EWMA, syntethic, syntethic_relative_matrix, divisor_array, reshaped_divisor, index_values, index_1000_base): # creating the array with human readable Date ref_date_arr = date_gen() human_date = timestamp_to_human(ref_date_arr) # put the "Crypto_Asset_Prices" dataframe on MongoDB crypto_asset_price["Date"] = human_date mongo_upload(crypto_asset_price, "collection_price", reorder="Y", column_set_val="complete") # put the "Crypto_Asset_Volumes" dataframe on MongoDB crypto_asset_vol["Date"] = human_date mongo_upload(crypto_asset_vol, "collection_volume", reorder="Y", column_set_val="complete") # put the exchange volumes on MongoDB mongo_upload(exc_vol_tot, "collection_all_exc_vol") # put the "price_ret" dataframe on MongoDB price_ret["Date"] = human_date mongo_upload(price_ret, "collection_price_ret", reorder="Y", column_set_val="complete") # put the "weights" dataframe on MongoDB weight_human_date = timestamp_to_human(weights_for_board["Time"]) weights_for_board["Date"] = weight_human_date mongo_upload(weights_for_board, "collection_weights", reorder="Y", column_set_val="complete") print(weights_for_board) # put the first logic matrix on MongoDB first_date = timestamp_to_human(first_logic_matrix_df["Time"]) first_logic_matrix_df["Date"] = first_date mongo_upload(first_logic_matrix_df, "collection_logic_one", reorder="Y", column_set_val="complete") # put the second logic matrix on MongoDB second_date = timestamp_to_human(second_logic_matrix_df["Time"]) second_logic_matrix_df["Date"] = second_date mongo_upload(second_logic_matrix_df, "collection_logic_two", reorder="Y", column_set_val="complete") # put the EWMA dataframe on MongoDB ewma_df["Date"] = human_date ewma_df["Time"] = ref_date_arr mongo_upload(ewma_df, "collection_EWMA", reorder="Y", column_set_val="complete") # put the double checked EWMA on MongoDB double_checked_EWMA["Date"] = human_date mongo_upload(double_checked_EWMA, "collection_EWMA_check", reorder="Y", column_set_val="complete") # put the synth matrix on MongoDB syntethic["Date"] = human_date syntethic["Time"] = ref_date_arr mongo_upload(syntethic, "collection_synth", reorder="Y", column_set_val="complete") # put the relative synth matrix on MongoDB syntethic_relative_matrix["Date"] = human_date syntethic_relative_matrix["Time"] = ref_date_arr mongo_upload(syntethic_relative_matrix, "collection_relative_synth", reorder="Y", column_set_val="complete") # put the divisor array on MongoDB divisor_date = timestamp_to_human(divisor_array["Time"]) divisor_array["Date"] = divisor_date mongo_upload(divisor_array, "collection_divisor", reorder="Y", column_set_val="divisor") # put the reshaped divisor array on MongoDB reshaped_divisor_date = timestamp_to_human(reshaped_divisor["Time"]) reshaped_divisor["Date"] = reshaped_divisor_date mongo_upload(reshaped_divisor, "collection_divisor_reshaped", reorder="Y", column_set_val="divisor") # put the index level raw on MongoDB index_values["Date"] = human_date index_values["Time"] = ref_date_arr mongo_upload(index_values, "collection_index_level_raw", reorder="Y", column_set_val="index") # put the index level 1000 on MongoDB index_1000_base["Date"] = human_date index_1000_base["Time"] = ref_date_arr mongo_upload(index_1000_base, "collection_index_level_1000", reorder="Y", column_set_val="index")
reference_date_vector, ) print(divisor_array) reshaped_divisor = divisor_reshape(divisor_array, reference_date_vector) index_values = index_level_calc(Crypto_Asset_Prices, syntethic_relative_matrix, divisor_array, reference_date_vector) index_1000_base = index_based(index_values) # pd.set_option('display.max_rows', None) # #################### MONGO DB UPLOADS ########################### # creating the array with human readable Date human_date = timestamp_to_human(reference_date_vector) # put the "Crypto_Asset_Prices" dataframe on MongoDB Crypto_Asset_Prices["Date"] = human_date mongo_upload(Crypto_Asset_Prices, "collection_price", reorder="Y", column_set_val="complete") # put the "Crypto_Asset_Volumes" dataframe on MongoDB Crypto_Asset_Volume["Date"] = human_date mongo_upload(Crypto_Asset_Volume, "collection_volume", reorder="Y", column_set_val="complete")
# adding Currency (USDT/USD), Time (timestamp), # and Standard Date (YYYY-MM-DD) columns usdt_rates["Currency"] = np.zeros(len(usdt_rates["Rate"])) usdt_rates["Currency"] = [ str(x).replace("0.0", "USDT/USD") for x in usdt_rates["Currency"] ] usdt_rates["Time"] = first_call["Time"] usdt_rates.fillna("NaN", inplace=True) index_to_remove = usdt_rates[usdt_rates.Time == "NaN"].index usdt_rates = usdt_rates.drop(index_to_remove) print(usdt_rates) usdt_rates["Standard Date"] = timestamp_to_human(first_call["Time"]) print(usdt_rates) # correcting the date 2016-10-02 using the previous day rate prev_rate = np.array(usdt_rates.loc[usdt_rates.Time == '1475280000', "Rate"]) usdt_rates.loc[usdt_rates.Time == '1475366400', "Rate"] = prev_rate # USDT mongoDB upload mongo_upload(usdt_rates, "collection_stable_rate") # usdt_data = usdt_rates.to_dict(orient="records") # collection_stable.insert_many(usdt_data) # ############# USDC exchange rates computation ############ # BTC/USDC is traded on Poloniex, Kraken and bittrex # Poloniex has the entire historoical values from 01/01/2016
def index_daily_uploader(crypto_asset_price, crypto_asset_vol, exc_vol_tot, price_ret, daily_ewma, daily_ewma_double_check, daily_synth, daily_rel, curr_divisor, daily_index_1000_df, raw_index_df, new_divisor=None, new_logic_one=None, new_logic_two=None, new_weights=None, day=None): if day is None: day_str = datetime.now().strftime("%Y-%m-%d") day_date = datetime.strptime(day_str, "%Y-%m-%d") day_TS = int(day_date.replace(tzinfo=timezone.utc).timestamp()) day_before_TS = day_TS - DAY_IN_SEC yesterday_human = timestamp_to_human([day_before_TS]) else: day_date = datetime.strptime(day, "%Y-%m-%d") day_before_TS = int(day_date.replace(tzinfo=timezone.utc).timestamp()) yesterday_human = timestamp_to_human([day_before_TS]) # put the "crypto_asset_price" dataframe on MongoDB crypto_asset_price["Date"] = yesterday_human mongo_upload(crypto_asset_price, "collection_price", reorder="Y", column_set_val="complete") # put the "crypto_asset_vols" dataframe on MongoDB crypto_asset_vol["Date"] = yesterday_human mongo_upload(crypto_asset_vol, "collection_volume", reorder="Y", column_set_val="complete") # put the exchange volumes on MongoDB mongo_upload(exc_vol_tot, "collection_all_exc_vol") # put the "price_ret" dataframe on MongoDB price_ret["Date"] = yesterday_human mongo_upload(price_ret, "collection_price_ret", reorder="Y", column_set_val="complete") # put the EWMA dataframe on MongoDB daily_ewma["Date"] = yesterday_human daily_ewma["Time"] = day_before_TS mongo_upload(daily_ewma, "collection_EWMA", reorder="Y", column_set_val="complete") # put the double checked EWMA on MongoDB daily_ewma_double_check["Date"] = yesterday_human daily_ewma_double_check["Time"] = day_before_TS mongo_upload(daily_ewma_double_check, "collection_EWMA_check", reorder="Y", column_set_val="complete") # put the synth matrix on MongoDB daily_synth["Date"] = yesterday_human daily_synth["Time"] = day_before_TS mongo_upload(daily_synth, "collection_synth", reorder="Y", column_set_val="complete") # put the relative synth matrix on MongoDB daily_rel["Date"] = yesterday_human daily_rel["Time"] = day_before_TS mongo_upload(daily_rel, "collection_relative_synth", reorder="Y", column_set_val="complete") # put the reshaped divisor array on MongoDB curr_divisor["Date"] = yesterday_human curr_divisor["Time"] = day_before_TS mongo_upload(curr_divisor, "collection_divisor_reshaped", reorder="Y", column_set_val="divisor") # put the index level 1000 on MongoDB daily_index_1000_df["Date"] = yesterday_human daily_index_1000_df["Time"] = day_before_TS mongo_upload(daily_index_1000_df, "collection_index_level_1000", reorder="Y", column_set_val="index") # put the index level raw on MongoDB raw_index_df["Date"] = yesterday_human raw_index_df["Time"] = day_before_TS mongo_upload(raw_index_df, "collection_index_level_raw", reorder="Y", column_set_val="index") if new_divisor is None: pass else: new_divisor["Date"] = yesterday_human new_divisor["Time"] = day_before_TS mongo_upload(new_divisor, "collection_divisor_reshaped", reorder="Y", column_set_val="divisor") if new_logic_one is None: pass else: mongo_upload(new_logic_one, "collection_logic_one", reorder="Y", column_set_val="complete") if new_logic_two is None: pass else: mongo_upload(new_logic_two, "collection_logic_one", reorder="Y", column_set_val="complete") if new_weights is None: pass else: mongo_upload(new_weights, "collection_weights", reorder="Y", column_set_val="complete") return None
def index_normal_day(crypto_asset, exc_list, pair_list, coll_to_use, day=None): # create the indexing for MongoDB mongo_indexing() day_before_TS, two_before_TS = days_variable(day) two_before_human = timestamp_to_human([two_before_TS]) # defining the dictionary for the MongoDB query query_dict = {"Time": int(day_before_TS)} # retriving the needed information on MongoDB daily_mat = query_mongo(DB_NAME, MONGO_DICT.get(coll_to_use), query_dict) (crypto_asset_price, crypto_asset_vol, exc_vol_tot, _) = index_daily_loop(daily_mat, crypto_asset, exc_list, pair_list, day_before_TS) # turn prices and volumes into pandas dataframe crypto_asset_price = pd.DataFrame(crypto_asset_price, columns=CRYPTO_ASSET) crypto_asset_vol = pd.DataFrame(crypto_asset_vol, columns=CRYPTO_ASSET) # compute the price return of the day two_before_price = query_mongo(DB_NAME, MONGO_DICT.get("coll_price"), {"Time": two_before_TS}) two_before_price = two_before_price.drop(columns=["Time", "Date"]) return_df = two_before_price.append(crypto_asset_price) price_ret = return_df.pct_change() price_ret = price_ret.iloc[[1]] price_ret = price_ret.replace(-1, 0) price_ret.fillna(0, inplace=True) # then add the 'Time' column time_header = ["Time"] time_header.extend(CRYPTO_ASSET) crypto_asset_price = pd.DataFrame(crypto_asset_price, columns=time_header) crypto_asset_price["Time"] = int(day_before_TS) crypto_asset_vol = pd.DataFrame(crypto_asset_vol, columns=time_header) crypto_asset_vol["Time"] = int(day_before_TS) # adding the Time column to the price ret df price_ret["Time"] = int(day_before_TS) # computing the Exponential Weighted Moving Average of the day hist_volume = query_mongo(DB_NAME, MONGO_DICT.get("coll_volume")) hist_volume = hist_volume.drop(columns=["Date"]) hist_volume = hist_volume.append(crypto_asset_vol) daily_ewma = daily_ewma_crypto_volume(hist_volume, CRYPTO_ASSET) # downloading from mongoDB the current logic matrices (1 e 2) logic_one = query_mongo(DB_NAME, MONGO_DICT.get("coll_log1")) # taking only the logic value referred to the current period current_logic_one = logic_one.iloc[[len(logic_one["Date"]) - 2]] current_logic_one = current_logic_one.drop(columns=["Date", "Time"]) logic_two = query_mongo(DB_NAME, MONGO_DICT.get("coll_log2")) # taking only the logic value referred to the current period current_logic_two = logic_two.iloc[[len(logic_two["Date"]) - 2]] current_logic_two = current_logic_two.drop(columns=["Date", "Time"]) # computing the ewma checked with both the first and second logic matrices daily_ewma_first_check = np.array(daily_ewma) * np.array(current_logic_one) daily_ewma_double_check = daily_ewma_first_check * \ np.array(current_logic_two) daily_ewma_double_check = pd.DataFrame(daily_ewma_double_check, columns=CRYPTO_ASSET) # compute the daily syntethic matrix yesterday_synt_matrix = query_mongo(DB_NAME, MONGO_DICT.get("coll_synt"), {"Date": two_before_human[0]}) yesterday_synt_matrix = yesterday_synt_matrix.drop( columns=["Date", "Time"]) daily_return = np.array(price_ret.loc[:, CRYPTO_ASSET]) new_synt = (1 + daily_return) * np.array(yesterday_synt_matrix) daily_synth = pd.DataFrame(new_synt, columns=CRYPTO_ASSET) # compute the daily relative syntethic matrix daily_tot = np.array(daily_synth.sum(axis=1)) daily_rel = np.array(daily_synth) / daily_tot daily_rel = pd.DataFrame(daily_rel, columns=CRYPTO_ASSET) # daily index value computation curr_divisor = query_mongo(DB_NAME, MONGO_DICT.get("coll_divisor_res"), {"Date": two_before_human[0]}) curr_div_val = np.array(curr_divisor["Divisor Value"]) index_numerator = np.array( crypto_asset_price[CRYPTO_ASSET]) * np.array(daily_rel) numerator_sum = index_numerator.sum(axis=1) num = pd.DataFrame(numerator_sum) daily_index_value = np.array(num) / curr_div_val raw_index_df = pd.DataFrame(daily_index_value, columns=["Index Value"]) # retrieving from mongoDB the yesterday value of the raw index yesterday_raw_index = query_mongo(DB_NAME, MONGO_DICT.get("coll_raw_index"), {"Date": two_before_human[0]}) yesterday_raw_index = yesterday_raw_index.drop(columns=["Date", "Time"]) raw_curr = yesterday_raw_index.append(raw_index_df) variation = raw_curr.pct_change() variation = np.array(variation.iloc[1]) # retrieving from mongoDB the yesterday value of the raw index yesterday_1000_index = query_mongo(DB_NAME, MONGO_DICT.get("coll_1000_index"), {"Date": two_before_human[0]}) daily_index_1000 = np.array( yesterday_1000_index["Index Value"]) * (1 + variation) daily_index_1000_df = pd.DataFrame(daily_index_1000, columns=["Index Value"]) index_daily_uploader(crypto_asset_price, crypto_asset_vol, exc_vol_tot, price_ret, daily_ewma, daily_ewma_double_check, daily_synth, daily_rel, curr_divisor, daily_index_1000_df, raw_index_df, day=day) return None
def index_daily_operation(crypto_asset, crypto_asset_price, crypto_asset_vol, day_before_TS, curr_reb_start, next_reb_start, curr_board_eve, logic_one_arr=None, day_type=None, day=None): two_before_TS = day_before_TS - DAY_IN_SEC two_before_human = timestamp_to_human([two_before_TS]) # turn prices and volumes into pandas dataframe crypto_asset_price = pd.DataFrame(crypto_asset_price, columns=crypto_asset) crypto_asset_vol = pd.DataFrame(crypto_asset_vol, columns=crypto_asset) # compute the price return of the day two_before_price = query_mongo(DB_NAME, MONGO_DICT.get("coll_price"), {"Time": two_before_TS}) two_before_price = two_before_price.drop(columns=["Time", "Date"]) return_df = two_before_price.append(crypto_asset_price) price_ret = return_df.pct_change() price_ret = price_ret.iloc[[1]] price_ret = price_ret.replace(-1, 0) price_ret.fillna(0, inplace=True) # then add the 'Time' column time_header = ["Time"] time_header.extend(crypto_asset) crypto_asset_price = pd.DataFrame(crypto_asset_price, columns=time_header) crypto_asset_price["Time"] = int(day_before_TS) crypto_asset_vol = pd.DataFrame(crypto_asset_vol, columns=time_header) crypto_asset_vol["Time"] = int(day_before_TS) # adding the Time column to the price ret df price_ret["Time"] = int(day_before_TS) # computing the Exponential Weighted Moving Average of the day hist_volume = query_mongo(DB_NAME, MONGO_DICT.get("coll_volume")) hist_volume = hist_volume.drop(columns=["Date"]) hist_volume = hist_volume.append(crypto_asset_vol) daily_ewma = daily_ewma_crypto_volume(hist_volume, crypto_asset) if day_type == "board": (first_logic_row, second_logic_row, weights_for_board, daily_ewma_double_check) = index_board_logic_op( crypto_asset, logic_one_arr, daily_ewma, curr_reb_start, next_reb_start, curr_board_eve) else: daily_ewma_double_check = index_norm_logic_op(crypto_asset, daily_ewma) first_logic_row = [] second_logic_row = [] # compute the daily syntethic matrix if day_type == "start_q": daily_synth = new_synth_op(crypto_asset, price_ret, SYNT_PTF_VALUE) else: yesterday_synt_matrix = query_mongo(DB_NAME, MONGO_DICT.get("coll_synt"), {"Date": two_before_human[0]}) yesterday_synt_matrix = yesterday_synt_matrix.drop( columns=["Date", "Time"]) daily_return = np.array(price_ret.loc[:, crypto_asset]) new_synt = (1 + daily_return) * np.array(yesterday_synt_matrix) daily_synth = pd.DataFrame(new_synt, columns=crypto_asset) # compute the daily relative syntethic matrix daily_tot = np.array(daily_synth.sum(axis=1)) daily_rel = np.array(daily_synth) / daily_tot daily_rel = pd.DataFrame(daily_rel, columns=crypto_asset) # daily index value computation if day_type == "start_q": new_divisor = new_divisor_comp(DB_NAME, two_before_price, crypto_asset) curr_divisor = new_divisor else: curr_divisor = query_mongo(DB_NAME, MONGO_DICT.get("coll_divisor_res"), {"Date": two_before_human[0]}) curr_div_val = np.array(curr_divisor["Divisor Value"]) index_numerator = np.array( crypto_asset_price[crypto_asset]) * np.array(daily_rel) numerator_sum = index_numerator.sum(axis=1) num = pd.DataFrame(numerator_sum) daily_index_value = np.array(num) / curr_div_val raw_index_df = pd.DataFrame(daily_index_value, columns=["Index Value"]) # retrieving from mongoDB the yesterday value of the raw index yesterday_raw_index = query_mongo(DB_NAME, MONGO_DICT.get("coll_raw_index"), {"Date": two_before_human[0]}) yesterday_raw_index = yesterday_raw_index.drop(columns=["Date", "Time"]) raw_curr = yesterday_raw_index.append(raw_index_df) variation = raw_curr.pct_change() variation = np.array(variation.iloc[1]) # retrieving from mongoDB the yesterday value of the raw index yesterday_1000_index = query_mongo(DB_NAME, MONGO_DICT.get("coll_1000_index"), {"Date": two_before_human[0]}) daily_index_1000 = np.array( yesterday_1000_index["Index Value"]) * (1 + variation) daily_index_1000_df = pd.DataFrame(daily_index_1000, columns=["Index Value"]) return (crypto_asset_price, crypto_asset_vol, price_ret, daily_ewma, daily_ewma_double_check, daily_synth, daily_rel, curr_divisor, first_logic_row, second_logic_row, weights_for_board, daily_index_1000_df, raw_index_df, new_divisor)