From f228bbb6a8c1d192c9f16f6a1c9a56d9a69648c1 Mon Sep 17 00:00:00 2001 From: Bumbelo Date: Sat, 30 May 2020 23:59:09 +0200 Subject: [PATCH 1/6] add gather data from api, analyze data, make plot --- currency_simulator.py | 111 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 currency_simulator.py diff --git a/currency_simulator.py b/currency_simulator.py new file mode 100644 index 00000000..177bd9f5 --- /dev/null +++ b/currency_simulator.py @@ -0,0 +1,111 @@ +from datetime import timezone, datetime +import requests +import matplotlib.pyplot as plt +import numpy as np + + + +def convert_date_to_utc(year, month, day, hour=0, minutes=0, seconds=0): + dt = datetime(year, month, day, hour, minutes, seconds) + timestamp = dt.replace(tzinfo=timezone.utc).timestamp() + return int(timestamp) + + +def convert_utc_to_date(timestamp): + return datetime.fromtimestamp(timestamp) + + +def gather_data(currency_pair, start, end, step): + limit = (end - start) // step + 1 + bitstamp = requests.get("https://www.bitstamp.net/api/v2/ohlc/{0}/".format(currency_pair), + params={'start': start, 'end': end, 'step': step, 'limit': limit}) + bitstamp_json = bitstamp.json() + return bitstamp_json + + +def analyze_data(currency_pair, start, end, step): + data = gather_data(currency_pair, start, end, step) + # print(data) + make_plot(data['data']['ohlc']) + simulate(data) + + +def simulate(data): + differences = [] + data = data['data']['ohlc'] + print(data) + previous_data = data[0] + print(previous_data) + for period in data[1::]: + curr_timestamp = int(period['timestamp']) + prev_close = float(previous_data['close']) + prev_volume = float(previous_data['volume']) + curr_close = float(period['close']) + curr_volume = float(period['volume']) + close_diff = curr_close - prev_close + volume_diff = curr_volume - prev_volume + close_percentage_change = close_diff / prev_close*100 + volume_percentage_change = volume_diff / prev_volume*100 + differences.append({ + 'timestamp': curr_timestamp, + 'close': curr_close, + 'volume': curr_volume, + 'close_diff': close_diff, + 'volume_diff': volume_diff, + 'close_percentage_change': close_percentage_change, + 'volume_percentage_change': volume_percentage_change + }) + print("CLOSING PRICE: {0}".format(curr_close)) + print("VOLUME: {0}".format(curr_volume)) + print("CLOSING PRICE DIFF: {0}".format(curr_close - prev_close)) + print("VOLUME DIFF: {0}".format(curr_volume - prev_volume)) + print(period) + previous_data = period + for difference in differences: + print(difference) + make_prediction(differences, 5, 4) + + +def make_prediction(data, last_timestamps, next_timestamps): + # for i in range(next_timestamps) + print("----------------") + data = data[-last_timestamps::] + print(data) + close = [single_data['close'] for single_data in data] + print(np.mean(close)) + print(np.median(close)) + print(np.std(close)) + + +def make_plot(data): + closing_prices = [float(period['close']) for period in data] + volumes = [float(period['volume']) for period in data] + timestamps = [float(period['timestamp']) for period in data] + date_labels = map(convert_utc_to_date, timestamps) + date_labels = list(date_labels) + fig, ax1 = plt.subplots() + color = 'tab:red' + ax1.set_xlabel('date') + ax1.set_ylabel('volume', color=color) + ax1.set_ylim([0, 3 * max(volumes)]) + ax1.bar(date_labels, volumes, color=color) + ax1.tick_params(axis='y', labelcolor=color) + + ax2 = ax1.twinx() + + color = 'tab:blue' + ax2.set_ylabel('closing price', color=color) + ax2.plot(date_labels, closing_prices, color=color) + ax2.tick_params(axis='y', labelcolor=color) + + fig.tight_layout() + plt.gcf().autofmt_xdate() + plt.show() + + +def main(): + analyze_data("btcusd", convert_date_to_utc(2020,3,15), convert_date_to_utc(2020,5,29), 3600 * 24) + + +if __name__ == "__main__": + main() From bcffc3e6178fa4e5b929d98872cd249fd16dd94e Mon Sep 17 00:00:00 2001 From: Bumbelo Date: Sun, 31 May 2020 05:05:05 +0200 Subject: [PATCH 2/6] added single iteration simulation --- currency_simulator.py | 93 ++++++++++++++++++++++++++++++++----------- 1 file changed, 70 insertions(+), 23 deletions(-) diff --git a/currency_simulator.py b/currency_simulator.py index 177bd9f5..24aec658 100644 --- a/currency_simulator.py +++ b/currency_simulator.py @@ -2,7 +2,9 @@ import requests import matplotlib.pyplot as plt import numpy as np +import random +step = 300 def convert_date_to_utc(year, month, day, hour=0, minutes=0, seconds=0): @@ -15,7 +17,7 @@ def convert_utc_to_date(timestamp): return datetime.fromtimestamp(timestamp) -def gather_data(currency_pair, start, end, step): +def gather_data(currency_pair, start, end): limit = (end - start) // step + 1 bitstamp = requests.get("https://www.bitstamp.net/api/v2/ohlc/{0}/".format(currency_pair), params={'start': start, 'end': end, 'step': step, 'limit': limit}) @@ -23,14 +25,14 @@ def gather_data(currency_pair, start, end, step): return bitstamp_json -def analyze_data(currency_pair, start, end, step): - data = gather_data(currency_pair, start, end, step) +def analyze_data(currency_pair, start, end, last_timestamps, next_timestamps): + data = gather_data(currency_pair, start, end) # print(data) - make_plot(data['data']['ohlc']) - simulate(data) + simulated_data = simulate(data, last_timestamps, next_timestamps) + make_plot(simulated_data, end) -def simulate(data): +def simulate(data, last_timestamps, next_timestamps): differences = [] data = data['data']['ohlc'] print(data) @@ -44,8 +46,8 @@ def simulate(data): curr_volume = float(period['volume']) close_diff = curr_close - prev_close volume_diff = curr_volume - prev_volume - close_percentage_change = close_diff / prev_close*100 - volume_percentage_change = volume_diff / prev_volume*100 + close_percentage_change = close_diff / prev_close * 100 + volume_percentage_change = volume_diff / prev_volume * 100 differences.append({ 'timestamp': curr_timestamp, 'close': curr_close, @@ -63,24 +65,65 @@ def simulate(data): previous_data = period for difference in differences: print(difference) - make_prediction(differences, 5, 4) + make_prediction(differences, last_timestamps, next_timestamps) + print("----------------") + for period in differences[-next_timestamps::]: + print(period) + return differences + def make_prediction(data, last_timestamps, next_timestamps): - # for i in range(next_timestamps) - print("----------------") - data = data[-last_timestamps::] - print(data) - close = [single_data['close'] for single_data in data] - print(np.mean(close)) - print(np.median(close)) - print(np.std(close)) + for i in range(next_timestamps): + # print("----------------") + iteration_data = data[-last_timestamps::] + close = [single_data['close'] for single_data in iteration_data] + close_diff = [single_data['close_diff'] for single_data in iteration_data] + close_percentage_change = [single_data['close_percentage_change'] for single_data in iteration_data] + # print(np.mean(close)) + # print(np.median(close)) + # print(np.std(close)) + + close_positive_count = sum(x > 0 for x in close_percentage_change) + close_grow_chance = close_positive_count / last_timestamps + close_is_growing = random.random() <= close_grow_chance + close_change = abs(np.mean(close_diff)) * random.uniform(0.1, 10) * (1 if close_is_growing else -1) + # print(np.mean(close_diff)) + new_close = round(close[-1] + close_change, 2) + # print(new_close) + + volume = [single_data['volume'] for single_data in iteration_data] + volume_diff = [single_data['volume_diff'] for single_data in iteration_data] + volume_percentage_change = [single_data['volume_percentage_change'] for single_data in iteration_data] + volume_positive_count = sum(x > 0 for x in volume_percentage_change) + volume_grow_chance = volume_positive_count / last_timestamps + volume_is_growing = random.random() <= volume_grow_chance + volume_change = abs(np.mean(volume_diff)) * random.uniform(0.5, 1.5) * (1 if volume_is_growing else -1) + # print(np.mean(volume_percentage_change)) + # print(volume_diff) + new_volume = round(volume[-1] + volume_change, 2) + # print(new_volume) + close_diff = new_close - iteration_data[-1]['close'] + volume_diff = new_volume - iteration_data[-1]['volume'] + close_percentage_change = close_diff / iteration_data[-1]['close'] * 100 + volume_percentage_change = volume_diff / iteration_data[-1]['close'] * 100 + data.append({ + 'timestamp': iteration_data[-1]['timestamp'] + step, + 'close': new_close, + 'volume': new_volume, + 'close_diff': close_diff, + 'volume_diff': volume_diff, + 'close_percentage_change': close_percentage_change, + 'volume_percentage_change': volume_percentage_change + }) + # print(data[-1]) -def make_plot(data): - closing_prices = [float(period['close']) for period in data] - volumes = [float(period['volume']) for period in data] - timestamps = [float(period['timestamp']) for period in data] +def make_plot(data, end_date): + print(data) + closing_prices = [period['close'] for period in data] + volumes = [period['volume'] for period in data] + timestamps = [period['timestamp'] for period in data] date_labels = map(convert_utc_to_date, timestamps) date_labels = list(date_labels) fig, ax1 = plt.subplots() @@ -88,7 +131,7 @@ def make_plot(data): ax1.set_xlabel('date') ax1.set_ylabel('volume', color=color) ax1.set_ylim([0, 3 * max(volumes)]) - ax1.bar(date_labels, volumes, color=color) + ax1.bar(date_labels, volumes, color=color, alpha=0.75) ax1.tick_params(axis='y', labelcolor=color) ax2 = ax1.twinx() @@ -99,12 +142,16 @@ def make_plot(data): ax2.tick_params(axis='y', labelcolor=color) fig.tight_layout() + plt.axvline(x=convert_utc_to_date(end_date), color='tab:green', alpha=0.5) plt.gcf().autofmt_xdate() + plt.show() def main(): - analyze_data("btcusd", convert_date_to_utc(2020,3,15), convert_date_to_utc(2020,5,29), 3600 * 24) + global step + step = 3600 * 24 + analyze_data("btcusd", convert_date_to_utc(2020, 3, 15), convert_date_to_utc(2020, 5, 29), 100, 10) if __name__ == "__main__": From c82adc99d82122d421de3210ee9d9f6414ae219e Mon Sep 17 00:00:00 2001 From: Bumbelo Date: Sun, 31 May 2020 17:20:20 +0200 Subject: [PATCH 3/6] Added number of iterations, adjusted simulator --- currency_simulator.py | 179 ++++++++++++++++++++---------------------- 1 file changed, 86 insertions(+), 93 deletions(-) diff --git a/currency_simulator.py b/currency_simulator.py index 24aec658..1739de31 100644 --- a/currency_simulator.py +++ b/currency_simulator.py @@ -4,7 +4,9 @@ import numpy as np import random -step = 300 +supported_steps = [60, 180, 300, 900, 1800, 3600, 7200, 14400, 21600, 43200, 86400, 259200] +supported_pairs = ["btcusd", "btceur", "eurusd", "xrpusd", "xrpeur", "xrpbtc", "ltcusd", "ltceur", "ltcbtc", "ethusd", + "etheur", "ethbtc", "bchusd", "bcheur", "bchbtc"] def convert_date_to_utc(year, month, day, hour=0, minutes=0, seconds=0): @@ -17,115 +19,106 @@ def convert_utc_to_date(timestamp): return datetime.fromtimestamp(timestamp) -def gather_data(currency_pair, start, end): +def gather_data(currency_pair, start, end, step): limit = (end - start) // step + 1 bitstamp = requests.get("https://www.bitstamp.net/api/v2/ohlc/{0}/".format(currency_pair), params={'start': start, 'end': end, 'step': step, 'limit': limit}) - bitstamp_json = bitstamp.json() - return bitstamp_json - - -def analyze_data(currency_pair, start, end, last_timestamps, next_timestamps): - data = gather_data(currency_pair, start, end) - # print(data) - simulated_data = simulate(data, last_timestamps, next_timestamps) - make_plot(simulated_data, end) - - -def simulate(data, last_timestamps, next_timestamps): + if bitstamp.status_code == 200: + bitstamp_json = bitstamp.json() + return True, bitstamp_json + else: + return False, None + + +def analyze_data(currency_pair, start, end, last_timestamps, next_timestamps, iterations, step): + valid, data = gather_data(currency_pair, start, end, step) + if valid: + simulated_data = simulate(data, last_timestamps, next_timestamps, iterations, step) + make_plot(simulated_data, data['data']['pair'], end) + else: + print("ERROR GATHERING DATA") + + +def prepare_single_data(timestamp, close, volume, prev_close, prev_volume): + close_diff = close - prev_close + volume_diff = volume - prev_volume + close_percentage_change = close_diff / prev_close * 100 + volume_percentage_change = volume_diff / prev_volume * 100 + return { + 'timestamp': timestamp, + 'close': close, + 'volume': volume, + 'close_diff': close_diff, + 'volume_diff': volume_diff, + 'close_percentage_change': close_percentage_change, + 'volume_percentage_change': volume_percentage_change + } + + +def simulate(data, last_timestamps, next_timestamps, iterations, step): differences = [] data = data['data']['ohlc'] - print(data) previous_data = data[0] - print(previous_data) for period in data[1::]: - curr_timestamp = int(period['timestamp']) - prev_close = float(previous_data['close']) - prev_volume = float(previous_data['volume']) - curr_close = float(period['close']) - curr_volume = float(period['volume']) - close_diff = curr_close - prev_close - volume_diff = curr_volume - prev_volume - close_percentage_change = close_diff / prev_close * 100 - volume_percentage_change = volume_diff / prev_volume * 100 - differences.append({ - 'timestamp': curr_timestamp, - 'close': curr_close, - 'volume': curr_volume, - 'close_diff': close_diff, - 'volume_diff': volume_diff, - 'close_percentage_change': close_percentage_change, - 'volume_percentage_change': volume_percentage_change - }) - print("CLOSING PRICE: {0}".format(curr_close)) - print("VOLUME: {0}".format(curr_volume)) - print("CLOSING PRICE DIFF: {0}".format(curr_close - prev_close)) - print("VOLUME DIFF: {0}".format(curr_volume - prev_volume)) - print(period) + differences.append( + prepare_single_data(int(period['timestamp']), float(period['close']), float(period['volume']), + float(previous_data['close']), float(previous_data['volume']))) previous_data = period - for difference in differences: - print(difference) - make_prediction(differences, last_timestamps, next_timestamps) - print("----------------") - for period in differences[-next_timestamps::]: - print(period) + make_predictions(differences, last_timestamps, next_timestamps, iterations, step) return differences - -def make_prediction(data, last_timestamps, next_timestamps): +def make_predictions(data, last_timestamps, next_timestamps, iterations, step): for i in range(next_timestamps): - # print("----------------") - iteration_data = data[-last_timestamps::] - close = [single_data['close'] for single_data in iteration_data] - close_diff = [single_data['close_diff'] for single_data in iteration_data] - close_percentage_change = [single_data['close_percentage_change'] for single_data in iteration_data] - # print(np.mean(close)) - # print(np.median(close)) - # print(np.std(close)) - + data_segment = data[-last_timestamps::] + close = [single_data['close'] for single_data in data_segment] + close_diff = [single_data['close_diff'] for single_data in data_segment] + close_percentage_change = [single_data['close_percentage_change'] for single_data in data_segment] close_positive_count = sum(x > 0 for x in close_percentage_change) close_grow_chance = close_positive_count / last_timestamps - close_is_growing = random.random() <= close_grow_chance - close_change = abs(np.mean(close_diff)) * random.uniform(0.1, 10) * (1 if close_is_growing else -1) - # print(np.mean(close_diff)) - new_close = round(close[-1] + close_change, 2) - # print(new_close) - - volume = [single_data['volume'] for single_data in iteration_data] - volume_diff = [single_data['volume_diff'] for single_data in iteration_data] - volume_percentage_change = [single_data['volume_percentage_change'] for single_data in iteration_data] + + volume = [single_data['volume'] for single_data in data_segment] + volume_diff = [single_data['volume_diff'] for single_data in data_segment] + volume_percentage_change = [single_data['volume_percentage_change'] for single_data in data_segment] + volume_positive_count = sum(x > 0 for x in volume_percentage_change) volume_grow_chance = volume_positive_count / last_timestamps - volume_is_growing = random.random() <= volume_grow_chance - volume_change = abs(np.mean(volume_diff)) * random.uniform(0.5, 1.5) * (1 if volume_is_growing else -1) - # print(np.mean(volume_percentage_change)) - # print(volume_diff) - new_volume = round(volume[-1] + volume_change, 2) - # print(new_volume) - close_diff = new_close - iteration_data[-1]['close'] - volume_diff = new_volume - iteration_data[-1]['volume'] - close_percentage_change = close_diff / iteration_data[-1]['close'] * 100 - volume_percentage_change = volume_diff / iteration_data[-1]['close'] * 100 - data.append({ - 'timestamp': iteration_data[-1]['timestamp'] + step, - 'close': new_close, - 'volume': new_volume, - 'close_diff': close_diff, - 'volume_diff': volume_diff, - 'close_percentage_change': close_percentage_change, - 'volume_percentage_change': volume_percentage_change - }) - # print(data[-1]) - - -def make_plot(data, end_date): - print(data) + + close_values = [] + volume_values = [] + for j in range(iterations): + close_is_growing = random.random() <= close_grow_chance + close_percentage_change = np.mean(np.abs(close_percentage_change)) * random.uniform(0.5, 1.5) * (1 if close_is_growing else -1) + new_close = round(close[-1] *(1+close_percentage_change/100), 2) + close_values.append(new_close) + + volume_is_growing=True + if close_percentage_change<-5 or close_percentage_change>5: + volume_is_growing = True + else: + volume_is_growing = random.random() <= volume_grow_chance + volume_percentage_change = np.mean(np.abs(volume_percentage_change)) * random.uniform(0.2, 1.6) * (1 if volume_is_growing else -1) + new_volume = round(volume[-1]*(1+volume_percentage_change/100), 2) + volume_values.append(new_volume) + + print("timestamp: {0}".format(data_segment[-1]['timestamp']+ step)) + print("close - MEAN: {0}, MEDIAN: {1}, STANDARD DEVIATION: {2}".format(np.mean(close_values),np.median(close_values),np.std(close_values))) + print("volume - MEAN: {0}, MEDIAN: {1}, STANDARD DEVIATION: {2}".format(np.mean(volume_values),np.median(volume_values),np.std(volume_values))) + new_close = np.mean(close_values) + new_volume = np.mean(volume_values) + data.append(prepare_single_data(data_segment[-1]['timestamp'] + step, new_close, new_volume, + data_segment[-1]['close'], data_segment[-1]['volume'])) + + +def make_plot(data, currency_pair, end_date): closing_prices = [period['close'] for period in data] volumes = [period['volume'] for period in data] timestamps = [period['timestamp'] for period in data] + date_labels = map(convert_utc_to_date, timestamps) date_labels = list(date_labels) + + plt.style.use('bmh') fig, ax1 = plt.subplots() color = 'tab:red' ax1.set_xlabel('date') @@ -138,10 +131,11 @@ def make_plot(data, end_date): color = 'tab:blue' ax2.set_ylabel('closing price', color=color) - ax2.plot(date_labels, closing_prices, color=color) + ax2.plot(date_labels, closing_prices, color=color, alpha=0.75) ax2.tick_params(axis='y', labelcolor=color) fig.tight_layout() + plt.title(currency_pair) plt.axvline(x=convert_utc_to_date(end_date), color='tab:green', alpha=0.5) plt.gcf().autofmt_xdate() @@ -149,9 +143,8 @@ def make_plot(data, end_date): def main(): - global step - step = 3600 * 24 - analyze_data("btcusd", convert_date_to_utc(2020, 3, 15), convert_date_to_utc(2020, 5, 29), 100, 10) + analyze_data(currency_pair="btcusd", start=convert_date_to_utc(2020, 3, 1), end=convert_date_to_utc(2020, 5, 29), + last_timestamps=50, next_timestamps=20, iterations=20, step=3600*24) if __name__ == "__main__": From 318d46ee3f445e4ebf3a40912f0d0a78a92894f5 Mon Sep 17 00:00:00 2001 From: Bumbelo Date: Sun, 31 May 2020 19:51:49 +0200 Subject: [PATCH 4/6] Further improvements --- currency_simulator.py | 77 +++++++++++++++++++++++++++---------------- 1 file changed, 48 insertions(+), 29 deletions(-) diff --git a/currency_simulator.py b/currency_simulator.py index 1739de31..c21e97aa 100644 --- a/currency_simulator.py +++ b/currency_simulator.py @@ -4,9 +4,11 @@ import numpy as np import random -supported_steps = [60, 180, 300, 900, 1800, 3600, 7200, 14400, 21600, 43200, 86400, 259200] -supported_pairs = ["btcusd", "btceur", "eurusd", "xrpusd", "xrpeur", "xrpbtc", "ltcusd", "ltceur", "ltcbtc", "ethusd", - "etheur", "ethbtc", "bchusd", "bcheur", "bchbtc"] +# Limit of gathered data from bitstamp is 1000 +SUPPORTED_STEPS = [60, 180, 300, 900, 1800, 3600, 7200, 14400, 21600, 43200, 86400, 259200] +SUPPORTED_CURRENCY_PAIRS = ["btcusd", "btceur", "eurusd", "xrpusd", "xrpeur", "xrpbtc", "ltcusd", "ltceur", "ltcbtc", + "ethusd", + "etheur", "ethbtc", "bchusd", "bcheur", "bchbtc"] def convert_date_to_utc(year, month, day, hour=0, minutes=0, seconds=0): @@ -21,11 +23,13 @@ def convert_utc_to_date(timestamp): def gather_data(currency_pair, start, end, step): limit = (end - start) // step + 1 - bitstamp = requests.get("https://www.bitstamp.net/api/v2/ohlc/{0}/".format(currency_pair), - params={'start': start, 'end': end, 'step': step, 'limit': limit}) - if bitstamp.status_code == 200: - bitstamp_json = bitstamp.json() - return True, bitstamp_json + if limit > 1000: + limit = 1000 + bitstamp_data = requests.get("https://www.bitstamp.net/api/v2/ohlc/{0}/".format(currency_pair), + params={'start': start, 'end': end, 'step': step, 'limit': limit}) + if bitstamp_data.status_code == 200: + bitstamp_data_json = bitstamp_data.json() + return True, bitstamp_data_json else: return False, None @@ -42,8 +46,15 @@ def analyze_data(currency_pair, start, end, last_timestamps, next_timestamps, it def prepare_single_data(timestamp, close, volume, prev_close, prev_volume): close_diff = close - prev_close volume_diff = volume - prev_volume - close_percentage_change = close_diff / prev_close * 100 - volume_percentage_change = volume_diff / prev_volume * 100 + if prev_close == 0: + close_percentage_change = None + else: + close_percentage_change = close_diff / prev_close * 100 + if prev_volume == 0: + volume_percentage_change = None + else: + volume_percentage_change = volume_diff / prev_volume * 100 + return { 'timestamp': timestamp, 'close': close, @@ -71,39 +82,47 @@ def simulate(data, last_timestamps, next_timestamps, iterations, step): def make_predictions(data, last_timestamps, next_timestamps, iterations, step): for i in range(next_timestamps): data_segment = data[-last_timestamps::] - close = [single_data['close'] for single_data in data_segment] - close_diff = [single_data['close_diff'] for single_data in data_segment] - close_percentage_change = [single_data['close_percentage_change'] for single_data in data_segment] - close_positive_count = sum(x > 0 for x in close_percentage_change) + close_list = [single_data['close'] for single_data in data_segment] + close_diff_list = [single_data['close_diff'] for single_data in data_segment] + close_percentage_change_list = [single_data['close_percentage_change'] for single_data in data_segment] + close_positive_count = sum(x > 0 for x in close_percentage_change_list) close_grow_chance = close_positive_count / last_timestamps - volume = [single_data['volume'] for single_data in data_segment] - volume_diff = [single_data['volume_diff'] for single_data in data_segment] - volume_percentage_change = [single_data['volume_percentage_change'] for single_data in data_segment] + volume_list = [single_data['volume'] for single_data in data_segment] + volume_diff_list = [single_data['volume_diff'] for single_data in data_segment] + volume_percentage_change_list = [single_data['volume_percentage_change'] for single_data in data_segment] - volume_positive_count = sum(x > 0 for x in volume_percentage_change) + volume_positive_count = sum(x > 0 for x in volume_percentage_change_list) volume_grow_chance = volume_positive_count / last_timestamps close_values = [] volume_values = [] for j in range(iterations): close_is_growing = random.random() <= close_grow_chance - close_percentage_change = np.mean(np.abs(close_percentage_change)) * random.uniform(0.5, 1.5) * (1 if close_is_growing else -1) - new_close = round(close[-1] *(1+close_percentage_change/100), 2) + close_percentage_change_list = np.mean(np.abs(close_percentage_change_list)) * random.uniform(0.5, 1.5) * ( + 1 if close_is_growing else -1) + new_close = round(close_list[-1] * (1 + close_percentage_change_list / 100), 2) close_values.append(new_close) - volume_is_growing=True - if close_percentage_change<-5 or close_percentage_change>5: + volume_is_growing = True + percentage_boost = 0 + if close_percentage_change_list < -5 or close_percentage_change_list > 5: volume_is_growing = True + percentage_boost = abs(close_percentage_change_list) else: volume_is_growing = random.random() <= volume_grow_chance - volume_percentage_change = np.mean(np.abs(volume_percentage_change)) * random.uniform(0.2, 1.6) * (1 if volume_is_growing else -1) - new_volume = round(volume[-1]*(1+volume_percentage_change/100), 2) + volume_percentage_change_list = (percentage_boost + np.mean(np.abs(volume_percentage_change_list))) * random.uniform( + 0.2, 1.5) * (1 if volume_is_growing else -1) + new_volume = round(volume_list[-1] * (1 + volume_percentage_change_list / 100), 2) volume_values.append(new_volume) - print("timestamp: {0}".format(data_segment[-1]['timestamp']+ step)) - print("close - MEAN: {0}, MEDIAN: {1}, STANDARD DEVIATION: {2}".format(np.mean(close_values),np.median(close_values),np.std(close_values))) - print("volume - MEAN: {0}, MEDIAN: {1}, STANDARD DEVIATION: {2}".format(np.mean(volume_values),np.median(volume_values),np.std(volume_values))) + print("timestamp: {0}".format(data_segment[-1]['timestamp'] + step)) + print("close - MEAN: {0}, MEDIAN: {1}, STANDARD DEVIATION: {2}".format(np.mean(close_values), + np.median(close_values), + np.std(close_values))) + print("volume - MEAN: {0}, MEDIAN: {1}, STANDARD DEVIATION: {2}".format(np.mean(volume_values), + np.median(volume_values), + np.std(volume_values))) new_close = np.mean(close_values) new_volume = np.mean(volume_values) data.append(prepare_single_data(data_segment[-1]['timestamp'] + step, new_close, new_volume, @@ -124,7 +143,7 @@ def make_plot(data, currency_pair, end_date): ax1.set_xlabel('date') ax1.set_ylabel('volume', color=color) ax1.set_ylim([0, 3 * max(volumes)]) - ax1.bar(date_labels, volumes, color=color, alpha=0.75) + ax1.bar(date_labels, volumes, color=color, alpha=0.75, width=0.5) ax1.tick_params(axis='y', labelcolor=color) ax2 = ax1.twinx() @@ -144,7 +163,7 @@ def make_plot(data, currency_pair, end_date): def main(): analyze_data(currency_pair="btcusd", start=convert_date_to_utc(2020, 3, 1), end=convert_date_to_utc(2020, 5, 29), - last_timestamps=50, next_timestamps=20, iterations=20, step=3600*24) + last_timestamps=50, next_timestamps=10, iterations=50, step=3600 * 24) if __name__ == "__main__": From 2b1c5fdc1eaf2d59bad95e28dec0a541a90022bc Mon Sep 17 00:00:00 2001 From: Bumbelo Date: Sat, 6 Jun 2020 23:29:50 +0200 Subject: [PATCH 5/6] added value correction, adjusted volume spikes --- currency_simulator.py | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/currency_simulator.py b/currency_simulator.py index c21e97aa..ec74c87d 100644 --- a/currency_simulator.py +++ b/currency_simulator.py @@ -80,6 +80,15 @@ def simulate(data, last_timestamps, next_timestamps, iterations, step): def make_predictions(data, last_timestamps, next_timestamps, iterations, step): + grows_in_a_row = 0 + data_segment = data[-last_timestamps::] + close_diff_list = [single_data['close_diff'] for single_data in data_segment] + for close_diff in close_diff_list: + if close_diff >= 0: + grows_in_a_row += 1 + else: + grows_in_a_row = 0 + last_spike = 0 for i in range(next_timestamps): data_segment = data[-last_timestamps::] close_list = [single_data['close'] for single_data in data_segment] @@ -101,19 +110,23 @@ def make_predictions(data, last_timestamps, next_timestamps, iterations, step): close_is_growing = random.random() <= close_grow_chance close_percentage_change_list = np.mean(np.abs(close_percentage_change_list)) * random.uniform(0.5, 1.5) * ( 1 if close_is_growing else -1) + new_close = round(close_list[-1] * (1 + close_percentage_change_list / 100), 2) close_values.append(new_close) - volume_is_growing = True percentage_boost = 0 - if close_percentage_change_list < -5 or close_percentage_change_list > 5: + if (close_percentage_change_list < -3 or close_percentage_change_list > 3): volume_is_growing = True - percentage_boost = abs(close_percentage_change_list) + percentage_boost = abs(close_percentage_change_list / 2) else: volume_is_growing = random.random() <= volume_grow_chance - volume_percentage_change_list = (percentage_boost + np.mean(np.abs(volume_percentage_change_list))) * random.uniform( + volume_percentage_change_list = (percentage_boost + np.mean( + np.abs(volume_percentage_change_list))) * random.uniform( 0.2, 1.5) * (1 if volume_is_growing else -1) - new_volume = round(volume_list[-1] * (1 + volume_percentage_change_list / 100), 2) + old_volume = volume_list[-1] + if last_spike == data_segment[-2]['timestamp']: + old_volume = volume_list[-3] + new_volume = round(old_volume * (1 + volume_percentage_change_list / 100), 2) volume_values.append(new_volume) print("timestamp: {0}".format(data_segment[-1]['timestamp'] + step)) @@ -125,8 +138,18 @@ def make_predictions(data, last_timestamps, next_timestamps, iterations, step): np.std(volume_values))) new_close = np.mean(close_values) new_volume = np.mean(volume_values) - data.append(prepare_single_data(data_segment[-1]['timestamp'] + step, new_close, new_volume, - data_segment[-1]['close'], data_segment[-1]['volume'])) + if (new_volume / volume_list[-1]) >= 1.5: + last_spike = data_segment[-1]['timestamp'] + step + correction_probability = grows_in_a_row * 0.1 + if random.random() <= correction_probability: + new_close = new_close * (1 - correction_probability / 10) + new_data = prepare_single_data(data_segment[-1]['timestamp'] + step, new_close, new_volume, + data_segment[-1]['close'], data_segment[-1]['volume']) + if new_data['close_diff'] >= 0: + grows_in_a_row += 1 + else: + grows_in_a_row = 0 + data.append(new_data) def make_plot(data, currency_pair, end_date): @@ -163,7 +186,7 @@ def make_plot(data, currency_pair, end_date): def main(): analyze_data(currency_pair="btcusd", start=convert_date_to_utc(2020, 3, 1), end=convert_date_to_utc(2020, 5, 29), - last_timestamps=50, next_timestamps=10, iterations=50, step=3600 * 24) + last_timestamps=80, next_timestamps=40, iterations=100, step=3600 * 24) if __name__ == "__main__": From f66d3e960f46391e722f26e7c6b6fd49db870b86 Mon Sep 17 00:00:00 2001 From: Bumbelo Date: Sun, 7 Jun 2020 02:26:55 +0200 Subject: [PATCH 6/6] adjustements to close values --- currency_simulator.py | 58 ++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/currency_simulator.py b/currency_simulator.py index ec74c87d..3168d345 100644 --- a/currency_simulator.py +++ b/currency_simulator.py @@ -10,6 +10,7 @@ "ethusd", "etheur", "ethbtc", "bchusd", "bcheur", "bchbtc"] +CORRECTION_POWER=0.08 def convert_date_to_utc(year, month, day, hour=0, minutes=0, seconds=0): dt = datetime(year, month, day, hour, minutes, seconds) @@ -80,14 +81,14 @@ def simulate(data, last_timestamps, next_timestamps, iterations, step): def make_predictions(data, last_timestamps, next_timestamps, iterations, step): - grows_in_a_row = 0 + close_grows_in_a_row = 0 data_segment = data[-last_timestamps::] close_diff_list = [single_data['close_diff'] for single_data in data_segment] for close_diff in close_diff_list: if close_diff >= 0: - grows_in_a_row += 1 + close_grows_in_a_row += 1 else: - grows_in_a_row = 0 + close_grows_in_a_row = 0 last_spike = 0 for i in range(next_timestamps): data_segment = data[-last_timestamps::] @@ -104,29 +105,23 @@ def make_predictions(data, last_timestamps, next_timestamps, iterations, step): volume_positive_count = sum(x > 0 for x in volume_percentage_change_list) volume_grow_chance = volume_positive_count / last_timestamps + close_percentage_change_std = np.std(close_percentage_change_list) + volume_diff_std = np.std(volume_diff_list) + close_values = [] volume_values = [] for j in range(iterations): close_is_growing = random.random() <= close_grow_chance - close_percentage_change_list = np.mean(np.abs(close_percentage_change_list)) * random.uniform(0.5, 1.5) * ( - 1 if close_is_growing else -1) + close_percentage_change = (np.mean(close_percentage_change_list) + random.uniform(0,close_percentage_change_std)) * random.uniform(0.5, 2) * (1 if close_is_growing else -1) - new_close = round(close_list[-1] * (1 + close_percentage_change_list / 100), 2) + new_close = round(close_list[-1] * (1 + close_percentage_change / 100), 2) close_values.append(new_close) - volume_is_growing = True - percentage_boost = 0 - if (close_percentage_change_list < -3 or close_percentage_change_list > 3): - volume_is_growing = True - percentage_boost = abs(close_percentage_change_list / 2) - else: - volume_is_growing = random.random() <= volume_grow_chance - volume_percentage_change_list = (percentage_boost + np.mean( - np.abs(volume_percentage_change_list))) * random.uniform( - 0.2, 1.5) * (1 if volume_is_growing else -1) - old_volume = volume_list[-1] - if last_spike == data_segment[-2]['timestamp']: - old_volume = volume_list[-3] - new_volume = round(old_volume * (1 + volume_percentage_change_list / 100), 2) + + volume_is_growing = random.random() <= volume_grow_chance + + volume_diff = np.mean(volume_diff_list) + random.uniform(0, volume_diff_std) * ( + 1 if volume_is_growing else -1) + new_volume = round((np.median(volume_list) + volume_diff), 2) volume_values.append(new_volume) print("timestamp: {0}".format(data_segment[-1]['timestamp'] + step)) @@ -138,17 +133,26 @@ def make_predictions(data, last_timestamps, next_timestamps, iterations, step): np.std(volume_values))) new_close = np.mean(close_values) new_volume = np.mean(volume_values) - if (new_volume / volume_list[-1]) >= 1.5: - last_spike = data_segment[-1]['timestamp'] + step - correction_probability = grows_in_a_row * 0.1 + + correction_probability = close_grows_in_a_row * CORRECTION_POWER if random.random() <= correction_probability: new_close = new_close * (1 - correction_probability / 10) + + percentage_diff = abs(1 - (new_close / close_list[-1])) * 100 + + if percentage_diff >= 3: + new_volume *= percentage_diff / 2 + last_spike = data_segment[-1]['timestamp'] + step + + if last_spike == data_segment[-1]['timestamp']: + new_volume = volume_list[-1] * (new_volume / volume_list[-2]) + new_data = prepare_single_data(data_segment[-1]['timestamp'] + step, new_close, new_volume, data_segment[-1]['close'], data_segment[-1]['volume']) if new_data['close_diff'] >= 0: - grows_in_a_row += 1 + close_grows_in_a_row += 1 else: - grows_in_a_row = 0 + close_grows_in_a_row = 0 data.append(new_data) @@ -186,7 +190,9 @@ def make_plot(data, currency_pair, end_date): def main(): analyze_data(currency_pair="btcusd", start=convert_date_to_utc(2020, 3, 1), end=convert_date_to_utc(2020, 5, 29), - last_timestamps=80, next_timestamps=40, iterations=100, step=3600 * 24) + last_timestamps=100, next_timestamps=40, iterations=1, step=3600 * 24) + analyze_data(currency_pair="btcusd", start=convert_date_to_utc(2020, 3, 1), end=convert_date_to_utc(2020, 5, 29), + last_timestamps=100, next_timestamps=40, iterations=100, step=3600 * 24) if __name__ == "__main__":