def compare_exchanges(): exchanges, time_list, prices, volumes, total_price, total_volume = legacy.get_lists_legacy() bitstamp_price = prices[0, :] bitstamp_volume = volumes[0, :] bitstamp_returns = jake_supp.logreturn(bitstamp_price) btce_price = prices[1, :] btce_volume = volumes[1, :] btce_returns = jake_supp.logreturn(btce_price) combined_stats(bitstamp_returns, btce_returns, name1="Bitstamp", name2="List 2")
n_exc = 1 intraday = 0 intraweek = 0 plots = 0 print_table = 0 time_list_minutes, prices, volumes = data_import.fetch_aggregate_csv(file_name, n_exc) y, mo, d, h, mi = supp.fix_time_list(time_list_minutes, move_n_hours=0) time_list_minutes = supp.make_time_list(y, mo, d, h, mi ) time_listH, returnsH, spreadH, log_volumesH, illiqH, log_illiqH, rvolH, log_rvolH = dis.clean_series_hour(time_list_minutes, prices, volumes) prices_minutes = prices[0, :] volumes_minutes = volumes[0, :] returns_minutes = jake_supp.logreturn(prices_minutes) time_list_hours, prices_hours, volumes_hours = dis.convert_to_hour(time_list_minutes, prices_minutes, volumes_minutes) spread_hours = rolls.rolls(prices_minutes, time_list_minutes, calc_basis="h", kill_output=1)[1] illiq_hours_time, illiq_hours = ILLIQ.illiq(time_list_minutes, returns_minutes, volumes_minutes, hourly_or_daily="h", threshold=0) rvol_hours, time_list_hours_rvol = realized_volatility.RVol(time_list_minutes, prices_minutes, daily=0, annualize=1) if intraday == 1: time_list_removed = [] time_list_hours, time_list_removed, volumes_hours, prices_hours, rvol_hours, spread_hours = supp.remove_list1_zeros_from_all_lists(time_list_hours, time_list_removed, volumes_hours, prices_hours, rvol_hours, spread_hours) time_list_hours, time_list_removed, rvol_hours, volumes_hours, prices_hours, spread_hours, illiq_hours = supp.remove_list1_zeros_from_all_lists(time_list_hours, time_list_removed, rvol_hours, volumes_hours, prices_hours, spread_hours, illiq_hours) time_list_hours, time_list_removed, spread_hours, rvol_hours, volumes_hours, prices_hours, illiq_hours = supp.remove_list1_zeros_from_all_lists(time_list_hours, time_list_removed, spread_hours, rvol_hours, volumes_hours, prices_hours, illiq_hours) time_list_hours, time_list_removed, illiq_hours, spread_hours, rvol_hours, volumes_hours, prices_hours = supp.remove_list1_zeros_from_all_lists(time_list_hours, time_list_removed, illiq_hours, spread_hours, rvol_hours, volumes_hours, prices_hours) print()
def clean_series_hour(time_listM, pricesM, volumesM, exc=0, convert_time_zones=1, plot_for_extreme=0): remove_extremes = 1 print(" \033[32;0;0mRunning 'clean_series_hour' ...\033[0;0;0m") if convert_time_zones: # Flytter nå Coincheck ni timer, men lar Bitstamp stå if exc == 0 or exc == 5: n_hours = 1 elif exc == 1: n_hours = 9 elif exc == 2: n_hours = 8 elif exc == 3: n_hours = -5 elif exc == 4: n_hours = 9 else: n_hours = 0 print(" Converting time zones: moving series %i hour(s)" % n_hours) else: n_hours = 0 if n_hours != 0: year, month, day, hour, minute = supp.fix_time_list( time_listM, move_n_hours=n_hours) time_listM = supp.make_time_list( year, month, day, hour, minute) # Lager en ny tidsliste fra de flyttede listene returnsM = jake_supp.logreturn(pricesM) time_listH, pricesH, volumesH = convert_to_hour(time_listM, pricesM, volumesM) returnsH = jake_supp.logreturn(pricesH) spread_abs, spreadH, time_list_spread, count_value_error = rolls.rolls( pricesM, time_listM, calc_basis="h", kill_output=1) illiq_hours_time, illiqH = ILLIQ.illiq(time_listM, returnsM, volumesM, hourly_or_daily="h", threshold=0) rvolH, time_list_rvol = realized_volatility.RVol(time_listM, pricesM, daily=0, annualize=1) time_list_removed = [] # Removing all hours where Volume is zero time_listH, time_list_removed, volumesH, spreadH, returnsH, rvolH = supp.remove_list1_zeros_from_all_lists( time_listH, time_list_removed, volumesH, spreadH, returnsH, rvolH) print(" dis.%i: Number of hours removed due to zero-volume: %i" % (gf(cf()).lineno, len(time_list_removed))) end_time = "" if exc == 0: cutoff_date = "01.01.2013 00:00" start_averaging_date = "01.01.2012 00:00" elif exc == 1: cutoff_date = "01.06.2016 00:00" start_averaging_date = "30.10.2014 00:00" elif exc == 2: cutoff_date = "01.01.2013 00:00" start_averaging_date = "01.01.2012 00:00" end_time = "29.09.2017 00:00" elif exc == 3: cutoff_date = "01.01.2015 00:00" start_averaging_date = "02.12.2014 00:00" elif exc == 4: cutoff_date = "01.01.2014 00:00" start_averaging_date = "01.10.2013 00:00" elif exc == 5: cutoff_date = "01.01.2015 00:00" start_averaging_date = "01.01.2014 00:00" else: print(" TEST SET") cutoff_date = "01.01.2017 00:00" start_averaging_date = "01.01.2017 00:00" cutoff_hour = supp.find_date_index(cutoff_date, time_listH, next_date=1) start_averaging_hour = supp.find_date_index(start_averaging_date, time_listH, next_date=1) if len(end_time) > 1: end_hour = supp.find_date_index(end_time, time_listH) else: end_hour = len(time_listH) - 1 mean_volume_prev_year = np.average( volumesH[start_averaging_hour:cutoff_hour]) time_listH = time_listH[cutoff_hour:end_hour] print(" Time period:", time_listH[0], "to", time_listH[-1]) returnsH = returnsH[cutoff_hour:end_hour] volumesH = volumesH[cutoff_hour:end_hour] spreadH = spreadH[cutoff_hour:end_hour] illiqH = illiqH[cutoff_hour:end_hour] rvolH = rvolH[cutoff_hour:end_hour] if plot_for_extreme == 1: plt.plot(rvolH) plt.title("Raw rvol") plt.figure() plt.plot(spreadH) plt.title("Raw spreadH") plt.figure() plt.plot(volumesH) plt.title("Raw volume") plt.figure() plt.plot(illiqH) plt.title("Raw illiq") plt.figure() plt.plot(returnsH) plt.title("Raw returnsH") plt.figure() hours_to_remove = [] if remove_extremes == 1: if exc == 0: hours_to_remove = supp.remove_extremes(hours_to_remove, returnsH, 0.1, threshold_lower=-0.1) hours_to_remove = supp.remove_extremes(hours_to_remove, rvolH, 2) hours_to_remove = supp.remove_extremes(hours_to_remove, spreadH, 0.01) hours_to_remove = supp.remove_extremes(hours_to_remove, illiqH, 0.1) elif exc == 1: hours_to_remove = supp.remove_extremes(hours_to_remove, returnsH, 0.075, threshold_lower=-0.075) hours_to_remove = supp.remove_extremes(hours_to_remove, rvolH, 2) hours_to_remove = supp.remove_extremes(hours_to_remove, spreadH, 0.1) hours_to_remove = supp.remove_extremes(hours_to_remove, illiqH, 0.1) elif exc == 2: hours_to_remove = supp.remove_extremes(hours_to_remove, returnsH, 0.075, threshold_lower=-0.075) hours_to_remove = supp.remove_extremes(hours_to_remove, rvolH, 2) hours_to_remove = supp.remove_extremes(hours_to_remove, spreadH, 0.1) hours_to_remove = supp.remove_extremes(hours_to_remove, illiqH, 0.1) elif exc == 3: hours_to_remove = supp.remove_extremes(hours_to_remove, returnsH, 0.075, threshold_lower=-0.075) hours_to_remove = supp.remove_extremes(hours_to_remove, rvolH, 2) hours_to_remove = supp.remove_extremes(hours_to_remove, volumesH, 15000) hours_to_remove = supp.remove_extremes(hours_to_remove, spreadH, 0.1) hours_to_remove = supp.remove_extremes(hours_to_remove, illiqH, 0.02) elif exc == 4: hours_to_remove = supp.remove_extremes(hours_to_remove, returnsH, 0.075, threshold_lower=-0.075) hours_to_remove = supp.remove_extremes(hours_to_remove, rvolH, 2) hours_to_remove = supp.remove_extremes(hours_to_remove, volumesH, 15000) hours_to_remove = supp.remove_extremes(hours_to_remove, spreadH, 0.1) hours_to_remove = supp.remove_extremes(hours_to_remove, illiqH, 0.02) time_listH = np.delete(time_listH, hours_to_remove) returnsH = np.delete(returnsH, hours_to_remove) volumesH = np.delete(volumesH, hours_to_remove) spreadH = np.delete(spreadH, hours_to_remove) illiqH = np.delete(illiqH, hours_to_remove) rvolH = np.delete(rvolH, hours_to_remove) if plot_for_extreme == 1: plt.plot(rvolH) plt.title("rvol") plt.figure() plt.plot(spreadH) plt.title("spreadH") plt.figure() plt.plot(volumesH) plt.title("volume") plt.figure() plt.plot(illiqH) plt.title("illiq") plt.figure() plt.plot(returnsH) plt.title("returnsH") plt.show() # Removing all days where Roll is zero time_listH, time_list_removed, spreadH, volumesH, returnsH, illiqH, rvolH = supp.remove_list1_zeros_from_all_lists( time_listH, time_list_removed, spreadH, volumesH, returnsH, illiqH, rvolH) # Removing all hours where Rvol is zero time_listH, time_list_removed, rvolH, spreadH, volumesH, returnsH, illiqH = supp.remove_list1_zeros_from_all_lists( time_listH, time_list_removed, rvolH, spreadH, volumesH, returnsH, illiqH) # Removing all hours where ILLIQ is zero time_listH, time_list_removed, illiqH, rvolH, spreadH, volumesH, returnsH = supp.remove_list1_zeros_from_all_lists( time_listH, time_list_removed, illiqH, rvolH, spreadH, volumesH, returnsH) # Turning ILLIQ, Volume and rvol into log log_illiqH = np.log(illiqH) log_volumesH = volume_transformation(volumesH, mean_volume_prev_year) log_rvolH = np.log(rvolH) #print(" dis.%i: Length of time %i, spread %i, rvol %i, illiq %i, and log_illiq %i" % (gf(cf()).lineno, len(time_listH), len(spreadH), len(rvolH), len(illiqH), len(log_illiqH))) print(" \033[32;0;0mFinished running 'clean_series_hour' ...\033[0;0;0m") return time_listH, returnsH, spreadH, volumesH, log_volumesH, illiqH, log_illiqH, rvolH, log_rvolH
def clean_series_days(time_listM, pricesM, volumesM, exc=0, print_days_excluded=0, convert_time_zones=1, plot_for_extreme=0): print(" \033[32;0;0mRunning 'clean_series_days' ...\033[0;0;0m") if convert_time_zones: if exc == 0 or exc == 5: n_hours = 1 elif exc == 1: n_hours = 9 elif exc == 2: n_hours = 8 elif exc == 3: n_hours = -5 elif exc == 4: n_hours = 9 else: n_hours = 0 print(" Converting time zones: moving series %i hour(s)" % n_hours) else: n_hours = 0 if n_hours != 0: year, month, day, hour, minute = supp.fix_time_list( time_listM, move_n_hours=n_hours) time_listM = supp.make_time_list(year, month, day, hour, minute) time_listD, pricesD, volumesD = convert_to_day(time_listM, pricesM, volumesM) end_time_D = "" if exc == 0: cutoff_date = "01.01.2013 00:00" cutoff_min_date = "01.01.2013 01:00" start_averaging_date = "01.01.2012 00:00" elif exc == 1: cutoff_date = "01.01.2016 00:00" cutoff_min_date = "01.01.2016 09:00" start_averaging_date = "30.10.2014 00:00" elif exc == 2: cutoff_date = "01.01.2013 00:00" cutoff_min_date = "01.01.2013 08:00" end_time_D = "01.01.2017 00:00" end_time_M = "01.01.2017 08:00" start_averaging_date = "01.01.2012 00:00" elif exc == 3: cutoff_date = "01.01.2015 00:00" cutoff_min_date = "01.01.2015 19:00" start_averaging_date = "02.12.2014 00:00" elif exc == 4: cutoff_date = "01.01.2014 00:00" cutoff_min_date = "01.01.2014 09:00" start_averaging_date = "01.10.2013 00:00" elif exc == 5: cutoff_date = "01.01.2015 00:00" cutoff_min_date = "01.01.2015 01:00" start_averaging_date = "01.03.2014 00:00" else: print(" TEST SET") cutoff_date = "01.01.2017 00:00" cutoff_min_date = "01.01.2017 00:00" start_averaging_date = "01.01.2017 00:00" cutoff_day = supp.find_date_index(cutoff_date, time_listD, next_date=1) cutoff_min = supp.find_date_index(cutoff_min_date, time_listM, next_date=1) start_averaging_day = supp.find_date_index(start_averaging_date, time_listD, next_date=1) mean_volume_prev_year = np.average( volumesD[start_averaging_day:cutoff_day]) if len(end_time_D) > 1: cutoff_endD = supp.find_date_index(end_time_D, time_listD) cutoff_endM = supp.find_date_index(end_time_M, time_listM) else: cutoff_endD = len(time_listD) cutoff_endM = len(time_listM) time_listM = time_listM[cutoff_min:cutoff_endM] print(" Time period:", time_listM[0], "to", time_listM[len(time_listM) - 1]) pricesM = pricesM[cutoff_min:cutoff_endM] volumesM = volumesM[cutoff_min:cutoff_endM] pricesD = pricesD[cutoff_day:cutoff_endD] volumesD = volumesD[cutoff_day:cutoff_endD] time_listD = time_listD[cutoff_day:cutoff_endD] # Rolls spread_abs, spreadD, time_list_rolls, count_value_error = rolls.rolls( pricesM, time_listM, calc_basis="d", kill_output=1) # Realized volatility rvolD, RVol_time = realized_volatility.RVol(time_listM, pricesM, daily=1, annualize=1) # Returns returnsM = jake_supp.logreturn(pricesM) returnsD = jake_supp.logreturn(pricesD) # Amihud's ILLIQ illiq_timeD, illiqD = ILLIQ.illiq(time_listM, returnsM, volumesM, threshold=0) # Already clean if plot_for_extreme == 1: plt.plot(rvolD) plt.title("Raw rvol") plt.figure() plt.plot(spreadD) plt.title("Raw spreadH") plt.figure() plt.plot(volumesD) plt.title("Raw volume") plt.figure() plt.plot(illiqD) plt.title("Raw illiq") plt.figure() plt.plot(returnsD) plt.title("Raw returnsH") plt.figure() time_list_removed = [] # Removing all days where Volume is zero time_listD, time_list_removed, volumesD, spreadD, returnsD, rvolD = supp.remove_list1_zeros_from_all_lists( time_listD, time_list_removed, volumesD, spreadD, returnsD, rvolD) # -------------------------------------------- days_to_remove = [] if exc == 0: days_to_remove = supp.remove_extremes(days_to_remove, returnsD, 0.1, threshold_lower=-0.1) days_to_remove = supp.remove_extremes(days_to_remove, rvolD, 2) days_to_remove = supp.remove_extremes(days_to_remove, spreadD, 0.01) days_to_remove = supp.remove_extremes(days_to_remove, illiqD, 0.1) elif exc == 1: days_to_remove = supp.remove_extremes(days_to_remove, returnsD, 0.1, threshold_lower=-0.1) days_to_remove = supp.remove_extremes(days_to_remove, rvolD, 2) days_to_remove = supp.remove_extremes(days_to_remove, spreadD, 0.01) days_to_remove = supp.remove_extremes(days_to_remove, illiqD, 0.1) elif exc == 2: days_to_remove = supp.remove_extremes(days_to_remove, returnsD, 0.1, threshold_lower=-0.1) days_to_remove = supp.remove_extremes(days_to_remove, rvolD, 2) days_to_remove = supp.remove_extremes(days_to_remove, spreadD, 0.01) days_to_remove = supp.remove_extremes(days_to_remove, illiqD, 0.1) elif exc == 3: days_to_remove = supp.remove_extremes(days_to_remove, returnsD, 0.1, threshold_lower=-0.1) days_to_remove = supp.remove_extremes(days_to_remove, rvolD, 2) days_to_remove = supp.remove_extremes(days_to_remove, spreadD, 0.01) days_to_remove = supp.remove_extremes(days_to_remove, volumesD, 50000) days_to_remove = supp.remove_extremes(days_to_remove, illiqD, 0.01) elif exc == 4: days_to_remove = supp.remove_extremes(days_to_remove, returnsD, 0.1, threshold_lower=-0.1) days_to_remove = supp.remove_extremes(days_to_remove, rvolD, 2) days_to_remove = supp.remove_extremes(days_to_remove, spreadD, 0.01) days_to_remove = supp.remove_extremes(days_to_remove, volumesD, 50000) days_to_remove = supp.remove_extremes(days_to_remove, illiqD, 0.01) elif exc == 5: days_to_remove = supp.remove_extremes(days_to_remove, returnsD, 0.1, threshold_lower=-0.1) days_to_remove = supp.remove_extremes(days_to_remove, rvolD, 2) days_to_remove = supp.remove_extremes(days_to_remove, spreadD, 0.01) days_to_remove = supp.remove_extremes(days_to_remove, volumesD, 50000) days_to_remove = supp.remove_extremes(days_to_remove, illiqD, 0.01) for d in days_to_remove: time_list_removed = np.append(time_list_removed, time_listD[d]) time_listD = np.delete(time_listD, days_to_remove) returnsD = np.delete(returnsD, days_to_remove) volumesD = np.delete(volumesD, days_to_remove) spreadD = np.delete(spreadD, days_to_remove) rvolD = np.delete(rvolD, days_to_remove) illiqD = np.delete(illiqD, days_to_remove) illiq_timeD = np.delete(illiq_timeD, days_to_remove) if plot_for_extreme == 1: plt.plot(rvolD) plt.title("rvol") plt.figure() plt.plot(spreadD) plt.title("spreadH") plt.figure() plt.plot(volumesD) plt.title("volume") plt.figure() plt.plot(illiqD) plt.title("illiq") plt.figure() plt.plot(returnsD) plt.title("returnsH") plt.show() # Removing all days where Roll is zero time_listD, time_list_removed, spreadD, volumesD, returnsD, \ rvolD, illiqD = supp.remove_list1_zeros_from_all_lists(time_listD, time_list_removed, spreadD, volumesD, returnsD, rvolD, illiqD) # Removing all days where Volatility is zero time_listD, time_list_removed, rvolD, volumesD, returnsD, \ spreadD, illiqD = supp.remove_list1_zeros_from_all_lists(time_listD, time_list_removed, rvolD, volumesD, returnsD, spreadD, illiqD) # Turning ILLIQ, Volume and RVol into log log_illiqD = np.log(illiqD) log_rvolD = np.log(rvolD) log_volumesD = volume_transformation(volumesD, mean_volume_prev_year) print( " dis.%i: Length of time %i, spread %i, rvol %i, illiq %i, and log_illiq %i" % (gf(cf()).lineno, len(time_listD), len(spreadD), len(rvolD), len(illiqD), len(log_illiqD))) print(" \033[32;0;0m Finished running 'clean_series_days' ...\033[0;0;0m") return time_listD, returnsD, volumesD, log_volumesD, spreadD, illiqD, log_illiqD, rvolD, log_rvolD
from Jacob import jacob_support as jacsup import numpy as np import matplotlib.pyplot as plt import os os.chdir("/Users/Jacob/Documents/GitHub/krypto") exchanges, time_list, prices, volumes = legacy.get_lists_legacy(data="all", opening_hours="y", make_totals="n") prices = prices[0][101790:] # exchanges = ["bitstampusd", "btceusd", "coinbaseusd", "krakenusd" volumes = volumes[0][101790:] time_list = time_list[101790:] returns = jacsup.logreturn(prices) print("length of prices",len(prices)) print("length of volume",len(volumes)) print("length of returnsH", len(returns)) <<<<<<< HEAD time_illiq, illiq = ilq.illiq(time_list, returns, volumes, day_or_hour=1, kill_output=0) ======= time_illiq, illiq = ilq.illiq(time_list, returns, volumes, hourly_or_daily=0, kill_output=0) >>>>>>> master plt.plot(illiq) plt.show() """
#RAW raw = 1 combined = 1 exchanges, time_list_minutes, prices_minutes, volumes_minutes = legacy.get_lists_legacy( opening_hours="n", make_totals="n") print() raw = 0 clean = 1 if raw == 1: # RAW time_list_days, prices_days, volumes_days = dis.convert_to_day( time_list_minutes, prices_minutes, volumes_minutes) returns_minutes = jake_supp.logreturn(prices_minutes[exc, :]) returns_days = jake_supp.logreturn(prices_days[exc, :]) spread_days = rolls.rolls(prices_minutes[exc, :], time_list_minutes, calc_basis=1, kill_output=1)[1] illiq_days_time, illiq_days = ILLIQ.illiq(time_list_minutes, returns_minutes, volumes_minutes[exc, :], hourly_or_daily=1) # Realized volatility volatility_days, rVol_time = realized_volatility.RVol( time_list_minutes, prices_minutes[exc, :]) # Annualize the volatility volatility_days = np.multiply(volatility_days, 365**0.5)
import numpy as np import data_import as di import legacy from Jacob import jacob_support as jake_supp import data_import_support as dis from matplotlib import pyplot as plt import os os.chdir("/Users/sondre/Documents/GitHub/krypto") # Blir ikke likt på deres pc exchanges = ["bitstampusd", "btceusd", "coinbaseusd", "krakenusd"] # Importing data for all minutes of the day exchanges, time_list, prices, volumes = legacy.get_lists_legacy(opening_hours="n", make_totals="n") # Converting to hourly data time_list, prices, volumes = dis.convert_to_hour(time_list, prices, volumes) full_returns = jake_supp.logreturn(prices[0, :]) # Only extracting opening hours time_list_open, prices_open, volumes_opeen = legacy.opening_hours_w_weekends(time_list, prices, volumes) bitstamp_price_open = np.transpose(prices_open[0, :]) open_returns = jake_supp.logreturn(bitstamp_price_open) # Finding average for entire day full_day_time, full_day_avg_returns = dis.cyclical_average_legacy(time_list, full_returns) # Finding average for opening hours open_time, open_avg_returns = dis.cyclical_average_legacy(time_list_open, open_returns) # Want to compare the two in a single graph x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23] opening_y = np.zeros(24) for i in range(0, 7):