-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathgridbot_websocket_client.py
87 lines (66 loc) · 3.25 KB
/
gridbot_websocket_client.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
import ccxt, config, time, sys
import websocket, json
ws = websocket.WebSocket()
ws.connect("ws://localhost:9001")
exchange = ccxt.ftxus({
'apiKey': config.API_KEY,
'secret': config.SECRET_KEY
})
ticker = exchange.fetch_ticker(config.SYMBOL)
buy_orders = []
sell_orders = []
# initial_buy_order = exchange.create_market_buy_order(config.SYMBOL, config.POSITION_SIZE * config.NUM_SELL_GRID_LINES)
for i in range(config.NUM_BUY_GRID_LINES):
price = ticker['bid'] - (config.GRID_SIZE * (i+1))
print("submitting market limit buy order at {}".format(price))
order = exchange.create_limit_buy_order(config.SYMBOL, config.POSITION_SIZE, price)
buy_orders.append(order['info'])
for i in range(config.NUM_SELL_GRID_LINES):
price = ticker['bid'] + (config.GRID_SIZE * (i+1))
print("submitting market limit sell order at {}".format(price))
order = exchange.create_limit_sell_order(config.SYMBOL, config.POSITION_SIZE, price)
sell_orders.append(order['info'])
closed_orders = []
while True:
# concatenate 3 order lists and send as jsonified string
ws.send(json.dumps(buy_orders + sell_orders + closed_orders))
closed_order_ids = []
for buy_order in buy_orders:
print("checking buy order {}".format(buy_order['id']))
try:
order = exchange.fetch_order(buy_order['id'])
except Exception as e:
print("request failed, retrying")
continue
order_info = order['info']
if order_info['status'] == config.CLOSED_ORDER_STATUS:
closed_orders.append(order_info)
closed_order_ids.append(order_info['id'])
print("buy order executed at {}".format(order_info['price']))
new_sell_price = float(order_info['price']) + config.GRID_SIZE
print("creating new limit sell order at {}".format(new_sell_price))
new_sell_order = exchange.create_limit_sell_order(config.SYMBOL, config.POSITION_SIZE, new_sell_price)
sell_orders.append(new_sell_order)
time.sleep(config.CHECK_ORDERS_FREQUENCY)
for sell_order in sell_orders:
print("checking sell order {}".format(sell_order['id']))
try:
order = exchange.fetch_order(sell_order['id'])
except Exception as e:
print("request failed, retrying")
continue
order_info = order['info']
if order_info['status'] == config.CLOSED_ORDER_STATUS:
closed_orders.append(order_info)
closed_order_ids.append(order_info['id'])
print("sell order executed at {}".format(order_info['price']))
new_buy_price = float(order_info['price']) - config.GRID_SIZE
print("creating new limit buy order at {}".format(new_buy_price))
new_buy_order = exchange.create_limit_buy_order(config.SYMBOL, config.POSITION_SIZE, new_buy_price)
buy_orders.append(new_buy_order)
time.sleep(config.CHECK_ORDERS_FREQUENCY)
for order_id in closed_order_ids:
buy_orders = [buy_order for buy_order in buy_orders if buy_order['id'] != order_id]
sell_orders = [sell_order for sell_order in sell_orders if sell_order['id'] != order_id]
if len(sell_orders) == 0:
sys.exit("stopping bot, nothing left to sell")