-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot_production.py
176 lines (132 loc) · 5.27 KB
/
bot_production.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
import datetime
import algorithms.algorithm
import config
POLLING_TIME = 60
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 db.client as db
from db.client import Order
import utils.stringutils as strutils
binance = binance_wrapper.Client()
orderDAO = db.OrderDAO()
app = Flask(__name__)
is_thread_started = False
trading_pause = False
client = binance_wrapper.Client()
# client = binance_simulator.ClientMock()
LIMIT = 100
USDT_BUY_THESHOLD = 5
def get_hourly_data(symbol):
return binance.get_historical_data(symbol, "1h", limit=LIMIT)
def check_and_buy(usdt_balance, coin, historical_data, index, algorithm, orders):
is_able_to_buy = usdt_balance > USDT_BUY_THESHOLD
if is_able_to_buy and len(orders) > 0 and not orders[-1].is_buy:
algo_return = algorithm(historical_data, index, check_condition="BUY", orders=orders)
should_buy = algo_return.should_buy()
if should_buy:
spent_cost = usdt_balance
bought_coin = spent_cost / historical_data["prices"][index]
deal_exact_time = datetime.datetime.now()
order = client.place_buy_order(coin, bought_coin)
client.wait_for_order_status(order["orderId"], coin, ["FULFILLED", "FILLED"])
print(
f"[{deal_exact_time}] Bought coin {coin} at price {historical_data['prices'][index]} with RSI {historical_data['RSI'][index]}")
order = Order(time=deal_exact_time, is_buy=True, coin=coin, price=historical_data['prices'][index],
usdt_price=spent_cost, reason=algo_return.get_reason())
return order
def check_and_sell(coin, historical_data, index, algorithm, orders):
algo_return = algorithm(historical_data, index, check_condition="SELL", orders=orders)
should_sell = algo_return.should_sell()
coin_balance = float(client.balance(coin.replace("USDT", ""))['free'])
if should_sell and coin_balance > 0.01:
deal_exact_time = datetime.datetime.now()
order = client.place_sell_order(coin, coin_balance)
client.wait_for_order_status(order["orderId"], coin, ["FULFILLED", "FILLED"])
gained_usdt = float(client.balance("USDT")['free'])
print(
f"[{deal_exact_time}] Sold coin {coin} at price {historical_data['prices'][index]} with RSI {historical_data['RSI'][index]}")
order = Order(time=deal_exact_time, is_buy=False, coin=coin, price=historical_data['prices'][index],
usdt_price=gained_usdt, reason=algo_return.get_reason())
return order
TRADING_SYMBOL = "MATICUSDT"
RECENT_INDEX = -1
TRADING_STEP_MINUTES = 20
def do_trading_round():
usdt_balance = float(client.balance("USDT")["free"])
historical_data = client.get_historical_data_on_interval(TRADING_SYMBOL, "1h", "3m")
orders = orderDAO.get_orders()
buy_order = check_and_buy(usdt_balance,
TRADING_SYMBOL,
historical_data,
RECENT_INDEX,
algorithms.algorithm.rsi_vwap_stop_loss,
orders)
if buy_order is not None:
orderDAO.put_order(buy_order)
sell_order = check_and_sell(
TRADING_SYMBOL,
historical_data,
RECENT_INDEX,
algorithms.algorithm.rsi_vwap_stop_loss,
orders)
if sell_order is not None:
orderDAO.put_order(sell_order)
print(f"Trading round is completed.")
def do_trade():
trading_counter = 0
global trading_pause
while True:
if not trading_pause:
print(f"Trading round: [{trading_counter}]")
do_trading_round()
print("Waiting for new trading round. Press key to finish")
try:
time.sleep(60 * TRADING_STEP_MINUTES)
except KeyboardInterrupt:
print("Loop interrupred")
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()
usdt_balance = client.balance("USDT")
coin_balance = client.balance(strutils.get_coin_out_of_symbol(TRADING_SYMBOL))
order_history = orderDAO.get_orders()
str = ""
for order in order_history:
str += f"<p>{order}</p>"
return layout.page.body_top() + f"<h1> RSI trading bot - UP</h1>" \
f"<h4>Balances: USDT: {usdt_balance}, coin: {coin_balance}</h4> " \
f"{str}" + layout.page.body_bottom()
@app.route("/drop")
def flush():
global trading_pause
trading_pause = True
try:
db.drop_all_tables()
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)