def __init__(self, name, price_series, degrees): self.stat_object = statistics.Calculate_Stats() self.helper = helper_methods.HelperMethods() # name and data self.name = name self.price_series = price_series self.degrees = degrees # price self.price_change = self.stat_object.helper.turn_prices_into_changes( self.price_series, permille=True) # statistical variables self.is_stationary, self.stat_sigma, self.stat_critical_value = self.stat_object.calculate_stationarity( self.price_change) self.is_random, self.rand_sigma, self.rand_critical_value = self.stat_object.calculate_randomness( self.stat_object.helper.remove_outliers(self.price_change)) self.is_independent, self.ind_sigma, self.ind_critical_value, self.dep_object = \ self.stat_object.calculate_relative_price_change_independence( self.stat_object.helper.remove_outliers(self.price_change), self.degrees)
def setUp(self): self.helper_methods_object = helper_methods.HelperMethods() self.stat_object = statistics.Calculate_Stats() self.test_data = self.helper_methods_object.parse_csv("test-data.csv") self.test_data_delta = self.helper_methods_object.turn_prices_into_changes(self.test_data, False) self.first_half_delta, self.second_half_delta = self.helper_methods_object.split_in_two(self.test_data_delta) self.first_half, self.second_half = self.helper_methods_object.split_in_two(self.test_data) self.first_freq = self.helper_methods_object.make_frequency_histogram(self.first_half) self.second_freq = self.helper_methods_object.make_frequency_histogram(self.second_half) self.first_cum = self.helper_methods_object.make_cumulative_probability_density_function(self.first_freq) self.second_cum = self.helper_methods_object.make_cumulative_probability_density_function(self.second_freq) self.is_stationary, self.stat_sigma, self.stat_critical_value = self.stat_object.calculate_stationarity(self.test_data) self.is_diff_independent, self.di_sigma, self.di_critical_value = self.stat_object.calculate_differential_spectrum_independence( self.test_data_delta) self.is_rel_independent, self.ri_sigma, self.ri_critical_value, self.dep_object= self.stat_object.calculate_relative_price_change_independence( self.test_data_delta, 2) self.is_rel_independent3, self.ri_sigma3, self.ri_critical_value3, self.dep_object = self.stat_object.calculate_relative_price_change_independence( self.test_data_delta, 3) self.is_random, self.rand_sigma, self.rand_critical_value = self.stat_object.calculate_randomness(self.test_data)
def main(): stat_object = statistics.Calculate_Stats() now = datetime.datetime.now() scrape_obj = scraper.Scraper() # stat_object.fourier_analysis(scrape_obj.get_price_history("ethereum")) coin_names = [ "ripple", "ethereum", "monero", "stellar", "nav-coin", "dash", "ethereum-classic", "lisk", "verge", "zcash", "stratis", "bitcoin", "litecoin" ] coins = [] for coin in coin_names: coins.append( crypto_object.CryptoObject(coin, scrape_obj.get_price_history(coin), 4)) # print(quick_report(coins, now)) OFFICIAL_BOA(coins, now)
def back_test(series, name): stat_object = statistics.Calculate_Stats() current_series = series current_name = name print(current_series.size) price_change = stat_object.helper.turn_prices_into_changes(current_series, permille=True) import time start_time = time.time() days_to_test = 150 for j in range(2, 8): boa_balance = 10000 inverse_boa_balance = 10000 likelihood_boa_balance = 10000 inverse_likelihood_balance = 10000 hodl_balance = 10000 boa_accuracy = 0 inv_boa_accuracy = 0 likeli_accuracy = 0 inv_likeli_accuracy = 0 hodl_accuracy = 0 order = j hodl_result = [] boa_result = [] for i in range(1, days_to_test + 1): if i % 30 == 0: print(i) up_to = current_series.size - (days_to_test - i + 1) coin = crypto_object.CryptoObject(current_name, current_series[:up_to], order) # calculate boa values up to current point prediction = coin.get_prediction(coin.get_most_recent_pattern()) num_prediction = coin.get_odds_of_increase( coin.get_most_recent_pattern()) current_change = ( 1.0 + price_change.values[(days_to_test - i + 1) * -1] / 1000.0) # decide buy or sell boa_balance *= current_change if (prediction is "Buy") else 1.0 inverse_boa_balance *= current_change if ( prediction is "Sell") else 1.0 likelihood_boa_balance *= current_change if ( num_prediction >= 50.0) else 1.0 inverse_likelihood_balance *= current_change if ( num_prediction < 50.0) else 1.0 hodl_balance *= current_change # add to the arrays hodl_result.append(hodl_balance) boa_result.append(boa_balance) # record accuracy boa_accuracy += 1 if ( (coin.get_numeric_prediction(coin.get_most_recent_pattern()) < 1.0 and current_change < 1.0) or (coin.get_numeric_prediction(coin.get_most_recent_pattern()) > 1.0 and current_change > 1.0)) else 0 inv_boa_accuracy += 1 if ( (coin.get_numeric_prediction(coin.get_most_recent_pattern()) > 1.0 and current_change < 1.0) or (coin.get_numeric_prediction(coin.get_most_recent_pattern()) < 1.0 and current_change > 1.0)) else 0 likeli_accuracy += 1 if ( num_prediction > .5 and current_change > 1.0) or ( num_prediction < .5 and current_change < 1.0) else 0 inv_likeli_accuracy += 1 if ( num_prediction < .5 and current_change > 1.0) or ( num_prediction > .5 and current_change < 1.0) else 0 hodl_accuracy += 1 if current_change > 1.0 else 0 print(prediction, "BOA: ", '%.2f' % boa_balance, "INV BOA: ", '%.2f' % inverse_boa_balance, "Likeli: ", '%.2f' % likelihood_boa_balance, "Inv Likeli: ", '%.2f' % inverse_likelihood_balance, "Hodl: ", '%.2f' % hodl_balance, coin.get_stationarity_as_string(), coin.get_stat_sigma(), coin.get_stat_critical_value(), " boa accuracy ", boa_accuracy, " inv boa accuracy ", inv_boa_accuracy, " likeli accuracy ", likeli_accuracy, " inv likeli accuracy ", inv_likeli_accuracy, " hodl accuracy ", hodl_accuracy) # Timer block m, s = divmod(time.time() - start_time, 60) h, m = divmod(m, 60) results = "The " + coin.get_name()+" results for " + str(days_to_test)+ " days and " + str(order) + " degrees are: BOA Final Balance: " + \ '%.2f' % boa_balance + "INV BOA: " + '%.2f' % inverse_boa_balance + "Likeli: " + '%.2f' % likelihood_boa_balance + "Inv Likeli: " + '%.2f' % inverse_likelihood_balance + " Hodl Final Balance: " + '%.2f' %(hodl_balance) + \ " Boa Accuracy: " + str(boa_accuracy) + " inv boa accuracy " + str(inv_boa_accuracy) + " likeli accuracy " + str(likeli_accuracy) + " inv likeli accuracy " + str(inv_likeli_accuracy) + " Hodl accuracy: " + str(hodl_accuracy) + "|| Series Size "+ str(current_series.size) + " Elapsed time: " + str("%d:%02d:%02d" % (h, m, s)) + "\n" f = open('backtest.txt', 'a+') f.write(results) f.close() import winsound frequency = 2500 # Set Frequency To 2500 Hertz duration = 500 # Set Duration To 1000 ms == 1 second winsound.Beep(frequency, duration) # pyplot.plot(hodl_result) # pyplot.plot(boa_result) # pyplot.show() print(results)
def backtest_alpha(): stat_object = statistics.Calculate_Stats() now = datetime.datetime.now() scraper_obj = scraper.Scraper() current_name = [ "ripple", "ethereum", "monero", "stellar", "nav-coin", "dash", "ethereum-classic", "lisk", "verge", "zcash","stratis", "bitcoin", "litecoin" ] current_degree = [] for i in range(current_name.__len__()): current_degree.append(4) current_series = [] price_change = [] for name in current_name: new_name = scraper_obj.get_price_history(name) current_series.append(new_name) price_change.append(stat_object.helper.turn_prices_into_changes(new_name, permille=True)) import time start_time = time.time() today = datetime.date.today() begin = datetime.date(2018, 1, 1) days_to_test = (today - begin).days print(days_to_test) for j in range(4, 5): boa_balance = 9999 classic_boa_balance = [] hodl_balance = [] for i in range(current_name.__len__()): classic_boa_balance.append(9999/current_name.__len__()) hodl_balance.append(9999/current_name.__len__()) random_balance = 9999 boa_accuracy = 0 random_accuracy = 0 hodl_result = [] random_result = [] boa_result = [] boa_classic_result = [] fee = 0.001 for i in range(1, days_to_test + 1-26): coins = [] for k in range(current_name.__len__()): up_to = current_series[k].size - (days_to_test - i + 1) coin = crypto_object.CryptoObject(current_name[k], current_series[k][:up_to], current_degree[k]) coins.append(coin) # calculate boa values up to current point prediction_array = [] for coin in coins: if coin.get_stationarity(): prediction_array.append(coin.get_numeric_prediction(coin.get_most_recent_pattern())) else: prediction_array.append(0.0) current_change = [] for change in price_change: current_change.append(( 1.0 + change.values[(days_to_test - i + 1) * -1] / 1000.0)) # decide buy or sell #boa_pick = current_change[prediction_array.index(max(prediction_array))]-fee boa_pick = 0 counter = 0 for val in range(prediction_array.__len__()): if prediction_array[val] >= 1.0: boa_pick += current_change[val] counter += 1 boa_balance *= boa_pick/counter if max(prediction_array) > 1.0 else 1.0 print(boa_pick/counter) for k in range(current_name.__len__()): hodl_balance[k] *= current_change[k] classic_boa_balance[k] *= current_change[k] if prediction_array[k] > 1.0 else 1.0 random_pick = randint(0, current_name.__len__()-1) random_value = current_change[random_pick] random_balance *= random_value # add to the arrays hodl_result.append(sum(hodl_balance)) boa_classic_result.append(sum(classic_boa_balance)) random_result.append(random_balance) boa_result.append(boa_balance) # record accuracy boa_accuracy += 1 if ( boa_pick + fee >= max(current_change)) else 0 random_accuracy += 1 if ( random_value >= max(current_change)) else 0 print_val = str(i) + ". \n" print_val2 = "" for k in range(current_name.__len__()): print_val += '%.2f' % prediction_array[k] + "(" + str(current_change[k]) + ")" print_val2 += coins[k].get_name() + ": " + coins[k].get_stationarity_as_string() + coins[k].get_stat_sigma() + coins[k].get_stat_critical_value() print_val += "BOA: " + '%.2f' % boa_balance + "BOA Classic: " + '%.2f' % (sum(classic_boa_balance)) + "Hodl: " + \ '%.2f' % (sum(hodl_balance)) + "Random: " + '%.2f' % random_balance + print_val2 print(print_val) # Timer block m, s = divmod(time.time() - start_time, 60) h, m = divmod(m, 60) results = "\n=======BACK TEST RESULTS " + now.strftime("%Y-%m-%d") + "=======\n\n" \ "The results for a portfolio containing " + str(current_name) + " with degrees " + str(current_degree) + " are" \ " BOA Final Balance: " + '%.2f' % boa_balance + " Random Final Balance: " + '%.2f' % random_balance + " Hodl Final Balance: " + \ '%.2f' % sum(hodl_balance) + " Boa Accuracy: " + str(boa_accuracy) + " Random accuracy: " + str(random_accuracy) + \ " Elapsed time: " + str("%d:%02d:%02d" % (h, m, s)) + "\n" f = open('backtest_alpha.txt', 'a+') f.write(results) f.close() import winsound frequency = 2500 # Set Frequency To 2500 Hertz duration = 1000 # Set Duration To 1000 ms == 1 second winsound.Beep(frequency, duration) Hold= go.Scatter(x=current_series[0].index.values[current_series[0].size-days_to_test:], y=hodl_result, name= "Buy and Hold") BOA = go.Scatter(x=current_series[0].index.values[current_series[0].size-days_to_test:], y=boa_result, name="BOA") layout = go.Layout( title='BOA Results 2017', xaxis=dict( title='Time', titlefont=dict( family='Courier New, monospace', size=18, color='#7f7f7f' ) ), yaxis=dict( title='Portfolio Value', titlefont=dict( family='Courier New, monospace', size=18, color='#7f7f7f' ) ) ) figure = go.Figure(data=[Hold, BOA],layout=layout) url = py.offline.plot(figure, image_height=400, image_width=400, auto_open=False, filename='results2018.html', ) # pyplot.plot(hodl_result) # pyplot.plot(hodl_card_result) # pyplot.plot(boa_result) # pyplot.show() print(results)