-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbot.py
428 lines (285 loc) · 14.1 KB
/
bot.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
from datetime import datetime, timedelta
import sys
from time import sleep
from market_maker.market_maker import ExchangeInterface
from market_maker.utils import log
import settings
from utils import math
class FundingBot:
def __init__(self) -> None:
self.logger = log.setup_custom_logger('fundingbot')
self.exchange = ExchangeInterface()
self.start_balance = self.exchange.get_margin()['marginBalance'] / 100000000
self.tick_size = self.exchange.get_instrument()['tickSize']
self.loop_count = 1
self.start_time = datetime.utcnow().isoformat(timespec='seconds') + 'Z'
self.last_request = datetime.utcnow()
self.limits_exist = False
position = self.exchange.get_position()['currentQty']
self.hedge_exists = position != 0 and (abs(position) not in
[settings.POSITION_SIZE_BUY, settings.POSITION_SIZE_SELL])
self.could_hedge = position == 0
self.cancel_open_orders()
def sanity_check(self) -> None:
self.exchange.check_if_orderbook_empty()
self.exchange.check_market_open()
def print_status(self) -> None:
position = self.exchange.get_position()
ticker = self.exchange.get_ticker()
current_balance = self.exchange.get_margin()['marginBalance'] / 100000000
open_orders = self.exchange.bitmex.open_orders()
self.logger.info('ticker buy: %.2f USD' % ticker['buy'])
self.logger.info('ticker sell: %.2f USD' % ticker['sell'])
current_quantity = position['currentQty']
self.logger.info('funding rate: %.4f%%' % (self.get_funding_rate() * 100))
self.logger.info('current position: %i USD' % current_quantity)
if current_quantity:
average_entry_price = position['avgEntryPrice']
self.logger.info(' ~ average entry price: %.2f USD' % average_entry_price)
original_value = current_quantity / average_entry_price
self.logger.info(' ~ original position value: %.6f %s' %
(original_value, settings.SYMBOL[:3]))
current_value = current_quantity / ticker['buy' if current_quantity > 0 else 'sell']
self.logger.info(' ~ current position value: %.6f %s' %
(current_value, settings.SYMBOL[:3]))
value_delta = current_value - original_value
self.logger.info(' ~ position value delta: %.6f %s' %
(value_delta, settings.SYMBOL[:3]))
profit = -value_delta * (ticker['buy'] if current_quantity < 0 else ticker['sell'])
self.logger.info(' ~ position profit: %.2f USD' % profit)
self.logger.info('starting XBT balance: %.6f XBT (%s)' %
(self.start_balance, self.start_time))
self.logger.info('current XBT balance: %.6f XBT' % current_balance)
self.logger.info('open orders:%s' % (' none' if not open_orders else ''))
for order in open_orders:
if order['ordType'] == 'Limit':
self.logger.info(' ~ limit order: %i @ %.2f USD' %
(order['leavesQty'], order['price']))
elif order['ordType'] == 'StopLimit':
if order['orderQty']:
self.logger.info((' ~ stop limit order: %i, stop price: %.2f USD, '
'price: %.2f USD') % (order['orderQty'], order['stopPx'],
order['price']))
else:
self.logger.info((' ~ stop limit order: close, stop price: %.2f USD, '
'price: %.2f USD') % (order['stopPx'], order['price']))
elif order['ordType'] == 'Stop':
if order['orderQty']:
self.logger.info(' ~ stop order: %i, stop price: %.2f USD' %
(order['orderQty'], order['stopPx']))
else:
self.logger.info(' ~ stop order: close, stop price: %.2f USD' %
order['stopPx'])
sys.stdout.write('-' * 20 + '\n')
sys.stdout.flush()
def get_price(self, side: str) -> float:
ticker = self.exchange.get_ticker()
if side.lower() not in ['buy', 'sell']:
raise ValueError('invalid side passed to get_price: %s' % side)
if side.lower() == 'buy':
return ticker['sell'] - self.tick_size
else:
return ticker['buy'] + self.tick_size
def monitor(self) -> None:
"""if the price moves negatively 1.5% away from a position, exit the position
if there is an open order and the ticker moves, move the order
"""
ticker = self.exchange.get_ticker()
open_orders = self.exchange.bitmex.open_orders()
to_amend = []
for order in open_orders:
if order['ordType'] != 'Limit':
continue
to_change = False
if order['side'] == 'Buy':
if order['price'] < self.get_price('buy'):
to_change = True
new_price = self.get_price('buy')
else:
if order['price'] > self.get_price('sell'):
to_change = True
new_price = self.get_price('sell')
if to_change:
to_amend.append({'orderID': order['orderID'], 'price': new_price})
self.logger.info('amending order %i from %.2f to %.2f' %
(order['leavesQty'], order['price'], new_price))
if to_amend:
self._amend_orders(to_amend)
position = self.exchange.get_position()
quantity = position['currentQty']
if quantity:
if not self.limits_exist and not self.hedge_exists:
avg_price = position['avgEntryPrice']
limit_delta = avg_price * settings.STOP_LIMIT_MULTIPLIER
market_delta = avg_price * settings.STOP_MARKET_MULTIPLIER
if quantity > 0:
limit_stopPx = math.to_nearest(avg_price - limit_delta, self.tick_size)
limit_stop_price = limit_stopPx + self.tick_size
market_stopPx = math.to_nearest(avg_price - market_delta, self.tick_size)
side = 'Sell'
else:
limit_stopPx = math.to_nearest(avg_price + limit_delta, self.tick_size)
limit_stop_price = limit_stopPx - self.tick_size
market_stopPx = math.to_nearest(avg_price + market_delta, self.tick_size)
side = 'Buy'
orders = []
if settings.STOP_LIMIT_MULTIPLIER > 0:
limit_stop = {'stopPx': limit_stopPx, 'price': limit_stop_price,
'execInst': 'LastPrice,Close', 'ordType': 'StopLimit',
'side': side}
orders.append(limit_stop)
if settings.STOP_MARKET_MULTIPLIER > 0:
market_stop = {'stopPx': market_stopPx, 'execInst': 'LastPrice,Close',
'ordType': 'Stop', 'side': side}
orders.append(market_stop)
if orders:
self._create_orders(orders)
self.limits_exist = True
self.could_hedge = True
else:
to_cancel = [o for o in open_orders if o['ordType'] in ['StopLimit', 'Stop']]
if to_cancel:
self._cancel_orders(to_cancel)
self.limits_exist = False
if settings.HEDGE and not self.hedge_exists and self.could_hedge:
self.hedge(settings.HEDGE_SIDE, market=False)
self.hedge_exists = True
def enter_position(self, side: str, trade_quantity: int, market=False) -> None:
if market:
self.logger.info('entering a position at market (%.2f): quantity: %i, side: %s' %
(self.exchange.get_ticker()[side.lower()], trade_quantity, side))
order = {'type': 'Market', 'orderQty': trade_quantity, 'side': side}
else:
price = self.get_price(side)
self.logger.info('entering a position ~ price: %.2f, quantity: %i, side: %s' %
(price, trade_quantity, side))
order = {'price': price, 'orderQty': trade_quantity, 'side': side}
self._create_orders([order])
self.could_hedge = False
def exit_position(self, market=False, wait_for_fill=False) -> None:
self.logger.info('exiting current position. at market: %s' %
('true' if market else 'false'))
position = self.exchange.get_position()
quantity = position['currentQty']
if quantity == 0:
self.logger.info(' ~ not currently in a position')
self.hedge_exists = False
self.could_hedge = True
return
if quantity < 0:
exit_side = 'Buy'
else:
exit_side = 'Sell'
if market:
order = {'type': 'Market', 'execInst': 'Close', 'side': exit_side}
else:
exit_price = self.get_price(exit_side)
order = {'price': exit_price, 'execInst': 'Close', 'side': exit_side}
self._create_orders([order])
if wait_for_fill and not market:
while True:
sleep(1)
postition = self.exchange.get_position()
if position['currentQty'] == 0:
break
self.hedge_exists = False
def hedge(self, side: str, market=False) -> None:
current_balance = self.exchange.get_margin()['marginBalance'] / 100000000
self.logger.info('current balance: %.6f' % current_balance)
if side not in ['Buy', 'Sell']:
raise ValueError('side %s is not a valid side. options: Buy, Sell' % side)
ticker = self.exchange.get_ticker()
price = ticker[side.lower()]
quantity = int((current_balance-.1) * settings.HEDGE_MULTIPLIER * price)
self.logger.info('entering a hedge (at market: %s): %i @ %.2f' %
('true' if market else 'false', quantity, price))
if market:
order = {'type': 'Market', 'orderQty': quantity, 'side': side}
else:
order = {'price': price, 'orderQty': quantity, 'side': side}
self._create_orders([order])
def cancel_open_orders(self) -> None:
self.logger.info('cancelling all open orders')
# saves an api request, as getting open orders is via the websocket
open_orders = self.exchange.bitmex.open_orders()
if not open_orders:
self.logger.info(' ~ no open orders')
return
try:
self.exchange.cancel_all_orders()
except Exception as e:
self.logger.error('unable to cancel orders: %s', e)
self.limits_exist = False
def exit(self, *args) -> None:
self.logger.info('shutting down, all open orders will be cancelled')
self.cancel_open_orders()
#self.exit_position()
self.exchange.bitmex.exit()
sys.exit()
def run_loop(self) -> None:
while True:
if not self.exchange.is_open():
self.logger.error('realtime data connection has closed, reloading')
self.reload()
continue
self.sanity_check()
if (self.loop_count*settings.LOOP_INTERVAL) % 10 == 0:
self.print_status()
self.loop_count = 0
self.loop_count += 1
self.monitor()
sleep(settings.LOOP_INTERVAL)
def reload(self) -> None:
self.logger.info('reloading data connection...')
try:
self.exchange = ExchangeInterface()
except Exception as e:
self.logger.error(e)
self.logger.error('attempting to reload in 3 seconds...')
sleep(3)
self.reload()
sleep(3)
def get_instrument(self):
return self.exchange.bitmex.instrument(symbol=settings.SYMBOL)
def get_funding_rate(self) -> float:
return self.get_instrument()['fundingRate']
def respect_rate_limit(fn):
def wrapped(self, *args, **kwargs):
new_datetime = self.last_request + timedelta(seconds=settings.API_REST_INTERVAL)
wait_time = (new_datetime - datetime.utcnow()).total_seconds()
if wait_time > 0:
sleep(wait_time)
return fn(self, *args, **kwargs)
return wrapped
@respect_rate_limit
def _create_orders(self, orders) -> None:
try:
self.exchange.bitmex.create_bulk_orders(orders)
except Exception as e:
self.logger.warning('caught an error when requesting to the bitmex api: %s', e)
self.logger.info('retrying request after 5 seconds...')
sleep(5)
self._create_orders(orders)
self.last_request = datetime.utcnow()
@respect_rate_limit
def _amend_orders(self, orders) -> None:
try:
self.exchange.bitmex.amend_bulk_orders(orders)
except Exception as e:
self.logger.warning('caught an error when requesting to the bitmex api: %s', e)
if '400 Client Error' in str(e):
self.logger.info(' ~ order has already been fulfilled')
else:
self.logger.info(' ~ retrying request after 5 seconds')
sleep(5)
self._amend_orders(orders)
self.last_request = datetime.utcnow()
@respect_rate_limit
def _cancel_orders(self, orders) -> None:
for order in orders:
try:
self.exchange.cancel_order(order)
except Exception as e:
self.logger.error('unable to cancel order: %s' % e)
sleep(settings.API_REST_INTERVAL)
self.last_request = datetime.utcnow()