-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.py
222 lines (163 loc) · 6.93 KB
/
main2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import threading
import time
import pandas as pd
from flask import Flask, redirect, url_for
import binance_wrapper
import layout
import os
import layout.page
import binance_simulator
import utils.stringutils as strutils
binance = binance_wrapper.Client()
app = Flask(__name__)
is_thread_started = False
trading_pause = False
# client = binance_wrapper.Client()
client = binance_simulator.ClientMock()
symbols = ["BTCUSDT", "ETHUSDT", "BNBUSDT", "XRPUSDT", "ADAUSDT", "SOLUSDT", "AVAXUSDT", "LUNAUSDT", "DOTUSDT",
"DOGEUSDT", "SHIBUSDT", "MATICUSDT", "LTCUSDT", "ATOMUSDT", "LINKUSDT", "TRXUSDT", "NEARUSDT", "BCHUSDT",
"ALGOUSDT", "FTTUSDT", "XLMUSDT", "FTMUSDT", "UNIUSDT", "HBARUSDT", "MANAUSDT", "ICPUSDT", "ETCUSDT",
"AXSUSDT", "SANDUSDT", "EGLDUSDT", "KLAYUSDT", "VETUSDT", "FILUSDT", "THETAUSDT", "XTZUSDT", "XMRUSDT",
"GRTUSDT", "HNTUSDT", "EOSUSDT", "CAKEUSDT", "GALAUSDT", "FLOWUSDT", "TFUELUSDT", "AAVEUSDT", "ONEUSDT",
"NEOUSDT", "MKRUSDT", "QNTUSDT", "ENJUSDT", "XECUSDT"]
posframe = pd.DataFrame(symbols)
posframe.columns = ["Currency"]
posframe["position"] = 0
posframe["quantity"] = 0
posframe.to_csv("positioncheck", index=False)
pd.read_csv("positioncheck")
LIMIT = 100
def get_hourly_data(symbol):
return binance.get_historical_data(symbol, "1h", limit=LIMIT)
# result = get_hourly_data("BTCUSDT")
#
#
# # rsa = RelativeStrengthIndex(result["prices"])
# # rsa.calculate()
#
#
# for i in range(0, LIMIT):
# print(str(result['prices'][i]) + " " + str(result['dates'][i]) + " " + str(result["RSI"][i]))
def changepos(curr, order, buy=True):
if buy:
posframe.loc[posframe.Currency == curr, 'position'] = 1
posframe.loc[posframe.Currency == curr, 'quantity'] = float(order['executedQty'])
else:
posframe.loc[posframe.Currency == curr, 'position'] = 0
posframe.loc[posframe.Currency == curr, 'quantity'] = 0
def trader(investment=100):
print("SIMULATED BALANCES: " + str(client.get_balances_simulated_from_db()))
for coin in posframe[posframe.position == 1].Currency:
print(f"Check availability {coin} for SELL trade")
hourly_data = get_hourly_data(coin)
last_rsi = hourly_data["RSI"][-1]
last_closing_price = hourly_data["prices"][-1]
if last_rsi > 70:
closing_price = last_closing_price
selling_qty = posframe[posframe.Currency == coin].quantity.values[0]
order = client.place_sell_order(symbol=coin,
qty=selling_qty,
price=closing_price)
changepos(coin, order, buy=False)
print(f"SOLD {order['executedQty']} of coin [{coin}]")
for coin in posframe[posframe.position == 0].Currency:
hourly_data = get_hourly_data(coin)
last_rsi = hourly_data["RSI"][-1]
last_closing_price = hourly_data["prices"][-1]
if last_rsi < 30:
buy_price = last_closing_price
order = client.place_buy_order(symbol=coin,
usdt_qty=investment,
price=buy_price)
if order['executedQty'] != 0:
changepos(coin, order, buy=True)
print(f"BOUGHT {order['executedQty']} of coin [{coin}]")
else:
print(f"Buying condition is not fulfilled for coin: {coin}")
def sell_everything():
for coin in posframe[posframe.position == 1].Currency:
df = get_hourly_data(coin)
lastrow = df.iloc[-1]
closing_price = lastrow.Close
selling_qty = posframe[posframe.Currency == coin].quantity.values[0]
order = client.place_sell_order(symbol=coin,
qty=selling_qty,
price=closing_price)
return client.get_balances_simulated()
def emulate_sell_everything():
emulated_usdt_balance = 0.0
balances = client.get_balances_simulated_from_db()
for balance in balances:
if balance.currency == "USDT":
emulated_usdt_balance += balance.amount
else:
symbol = strutils.get_binance_symbol_out_of_coin(balance.currency)
currency_data = get_hourly_data(symbol)
closing_price = currency_data["prices"][-1]
order = client.emulate_place_sell_order(symbol=symbol,
qty=balance.amount,
price=closing_price)
order_price_in_usdt = order['executedQty']
emulated_usdt_balance += order_price_in_usdt
return emulated_usdt_balance
def do_trade():
trading_counter = 0
global trading_pause
while True:
if not trading_pause:
print(f"Trading round: [{trading_counter}]")
trader(100)
print("Waiting for new trading round. Press key to finish")
try:
time.sleep(60)
except KeyboardInterrupt:
print("Loop interrupred, selling everything")
final_balance = sell_everything()
print("-----------------SOLD EVERYTHING----------------")
print("----------------SEE FINAL BAlANCE----------------")
print(final_balance)
exit(0)
else:
# small timeout between checks
time.sleep(1)
trading_counter += 1
def is_started_trading():
return is_thread_started
def start_trading():
global is_thread_started
is_thread_started = True
thread = threading.Thread(target=do_trade)
thread.daemon = True
thread.start()
@app.route("/")
def root():
if not is_started_trading():
start_trading()
balances = client.get_balances_simulated_from_db()
calculated_current_usdt_balance = emulate_sell_everything()
order_history = client.get_order_history()
order_history_str = ""
for order in order_history:
order_history_str += "<p>" + repr(order) + " ------ " + \
f'<a target="blank" href="https://www.binance.com/uk-UA/trade/{order.coin}_USDT" >' \
+ order.coin + '</a>' + "<p/>\n"
return layout.page.body_top() + f"<h1> RSI trading bot - UP</h1>" \
f"<h4>DB balances {balances}</h4> " \
f"<h4>Simulated USDT balance is {calculated_current_usdt_balance}</h4>" \
f"<p>{order_history_str}<p>" + layout.page.body_bottom()
#
#
@app.route("/flush")
def flush():
global trading_pause
trading_pause = True
try:
client.flush()
except Exception as e:
return f"<h1> Error during flushing data: {e}</h1>"
finally:
trading_pause = False
return redirect(url_for("root"))
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5201))
app.run(host='0.0.0.0', port=port)