From 8020caac94bf3d945f899010e0107b57ffa5d6d7 Mon Sep 17 00:00:00 2001 From: Maja Date: Sun, 28 Mar 2021 15:49:18 +0200 Subject: [PATCH 1/9] lista2 --- financialAPI.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 financialAPI.py diff --git a/financialAPI.py b/financialAPI.py new file mode 100644 index 00000000..dfc811af --- /dev/null +++ b/financialAPI.py @@ -0,0 +1,49 @@ +import time +import requests +from requests.exceptions import HTTPError + + +def data(): + currency_1 = ['BTC', 'LTC', 'DASH'] + currency_2 = 'USD' + category = 'trades' + while True: + for i in currency_1: + data = download_data(i, currency_2, category) + print(data) + calculate(data, i) + time.sleep(5) + return 'done' + + +def download_data(currency_1, currency_2, category): + try: + response = requests.get(url=f'https://bitbay.net/API/Public/{currency_1}{currency_2}/{category}.json') + data = response.json() + except HTTPError: + print('HTTP error:', HTTPError) + return(data) + + +def calculate(data, currency_1): + sell_price = [] + buy_price = [] + for i in range(len(data)): + if data[i]["type"] == "buy": + buy_price.append(data[i]["price"]) + + elif data[i]["type"] == "sell": + sell_price.append(data[i]["price"]) + + if (len(buy_price) != 0) and (len(sell_price) != 0): + diffrence = 1 - ((buy_price[-1] - sell_price[-1]) / buy_price[-1]) + print({ + 'valute': currency_1, + 'buy_price': buy_price[-1], + 'sell_price': sell_price[-1], + 'procent': diffrence}) + return 'thx' + + +if __name__ == "__main__": + data() From 5076c5b923f15cfd28e8453e4a9636c1c08cac51 Mon Sep 17 00:00:00 2001 From: Maja Date: Wed, 31 Mar 2021 20:32:47 +0200 Subject: [PATCH 2/9] Change the logic of calculating diferences --- financialAPI.py | 50 +++++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/financialAPI.py b/financialAPI.py index dfc811af..f16a8dac 100644 --- a/financialAPI.py +++ b/financialAPI.py @@ -2,18 +2,22 @@ import requests from requests.exceptions import HTTPError +data_sells = [] + def data(): currency_1 = ['BTC', 'LTC', 'DASH'] currency_2 = 'USD' - category = 'trades' + category = 'orderbook' while True: - for i in currency_1: - data = download_data(i, currency_2, category) - print(data) - calculate(data, i) + for c in currency_1: + data = download_data(c, currency_2, category) + # print(data) + if data is not None: + pass + diffrence = calculate(data, c) time.sleep(5) - return 'done' + return diffrence def download_data(currency_1, currency_2, category): @@ -22,27 +26,25 @@ def download_data(currency_1, currency_2, category): data = response.json() except HTTPError: print('HTTP error:', HTTPError) - return(data) + return None + return data def calculate(data, currency_1): - sell_price = [] - buy_price = [] - for i in range(len(data)): - if data[i]["type"] == "buy": - buy_price.append(data[i]["price"]) - - elif data[i]["type"] == "sell": - sell_price.append(data[i]["price"]) - - if (len(buy_price) != 0) and (len(sell_price) != 0): - diffrence = 1 - ((buy_price[-1] - sell_price[-1]) / buy_price[-1]) - print({ - 'valute': currency_1, - 'buy_price': buy_price[-1], - 'sell_price': sell_price[-1], - 'procent': diffrence}) - return 'thx' + buy = data['bids'][0][0] + sell = data['asks'][0][0] + procenty = (1-(sell-buy)/sell) * 100 + diffrence = { + 'currency': currency_1, + 'buy_price': data['bids'][0][0], + 'sell_price': data['asks'][0][0], + 'procents': procenty, + } + + data_sells.append(diffrence) + print(diffrence) + print('=========`======') + return diffrence if __name__ == "__main__": From 25e59d8c1324b0b0ec65efd539132fec7c5aaa11 Mon Sep 17 00:00:00 2001 From: Maja Date: Wed, 14 Apr 2021 15:42:09 +0200 Subject: [PATCH 3/9] L3 --- financialAPI.py | 67 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 8 deletions(-) diff --git a/financialAPI.py b/financialAPI.py index f16a8dac..5ab5a049 100644 --- a/financialAPI.py +++ b/financialAPI.py @@ -1,21 +1,28 @@ import time import requests from requests.exceptions import HTTPError +import matplotlib.pyplot as plt -data_sells = [] +data_currency = { + "BTC": [], + "LTC": [], + "DASH": [], +} -def data(): + +def data(lines): currency_1 = ['BTC', 'LTC', 'DASH'] currency_2 = 'USD' category = 'orderbook' + iterator = 0 while True: + iterator += 1 for c in currency_1: data = download_data(c, currency_2, category) - # print(data) if data is not None: - pass diffrence = calculate(data, c) + create_graph(iterator, lines) time.sleep(5) return diffrence @@ -35,17 +42,61 @@ def calculate(data, currency_1): sell = data['asks'][0][0] procenty = (1-(sell-buy)/sell) * 100 diffrence = { - 'currency': currency_1, 'buy_price': data['bids'][0][0], 'sell_price': data['asks'][0][0], 'procents': procenty, } - data_sells.append(diffrence) + data_currency[f"{currency_1}"].append(diffrence) print(diffrence) - print('=========`======') return diffrence +def create_graph(iterator, lines): + + for c, l in lines.items(): + data = data_currency[c][0:iterator] + buy = [] + sell = [] + t = [] + for i in range(len(data)): + buy.append(data[i]["buy_price"]) + sell.append(data[i]["sell_price"]) + t.append(i+1) + + l[0].set_data(t, buy) + l[1].set_data(t, sell) + plts[c].set_xlim([max(iterator-10, 1), iterator+3]) + plts[c].set_ylim([min(buy)*0.95, max(sell)*1.05]) + + plt.draw() + plt.pause(1e-17) + + if __name__ == "__main__": - data() + + plt.ion() + + plts = {} + lines = {} + + fig = plt.figure() + + itr = 1 + for c in data_currency.keys(): + plts[c] = fig.add_subplot(len(data_currency), 1, itr) + buy_line, = plts[c].plot([0], [0]) + sell_line, = plts[c].plot([0], [0]) + lines[c] = [buy_line, sell_line] + + plts[c].set_ylim([1, 5]) + plts[c].set_title(f"{c}") + + plts[c].set_xlabel("time") + plts[c].set_ylabel("value") + + itr += 1 + + fig.tight_layout(h_pad=0.5) + + data(lines) From f38020cba2089f97bbb4d040313e41297a03637c Mon Sep 17 00:00:00 2001 From: Maja Date: Mon, 19 Apr 2021 16:03:05 +0200 Subject: [PATCH 4/9] update for l3 --- financialAPI.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/financialAPI.py b/financialAPI.py index 5ab5a049..07af56e4 100644 --- a/financialAPI.py +++ b/financialAPI.py @@ -41,10 +41,12 @@ def calculate(data, currency_1): buy = data['bids'][0][0] sell = data['asks'][0][0] procenty = (1-(sell-buy)/sell) * 100 + t = time.strftime("%H:%M:%S", time.localtime()) diffrence = { 'buy_price': data['bids'][0][0], 'sell_price': data['asks'][0][0], 'procents': procenty, + 'time': str(t), } data_currency[f"{currency_1}"].append(diffrence) @@ -62,11 +64,13 @@ def create_graph(iterator, lines): for i in range(len(data)): buy.append(data[i]["buy_price"]) sell.append(data[i]["sell_price"]) + tim = data[i]["time"] t.append(i+1) l[0].set_data(t, buy) l[1].set_data(t, sell) - plts[c].set_xlim([max(iterator-10, 1), iterator+3]) + plts[c].set_xticklabels(tim, rotation='horizontal', fontsize=7) + plts[c].set_xlim([max(iterator-8, 1), iterator+3]) plts[c].set_ylim([min(buy)*0.95, max(sell)*1.05]) plt.draw() @@ -90,7 +94,7 @@ def create_graph(iterator, lines): lines[c] = [buy_line, sell_line] plts[c].set_ylim([1, 5]) - plts[c].set_title(f"{c}") + plts[c].set_title(f"{c} - USD") plts[c].set_xlabel("time") plts[c].set_ylabel("value") From 074838ac777a5e01fdcb7d52c8aa08055151e81d Mon Sep 17 00:00:00 2001 From: Maja Date: Wed, 5 May 2021 15:20:51 +0200 Subject: [PATCH 5/9] update for L5 --- financialAPI.py | 226 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 194 insertions(+), 32 deletions(-) diff --git a/financialAPI.py b/financialAPI.py index 07af56e4..089ba227 100644 --- a/financialAPI.py +++ b/financialAPI.py @@ -1,19 +1,31 @@ import time import requests +import statistics from requests.exceptions import HTTPError import matplotlib.pyplot as plt data_currency = { - "BTC": [], - "LTC": [], - "DASH": [], + "LSK": [], + "BCP": [], + "GNT": [], } -def data(lines): - currency_1 = ['BTC', 'LTC', 'DASH'] - currency_2 = 'USD' +data_currency_extra = { + "LSK": [], + "BCP": [], + "GNT": [], +} + + +# beginning = 0 +# end = 0 + + +def data(lines, beginning, end): + currency_1 = ['LSK', 'BCP', 'GNT'] + currency_2 = 'PLN' category = 'orderbook' iterator = 0 while True: @@ -21,7 +33,7 @@ def data(lines): for c in currency_1: data = download_data(c, currency_2, category) if data is not None: - diffrence = calculate(data, c) + diffrence = calculate(data, c, beginning, end) create_graph(iterator, lines) time.sleep(5) return diffrence @@ -31,17 +43,71 @@ def download_data(currency_1, currency_2, category): try: response = requests.get(url=f'https://bitbay.net/API/Public/{currency_1}{currency_2}/{category}.json') data = response.json() + except HTTPError: print('HTTP error:', HTTPError) return None return data -def calculate(data, currency_1): +def get_volumen(currency): + try: + response = requests.get(url=f'https://api.bitbay.net/rest/trading/transactions/{currency}-PLN') + data = response.json() + + except HTTPError: + print('HTTP error:', HTTPError) + return None + return data['items'][1]['a'] + + +def calculate_averange(currency, beginning, end): + data = data_currency[currency] + data_points_ctr = len(data) + print(data_points_ctr) + if beginning > end or beginning >= data_points_ctr: + return 0, 0 + + end = min(data_points_ctr, end+1) + b_avg = statistics.mean([dp["buy_price"] for dp in data[beginning:end]]) + s_avg = statistics.mean([dp["sell_price"] for dp in data[beginning:end]]) + + print(data[beginning:end]) + return b_avg, s_avg + + +def calculate_RSI(currency, beginning, end): + average_price_increase = [] + average_price_decrease = [] + data = data_currency[currency] + data_points_ctr = len(data) + + if beginning > end or beginning >= data_points_ctr: + return 0 + + end = min(data_points_ctr, end+1) + + if len(data) >= (end-beginning+1): + value = data[end-1]["buy_price"] - data[beginning]["buy_price"] + if value > 0: + average_price_increase.append(value) + elif value <= 0: + average_price_decrease.append(value) + a = (sum(average_price_increase) + 1)/(len(average_price_increase) + 1) + b = (sum(average_price_decrease) + 1)/(len(average_price_decrease) + 1) + else: + a = 1 + b = 1 + RSI = 100 - (100 / (1 + (a/b))) + return RSI + + +def calculate(data, currency, beginning, end): buy = data['bids'][0][0] sell = data['asks'][0][0] procenty = (1-(sell-buy)/sell) * 100 t = time.strftime("%H:%M:%S", time.localtime()) + diffrence = { 'buy_price': data['bids'][0][0], 'sell_price': data['asks'][0][0], @@ -49,29 +115,93 @@ def calculate(data, currency_1): 'time': str(t), } - data_currency[f"{currency_1}"].append(diffrence) - print(diffrence) + data_currency[currency].append(diffrence) + print(beginning, end) + value_buy, value_sell = calculate_averange(currency, beginning, end) + RSI = calculate_RSI(currency, beginning, end) + + volumen = get_volumen(currency) + diffrence2 = { + 'buy_averange': value_buy, + 'sell_averange': value_sell, + 'RSI': RSI, + 'volumen': volumen, + } + + data_currency_extra[currency].append(diffrence2) + + print(diffrence, diffrence2) return diffrence -def create_graph(iterator, lines): +xrang = 8 + +def create_graph(j, lines): for c, l in lines.items(): - data = data_currency[c][0:iterator] + data = data_currency[c] + data2 = data_currency_extra[c] buy = [] sell = [] - t = [] - for i in range(len(data)): - buy.append(data[i]["buy_price"]) - sell.append(data[i]["sell_price"]) - tim = data[i]["time"] - t.append(i+1) - - l[0].set_data(t, buy) - l[1].set_data(t, sell) - plts[c].set_xticklabels(tim, rotation='horizontal', fontsize=7) - plts[c].set_xlim([max(iterator-8, 1), iterator+3]) - plts[c].set_ylim([min(buy)*0.95, max(sell)*1.05]) + tim = [] + buy_average = [] + sell_average = [] + # RSI1 = [] + RSI2 = [] + volumen = [] + width = 0.35 + if len(data) > 0: + itr = range(xrang) + if len(data) < xrang: + itr = range(len(data)) + for i in itr: + buy.append(data[i-len(itr)]["buy_price"]) + sell.append(data[i-len(itr)]["sell_price"]) + tim.append(data[i-len(itr)]["time"]) + buy_average.append(data2[i-len(itr)]["buy_averange"]) + sell_average.append(data2[i-len(itr)]["sell_averange"]) + # RSI1.append(0.1*data2[i-len(itr)]["RSI"]) + RSI2.append(data2[i-len(itr)]["RSI"]) + volumen.append(float(data2[i-len(itr)]["volumen"])) + + l[0].set_data(itr, buy) + l[1].set_data(itr, sell) + l[2].set_data(itr, buy_average) + l[3].set_data(itr, sell_average) + plts[c].set_xticklabels(tim, rotation='horizontal', fontsize=7) + plts[c].set_xlim(0, xrang-1) + + if min(buy) >= min(buy_average): + bottom = min(buy_average) + else: + bottom = min(buy) + + if max(sell) <= max(sell_average): + top = max(sell_average) + else: + top = max(sell) + plts[c].set_ylim([bottom*0.95, top*1.05]) + + br1 = itr + br2 = [x + width for x in br1] + + plts_ex[c].bar(br1, volumen, width, align='center', color='blue', label=volumen) + plts_ex[c].bar(br2, RSI2, width, align='center', color='orange', label=RSI2) + + plts_ex[c].set_xticklabels(tim, rotation='horizontal', fontsize=7) + plts_ex[c].set_xlim(0, xrang-1) + + if min(RSI2) >= min(volumen): + bottom1 = min(volumen) + else: + bottom1 = min(RSI2) + + if max(RSI2) <= max(volumen): + top1 = max(volumen) + else: + top1 = max(RSI2) + plts_ex[c].legend(labels=[volumen[-1], RSI2[-1]]) + plts_ex[c].set_ylim([bottom1*0.95, top1*1.05]) plt.draw() plt.pause(1e-17) @@ -79,28 +209,60 @@ def create_graph(iterator, lines): if __name__ == "__main__": + beginning = int(input("podaj początek zakresu z którego chcesz otrzymać średnią: ")) + end = int(input("podaj koniec zakresu z którego chcesz otrzymać średnią: ")) + plt.ion() plts = {} + plts_ex = {} lines = {} + lines_ex = {} - fig = plt.figure() + fig1 = plt.figure() + fig2 = plt.figure() itr = 1 + itr2 = 1 + for c in data_currency.keys(): - plts[c] = fig.add_subplot(len(data_currency), 1, itr) - buy_line, = plts[c].plot([0], [0]) - sell_line, = plts[c].plot([0], [0]) - lines[c] = [buy_line, sell_line] + plts[c] = fig1.add_subplot(len(data_currency), 1, itr) + buy_line, = plts[c].plot([0], [0], label='buy') + sell_line, = plts[c].plot([0], [0], label='sell') + buy_average, = plts[c].plot([0], [0], label='buy_average') + sell_average, = plts[c].plot([0], [0], label='sell_average') + # RSI, = plts[c].plot([0], [0], label='RSI') + lines[c] = [buy_line, sell_line, buy_average, sell_average] plts[c].set_ylim([1, 5]) - plts[c].set_title(f"{c} - USD") + plts[c].set_title(f"{c} - PLN") plts[c].set_xlabel("time") plts[c].set_ylabel("value") + plts[c].legend() + itr += 1 - fig.tight_layout(h_pad=0.5) + fig1.tight_layout(h_pad=0.5) + + for c in data_currency.keys(): + plts_ex[c] = fig2.add_subplot(len(data_currency), 1, itr2) + + volumen, = plts_ex[c].plot([0], [0], label='Volumen') + RSI, = plts_ex[c].plot([0], [0], label='RSI') + lines_ex[c] = [volumen, RSI] + + plts_ex[c].set_ylim([1, 5]) + plts_ex[c].set_title(f"{c} - PLN") + + plts_ex[c].set_xlabel("time") + plts_ex[c].set_ylabel("value") + + plts_ex[c].legend() + + itr2 += 1 + + fig2.tight_layout(h_pad=0.5) - data(lines) + data(lines, beginning, end) From 4666b62f06dbda57951924f78342c34913fc4907 Mon Sep 17 00:00:00 2001 From: Maja Date: Sun, 9 May 2021 12:03:02 +0200 Subject: [PATCH 6/9] update --- financialAPI.py | 69 +++++++++++++++++++------------------------------ 1 file changed, 26 insertions(+), 43 deletions(-) diff --git a/financialAPI.py b/financialAPI.py index 089ba227..5d5e47fd 100644 --- a/financialAPI.py +++ b/financialAPI.py @@ -11,7 +11,6 @@ "GNT": [], } - data_currency_extra = { "LSK": [], "BCP": [], @@ -19,11 +18,7 @@ } -# beginning = 0 -# end = 0 - - -def data(lines, beginning, end): +def data(lines, window, zakres): currency_1 = ['LSK', 'BCP', 'GNT'] currency_2 = 'PLN' category = 'orderbook' @@ -33,7 +28,7 @@ def data(lines, beginning, end): for c in currency_1: data = download_data(c, currency_2, category) if data is not None: - diffrence = calculate(data, c, beginning, end) + diffrence = calculate(data, c, window, zakres) create_graph(iterator, lines) time.sleep(5) return diffrence @@ -61,34 +56,28 @@ def get_volumen(currency): return data['items'][1]['a'] -def calculate_averange(currency, beginning, end): +def calculate_averange(currency, window_len): data = data_currency[currency] data_points_ctr = len(data) - print(data_points_ctr) - if beginning > end or beginning >= data_points_ctr: - return 0, 0 + if window_len >= data_points_ctr: + window_len = data_points_ctr - end = min(data_points_ctr, end+1) - b_avg = statistics.mean([dp["buy_price"] for dp in data[beginning:end]]) - s_avg = statistics.mean([dp["sell_price"] for dp in data[beginning:end]]) + window = data[-window_len:] + + b_avg = statistics.mean([dp["buy_price"] for dp in window]) + s_avg = statistics.mean([dp["sell_price"] for dp in window]) - print(data[beginning:end]) return b_avg, s_avg -def calculate_RSI(currency, beginning, end): +def calculate_RSI(currency, window_len): average_price_increase = [] average_price_decrease = [] data = data_currency[currency] data_points_ctr = len(data) - if beginning > end or beginning >= data_points_ctr: - return 0 - - end = min(data_points_ctr, end+1) - - if len(data) >= (end-beginning+1): - value = data[end-1]["buy_price"] - data[beginning]["buy_price"] + if len(data) >= window_len: + value = data[-1]["buy_price"] - data[data_points_ctr-window_len]["buy_price"] if value > 0: average_price_increase.append(value) elif value <= 0: @@ -116,9 +105,8 @@ def calculate(data, currency, beginning, end): } data_currency[currency].append(diffrence) - print(beginning, end) - value_buy, value_sell = calculate_averange(currency, beginning, end) - RSI = calculate_RSI(currency, beginning, end) + value_buy, value_sell = calculate_averange(currency, window) + RSI = calculate_RSI(currency, zakres) volumen = get_volumen(currency) diffrence2 = { @@ -146,8 +134,7 @@ def create_graph(j, lines): tim = [] buy_average = [] sell_average = [] - # RSI1 = [] - RSI2 = [] + RSI = [] volumen = [] width = 0.35 if len(data) > 0: @@ -160,10 +147,9 @@ def create_graph(j, lines): tim.append(data[i-len(itr)]["time"]) buy_average.append(data2[i-len(itr)]["buy_averange"]) sell_average.append(data2[i-len(itr)]["sell_averange"]) - # RSI1.append(0.1*data2[i-len(itr)]["RSI"]) - RSI2.append(data2[i-len(itr)]["RSI"]) + RSI.append(data2[i-len(itr)]["RSI"]) volumen.append(float(data2[i-len(itr)]["volumen"])) - + # dodać przerywane linie l[0].set_data(itr, buy) l[1].set_data(itr, sell) l[2].set_data(itr, buy_average) @@ -184,23 +170,22 @@ def create_graph(j, lines): br1 = itr br2 = [x + width for x in br1] - plts_ex[c].bar(br1, volumen, width, align='center', color='blue', label=volumen) - plts_ex[c].bar(br2, RSI2, width, align='center', color='orange', label=RSI2) + plts_ex[c].bar(br2, RSI, width, align='center', color='orange', label=RSI) plts_ex[c].set_xticklabels(tim, rotation='horizontal', fontsize=7) plts_ex[c].set_xlim(0, xrang-1) - if min(RSI2) >= min(volumen): + if min(RSI) >= min(volumen): bottom1 = min(volumen) else: - bottom1 = min(RSI2) + bottom1 = min(RSI) - if max(RSI2) <= max(volumen): + if max(RSI) <= max(volumen): top1 = max(volumen) else: - top1 = max(RSI2) - plts_ex[c].legend(labels=[volumen[-1], RSI2[-1]]) + top1 = max(RSI) + plts_ex[c].legend(labels=[f'Volumen: {volumen[-1]}', f'RSI: {RSI[-1]}']) plts_ex[c].set_ylim([bottom1*0.95, top1*1.05]) plt.draw() @@ -209,9 +194,8 @@ def create_graph(j, lines): if __name__ == "__main__": - beginning = int(input("podaj początek zakresu z którego chcesz otrzymać średnią: ")) - end = int(input("podaj koniec zakresu z którego chcesz otrzymać średnią: ")) - + window = int(input("podaj koniec zakresu z którego chcesz otrzymać średnią: ")) + zakres = int(input("podaj koniec zakresu z którego chcesz otrzymać RSI: ")) plt.ion() plts = {} @@ -231,7 +215,6 @@ def create_graph(j, lines): sell_line, = plts[c].plot([0], [0], label='sell') buy_average, = plts[c].plot([0], [0], label='buy_average') sell_average, = plts[c].plot([0], [0], label='sell_average') - # RSI, = plts[c].plot([0], [0], label='RSI') lines[c] = [buy_line, sell_line, buy_average, sell_average] plts[c].set_ylim([1, 5]) @@ -265,4 +248,4 @@ def create_graph(j, lines): fig2.tight_layout(h_pad=0.5) - data(lines, beginning, end) + data(lines, window, zakres) From 81bb9ac1d7f8ddcfe058bce3e0d9cdf312866e19 Mon Sep 17 00:00:00 2001 From: Maja Date: Wed, 19 May 2021 16:22:35 +0200 Subject: [PATCH 7/9] lista 5 --- financialAPI.py | 188 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 178 insertions(+), 10 deletions(-) diff --git a/financialAPI.py b/financialAPI.py index 5d5e47fd..acebf602 100644 --- a/financialAPI.py +++ b/financialAPI.py @@ -1,6 +1,7 @@ import time import requests import statistics +import numpy as np from requests.exceptions import HTTPError import matplotlib.pyplot as plt @@ -17,6 +18,16 @@ "GNT": [], } +data_currency_extra1 = { + "LSK": [], + "BCP": [], + "GNT": [], +} + +S = 5 +X = 5 +Y = 3 + def data(lines, window, zakres): currency_1 = ['LSK', 'BCP', 'GNT'] @@ -30,6 +41,9 @@ def data(lines, window, zakres): if data is not None: diffrence = calculate(data, c, window, zakres) create_graph(iterator, lines) + candidate = find_candidate(currency_1[0], currency_1[1], currency_1[2]) + define_liquid(candidate) + define_volatile(candidate) time.sleep(5) return diffrence @@ -91,19 +105,151 @@ def calculate_RSI(currency, window_len): return RSI +def check_trend(currency): + down_hills_points = [] + up_hills_points = [] + data = data_currency_extra[currency] + + for items in range(1, len(data)-1): + + if (data[items - 1]["RSI"] > data[items]["RSI"]) and (data[items]["RSI"] < data[items + 1]["RSI"]): # sprawdzam czy to dołek and (data[items]["RSI"] < data[items + 1]["RSI"]) + down_hills_points.append(data[items]["RSI"]) + print(down_hills_points, '-') + else: + down_hills_points.append(0) + + if (data[items - 1]["RSI"] < data[items]["RSI"]) and (data[items]["RSI"] > data[items + 1]["RSI"]): # sprawdzam czy to wierzchołek and (data[items]["RSI"] > data[items + 1]["RSI"]) + up_hills_points.append(data[items]["RSI"]) + print(up_hills_points, '+') + else: + up_hills_points.append(0) + + if (down_hills_points[-1] < down_hills_points[len(down_hills_points) - 2]) and \ + (down_hills_points[len(down_hills_points) - 2] != 0): + print("down") + return "Downward" + + elif (up_hills_points[-1] > up_hills_points[len(up_hills_points) - 2]) and \ + (up_hills_points[-1] != 0): + print("up") + return "Rising" + + elif ((up_hills_points[-1] == up_hills_points[len(up_hills_points) - 2]) + or (down_hills_points[-1] == down_hills_points[len(down_hills_points) - 2])) \ + and (up_hills_points[-1] != 0 and up_hills_points[len(up_hills_points) - 2] != 0) \ + and (down_hills_points[-1] != 0 and down_hills_points[len(down_hills_points) - 2] != 0): + print("side") + return "Sideways" + + else: + print("insufficient amount of data") + return "Insufficient amount of data" + + +def find_candidate(c1, c2, c3): + currency1 = False + currency2 = False + currency3 = False + + c1_trend = data_currency_extra1[c1][-1]["trend"] + c2_trend = data_currency_extra1[c2][-1]["trend"] + c3_trend = data_currency_extra1[c3][-1]["trend"] + + print(c1_trend, c2_trend, c3_trend) + bool_array = [currency1, currency2, currency3] + + LastVolumen_c1 = data_currency_extra[c1][-1]['volumen'] + LastVolumen_c2 = data_currency_extra[c2][-1]['volumen'] + LastVolumen_c3 = data_currency_extra[c3][-1]['volumen'] + + LastVolumeArray = [LastVolumen_c1, LastVolumen_c2, LastVolumen_c3] + max = "" + + if c1_trend != "Downward": + currency1 = True + if c2_trend != "Downward": + currency2 = True + if c3_trend != "Downward": + currency3 = True + + print(bool_array, ' ------- ') + + max_array = [] + for items in bool_array: + if items is True: + max_array.append(LastVolumeArray[items]) + else: + max_array.append(0) + out = max_array.index(np.max(max_array)) + + if out == 0: + max = c1 + elif out == 1: + max = c2 + else: + max = c3 + + data_currency_extra1[max][-1]['candidate'] = 'C' + return max + + +def define_liquid(currency): + + buy = data_currency[currency][-1]["buy_price"] + sell = data_currency[currency][-1]["sell_price"] + + maximum = max(buy, sell) + minimum = min(buy, sell) + + out = maximum * 100/minimum + distance = 100 - out + + if distance < 5: + data_currency_extra1[currency][-1]['liquid']='L' + return True + else: + return False + + +def define_volatile(currency): + buy_array = [] + for i in range(len(data_currency[currency])): + data = data_currency[currency][i]["buy_price"] + buy_array.append(data) + + if Y < len(buy_array): + value_Y = buy_array[len(buy_array)-Y] + else: + value_Y = buy_array[-1] + + current = buy_array[0] + + maximum = max(value_Y, current) + minimum = min(value_Y, current) + + out = minimum * 100/maximum + distance = 100 - out + + if distance > X: + data_currency_extra1[currency][-1]['volatile'] = 'V' + return True + else: + return False + + def calculate(data, currency, beginning, end): buy = data['bids'][0][0] sell = data['asks'][0][0] - procenty = (1-(sell-buy)/sell) * 100 + procent = (1-(sell-buy)/sell) * 100 t = time.strftime("%H:%M:%S", time.localtime()) diffrence = { 'buy_price': data['bids'][0][0], 'sell_price': data['asks'][0][0], - 'procents': procenty, + 'procents': procent, 'time': str(t), } - + print(currency, '-----', procent) data_currency[currency].append(diffrence) value_buy, value_sell = calculate_averange(currency, window) RSI = calculate_RSI(currency, zakres) @@ -117,8 +263,19 @@ def calculate(data, currency, beginning, end): } data_currency_extra[currency].append(diffrence2) + if len(data_currency_extra[currency]) >= 3: + trend = check_trend(currency) - print(diffrence, diffrence2) + else: + trend = None + + diffrence3 = { + 'trend': trend, + 'candidate': '-', + 'volatile': '-', + 'liquid': '-', + } + data_currency_extra1[currency].append(diffrence3) return diffrence @@ -129,6 +286,7 @@ def create_graph(j, lines): for c, l in lines.items(): data = data_currency[c] data2 = data_currency_extra[c] + data3 = data_currency_extra1[c] buy = [] sell = [] tim = [] @@ -136,6 +294,10 @@ def create_graph(j, lines): sell_average = [] RSI = [] volumen = [] + trend = [] + candidate = [] + volatile = [] + liquid = [] width = 0.35 if len(data) > 0: itr = range(xrang) @@ -149,13 +311,18 @@ def create_graph(j, lines): sell_average.append(data2[i-len(itr)]["sell_averange"]) RSI.append(data2[i-len(itr)]["RSI"]) volumen.append(float(data2[i-len(itr)]["volumen"])) - # dodać przerywane linie + trend.append(data3[i-len(itr)]["trend"]) + candidate.append(data3[i-len(itr)]["candidate"]) + volatile.append(data3[i-len(itr)]["volatile"]) + liquid.append(data3[i-len(itr)]["liquid"]) + l[0].set_data(itr, buy) l[1].set_data(itr, sell) l[2].set_data(itr, buy_average) l[3].set_data(itr, sell_average) plts[c].set_xticklabels(tim, rotation='horizontal', fontsize=7) plts[c].set_xlim(0, xrang-1) + plts[c].set_title(f"{c} - PLN, {candidate[0]}, {volatile[0]}, {liquid[0]}") if min(buy) >= min(buy_average): bottom = min(buy_average) @@ -175,6 +342,7 @@ def create_graph(j, lines): plts_ex[c].set_xticklabels(tim, rotation='horizontal', fontsize=7) plts_ex[c].set_xlim(0, xrang-1) + plts_ex[c].set_title(f"{c} - PLN, trend:{trend[-1]}") if min(RSI) >= min(volumen): bottom1 = min(volumen) @@ -186,7 +354,7 @@ def create_graph(j, lines): else: top1 = max(RSI) plts_ex[c].legend(labels=[f'Volumen: {volumen[-1]}', f'RSI: {RSI[-1]}']) - plts_ex[c].set_ylim([bottom1*0.95, top1*1.05]) + plts_ex[c].set_ylim([bottom1*0.85, top1*1.15]) plt.draw() plt.pause(1e-17) @@ -194,8 +362,8 @@ def create_graph(j, lines): if __name__ == "__main__": - window = int(input("podaj koniec zakresu z którego chcesz otrzymać średnią: ")) - zakres = int(input("podaj koniec zakresu z którego chcesz otrzymać RSI: ")) + window = int(input("podaj długość zakresu z którego chcesz otrzymać średnią: ")) + zakres = int(input("podaj długość zakresu z którego chcesz otrzymać RSI: ")) plt.ion() plts = {} @@ -213,8 +381,8 @@ def create_graph(j, lines): plts[c] = fig1.add_subplot(len(data_currency), 1, itr) buy_line, = plts[c].plot([0], [0], label='buy') sell_line, = plts[c].plot([0], [0], label='sell') - buy_average, = plts[c].plot([0], [0], label='buy_average') - sell_average, = plts[c].plot([0], [0], label='sell_average') + buy_average, = plts[c].plot([0], [0], color='blue', linestyle='dashed', label='buy_average') + sell_average, = plts[c].plot([0], [0], color='orange', linestyle='dashed', label='sell_average') lines[c] = [buy_line, sell_line, buy_average, sell_average] plts[c].set_ylim([1, 5]) From 7e485618d034ee9e5bbe2a7845ac533f70d2b736 Mon Sep 17 00:00:00 2001 From: Maja Date: Wed, 19 May 2021 16:25:11 +0200 Subject: [PATCH 8/9] lista 5 --- financialAPI.py | 1 + 1 file changed, 1 insertion(+) diff --git a/financialAPI.py b/financialAPI.py index acebf602..e90db74d 100644 --- a/financialAPI.py +++ b/financialAPI.py @@ -163,6 +163,7 @@ def find_candidate(c1, c2, c3): LastVolumen_c3 = data_currency_extra[c3][-1]['volumen'] LastVolumeArray = [LastVolumen_c1, LastVolumen_c2, LastVolumen_c3] + max = "" if c1_trend != "Downward": From a6277a66199b9358850b1fa43f6b8fa568ac9cb5 Mon Sep 17 00:00:00 2001 From: Maja Date: Wed, 9 Jun 2021 16:14:15 +0200 Subject: [PATCH 9/9] lista 6 --- buy_json.json | 1 + file_name.txt | 1 + finacialGUI.py | 207 +++++++++++++++++++++++++++++++++++++++++ financialAPI.py | 239 ++++++++++++++++++++++++++++++++++++------------ sell_json.json | 1 + 5 files changed, 391 insertions(+), 58 deletions(-) create mode 100644 buy_json.json create mode 100644 file_name.txt create mode 100644 finacialGUI.py create mode 100644 sell_json.json diff --git a/buy_json.json b/buy_json.json new file mode 100644 index 00000000..f47851ac --- /dev/null +++ b/buy_json.json @@ -0,0 +1 @@ +{"BCP": [], "LSK": [], "GNT": []} \ No newline at end of file diff --git a/file_name.txt b/file_name.txt new file mode 100644 index 00000000..e45a2e21 --- /dev/null +++ b/file_name.txt @@ -0,0 +1 @@ +buy_json.json \ No newline at end of file diff --git a/finacialGUI.py b/finacialGUI.py new file mode 100644 index 00000000..bf548ef2 --- /dev/null +++ b/finacialGUI.py @@ -0,0 +1,207 @@ +from tkinter import * +import json + + +def read_file(): + file_name = 'buy_json.json' + text_file = open('file_name.txt', 'w') + text_file.write(file_name) + text_file.close() + + return file_name + + +def read_other_file(): + print("Podaj nazwe pliku do odczytu:") + file_name = str(input()) + + text_file = open('file_name.txt', 'w') + text_file.write(file_name) + text_file.close() + return file_name + + +def clean_buy(): + file = open(FILE_NAME, 'r') + json_data = json.load(file) + file.close() + + json_data["LSK"] = [] + json_data["BCP"] = [] + json_data["GNT"] = [] + + file = open(FILE_NAME, 'w') + json.dump(json_data, file) + file.close() + + +def clean_sell(): + file = open('sell_json.json', 'r') + json_data = json.load(file) + file.close() + + json_data["LSK"] = [] + json_data["BCP"] = [] + json_data["GNT"] = [] + + file = open('sell_json.json', 'w') + json.dump(json_data, file) + file.close() + +def clean_all_date(): + clean_sell() + clean_buy() + + +def window_tk(): + global currency, action, amount_value, price_value, file_name + + window = Tk() + + window.configure(background='snow4') + window.title('Buy and Sell') + window.geometry("600x600") + + currencies = ["LSK", "BCP", "GNT"] + actions = ["BUY", "SELL"] + + chose_currency = Label(window, text='Chose currency', font=('Calibri', 17), fg='white') + chose_currency.pack(expand=True) + chose_currency.configure(background='snow4') + + currency = StringVar(window) + which_option = OptionMenu(window, currency, *currencies) + currency.set("-----") + which_option.config(width=32, background='snow2', font=('Calibri', 17), fg='snow4') + which_option.pack(expand=True) + which_option.configure(background='snow4', width=20) + + b_or_s = Label(window, text='Buy or sell?', font=('Calibri', 17), fg='white') + b_or_s.pack(expand=True) + b_or_s.configure(background='snow4') + + action = StringVar(window) + action.set("-----") + which_action = OptionMenu(window, action, *actions) + which_action.config(width=32, background='snow2', font=('Calibri', 17), fg='snow4') + which_action.pack(expand=True) + which_action.configure(background='snow4', width=20) + + price_label = Label(window, text='Price:', font=('Calibri', 17), fg='white') + price_label.pack(expand=True) + price_label.configure(background='snow4') + + price_value = Entry(window, font=('Calibri', 17), fg='snow4') + price_value.configure(background='snow2', width=20) + price_value.pack(expand=True) + + amount_label = Label(window, text='Amount:', font=('Calibri', 17), fg='white') + amount_label.pack(expand=True) + amount_label.configure(background='snow4') + + amount_value = Entry(window, font=('Calibri', 17), fg='snow4') + amount_value.configure(background='snow2', width=20) + amount_value.pack(expand=True) + + clean = Button(window, text='CLEAN BUY FILE', font=('Calibri', 17), fg='snow4', command=clean_buy) + clean.pack(anchor="s", side=LEFT) + clean.configure(background='snow4') + + clean1 = Button(window, text='CLEAN GAIN FILE', font=('Calibri', 17), fg='snow4', command=clean_sell) + clean1.pack(anchor="s", side=RIGHT) + clean1.configure(background='snow4') + + clean2 = Button(window, text='CLEAN ALL FILES', font=('Calibri', 17), fg='snow4', command=clean_all_date) + clean2.pack(side=BOTTOM) + clean2.configure(background='snow4') + + confirmation = Button(window, text='CONFIRM', font=('Calibri', 17), fg='snow4', command=save_data) + confirmation.pack(expand=True) + confirmation.configure(background='snow4') + + window.mainloop() + + +def save_data(): + + total_amount = 0 + buying_price = 0 + which_currency = currency.get() + buy_sell = action.get() + how_much = float(amount_value.get()) + price = float(price_value.get()) + + if buy_sell == "BUY": + file = open(FILE_NAME, 'r') + json_data = json.load(file) + file.close() + + json_data[which_currency].append([how_much, price]) + file = open(FILE_NAME, 'w') + json.dump(json_data, file) + + file.close() + + if buy_sell == "SELL": + + file = open(FILE_NAME, 'r') + json_data = json.load(file) + file.close() + + currency_amount = json_data[which_currency] + for i in range(len(currency_amount)): + total_amount += currency_amount[i][0] + + if how_much > total_amount: + print("Nie masz tylu zakupionych walut") + + else: + + counter = 0 + j = 0 + while counter < how_much: + if currency_amount[j][0] > 0: + buying_price += currency_amount[j][1] + currency_amount[j][0] = currency_amount[j][0] - 1 + counter += 1 + + elif currency_amount[j][0] == 0: + j = j + 1 + + seling_price = (how_much * price) + profit = seling_price - buying_price + + for i in range(0, len(currency_amount)): + if currency_amount[0][0] == 0: + del currency_amount[0] + + else: + break + + json_data[which_currency] = currency_amount + file = open(FILE_NAME, 'w') + json.dump(json_data, file) + file.close() + + file_gain = open('sell_json.json', 'r') + profit_data = json.load(file_gain) + file_gain.close() + profit_data[which_currency].append(profit) + + file_gain = open('sell_json.json', 'w') + json.dump(profit_data, file_gain) + file_gain.close() + + amount_value.delete(0, 'end') + price_value.delete(0, 'end') + + +if __name__ == "__main__": + + print("Jesli chcesz skorzystac z pliku istniejacymi z danymi nacisnij 1, jezeli nie wybierz 2") + y_or_n = str(input()) + if y_or_n == '1': + FILE_NAME = read_other_file() + else: + FILE_NAME = read_file() + window_tk() diff --git a/financialAPI.py b/financialAPI.py index e90db74d..b92a2cfb 100644 --- a/financialAPI.py +++ b/financialAPI.py @@ -1,9 +1,10 @@ +import json import time import requests import statistics -import numpy as np from requests.exceptions import HTTPError import matplotlib.pyplot as plt +from matplotlib.ticker import MaxNLocator data_currency = { @@ -24,8 +25,8 @@ "GNT": [], } -S = 5 -X = 5 +S = 0.05 +X = 3 Y = 3 @@ -44,6 +45,7 @@ def data(lines, window, zakres): candidate = find_candidate(currency_1[0], currency_1[1], currency_1[2]) define_liquid(candidate) define_volatile(candidate) + gain_from_json() time.sleep(5) return diffrence @@ -127,7 +129,7 @@ def check_trend(currency): if (down_hills_points[-1] < down_hills_points[len(down_hills_points) - 2]) and \ (down_hills_points[len(down_hills_points) - 2] != 0): print("down") - return "Downward" + return "Decrease" elif (up_hills_points[-1] > up_hills_points[len(up_hills_points) - 2]) and \ (up_hills_points[-1] != 0): @@ -146,6 +148,19 @@ def check_trend(currency): return "Insufficient amount of data" +def check_trend_vol2(currency): + data = data_currency_extra[currency][- 1]["RSI"] + if data > 70: + print("up") + return "Rising" + elif data < 30: + print("down") + return "Decrease" + else: + print("side") + return "Sideways" + + def find_candidate(c1, c2, c3): currency1 = False currency2 = False @@ -155,43 +170,49 @@ def find_candidate(c1, c2, c3): c2_trend = data_currency_extra1[c2][-1]["trend"] c3_trend = data_currency_extra1[c3][-1]["trend"] - print(c1_trend, c2_trend, c3_trend) bool_array = [currency1, currency2, currency3] - LastVolumen_c1 = data_currency_extra[c1][-1]['volumen'] - LastVolumen_c2 = data_currency_extra[c2][-1]['volumen'] - LastVolumen_c3 = data_currency_extra[c3][-1]['volumen'] - - LastVolumeArray = [LastVolumen_c1, LastVolumen_c2, LastVolumen_c3] - - max = "" + last_volumen_c1 = float(data_currency_extra[c1][-1]['volumen']) + last_volumen_c2 = float(data_currency_extra[c2][-1]['volumen']) + last_volumen_c3 = float(data_currency_extra[c3][-1]['volumen']) - if c1_trend != "Downward": - currency1 = True - if c2_trend != "Downward": - currency2 = True - if c3_trend != "Downward": - currency3 = True + last_volumen_array = [last_volumen_c1, last_volumen_c2, last_volumen_c3] + maximum = '' - print(bool_array, ' ------- ') + if c1_trend != "Decrease": + bool_array[0] = True + if c2_trend != "Decrease": + bool_array[1] = True + if c3_trend != "Decrease": + bool_array[2] = True max_array = [] - for items in bool_array: - if items is True: - max_array.append(LastVolumeArray[items]) + for i in range(len(bool_array)): + if bool_array[i]: + max_array.append(last_volumen_array[i]) else: max_array.append(0) - out = max_array.index(np.max(max_array)) + out = max_array.index(max(max_array)) + # print(last_volumen_array) if out == 0: - max = c1 + maximum = c1 + else1 = c2 + else2 = c3 elif out == 1: - max = c2 + maximum = c2 + else1 = c1 + else2 = c3 else: - max = c3 + maximum = c3 + else1 = c2 + else2 = c1 - data_currency_extra1[max][-1]['candidate'] = 'C' - return max + data_currency_extra1[maximum][-1]['candidate'] = 'C' + data_currency_extra1[else2][-1]['candidate'] = '-' + data_currency_extra1[else1][-1]['candidate'] = '-' + + return maximum def define_liquid(currency): @@ -201,12 +222,10 @@ def define_liquid(currency): maximum = max(buy, sell) minimum = min(buy, sell) + diffrence = (maximum - minimum) / 100 - out = maximum * 100/minimum - distance = 100 - out - - if distance < 5: - data_currency_extra1[currency][-1]['liquid']='L' + if diffrence < S: + data_currency_extra1[currency][-1]['liquid'] = 'L' return True else: return False @@ -218,20 +237,28 @@ def define_volatile(currency): data = data_currency[currency][i]["buy_price"] buy_array.append(data) - if Y < len(buy_array): - value_Y = buy_array[len(buy_array)-Y] - else: - value_Y = buy_array[-1] + y_value_array = [] + for d in range(Y): + if Y < len(buy_array): + Y_value = buy_array[len(buy_array)-i] + y_value_array.append(Y_value) + else: + Y_value = buy_array[-1] + + current = buy_array[0] - current = buy_array[0] + counter = 0 + for y in range(len(y_value_array)): + maximum = max(Y_value, current) + minimum = min(Y_value, current) - maximum = max(value_Y, current) - minimum = min(value_Y, current) + out = minimum * 100/maximum + distance = 100 - out - out = minimum * 100/maximum - distance = 100 - out + if distance > X: + counter += 1 - if distance > X: + if counter > 0: data_currency_extra1[currency][-1]['volatile'] = 'V' return True else: @@ -262,10 +289,10 @@ def calculate(data, currency, beginning, end): 'RSI': RSI, 'volumen': volumen, } - data_currency_extra[currency].append(diffrence2) if len(data_currency_extra[currency]) >= 3: - trend = check_trend(currency) + # trend = check_trend(currency) + trend = check_trend_vol2(currency) else: trend = None @@ -280,10 +307,69 @@ def calculate(data, currency, beginning, end): return diffrence +def get_file_name(): + file = open('file_name.txt', 'r') + filename = file.read() + return filename + + +def mean_from_json(file): + currency = ['LSK', 'BCP', 'GNT'] + currency_mean = { + "LSK": [], + "BCP": [], + "GNT": [], + } + currency_amount = { + "LSK": [], + "BCP": [], + "GNT": [], + } + + with open(file, 'r') as openfile: + data = json.load(openfile) + + for c in currency: + mean = 0 + price = list() + quantity = list() + for i in range(len(data[c])): + price_of_one = data[c][i][1] * data[c][i][0] + quantity.append(data[c][i][0]) + price.append(price_of_one) + if len(price) < 1: + mean = 0 + else: + mean = (sum(price)) / (sum(quantity)) + + currency_mean[c].append(mean) + currency_amount[c].append(sum(quantity)) + + openfile.close() + return currency_mean, currency_amount + + +def gain_from_json(): + currency = ['LSK', 'BCP', 'GNT'] + currency_gain ={ + "LSK": [], + "BCP": [], + "GNT": [], + } + + with open('sell_json.json', 'r') as openfile: + gain_data = json.load(openfile) + for c in currency: + c_gain = gain_data[c] + currency_gain[c].append(sum(c_gain)) + + return currency_gain + xrang = 8 def create_graph(j, lines): + for c, l in lines.items(): data = data_currency[c] data2 = data_currency_extra[c] @@ -300,6 +386,15 @@ def create_graph(j, lines): volatile = [] liquid = [] width = 0.35 + our_average = [] + our_gain = [] + our_amount = [] + # count1 = {} + averages, amount = mean_from_json(FILE_NAME) + gains = gain_from_json() + + # count1[c] = plts[c].text(0.0, 1.3, '', transform=plts[c].transAxes, fontsize=7) + if len(data) > 0: itr = range(xrang) if len(data) < xrang: @@ -316,31 +411,53 @@ def create_graph(j, lines): candidate.append(data3[i-len(itr)]["candidate"]) volatile.append(data3[i-len(itr)]["volatile"]) liquid.append(data3[i-len(itr)]["liquid"]) + our_average.append(averages[c][0]) + + g = gains[c][0] + a = amount[c][0] + + our_amount.append(a) + our_gain.append(g) + locator = MaxNLocator(nbins=4) l[0].set_data(itr, buy) l[1].set_data(itr, sell) l[2].set_data(itr, buy_average) l[3].set_data(itr, sell_average) + l[4].set_data(itr, our_average) plts[c].set_xticklabels(tim, rotation='horizontal', fontsize=7) plts[c].set_xlim(0, xrang-1) - plts[c].set_title(f"{c} - PLN, {candidate[0]}, {volatile[0]}, {liquid[0]}") + plts[c].yaxis.set_major_locator(locator) + + plts[c].legend(labels=[f'Buy: {round(buy[-1], 2)}', f'Sell: {round(sell[-1],2)}', f'Buy Average: {round(buy_average[-1],2)}', f'Sell Average: {round(sell_average[-1],2)}',f'Our Average: {round(our_average[-1],2)}']) + plts[c].set_title(f"{c} - PLN, {candidate[0]}, {volatile[0]}, {liquid[0]}, Profit: {round(our_gain[-1],2)}, Amount: {round(our_amount[-1],2)}") - if min(buy) >= min(buy_average): - bottom = min(buy_average) + min_buy_avg = min(buy_average) + min_buy = min(buy) + + if our_average[-1] != 0: + our_avg = our_average[-1] + bottom = min(min_buy_avg, min_buy, our_avg) else: - bottom = min(buy) + bottom = min(min_buy_avg, min_buy) + + max_sell_avg = max(sell_average) + max_sell = max(sell) - if max(sell) <= max(sell_average): - top = max(sell_average) + if our_average[-1] != 0: + max_our_avg = max(our_average) + top = max(max_sell_avg, max_sell, max_our_avg) else: - top = max(sell) + top = max(max_sell_avg, max_sell) plts[c].set_ylim([bottom*0.95, top*1.05]) + locator1 = MaxNLocator(nbins=4) + br1 = itr br2 = [x + width for x in br1] plts_ex[c].bar(br1, volumen, width, align='center', color='blue', label=volumen) plts_ex[c].bar(br2, RSI, width, align='center', color='orange', label=RSI) - + plts_ex[c].yaxis.set_major_locator(locator1) plts_ex[c].set_xticklabels(tim, rotation='horizontal', fontsize=7) plts_ex[c].set_xlim(0, xrang-1) plts_ex[c].set_title(f"{c} - PLN, trend:{trend[-1]}") @@ -355,7 +472,7 @@ def create_graph(j, lines): else: top1 = max(RSI) plts_ex[c].legend(labels=[f'Volumen: {volumen[-1]}', f'RSI: {RSI[-1]}']) - plts_ex[c].set_ylim([bottom1*0.85, top1*1.15]) + plts_ex[c].set_ylim([bottom1*0.80, top1*1.20]) plt.draw() plt.pause(1e-17) @@ -363,8 +480,14 @@ def create_graph(j, lines): if __name__ == "__main__": - window = int(input("podaj długość zakresu z którego chcesz otrzymać średnią: ")) - zakres = int(input("podaj długość zakresu z którego chcesz otrzymać RSI: ")) + window = 4 + zakres = 3 + + FILE_EXTENSION = "json" + CONNECTION_TYPE = "ticker" + FILE_NAME = get_file_name() + print(FILE_NAME) + plt.ion() plts = {} @@ -384,11 +507,11 @@ def create_graph(j, lines): sell_line, = plts[c].plot([0], [0], label='sell') buy_average, = plts[c].plot([0], [0], color='blue', linestyle='dashed', label='buy_average') sell_average, = plts[c].plot([0], [0], color='orange', linestyle='dashed', label='sell_average') - lines[c] = [buy_line, sell_line, buy_average, sell_average] + our_average, = plts[c].plot([0], [0], color='purple', linestyle='dashed', label='our_average') + lines[c] = [buy_line, sell_line, buy_average, sell_average, our_average, ] plts[c].set_ylim([1, 5]) plts[c].set_title(f"{c} - PLN") - plts[c].set_xlabel("time") plts[c].set_ylabel("value") diff --git a/sell_json.json b/sell_json.json new file mode 100644 index 00000000..f47851ac --- /dev/null +++ b/sell_json.json @@ -0,0 +1 @@ +{"BCP": [], "LSK": [], "GNT": []} \ No newline at end of file