From dd7408371ed9ed109c34d9246078ce9a3704c9fa Mon Sep 17 00:00:00 2001 From: nielogicznie <254292@student.pwr.edu.pl> Date: Fri, 4 Jun 2021 16:46:47 +0200 Subject: [PATCH 1/8] l6 first attempt GUI only --- llist_6.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 llist_6.py diff --git a/llist_6.py b/llist_6.py new file mode 100644 index 0000000..e69de29 From 9c712fa5f03083d195ca03a67476bc800212edb0 Mon Sep 17 00:00:00 2001 From: nielogicznie <254292@student.pwr.edu.pl> Date: Fri, 4 Jun 2021 22:07:12 +0200 Subject: [PATCH 2/8] working GUI --- llist_6.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/llist_6.py b/llist_6.py index e69de29..cf532c5 100644 --- a/llist_6.py +++ b/llist_6.py @@ -0,0 +1,61 @@ +from tkinter import * +import json +from tkinter.filedialog import asksaveasfile +import tkinter.ttk as ttk +import tkinter.messagebox as msb + +window = Tk() +window.geometry('640x300') +window.title('Market') + +CURRENCIES = ['ETH-PLN', 'BTC-PLN', 'LTC-PLN'] +box_value = StringVar() +combobox = ttk.Combobox(window) +combobox['values'] = CURRENCIES +combobox.current(0) + +# box_value = StringVar() +# locationBox = ttk.Combobox(master, textvariable=box_value) +# locationBox.bind("<>", self.justamethod()) +# locationBox['values'] = CURRENCIES +# locationBox.current(0) + + + +action = IntVar() +buy_button = Radiobutton(window, text="Buy", variable=action, value=1) +sell_button = Radiobutton(window, text="Sell", variable=action, value=2) + +amount = Label(window, text="Amount:") +Amount = Entry(window) + +price = Label(window, text="Price:") +Price = Entry(window) +test = 1 + + + +def check(): + amount_value = float(Amount.get()) + price_value = float(Price.get()) + action_value = float(action.get()) + currency_= combobox.get() + print(amount_value) + print(price_value) + data = {'Currency': currency_, 'Amount': amount_value, 'Price': price_value, 'Action': action_value} + print(data) + with open('file.json', 'a') as file: + file.write(json.dumps(data) + '\n') + +submit_button = Button(window, text='Accept', command=check).grid(row=4, column=1) + +buy_button.grid(row=1, column=0) +sell_button.grid(row=1, column=1) +combobox.grid(row=0, column=0) +amount.grid(row=2, column=0) +Amount.grid(row=2, column=1) +price.grid(row=3,column=0) +Price.grid(row=3, column=1) + +window.mainloop() + From 14af501e5d76e0fee41ed6353de8fd28cdad496d Mon Sep 17 00:00:00 2001 From: nielogicznie <254292@student.pwr.edu.pl> Date: Sun, 6 Jun 2021 13:28:09 +0200 Subject: [PATCH 3/8] working base for L6 --- .idea/.gitignore | 3 + .../inspectionProfiles/profiles_settings.xml | 6 + .idea/misc.xml | 4 + .idea/modules.xml | 8 + .idea/streams21.iml | 8 + .idea/vcs.xml | 6 + __pycache__/visualise.cpython-37.pyc | Bin 0 -> 2030 bytes file.json | 31 +++ l6.py | 254 ++++++++++++++++++ llist_6.py | 61 ----- visualise.py | 65 +++++ 11 files changed, 385 insertions(+), 61 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/streams21.iml create mode 100644 .idea/vcs.xml create mode 100644 __pycache__/visualise.cpython-37.pyc create mode 100644 file.json create mode 100644 l6.py delete mode 100644 llist_6.py create mode 100644 visualise.py diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..e54de75 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..136d6b2 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/streams21.iml b/.idea/streams21.iml new file mode 100644 index 0000000..d0876a7 --- /dev/null +++ b/.idea/streams21.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/__pycache__/visualise.cpython-37.pyc b/__pycache__/visualise.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..75f8501a8d59668c9be6257929cbf63e46f8a0d5 GIT binary patch literal 2030 zcmZ`(&2QT_6ep<<%Zi<*X_~l6zqYmAJaj?U?GyyvmNdf(bU~M-?LegPWRdk$>%*n! zBo5@0d)TE|ci$m7?l0J3{|T=<<+gKAeWVq49WV(Z-|s!@@$vnB(t5q_Be<6S{OhOF z3POKtaCW(1d<{?j3r2_`hGVp=XA)zIVYXtDSk%H}pPe|=DQ!2YP!DXIIWG~dGM80g z_W2a^y5uz$FL`Z*Xr0%Or&;v`V+7VY=4*>hu^Ov)EIPx_!?XspIv%oJ^zR;Le3@F`vzYGq)!S6>RmF(!~o3!nw`Yv4LXZm26S-} zyK2x`>>8lUlh}2G%2)@{o4m?zj>g@&b%LNbWCt~`oN9iWSm95%V5|puHxKrCkUxuJ z9s*P5a$u~&vjI>37={iSVhJ2FG9tgu4GFWD{Sp=A9vb1H^#o4#1KLJEqQg%lmZShL zjMfN`V8zqfpdn*s{- zbXpg2j0=2%etJ#ew6Lu+AQ2%gFo3uK@?ozz&i?gsUO zI38mhy*q`_#i+*rRRP~0%4;wnY~vbkkOrQ`;sW4MJN2cPOlW-#Kq85c$%!UW+g52q zm!)0Wq#z7;iCAGI1=@#}baZ5BOIOp35zJW6gnFXuS)sMSY}wim;y$NSusq}?sExCgs8S+Y9rGIwTFTbMit~{0zf?Nm&phnT zK*UHK0c-l6!JfINVXR?sLzm*UT1gP4tro3(@#V_los|{pejkV~mq(S&-S0ko_+aa( zvc9`9HXf8l^{i>D@TuF9A4=t#Gvw4-?+;G(=iao$EwjUZAflihbLEVWY41SiPqp@s zD&v-~9=WFHtGekS@5v?^zgvM;CPB(hZJm%POJoPi4YRnPq^FkleXJ_73r*%?+}ha^ zEo;5AD2TI;&65eVlvfEYeL6kBm z#_RAb0R;?{BO?ii^fw-DZ*OjG+~3?$)g1`}=sXSttwQ!Om&$n%w0R6NDTPP9jZ!e; z8dR#>`>BLi#8nNZwgVPr?Y@**N_`M+tF))IeoFw-E2X;$avK<7QZ2rakEIC| data[i]: + down+= data[i - 1] - data[i] + down_c += 1 + if up_c == 0: + a = 1 + else: + a = up/up_c + if down_c == 0: + b = 1 + else: + b = down/down_c + rsi = 100 - (100 / (1 + (a / b))) + return rsi + +def draw_axes(): + fig, axs = plt.subplots(2, 3, figsize=(12, 5), constrained_layout=True) + for i, currency_type in enumerate(CURRENCIES): + plt.suptitle('Cryptocurrencies trading',fontsize=24) + locator = MaxNLocator(nbins = 5) + axs[0][i].set_title(currency_type) + axs[0][i].set_xlabel('Time') + axs[0][i].set_ylabel('Value') + + axs[1][i].set_title('RSI') + axs[1][i].set_xlabel('Time') + axs[1][i].set_ylabel('Value') + + axs[0][i].xaxis.set_major_locator(locator) + axs[1][i].xaxis.set_major_locator(locator) + return fig, axs + + +def set_Legend(axs): + for i in range(2): + axs[i][2].legend(bbox_to_anchor=(1.05, 1.0), loc='upper left') + + +def user_average(dict_, curr): + buy, sell = [], [] + for i in range(len(dict_[curr]['buy'])): + buy.append(dict_[curr]['buy'][i]) + for i in range(len(dict_[curr]['sell'])): + sell.append(dict_[curr]['sell'][i]) + sum_buy = 0 + sold = 0 + sum_ = 0 + for i in buy: + sum_buy += i[0] + + for i in range(len(dict_[curr]['sell'])): + if sell[i][0] <= sum_buy: + sum_buy -= sell[i][0] + sell[i] = (0, sell[i][1]) + + else: + sell[i] = (sell[i][0] - sum_buy, sell[i][1]) + + for i in sell: + # print('i[0]',i[0], 'i[1]', i[1]) + sold += i[0] * i[1] + sum_ += i[0] + + if sum_ == 0: + sum_ = 1 + print ('sold / sum_', sold / sum_) + return sold / sum_ + _ +def avg_list_of_tuples(list): + sum_, amount = 0, 0 + for i in list: + sum_ += i[0] * i[1] + amount += i[0] + return sum_/amount + +# def buy_user_avg(dict_, currency_type): +# sum_buy, amount_buy = 0, 0 + +# for element in dict_[currency_type]['buy']: +# sum_buy += element[0] * element[1] +# amount_buy += element[0] +# return sum_buy/amount_buy + +def draw_plot(time_interval): + + fig, axs = draw_axes() + buy, sell, buy_list, sell_list, avg_buy_list, avg_sell_list, rsi_buy_list, rsi_sell_list, user_average_list = [], [], [], [], [], [], [], [], [] + i = 0 + # locator = MaxNLocator(nbins = 3) + # current_time = [] + user_data = [] + datas = {} + while True: + new_data = read_new_lines_from_json(user_data) + for element in new_data: + user_data.append(element) + # print('i=', i, 'c=', currency_type, 'crfincr', user_data) + + for a, currency_type in enumerate(CURRENCIES): + if i == 0: + datas[currency_type] = {'sell': [], 'buy':[]} + # print('c=', currency_type, 'i=', i, 'dict=', datas) + for element in new_data: + if element['Currency'] == currency_type and element['Action'] == 'sell' : + datas[currency_type]['sell'].append((element['Amount'], element['Price'])) + elif element['Currency'] == currency_type and element['Action'] == 'buy': + datas[currency_type]['buy'].append((element['Amount'], element['Price'])) + + + user_avg = user_average(datas, currency_type) + # user_average_list.append(user_avg) + + user_sell_avg = avg_list_of_tuples(datas[currency_type]['sell']) + + data = get_data(currency_type) + buy.append(data[0]) + sell.append(data[1]) + + + buy_, sell_ = get_data(currency_type) + buy_list.append(buy_) + sell_list.append(sell_) + + buy_avg = list_average(buy_list, elements_a) + sell_avg = list_average(sell_list, elements_a) + + avg_buy_list.append(buy_avg) + avg_sell_list.append(sell_avg) + rsi_buy_list.append(RSI_value(buy_list, elements_r)) + rsi_sell_list.append(RSI_value(sell_list, elements_r)) + + + if len(buy) and len(sell) and len(rsi_buy_list) and len(rsi_sell_list) >= 4: + + # time = [time_list[-2].strftime("%H:%M:%S:"), time_list[-1].strftime("%H:%M:%S:")] + x = [(i - 1) * time_interval, i * time_interval] + selling_cost = [sell[-4], sell[-1]] + purchase_cost = [buy[-4], buy[-1]] + + avg_buy_value = [avg_buy_list[-4], avg_buy_list[-1]] + avg_sell_value = [avg_sell_list[-4], avg_sell_list[-1]] + RSI_buy_value = [rsi_buy_list[-4], rsi_buy_list[-1]] + RSI_sell_value = [rsi_sell_list[-4], rsi_sell_list[-1]] + avg_user_sell_value = [user_sell_avg, user_sell_avg] + # print(currency_type, user_average_list[-1], sell[-1]) + + axs[0][a].plot(x, selling_cost, label="Selling cost", color='red') + axs[0][a].plot(x, purchase_cost, label="Purchase cost", color='yellow') + axs[0][a].plot(x, avg_buy_value, '--', label='Average Buy Cost', color='darkred') + axs[0][a].plot(x, avg_sell_value, '--', label='Average Sell Cost', color='#9B870C') + axs[0][a].plot(x, avg_user_sell_value, '--', label = 'Average User Sell Cost', color = 'green') + + + + axs[1][a].plot(x, RSI_buy_value, label='RSI buy', color='lightgreen' ) + axs[1][a].plot(x, RSI_sell_value, label='RSI sell', color='darkgreen') + + + at2 = AnchoredText('User actual profit: ' + str(round(user_avg,2) ), loc='lower left', prop=dict(size=8), frameon=True, bbox_to_anchor=(0., 1.25), bbox_transform=axs[1][a].transAxes) + at2.patch.set_boxstyle("round,pad=0.,rounding_size=0.2") + axs[1][a].add_artist(at2) + + + if len(buy) and len(sell) and len(rsi_buy_list) and len(rsi_sell_list) == 6: + set_Legend(axs) + + # if len(buy) and len(sell) and len(rsi_buy_list) and len(rsi_sell_list) >= 6: + + # at2 = AnchoredText('User actual profit: ' + str(round(user_avg,2) ), loc='lower left', prop=dict(size=8), frameon=True, bbox_to_anchor=(0., 1.25), bbox_transform=axs[1][a].transAxes) + # at2.patch.set_boxstyle("round,pad=0.,rounding_size=0.2") + # axs[1][a].add_artist(at2) + + + i += 1 + plt.pause(time_interval) + +if __name__ == '__main__': + SLEEPING_TIME = 5 + CURRENCIES = ['ETH-PLN', 'BTC-PLN', 'LTC-PLN'] + elements_a = 10 + elements_a_start = 10 + elements_a_stop = 15 + elements_r = 15 + + X = 5 + Y = 5 + S = 3 + + draw_plot(SLEEPING_TIME) \ No newline at end of file diff --git a/llist_6.py b/llist_6.py deleted file mode 100644 index cf532c5..0000000 --- a/llist_6.py +++ /dev/null @@ -1,61 +0,0 @@ -from tkinter import * -import json -from tkinter.filedialog import asksaveasfile -import tkinter.ttk as ttk -import tkinter.messagebox as msb - -window = Tk() -window.geometry('640x300') -window.title('Market') - -CURRENCIES = ['ETH-PLN', 'BTC-PLN', 'LTC-PLN'] -box_value = StringVar() -combobox = ttk.Combobox(window) -combobox['values'] = CURRENCIES -combobox.current(0) - -# box_value = StringVar() -# locationBox = ttk.Combobox(master, textvariable=box_value) -# locationBox.bind("<>", self.justamethod()) -# locationBox['values'] = CURRENCIES -# locationBox.current(0) - - - -action = IntVar() -buy_button = Radiobutton(window, text="Buy", variable=action, value=1) -sell_button = Radiobutton(window, text="Sell", variable=action, value=2) - -amount = Label(window, text="Amount:") -Amount = Entry(window) - -price = Label(window, text="Price:") -Price = Entry(window) -test = 1 - - - -def check(): - amount_value = float(Amount.get()) - price_value = float(Price.get()) - action_value = float(action.get()) - currency_= combobox.get() - print(amount_value) - print(price_value) - data = {'Currency': currency_, 'Amount': amount_value, 'Price': price_value, 'Action': action_value} - print(data) - with open('file.json', 'a') as file: - file.write(json.dumps(data) + '\n') - -submit_button = Button(window, text='Accept', command=check).grid(row=4, column=1) - -buy_button.grid(row=1, column=0) -sell_button.grid(row=1, column=1) -combobox.grid(row=0, column=0) -amount.grid(row=2, column=0) -Amount.grid(row=2, column=1) -price.grid(row=3,column=0) -Price.grid(row=3, column=1) - -window.mainloop() - diff --git a/visualise.py b/visualise.py new file mode 100644 index 0000000..a96997b --- /dev/null +++ b/visualise.py @@ -0,0 +1,65 @@ +from tkinter import * +import json +from tkinter.filedialog import asksaveasfile +import tkinter.ttk as ttk +import tkinter.messagebox as msb + +# def read_new_lines_from_json(current_data): +# new_data = [] +# with open('file.json', 'r') as file: +# all_data = [json.loads(line) for line in file] +# # print(all_data) +# for i in range (len(current_data), len(all_data)): +# new_data.append(all_data[i]) +# return new_data + +def check(): + amount_value = float(Amount.get()) + price_value = float(Price.get()) + action_value = float(action.get()) + action_ = 'buy' if action_value == 1.0 else 'sell' + currency_= combobox.get() + print(amount_value) + print(price_value) + data = {'Currency': currency_, 'Amount': amount_value, 'Price': price_value, 'Action': action_} + print(data) + with open('file.json', 'a') as file: + file.write(json.dumps(data) + '\n') + +if __name__ == '__main__': + + window = Tk() + window.geometry('340x200') + window.title('Market') + + CURRENCIES = ['ETH-PLN', 'BTC-PLN', 'LTC-PLN'] + box_value = StringVar() + currency = Label(window, text="Currency:") + combobox = ttk.Combobox(window) + combobox['values'] = CURRENCIES + + action = IntVar() + buy_button = Radiobutton(window, text="Buy", variable=action, value=1) + sell_button = Radiobutton(window, text="Sell", variable=action, value=2) + + amount = Label(window, text="Amount:") + Amount = Entry(window) + + price = Label(window, text="Price:") + Price = Entry(window) + test = 1 + + submit_button = Button(window, text='Accept', command=check).grid(row=4, column=1) + + buy_button.grid(row=0, column=0) + sell_button.grid(row=0, column=1) + currency.grid(row = 1,column = 0) + combobox.grid(row=1, column=1) + amount.grid(row=2, column=0) + Amount.grid(row=2, column=1) + price.grid(row=3,column=0) + Price.grid(row=3, column=1) + # a = [] + # read_new_lines_from_json(a) + window.mainloop() + From 88f4c202abdb5c8a2fff7b5e27e47678d32d5709 Mon Sep 17 00:00:00 2001 From: nielogicznie <254292@student.pwr.edu.pl> Date: Sun, 6 Jun 2021 22:15:06 +0200 Subject: [PATCH 4/8] works properly --- file.json | 47 +++++++------------- l6.py | 120 +++++++++++++++++++++++++++++---------------------- visualise.py | 1 - 3 files changed, 84 insertions(+), 84 deletions(-) diff --git a/file.json b/file.json index c06edad..d1fc75d 100644 --- a/file.json +++ b/file.json @@ -1,31 +1,16 @@ -{"Currency": "LTC-PLN", "Amount": 12.0, "Price": 643.0, "Action": "sell"} -{"Currency": "BTC-PLN", "Amount": 10.0, "Price": 136000.0, "Action": "sell"} -{"Currency": "ETH-PLN", "Amount": 15.0, "Price": 9700.0, "Action": "buy"} -{"Currency": "ETH-PLN", "Amount": 15.0, "Price": 9700.0, "Action": "buy"} -{"Currency": "ETH-PLN", "Amount": 12.0, "Price": 9800.0, "Action": "sell"} -{"Currency": "BTC-PLN", "Amount": 10.0, "Price": 13000.0, "Action": "buy"} -{"Currency": "LTC-PLN", "Amount": 15.0, "Price": 640.0, "Action": "sell"} -{"Currency": "BTC-PLN", "Amount": 10.0, "Price": 137000.0, "Action": "sell"} -{"Currency": "LTC-PLN", "Amount": 12.0, "Price": 630.0, "Action": "buy"} -{"Currency": "ETH-PLN", "Amount": 20.0, "Price": 9750.0, "Action": "sell"} -{"Currency": "BTC-PLN", "Amount": 12.0, "Price": 133000.0, "Action": "buy"} -{"Currency": "BTC-PLN", "Amount": 20.0, "Price": 134000.0, "Action": "sell"} -{"Currency": "BTC-PLN", "Amount": 23.0, "Price": 10000.0, "Action": "sell"} -{"Currency": "BTC-PLN", "Amount": 12.0, "Price": 133000.0, "Action": "buy"} -{"Currency": "BTC-PLN", "Amount": 20.0, "Price": 134000.0, "Action": "sell"} -{"Currency": "ETH-PLN", "Amount": 20.0, "Price": 9850.0, "Action": "sell"} -{"Currency": "BTC-PLN", "Amount": 12.0, "Price": 134000.0, "Action": "buy"} -{"Currency": "BTC-PLN", "Amount": 20.0, "Price": 134000.0, "Action": "sell"} -{"Currency": "BTC-PLN", "Amount": 23.0, "Price": 11000.0, "Action": "sell"} -{"Currency": "BTC-PLN", "Amount": 20.0, "Price": 134000.0, "Action": "sell"} -{"Currency": "BTC-PLN", "Amount": 23.0, "Price": 10000.0, "Action": "sell"} -{"Currency": "BTC-PLN", "Amount": 12.0, "Price": 133000.0, "Action": "buy"} -{"Currency": "BTC-PLN", "Amount": 20.0, "Price": 134000.0, "Action": "sell"} -{"Currency": "ETH-PLN", "Amount": 12.0, "Price": 9850.0, "Action": "sell"} -{"Currency": "BTC-PLN", "Amount": 2.0, "Price": 134000.0, "Action": "buy"} -{"Currency": "BTC-PLN", "Amount": 2.0, "Price": 134000.0, "Action": "sell"} -{"Currency": "LTC-PLN", "Amount": 12.0, "Price": 630.0, "Action": "buy"} -{"Currency": "LTC-PLN", "Amount": 22.0, "Price": 630.0, "Action": "buy"} -{"Currency": "BTC-PLN", "Amount": 10.0, "Price": 133000.0, "Action": "sell"} -{"Currency": "LTC-PLN", "Amount": 12.0, "Price": 650.0, "Action": "sell"} -{"Currency": "LTC-PLN", "Amount": 12.0, "Price": 650.0, "Action": "sell"} +{"Currency": "BTC-PLN", "Amount": 10.0, "Price": 4000.0, "Action": "buy"} +{"Currency": "LTC-PLN", "Amount": 50.0, "Price": 650.0, "Action": "buy"} +{"Currency": "ETH-PLN", "Amount": 50.0, "Price": 9950.0, "Action": "buy"} +{"Currency": "BTC-PLN", "Amount": 20.0, "Price": 6000.0, "Action": "buy"} +{"Currency": "BTC-PLN", "Amount": 20.0, "Price": 10000.0, "Action": "buy"} +{"Currency": "BTC-PLN", "Amount": 10.0, "Price": 50000.0, "Action": "sell"} +{"Currency": "ETH-PLN", "Amount": 10.0, "Price": 10000.0, "Action": "sell"} +{"Currency": "ETH-PLN", "Amount": 15.0, "Price": 10000.0, "Action": "buy"} +{"Currency": "ETH-PLN", "Amount": 12.0, "Price": 10000.0, "Action": "sell"} +{"Currency": "ETH-PLN", "Amount": 10.0, "Price": 10000.0, "Action": "sell"} +{"Currency": "ETH-PLN", "Amount": 15.0, "Price": 10000.0, "Action": "buy"} +{"Currency": "ETH-PLN", "Amount": 12.0, "Price": 10000.0, "Action": "sell"} +{"Currency": "LTC-PLN", "Amount": 50.0, "Price": 650.0, "Action": "buy"} +{"Currency": "LTC-PLN", "Amount": 25.0, "Price": 650.0, "Action": "sell"} +{"Currency": "ETH-PLN", "Amount": 15.0, "Price": 10000.0, "Action": "buy"} +{"Currency": "LTC-PLN", "Amount": 5.0, "Price": 650.0, "Action": "buy"} diff --git a/l6.py b/l6.py index d741146..7188066 100644 --- a/l6.py +++ b/l6.py @@ -44,7 +44,7 @@ def get_time(current_time): def list_average(list_, elements_a): clean = [] - for i in range(len(list_)-1, 0, -3): + for i in range(len(list_)-1, -1, -3): clean.append(list_[i]) data = clean[-elements_a:] if len(data) == 0: @@ -100,71 +100,80 @@ def set_Legend(axs): for i in range(2): axs[i][2].legend(bbox_to_anchor=(1.05, 1.0), loc='upper left') +def separate_tuples(list_of_tuples): + amount, price_x_amount = 0, 0 + for element in list_of_tuples: + amount += element[0] + price_x_amount += element[0]*element[1] + + return amount, price_x_amount -def user_average(dict_, curr): +def user_amount_and_profit(dict_, curr): buy, sell = [], [] + + if dict_[curr]['buy'] == [] and dict_[curr]['sell'] == []: + return 0,0 + for i in range(len(dict_[curr]['buy'])): buy.append(dict_[curr]['buy'][i]) for i in range(len(dict_[curr]['sell'])): sell.append(dict_[curr]['sell'][i]) - sum_buy = 0 - sold = 0 - sum_ = 0 - for i in buy: - sum_buy += i[0] - for i in range(len(dict_[curr]['sell'])): - if sell[i][0] <= sum_buy: - sum_buy -= sell[i][0] - sell[i] = (0, sell[i][1]) - - else: - sell[i] = (sell[i][0] - sum_buy, sell[i][1]) - - for i in sell: - # print('i[0]',i[0], 'i[1]', i[1]) - sold += i[0] * i[1] - sum_ += i[0] - - if sum_ == 0: - sum_ = 1 - print ('sold / sum_', sold / sum_) - return sold / sum_ + buy_amount = separate_tuples(buy)[0] + sell_amount = separate_tuples(sell)[0] + + if buy_amount > sell_amount: + for i in range(len(dict_[curr]['buy'])): + while sell_amount > 0: + if buy[i][0] <= sell_amount: + sell_amount -= buy[i][0] + buy[i] = (0, 0) + elif buy[i][0]>sell_amount: + buy[i] = (buy[i][0] - sell_amount, buy[i][1]) + sell_amount = 0 + + return separate_tuples(buy)[0], separate_tuples(buy)[1], 1 #amount + profit+info + + elif buy_amount < sell_amount: + for i in range(len(dict_[curr]['sell'])): + while buy_amount > 0: + if sell[i][0] <= buy_amount: + buy_amount -= sell[i][0] + sell[i] = (0, 0) + elif sell[i][0] > buy_amount: + sell[i] = (sell[i][0] - buy_amount, sell[i][1]) + buy_amount = 0 + return -1 * separate_tuples(sell)[0], -1 * separate_tuples(sell)[1], 0 #amount + loss +info + + return 0, 0 _ def avg_list_of_tuples(list): + if len(list) == 0: + return 0 sum_, amount = 0, 0 for i in list: sum_ += i[0] * i[1] amount += i[0] return sum_/amount -# def buy_user_avg(dict_, currency_type): -# sum_buy, amount_buy = 0, 0 - -# for element in dict_[currency_type]['buy']: -# sum_buy += element[0] * element[1] -# amount_buy += element[0] -# return sum_buy/amount_buy - -def draw_plot(time_interval): +def draw_plot(time_interval): fig, axs = draw_axes() buy, sell, buy_list, sell_list, avg_buy_list, avg_sell_list, rsi_buy_list, rsi_sell_list, user_average_list = [], [], [], [], [], [], [], [], [] i = 0 - # locator = MaxNLocator(nbins = 3) # current_time = [] user_data = [] datas = {} while True: + print('true') new_data = read_new_lines_from_json(user_data) for element in new_data: user_data.append(element) - # print('i=', i, 'c=', currency_type, 'crfincr', user_data) - + for a, currency_type in enumerate(CURRENCIES): + print(currency_type) if i == 0: datas[currency_type] = {'sell': [], 'buy':[]} - # print('c=', currency_type, 'i=', i, 'dict=', datas) for element in new_data: if element['Currency'] == currency_type and element['Action'] == 'sell' : datas[currency_type]['sell'].append((element['Amount'], element['Price'])) @@ -172,10 +181,12 @@ def draw_plot(time_interval): datas[currency_type]['buy'].append((element['Amount'], element['Price'])) - user_avg = user_average(datas, currency_type) - # user_average_list.append(user_avg) - - user_sell_avg = avg_list_of_tuples(datas[currency_type]['sell']) + amount, profit, info = user_amount_and_profit(datas, currency_type) + user_avg = profit / amount + # print(user_avg) + user_average_list.append(user_avg) + # user_sell_avg = avg_list_of_tuples(datas[currency_type]['buy']) + # print(user_sell_avg) data = get_data(currency_type) buy.append(data[0]) @@ -198,6 +209,7 @@ def draw_plot(time_interval): if len(buy) and len(sell) and len(rsi_buy_list) and len(rsi_sell_list) >= 4: # time = [time_list[-2].strftime("%H:%M:%S:"), time_list[-1].strftime("%H:%M:%S:")] + x = [(i - 1) * time_interval, i * time_interval] selling_cost = [sell[-4], sell[-1]] purchase_cost = [buy[-4], buy[-1]] @@ -206,35 +218,39 @@ def draw_plot(time_interval): avg_sell_value = [avg_sell_list[-4], avg_sell_list[-1]] RSI_buy_value = [rsi_buy_list[-4], rsi_buy_list[-1]] RSI_sell_value = [rsi_sell_list[-4], rsi_sell_list[-1]] - avg_user_sell_value = [user_sell_avg, user_sell_avg] + avg_user_sell_value = [user_average_list[-4], user_average_list[-1]] # print(currency_type, user_average_list[-1], sell[-1]) axs[0][a].plot(x, selling_cost, label="Selling cost", color='red') axs[0][a].plot(x, purchase_cost, label="Purchase cost", color='yellow') axs[0][a].plot(x, avg_buy_value, '--', label='Average Buy Cost', color='darkred') axs[0][a].plot(x, avg_sell_value, '--', label='Average Sell Cost', color='#9B870C') - axs[0][a].plot(x, avg_user_sell_value, '--', label = 'Average User Sell Cost', color = 'green') + axs[0][a].plot(x, avg_user_sell_value, label = 'Average User Sell Cost', color = 'limegreen') + # axs[0][a].axhline()[-1].remove() + # axs[0][a].axhline(user_avg,color ='limegreen') axs[1][a].plot(x, RSI_buy_value, label='RSI buy', color='lightgreen' ) axs[1][a].plot(x, RSI_sell_value, label='RSI sell', color='darkgreen') - - at2 = AnchoredText('User actual profit: ' + str(round(user_avg,2) ), loc='lower left', prop=dict(size=8), frameon=True, bbox_to_anchor=(0., 1.25), bbox_transform=axs[1][a].transAxes) + if info == 0: + text = 'loss' + amount =0 + else: + text = 'profit' + at2 = AnchoredText('User actual ' + text +' : ' + str(round(profit,2) ), loc='lower left', prop=dict(size=8), frameon=True, bbox_to_anchor=(0., 1.30), bbox_transform=axs[1][a].transAxes) at2.patch.set_boxstyle("round,pad=0.,rounding_size=0.2") axs[1][a].add_artist(at2) + at3 = AnchoredText('User actual amount: ' + str(amount) , loc='lower left', prop=dict(size=8), frameon=True, bbox_to_anchor=(0., 1.150), bbox_transform=axs[1][a].transAxes) + at3.patch.set_boxstyle("round,pad=0.,rounding_size=0.2") + axs[1][a].add_artist(at3) + if len(buy) and len(sell) and len(rsi_buy_list) and len(rsi_sell_list) == 6: set_Legend(axs) - # if len(buy) and len(sell) and len(rsi_buy_list) and len(rsi_sell_list) >= 6: - - # at2 = AnchoredText('User actual profit: ' + str(round(user_avg,2) ), loc='lower left', prop=dict(size=8), frameon=True, bbox_to_anchor=(0., 1.25), bbox_transform=axs[1][a].transAxes) - # at2.patch.set_boxstyle("round,pad=0.,rounding_size=0.2") - # axs[1][a].add_artist(at2) - i += 1 plt.pause(time_interval) @@ -251,4 +267,4 @@ def draw_plot(time_interval): Y = 5 S = 3 - draw_plot(SLEEPING_TIME) \ No newline at end of file + draw_plot(SLEEPING_TIME) diff --git a/visualise.py b/visualise.py index a96997b..234481a 100644 --- a/visualise.py +++ b/visualise.py @@ -62,4 +62,3 @@ def check(): # a = [] # read_new_lines_from_json(a) window.mainloop() - From 9b0e514caf837c5055115b573d4874789c5ae5b6 Mon Sep 17 00:00:00 2001 From: nielogicznie <254292@student.pwr.edu.pl> Date: Mon, 7 Jun 2021 18:12:59 +0200 Subject: [PATCH 5/8] l6 done --- file.json | 18 ++++-------------- l6.py | 23 +++++++++-------------- 2 files changed, 13 insertions(+), 28 deletions(-) diff --git a/file.json b/file.json index d1fc75d..65ef24a 100644 --- a/file.json +++ b/file.json @@ -1,16 +1,6 @@ {"Currency": "BTC-PLN", "Amount": 10.0, "Price": 4000.0, "Action": "buy"} -{"Currency": "LTC-PLN", "Amount": 50.0, "Price": 650.0, "Action": "buy"} -{"Currency": "ETH-PLN", "Amount": 50.0, "Price": 9950.0, "Action": "buy"} -{"Currency": "BTC-PLN", "Amount": 20.0, "Price": 6000.0, "Action": "buy"} -{"Currency": "BTC-PLN", "Amount": 20.0, "Price": 10000.0, "Action": "buy"} -{"Currency": "BTC-PLN", "Amount": 10.0, "Price": 50000.0, "Action": "sell"} -{"Currency": "ETH-PLN", "Amount": 10.0, "Price": 10000.0, "Action": "sell"} -{"Currency": "ETH-PLN", "Amount": 15.0, "Price": 10000.0, "Action": "buy"} -{"Currency": "ETH-PLN", "Amount": 12.0, "Price": 10000.0, "Action": "sell"} -{"Currency": "ETH-PLN", "Amount": 10.0, "Price": 10000.0, "Action": "sell"} -{"Currency": "ETH-PLN", "Amount": 15.0, "Price": 10000.0, "Action": "buy"} -{"Currency": "ETH-PLN", "Amount": 12.0, "Price": 10000.0, "Action": "sell"} -{"Currency": "LTC-PLN", "Amount": 50.0, "Price": 650.0, "Action": "buy"} -{"Currency": "LTC-PLN", "Amount": 25.0, "Price": 650.0, "Action": "sell"} -{"Currency": "ETH-PLN", "Amount": 15.0, "Price": 10000.0, "Action": "buy"} {"Currency": "LTC-PLN", "Amount": 5.0, "Price": 650.0, "Action": "buy"} +{"Currency": "ETH-PLN", "Amount": 15.0, "Price": 9950.0, "Action": "buy"} +{"Currency": "BTC-PLN", "Amount": 15.0, "Price": 4000.0, "Action": "sell"} +{"Currency": "LTC-PLN", "Amount": 5.0, "Price": 650.0, "Action": "sell"} +{"Currency": "ETH-PLN", "Amount": 10.0, "Price": 9950.0, "Action": "sell"} diff --git a/l6.py b/l6.py index 7188066..c7df9e5 100644 --- a/l6.py +++ b/l6.py @@ -112,7 +112,7 @@ def user_amount_and_profit(dict_, curr): buy, sell = [], [] if dict_[curr]['buy'] == [] and dict_[curr]['sell'] == []: - return 0,0 + return 0,0,0 for i in range(len(dict_[curr]['buy'])): buy.append(dict_[curr]['buy'][i]) @@ -145,7 +145,7 @@ def user_amount_and_profit(dict_, curr): buy_amount = 0 return -1 * separate_tuples(sell)[0], -1 * separate_tuples(sell)[1], 0 #amount + loss +info - return 0, 0 + return 0, 0, 0 _ def avg_list_of_tuples(list): if len(list) == 0: @@ -161,7 +161,6 @@ def draw_plot(time_interval): fig, axs = draw_axes() buy, sell, buy_list, sell_list, avg_buy_list, avg_sell_list, rsi_buy_list, rsi_sell_list, user_average_list = [], [], [], [], [], [], [], [], [] i = 0 - # current_time = [] user_data = [] datas = {} while True: @@ -182,12 +181,12 @@ def draw_plot(time_interval): amount, profit, info = user_amount_and_profit(datas, currency_type) - user_avg = profit / amount - # print(user_avg) + if amount != 0: + user_avg = profit / amount + else: + user_avg = 0 user_average_list.append(user_avg) - # user_sell_avg = avg_list_of_tuples(datas[currency_type]['buy']) - # print(user_sell_avg) - + data = get_data(currency_type) buy.append(data[0]) sell.append(data[1]) @@ -219,16 +218,12 @@ def draw_plot(time_interval): RSI_buy_value = [rsi_buy_list[-4], rsi_buy_list[-1]] RSI_sell_value = [rsi_sell_list[-4], rsi_sell_list[-1]] avg_user_sell_value = [user_average_list[-4], user_average_list[-1]] - # print(currency_type, user_average_list[-1], sell[-1]) - + axs[0][a].plot(x, selling_cost, label="Selling cost", color='red') axs[0][a].plot(x, purchase_cost, label="Purchase cost", color='yellow') axs[0][a].plot(x, avg_buy_value, '--', label='Average Buy Cost', color='darkred') axs[0][a].plot(x, avg_sell_value, '--', label='Average Sell Cost', color='#9B870C') axs[0][a].plot(x, avg_user_sell_value, label = 'Average User Sell Cost', color = 'limegreen') - # axs[0][a].axhline()[-1].remove() - # axs[0][a].axhline(user_avg,color ='limegreen') - axs[1][a].plot(x, RSI_buy_value, label='RSI buy', color='lightgreen' ) @@ -239,7 +234,7 @@ def draw_plot(time_interval): amount =0 else: text = 'profit' - at2 = AnchoredText('User actual ' + text +' : ' + str(round(profit,2) ), loc='lower left', prop=dict(size=8), frameon=True, bbox_to_anchor=(0., 1.30), bbox_transform=axs[1][a].transAxes) + at2 = AnchoredText('User actual ' + text +' : ' + str(round(abs(profit),2) ), loc='lower left', prop=dict(size=8), frameon=True, bbox_to_anchor=(0., 1.30), bbox_transform=axs[1][a].transAxes) at2.patch.set_boxstyle("round,pad=0.,rounding_size=0.2") axs[1][a].add_artist(at2) From af70849dcb9a80f76f96f3c36d0051f5935ce908 Mon Sep 17 00:00:00 2001 From: nielogicznie <254292@student.pwr.edu.pl> Date: Mon, 7 Jun 2021 23:21:14 +0200 Subject: [PATCH 6/8] label + time on x axis --- .idea/.name | 1 + file.json | 19 +++++++++--- l6.py | 89 +++++++++++++++++++++++++++-------------------------- 3 files changed, 61 insertions(+), 48 deletions(-) create mode 100644 .idea/.name diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..6b7bb3c --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +l6.py \ No newline at end of file diff --git a/file.json b/file.json index 65ef24a..8287adb 100644 --- a/file.json +++ b/file.json @@ -1,6 +1,15 @@ -{"Currency": "BTC-PLN", "Amount": 10.0, "Price": 4000.0, "Action": "buy"} -{"Currency": "LTC-PLN", "Amount": 5.0, "Price": 650.0, "Action": "buy"} +{"Currency": "BTC-PLN", "Amount": 10.0, "Price": 133000.0, "Action": "buy"} +{"Currency": "LTC-PLN", "Amount": 15.0, "Price": 645.0, "Action": "buy"} {"Currency": "ETH-PLN", "Amount": 15.0, "Price": 9950.0, "Action": "buy"} -{"Currency": "BTC-PLN", "Amount": 15.0, "Price": 4000.0, "Action": "sell"} -{"Currency": "LTC-PLN", "Amount": 5.0, "Price": 650.0, "Action": "sell"} -{"Currency": "ETH-PLN", "Amount": 10.0, "Price": 9950.0, "Action": "sell"} +{"Currency": "BTC-PLN", "Amount": 1.0, "Price": 132000.0, "Action": "sell"} +{"Currency": "LTC-PLN", "Amount": 15.0, "Price": 645.0, "Action": "buy"} +{"Currency": "ETH-PLN", "Amount": 7.0, "Price": 10000.0, "Action": "buy"} +{"Currency": "BTC-PLN", "Amount": 10.0, "Price": 133000.0, "Action": "buy"} +{"Currency": "LTC-PLN", "Amount": 10.0, "Price": 660.0, "Action": "buy"} +{"Currency": "ETH-PLN", "Amount": 3.0, "Price": 9950.0, "Action": "sell"} +{"Currency": "BTC-PLN", "Amount": 30.0, "Price": 120000.0, "Action": "buy"} +{"Currency": "LTC-PLN", "Amount": 20.0, "Price": 620.0, "Action": "buy"} +{"Currency": "ETH-PLN", "Amount": 20.0, "Price": 9700.0, "Action": "buy"} +{"Currency": "BTC-PLN", "Amount": 9.0, "Price": 120000.0, "Action": "sell"} +{"Currency": "ETH-PLN", "Amount": 9.0, "Price": 9200.0, "Action": "buy"} +{"Currency": "ETH-PLN", "Amount": 10.0, "Price": 9200.0, "Action": "sell"} diff --git a/l6.py b/l6.py index c7df9e5..7a8935d 100644 --- a/l6.py +++ b/l6.py @@ -1,26 +1,26 @@ import requests import matplotlib.pyplot as plt -from matplotlib.offsetbox import AnchoredText -from sys import exit from datetime import datetime, timedelta from matplotlib.ticker import MaxNLocator -import json -# from visualise import read_new_lines_from_json +from json import loads + def read_new_lines_from_json(current_data): new_data = [] with open('file.json', 'r') as file: - all_data = [json.loads(line) for line in file] - # print(all_data) + all_data = [loads(line) for line in file] for i in range (len(current_data), len(all_data)): new_data.append(all_data[i]) return new_data + def link(currency_type, type): if type == 'ticker': - return f'https://bitbay.net/API/Public/{currency_type}/ticker.json' - if type == 'transactions': - return f'https://api.bitbay.net/rest/trading/transactions/{currency_type}' + URL = f'https://bitbay.net/API/Public/{currency_type}/ticker.json' + elif type == 'transactions': + URL = f'https://api.bitbay.net/rest/trading/transactions/{currency_type}' + return URL + def get_data(currency_type): try: @@ -51,6 +51,7 @@ def list_average(list_, elements_a): return None return sum(data) / len(data) + def RSI_value(list_, elements_r): clean = [] for i in range(len(list_)-1, 0, -3): @@ -76,13 +77,16 @@ def RSI_value(list_, elements_r): else: b = down/down_c rsi = 100 - (100 / (1 + (a / b))) + return rsi + def draw_axes(): - fig, axs = plt.subplots(2, 3, figsize=(12, 5), constrained_layout=True) + fig, axs = plt.subplots(2, 3, figsize=(12, 8), constrained_layout=True) + locator = MaxNLocator(nbins = 4) for i, currency_type in enumerate(CURRENCIES): plt.suptitle('Cryptocurrencies trading',fontsize=24) - locator = MaxNLocator(nbins = 5) + axs[0][i].set_title(currency_type) axs[0][i].set_xlabel('Time') axs[0][i].set_ylabel('Value') @@ -93,6 +97,7 @@ def draw_axes(): axs[0][i].xaxis.set_major_locator(locator) axs[1][i].xaxis.set_major_locator(locator) + return fig, axs @@ -100,6 +105,7 @@ def set_Legend(axs): for i in range(2): axs[i][2].legend(bbox_to_anchor=(1.05, 1.0), loc='upper left') + def separate_tuples(list_of_tuples): amount, price_x_amount = 0, 0 for element in list_of_tuples: @@ -108,6 +114,7 @@ def separate_tuples(list_of_tuples): return amount, price_x_amount + def user_amount_and_profit(dict_, curr): buy, sell = [], [] @@ -119,6 +126,8 @@ def user_amount_and_profit(dict_, curr): for i in range(len(dict_[curr]['sell'])): sell.append(dict_[curr]['sell'][i]) + profit = separate_tuples(sell)[1] - separate_tuples(buy)[1] + buy_amount = separate_tuples(buy)[0] sell_amount = separate_tuples(sell)[0] @@ -132,7 +141,7 @@ def user_amount_and_profit(dict_, curr): buy[i] = (buy[i][0] - sell_amount, buy[i][1]) sell_amount = 0 - return separate_tuples(buy)[0], separate_tuples(buy)[1], 1 #amount + profit+info + return separate_tuples(buy)[0], profit, 1 #amount + profit+info elif buy_amount < sell_amount: for i in range(len(dict_[curr]['sell'])): @@ -143,10 +152,11 @@ def user_amount_and_profit(dict_, curr): elif sell[i][0] > buy_amount: sell[i] = (sell[i][0] - buy_amount, sell[i][1]) buy_amount = 0 - return -1 * separate_tuples(sell)[0], -1 * separate_tuples(sell)[1], 0 #amount + loss +info + return -1 * separate_tuples(sell)[0], profit, 0 #amount + loss +info return 0, 0, 0 - _ + + def avg_list_of_tuples(list): if len(list) == 0: return 0 @@ -157,14 +167,15 @@ def avg_list_of_tuples(list): return sum_/amount + def draw_plot(time_interval): fig, axs = draw_axes() buy, sell, buy_list, sell_list, avg_buy_list, avg_sell_list, rsi_buy_list, rsi_sell_list, user_average_list = [], [], [], [], [], [], [], [], [] i = 0 - user_data = [] - datas = {} + user_data, current_time = [], [] + data = {} #bo w ang nie ma datas! while True: - print('true') + current_time = get_time(current_time) new_data = read_new_lines_from_json(user_data) for element in new_data: user_data.append(element) @@ -172,24 +183,24 @@ def draw_plot(time_interval): for a, currency_type in enumerate(CURRENCIES): print(currency_type) if i == 0: - datas[currency_type] = {'sell': [], 'buy':[]} + data[currency_type] = {'sell': [], 'buy':[]} for element in new_data: if element['Currency'] == currency_type and element['Action'] == 'sell' : - datas[currency_type]['sell'].append((element['Amount'], element['Price'])) + data[currency_type]['sell'].append((element['Amount'], element['Price'])) elif element['Currency'] == currency_type and element['Action'] == 'buy': - datas[currency_type]['buy'].append((element['Amount'], element['Price'])) + data[currency_type]['buy'].append((element['Amount'], element['Price'])) - amount, profit, info = user_amount_and_profit(datas, currency_type) + amount, profit, info = user_amount_and_profit(data, currency_type) if amount != 0: user_avg = profit / amount else: user_avg = 0 user_average_list.append(user_avg) - data = get_data(currency_type) - buy.append(data[0]) - sell.append(data[1]) + dataset = get_data(currency_type) + buy.append(dataset[0]) + sell.append(dataset[1]) buy_, sell_ = get_data(currency_type) @@ -207,9 +218,8 @@ def draw_plot(time_interval): if len(buy) and len(sell) and len(rsi_buy_list) and len(rsi_sell_list) >= 4: - # time = [time_list[-2].strftime("%H:%M:%S:"), time_list[-1].strftime("%H:%M:%S:")] - - x = [(i - 1) * time_interval, i * time_interval] + x = [current_time[-2].strftime("%H:%M:%S"), current_time[-1].strftime("%H:%M:%S")] + selling_cost = [sell[-4], sell[-1]] purchase_cost = [buy[-4], buy[-1]] @@ -217,40 +227,33 @@ def draw_plot(time_interval): avg_sell_value = [avg_sell_list[-4], avg_sell_list[-1]] RSI_buy_value = [rsi_buy_list[-4], rsi_buy_list[-1]] RSI_sell_value = [rsi_sell_list[-4], rsi_sell_list[-1]] - avg_user_sell_value = [user_average_list[-4], user_average_list[-1]] + avg_user_sell_value = [abs(user_average_list[-4]), abs(user_average_list[-1])] axs[0][a].plot(x, selling_cost, label="Selling cost", color='red') axs[0][a].plot(x, purchase_cost, label="Purchase cost", color='yellow') axs[0][a].plot(x, avg_buy_value, '--', label='Average Buy Cost', color='darkred') axs[0][a].plot(x, avg_sell_value, '--', label='Average Sell Cost', color='#9B870C') - axs[0][a].plot(x, avg_user_sell_value, label = 'Average User Sell Cost', color = 'limegreen') + axs[0][a].plot(x, avg_user_sell_value, label = 'Average User Price', color = 'limegreen') axs[1][a].plot(x, RSI_buy_value, label='RSI buy', color='lightgreen' ) axs[1][a].plot(x, RSI_sell_value, label='RSI sell', color='darkgreen') - if info == 0: - text = 'loss' - amount =0 - else: - text = 'profit' - at2 = AnchoredText('User actual ' + text +' : ' + str(round(abs(profit),2) ), loc='lower left', prop=dict(size=8), frameon=True, bbox_to_anchor=(0., 1.30), bbox_transform=axs[1][a].transAxes) - at2.patch.set_boxstyle("round,pad=0.,rounding_size=0.2") - axs[1][a].add_artist(at2) - - at3 = AnchoredText('User actual amount: ' + str(amount) , loc='lower left', prop=dict(size=8), frameon=True, bbox_to_anchor=(0., 1.150), bbox_transform=axs[1][a].transAxes) - at3.patch.set_boxstyle("round,pad=0.,rounding_size=0.2") - axs[1][a].add_artist(at3) - - + if len(buy) and len(sell) and len(rsi_buy_list) and len(rsi_sell_list) == 6: set_Legend(axs) + axs[1][a].text(0.0,1.3, f'Actual profit / loss: {profit}', transform = axs[1][a].transAxes , fontsize = 7) + axs[1][a].text(0.0,1.2, f'Actual amount: {amount}' , transform = axs[1][a].transAxes, fontsize = 7 ) + i += 1 plt.pause(time_interval) + + if __name__ == '__main__': + SLEEPING_TIME = 5 CURRENCIES = ['ETH-PLN', 'BTC-PLN', 'LTC-PLN'] elements_a = 10 From 106aeaafebaada741b719e57e0797e40639cf461 Mon Sep 17 00:00:00 2001 From: nielogicznie <254292@student.pwr.edu.pl> Date: Tue, 8 Jun 2021 18:07:22 +0200 Subject: [PATCH 7/8] transition state --- .gitignore | 21 +++++++++++++++++++++ file.json | 40 +++++++++++++++++++++++++--------------- l6.py | 49 ++++++++++++++++++++++++++++++++++--------------- 3 files changed, 80 insertions(+), 30 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c4ca372 --- /dev/null +++ b/.gitignore @@ -0,0 +1,21 @@ +# Created by https://www.toptal.com/developers/gitignore/api/visualstudiocode +# Edit at https://www.toptal.com/developers/gitignore?templates=visualstudiocode + +### VisualStudioCode ### +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +*.code-workspace + +### VisualStudioCode Patch ### +# Ignore all local history of files +.history +.ionide +# .idea +# __pycache__ + + + +# End of https://www.toptal.com/developers/gitignore/api/visualstudiocode \ No newline at end of file diff --git a/file.json b/file.json index 8287adb..497032b 100644 --- a/file.json +++ b/file.json @@ -1,15 +1,25 @@ -{"Currency": "BTC-PLN", "Amount": 10.0, "Price": 133000.0, "Action": "buy"} -{"Currency": "LTC-PLN", "Amount": 15.0, "Price": 645.0, "Action": "buy"} -{"Currency": "ETH-PLN", "Amount": 15.0, "Price": 9950.0, "Action": "buy"} -{"Currency": "BTC-PLN", "Amount": 1.0, "Price": 132000.0, "Action": "sell"} -{"Currency": "LTC-PLN", "Amount": 15.0, "Price": 645.0, "Action": "buy"} -{"Currency": "ETH-PLN", "Amount": 7.0, "Price": 10000.0, "Action": "buy"} -{"Currency": "BTC-PLN", "Amount": 10.0, "Price": 133000.0, "Action": "buy"} -{"Currency": "LTC-PLN", "Amount": 10.0, "Price": 660.0, "Action": "buy"} -{"Currency": "ETH-PLN", "Amount": 3.0, "Price": 9950.0, "Action": "sell"} -{"Currency": "BTC-PLN", "Amount": 30.0, "Price": 120000.0, "Action": "buy"} -{"Currency": "LTC-PLN", "Amount": 20.0, "Price": 620.0, "Action": "buy"} -{"Currency": "ETH-PLN", "Amount": 20.0, "Price": 9700.0, "Action": "buy"} -{"Currency": "BTC-PLN", "Amount": 9.0, "Price": 120000.0, "Action": "sell"} -{"Currency": "ETH-PLN", "Amount": 9.0, "Price": 9200.0, "Action": "buy"} -{"Currency": "ETH-PLN", "Amount": 10.0, "Price": 9200.0, "Action": "sell"} +{"Currency": "BTC-PLN", "Amount": 10.0, "Price": 122000.0, "Action": "buy"} +{"Currency": "LTC-PLN", "Amount": 15.0, "Price": 590.0, "Action": "buy"} +{"Currency": "ETH-PLN", "Amount": 15.0, "Price": 9200.0, "Action": "buy"} +{"Currency": "BTC-PLN", "Amount": 8.0, "Price": 122000.0, "Action": "sell"} +{"Currency": "ETH-PLN", "Amount": 3.0, "Price": 2000.0, "Action": "buy"} +{"Currency": "ETH-PLN", "Amount": 2.0, "Price": 3000.0, "Action": "buy"} +{"Currency": "ETH-PLN", "Amount": 5.0, "Price": 10000.0, "Action": "sell"} +{"Currency": "BTC-PLN", "Amount": 2.0, "Price": 122000.0, "Action": "sell"} +{"Currency": "LTC-PLN", "Amount": 10.0, "Price": 550.0, "Action": "sell"} +{"Currency": "LTC-PLN", "Amount": 2.0, "Price": 550.0, "Action": "sell"} +{"Currency": "LTC-PLN", "Amount": 5.0, "Price": 520.0, "Action": "buy"} +{"Currency": "ETH-PLN", "Amount": 10.0, "Price": 9000.0, "Action": "sell"} +{"Currency": "ETH-PLN", "Amount": 3.0, "Price": 9100.0, "Action": "buy"} +{"Currency": "ETH-PLN", "Amount": 3.0, "Price": 10000.0, "Action": "sell"} +{"Currency": "ETH-PLN", "Amount": 5.0, "Price": 10000.0, "Action": "sell"} +{"Currency": "LTC-PLN", "Amount": 10.0, "Price": 550.0, "Action": "sell"} +{"Currency": "LTC-PLN", "Amount": 2.0, "Price": 550.0, "Action": "sell"} +{"Currency": "LTC-PLN", "Amount": 5.0, "Price": 520.0, "Action": "buy"} +{"Currency": "ETH-PLN", "Amount": 10.0, "Price": 9000.0, "Action": "sell"} +{"Currency": "ETH-PLN", "Amount": 313.0, "Price": 9100.0, "Action": "buy"} +{"Currency": "ETH-PLN", "Amount": 3.0, "Price": 10000.0, "Action": "sell"} +{"Currency": "LTC-PLN", "Amount": 12.0, "Price": 540.0, "Action": "buy"} +{"Currency": "ETH-PLN", "Amount": 200.0, "Price": 8800.0, "Action": "sell"} +{"Currency": "ETH-PLN", "Amount": 15.0, "Price": 8500.0, "Action": "sell"} +{"Currency": "ETH-PLN", "Amount": 15.0, "Price": 8200.0, "Action": "buy"} diff --git a/l6.py b/l6.py index 7a8935d..abcd956 100644 --- a/l6.py +++ b/l6.py @@ -3,6 +3,7 @@ from datetime import datetime, timedelta from matplotlib.ticker import MaxNLocator from json import loads +from sys import exit def read_new_lines_from_json(current_data): @@ -116,6 +117,7 @@ def separate_tuples(list_of_tuples): def user_amount_and_profit(dict_, curr): + print('user_amount_and_profit') buy, sell = [], [] if dict_[curr]['buy'] == [] and dict_[curr]['sell'] == []: @@ -126,24 +128,37 @@ def user_amount_and_profit(dict_, curr): for i in range(len(dict_[curr]['sell'])): sell.append(dict_[curr]['sell'][i]) - profit = separate_tuples(sell)[1] - separate_tuples(buy)[1] + profit = separate_tuples(sell)[1] - separate_tuples(buy)[1] buy_amount = separate_tuples(buy)[0] sell_amount = separate_tuples(sell)[0] if buy_amount > sell_amount: - for i in range(len(dict_[curr]['buy'])): - while sell_amount > 0: + # print('sinfull') + # print("buy amount: ",buy_amount, "buy: ", buy,"\nsell amount: ",sell_amount, "sell: ", sell) + while sell_amount > 0: + for i in range(len(dict_[curr]['buy'])): + x = 0 + + # print("x: ", x) + # print("i: ", i, "\nbuy[i][0]",buy[i][0]) if buy[i][0] <= sell_amount: + # print("a) tu odejmujemy: ",sell_amount, "-",buy[i][0]) sell_amount -= buy[i][0] buy[i] = (0, 0) - elif buy[i][0]>sell_amount: + # print("tu odjeslismy: ",sell_amount, "-",buy[i][0]) + if buy[i][0] > sell_amount: + # print("b) tu odejmujemy: ",buy[i][0], "-", sell_amount) buy[i] = (buy[i][0] - sell_amount, buy[i][1]) sell_amount = 0 + x+=1 - return separate_tuples(buy)[0], profit, 1 #amount + profit+info + return separate_tuples(buy)[0], separate_tuples(buy)[1], profit #amount + profit+info - elif buy_amount < sell_amount: + elif buy_amount < sell_amount: + print ("Nice try, you can't sell the currency, you don't have, \nEnd of your purchase") + exit() + print('sinless') for i in range(len(dict_[curr]['sell'])): while buy_amount > 0: if sell[i][0] <= buy_amount: @@ -152,7 +167,7 @@ def user_amount_and_profit(dict_, curr): elif sell[i][0] > buy_amount: sell[i] = (sell[i][0] - buy_amount, sell[i][1]) buy_amount = 0 - return -1 * separate_tuples(sell)[0], profit, 0 #amount + loss +info + return -1 * separate_tuples(sell)[0], separate_tuples(sell)[1], profit #amount + loss +info return 0, 0, 0 @@ -171,6 +186,7 @@ def avg_list_of_tuples(list): def draw_plot(time_interval): fig, axs = draw_axes() buy, sell, buy_list, sell_list, avg_buy_list, avg_sell_list, rsi_buy_list, rsi_sell_list, user_average_list = [], [], [], [], [], [], [], [], [] + cout1, cout2,cout3 = {}, {}, {} i = 0 user_data, current_time = [], [] data = {} #bo w ang nie ma datas! @@ -184,6 +200,9 @@ def draw_plot(time_interval): print(currency_type) if i == 0: data[currency_type] = {'sell': [], 'buy':[]} + cout1[currency_type] = axs[1][a].text(0.0, 1.3, '', transform=axs[1][a].transAxes, fontsize=7) + cout2[currency_type] = axs[1][a].text(0.0, 1.2, '', transform=axs[1][a].transAxes, fontsize=7) + cout3[currency_type] = axs[1][a].text(0.0, 1.1, '', transform=axs[1][a].transAxes, fontsize=7) for element in new_data: if element['Currency'] == currency_type and element['Action'] == 'sell' : data[currency_type]['sell'].append((element['Amount'], element['Price'])) @@ -191,13 +210,13 @@ def draw_plot(time_interval): data[currency_type]['buy'].append((element['Amount'], element['Price'])) - amount, profit, info = user_amount_and_profit(data, currency_type) + amount, div, profit = user_amount_and_profit(data, currency_type) if amount != 0: - user_avg = profit / amount + user_avg = div / amount else: user_avg = 0 user_average_list.append(user_avg) - + # print("user_avg: ", user_avg) dataset = get_data(currency_type) buy.append(dataset[0]) sell.append(dataset[1]) @@ -216,7 +235,7 @@ def draw_plot(time_interval): rsi_sell_list.append(RSI_value(sell_list, elements_r)) - if len(buy) and len(sell) and len(rsi_buy_list) and len(rsi_sell_list) >= 4: + if len(rsi_sell_list) >= 4: x = [current_time[-2].strftime("%H:%M:%S"), current_time[-1].strftime("%H:%M:%S")] @@ -240,12 +259,12 @@ def draw_plot(time_interval): axs[1][a].plot(x, RSI_sell_value, label='RSI sell', color='darkgreen') - if len(buy) and len(sell) and len(rsi_buy_list) and len(rsi_sell_list) == 6: + if len(rsi_sell_list) == 6: set_Legend(axs) - axs[1][a].text(0.0,1.3, f'Actual profit / loss: {profit}', transform = axs[1][a].transAxes , fontsize = 7) - axs[1][a].text(0.0,1.2, f'Actual amount: {amount}' , transform = axs[1][a].transAxes, fontsize = 7 ) - + cout1[currency_type].set_text(f'Actual profit : {profit}') + cout2[currency_type].set_text(f'Actual amount : {amount}') + cout3[currency_type].set_text(f'Actual fifo_avg : {user_avg}') i += 1 plt.pause(time_interval) From 47ee1a5ac5e9230b15389daaf9eada65ca8de958 Mon Sep 17 00:00:00 2001 From: nielogicznie <254292@student.pwr.edu.pl> Date: Tue, 8 Jun 2021 18:24:09 +0200 Subject: [PATCH 8/8] final effect --- file.json | 31 ++++++------------------------- l6.py | 24 +----------------------- 2 files changed, 7 insertions(+), 48 deletions(-) diff --git a/file.json b/file.json index 497032b..e178f16 100644 --- a/file.json +++ b/file.json @@ -1,25 +1,6 @@ -{"Currency": "BTC-PLN", "Amount": 10.0, "Price": 122000.0, "Action": "buy"} -{"Currency": "LTC-PLN", "Amount": 15.0, "Price": 590.0, "Action": "buy"} -{"Currency": "ETH-PLN", "Amount": 15.0, "Price": 9200.0, "Action": "buy"} -{"Currency": "BTC-PLN", "Amount": 8.0, "Price": 122000.0, "Action": "sell"} -{"Currency": "ETH-PLN", "Amount": 3.0, "Price": 2000.0, "Action": "buy"} -{"Currency": "ETH-PLN", "Amount": 2.0, "Price": 3000.0, "Action": "buy"} -{"Currency": "ETH-PLN", "Amount": 5.0, "Price": 10000.0, "Action": "sell"} -{"Currency": "BTC-PLN", "Amount": 2.0, "Price": 122000.0, "Action": "sell"} -{"Currency": "LTC-PLN", "Amount": 10.0, "Price": 550.0, "Action": "sell"} -{"Currency": "LTC-PLN", "Amount": 2.0, "Price": 550.0, "Action": "sell"} -{"Currency": "LTC-PLN", "Amount": 5.0, "Price": 520.0, "Action": "buy"} -{"Currency": "ETH-PLN", "Amount": 10.0, "Price": 9000.0, "Action": "sell"} -{"Currency": "ETH-PLN", "Amount": 3.0, "Price": 9100.0, "Action": "buy"} -{"Currency": "ETH-PLN", "Amount": 3.0, "Price": 10000.0, "Action": "sell"} -{"Currency": "ETH-PLN", "Amount": 5.0, "Price": 10000.0, "Action": "sell"} -{"Currency": "LTC-PLN", "Amount": 10.0, "Price": 550.0, "Action": "sell"} -{"Currency": "LTC-PLN", "Amount": 2.0, "Price": 550.0, "Action": "sell"} -{"Currency": "LTC-PLN", "Amount": 5.0, "Price": 520.0, "Action": "buy"} -{"Currency": "ETH-PLN", "Amount": 10.0, "Price": 9000.0, "Action": "sell"} -{"Currency": "ETH-PLN", "Amount": 313.0, "Price": 9100.0, "Action": "buy"} -{"Currency": "ETH-PLN", "Amount": 3.0, "Price": 10000.0, "Action": "sell"} -{"Currency": "LTC-PLN", "Amount": 12.0, "Price": 540.0, "Action": "buy"} -{"Currency": "ETH-PLN", "Amount": 200.0, "Price": 8800.0, "Action": "sell"} -{"Currency": "ETH-PLN", "Amount": 15.0, "Price": 8500.0, "Action": "sell"} -{"Currency": "ETH-PLN", "Amount": 15.0, "Price": 8200.0, "Action": "buy"} +{"Currency": "BTC-PLN", "Amount": 10.0, "Price": 119000.0, "Action": "buy"} +{"Currency": "LTC-PLN", "Amount": 15.0, "Price": 575.0, "Action": "buy"} +{"Currency": "ETH-PLN", "Amount": 15.0, "Price": 8950.0, "Action": "buy"} +{"Currency": "BTC-PLN", "Amount": 8.0, "Price": 120000.0, "Action": "sell"} +{"Currency": "ETH-PLN", "Amount": 3.0, "Price": 9500.0, "Action": "buy"} +{"Currency": "ETH-PLN", "Amount": 12.0, "Price": 7000.0, "Action": "buy"} diff --git a/l6.py b/l6.py index abcd956..ac18b44 100644 --- a/l6.py +++ b/l6.py @@ -134,40 +134,21 @@ def user_amount_and_profit(dict_, curr): sell_amount = separate_tuples(sell)[0] if buy_amount > sell_amount: - # print('sinfull') - # print("buy amount: ",buy_amount, "buy: ", buy,"\nsell amount: ",sell_amount, "sell: ", sell) while sell_amount > 0: for i in range(len(dict_[curr]['buy'])): - x = 0 - # print("x: ", x) - # print("i: ", i, "\nbuy[i][0]",buy[i][0]) if buy[i][0] <= sell_amount: - # print("a) tu odejmujemy: ",sell_amount, "-",buy[i][0]) sell_amount -= buy[i][0] buy[i] = (0, 0) - # print("tu odjeslismy: ",sell_amount, "-",buy[i][0]) if buy[i][0] > sell_amount: - # print("b) tu odejmujemy: ",buy[i][0], "-", sell_amount) buy[i] = (buy[i][0] - sell_amount, buy[i][1]) sell_amount = 0 - x+=1 return separate_tuples(buy)[0], separate_tuples(buy)[1], profit #amount + profit+info elif buy_amount < sell_amount: print ("Nice try, you can't sell the currency, you don't have, \nEnd of your purchase") exit() - print('sinless') - for i in range(len(dict_[curr]['sell'])): - while buy_amount > 0: - if sell[i][0] <= buy_amount: - buy_amount -= sell[i][0] - sell[i] = (0, 0) - elif sell[i][0] > buy_amount: - sell[i] = (sell[i][0] - buy_amount, sell[i][1]) - buy_amount = 0 - return -1 * separate_tuples(sell)[0], separate_tuples(sell)[1], profit #amount + loss +info return 0, 0, 0 @@ -186,7 +167,7 @@ def avg_list_of_tuples(list): def draw_plot(time_interval): fig, axs = draw_axes() buy, sell, buy_list, sell_list, avg_buy_list, avg_sell_list, rsi_buy_list, rsi_sell_list, user_average_list = [], [], [], [], [], [], [], [], [] - cout1, cout2,cout3 = {}, {}, {} + cout1, cout2 = {}, {} i = 0 user_data, current_time = [], [] data = {} #bo w ang nie ma datas! @@ -202,7 +183,6 @@ def draw_plot(time_interval): data[currency_type] = {'sell': [], 'buy':[]} cout1[currency_type] = axs[1][a].text(0.0, 1.3, '', transform=axs[1][a].transAxes, fontsize=7) cout2[currency_type] = axs[1][a].text(0.0, 1.2, '', transform=axs[1][a].transAxes, fontsize=7) - cout3[currency_type] = axs[1][a].text(0.0, 1.1, '', transform=axs[1][a].transAxes, fontsize=7) for element in new_data: if element['Currency'] == currency_type and element['Action'] == 'sell' : data[currency_type]['sell'].append((element['Amount'], element['Price'])) @@ -216,7 +196,6 @@ def draw_plot(time_interval): else: user_avg = 0 user_average_list.append(user_avg) - # print("user_avg: ", user_avg) dataset = get_data(currency_type) buy.append(dataset[0]) sell.append(dataset[1]) @@ -264,7 +243,6 @@ def draw_plot(time_interval): cout1[currency_type].set_text(f'Actual profit : {profit}') cout2[currency_type].set_text(f'Actual amount : {amount}') - cout3[currency_type].set_text(f'Actual fifo_avg : {user_avg}') i += 1 plt.pause(time_interval)