forked from enarjord/passivbot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
passivbot.py
764 lines (697 loc) · 34.8 KB
/
passivbot.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
import os
if 'NOJIT' not in os.environ:
os.environ['NOJIT'] = 'true'
import traceback
import argparse
import asyncio
import json
import logging
import signal
import pprint
import numpy as np
from time import time
from procedures import load_live_config, make_get_filepath, load_exchange_key_secret, print_, utc_ms
from pure_funcs import filter_orders, compress_float, create_xk, round_dynamic, denumpyize, \
spotify_config, get_position_fills
from njit_funcs import qty_to_cost, calc_diff, round_, calc_long_close_grid, calc_shrt_close_grid, calc_upnl, calc_long_entry_grid, calc_shrt_entry_grid
from typing import Union, Dict, List
import websockets
class Bot:
def __init__(self, config: dict):
self.spot = False
self.config = config
self.config['do_long'] = config['long']['enabled']
self.config['do_shrt'] = config['shrt']['enabled']
self.config['max_leverage'] = 25
self.xk = {}
self.ws = None
self.hedge_mode = self.config['hedge_mode'] = True
self.set_config(self.config)
self.ts_locked = {k: 0.0 for k in ['cancel_orders', 'update_open_orders', 'cancel_and_create',
'update_position', 'print', 'create_orders',
'check_fills', 'update_fills', 'force_update']}
self.ts_released = {k: 1.0 for k in self.ts_locked}
self.error_halt = {'update_open_orders': False, 'update_fills': False, 'update_position': False}
self.heartbeat_ts = 0
self.listen_key = None
self.position = {}
self.open_orders = []
self.fills = []
self.price = 0.0
self.ob = [0.0, 0.0]
self.n_orders_per_execution = 2
self.delay_between_executions = 2
self.force_update_interval = 30
self.c_mult = self.config['c_mult'] = 1.0
self.log_filepath = make_get_filepath(f"logs/{self.exchange}/{config['config_name']}.log")
_, self.key, self.secret = load_exchange_key_secret(self.user)
self.log_level = 0
self.user_stream_task = None
self.market_stream_task = None
self.stop_websocket = False
self.process_websocket_ticks = True
def set_config(self, config):
if 'long_mode' not in config:
config['long_mode'] = None
if 'shrt_mode' not in config:
config['shrt_mode'] = None
if 'last_price_diff_limit' not in config:
config['last_price_diff_limit'] = 0.3
if 'profit_trans_pct' not in config:
config['profit_trans_pct'] = 0.0
if 'assigned_balance' not in config:
config['assigned_balance'] = None
if 'cross_wallet_pct' not in config:
config['cross_wallet_pct'] = 1.0
if config['cross_wallet_pct'] > 1.0 or config['cross_wallet_pct'] <= 0.0:
print(f'Invalid cross_wallet_pct given: {config["cross_wallet_pct"]}. It must be greater than zero and less than or equal to one. Defaulting to 1.0.')
config['cross_wallet_pct'] = 1.0
self.config = config
for key in config:
setattr(self, key, config[key])
if key in self.xk:
self.xk[key] = config[key]
def set_config_value(self, key, value):
self.config[key] = value
setattr(self, key, self.config[key])
async def _init(self):
self.xk = create_xk(self.config)
await self.init_fills()
def dump_log(self, data) -> None:
if self.config['logging_level'] > 0:
with open(self.log_filepath, 'a') as f:
f.write(json.dumps({**{'log_timestamp': time()}, **data}) + '\n')
async def update_open_orders(self) -> None:
if self.ts_locked['update_open_orders'] > self.ts_released['update_open_orders']:
return
try:
open_orders = await self.fetch_open_orders()
open_orders = [x for x in open_orders if x['symbol'] == self.symbol]
if self.open_orders != open_orders:
self.dump_log({'log_type': 'open_orders', 'data': open_orders})
self.open_orders = open_orders
self.error_halt['update_open_orders'] = False
except Exception as e:
self.error_halt['update_open_orders'] = True
print('error with update open orders', e)
traceback.print_exc()
finally:
self.ts_released['update_open_orders'] = time()
def adjust_wallet_balance(self, balance: float) -> float:
return (balance if self.assigned_balance is None else self.assigned_balance) * self.cross_wallet_pct
def add_pbrs_to_pos(self, position_: dict):
position = position_.copy()
position['long']['pbr'] = (qty_to_cost(position['long']['size'], position['long']['price'],
self.xk['inverse'], self.xk['c_mult']) /
position['wallet_balance']) if position['wallet_balance'] else 0.0
position['shrt']['pbr'] = (qty_to_cost(position['shrt']['size'], position['shrt']['price'],
self.xk['inverse'], self.xk['c_mult']) /
position['wallet_balance']) if position['wallet_balance'] else 0.0
return position
async def update_position(self) -> None:
if self.ts_locked['update_position'] > self.ts_released['update_position']:
return
self.ts_locked['update_position'] = time()
try:
position = await self.fetch_position()
position['wallet_balance'] = self.adjust_wallet_balance(position['wallet_balance'])
# isolated equity, not cross equity
position['equity'] = position['wallet_balance'] + \
calc_upnl(position['long']['size'], position['long']['price'],
position['shrt']['size'], position['shrt']['price'],
self.price, self.inverse, self.c_mult)
position = self.add_pbrs_to_pos(position)
if self.position != position:
if self.position and 'spot' in self.market_type and \
(self.position['long']['size'] != position['long']['size'] or
self.position['shrt']['size'] != position['shrt']['size']):
# update fills if position size changed
await self.update_fills()
self.dump_log({'log_type': 'position', 'data': position})
self.position = position
self.error_halt['update_position'] = False
except Exception as e:
self.error_halt['update_position'] = True
print('error with update position', e)
traceback.print_exc()
finally:
self.ts_released['update_position'] = time()
async def init_fills(self, n_days_limit=60):
self.fills = await self.fetch_fills()
async def update_fills(self) -> [dict]:
'''
fetches recent fills
returns list of new fills
'''
if self.ts_locked['update_fills'] > self.ts_released['update_fills']:
return
self.ts_locked['update_fills'] = time()
try:
fetched = await self.fetch_fills()
seen = set()
updated_fills = []
for fill in fetched + self.fills:
if fill['order_id'] not in seen:
updated_fills.append(fill)
seen.add(fill['order_id'])
self.fills = sorted(updated_fills, key=lambda x: x['order_id'])[-5000:]
self.error_halt['update_fills'] = False
except Exception as e:
self.error_halt['update_fills'] = True
print('error with update fills', e)
traceback.print_exc()
finally:
self.ts_released['update_fills'] = time()
async def create_orders(self, orders_to_create: [dict]) -> [dict]:
if not orders_to_create:
return []
if self.ts_locked['create_orders'] > self.ts_released['create_orders']:
return []
self.ts_locked['create_orders'] = time()
try:
creations = []
for oc in sorted(orders_to_create, key=lambda x: calc_diff(x['price'], self.price)):
try:
creations.append((oc, asyncio.create_task(self.execute_order(oc))))
except Exception as e:
print_(['error creating order a', oc, e], n=True)
created_orders = []
for oc, c in creations:
try:
o = await c
created_orders.append(o)
if 'side' in o:
print_([' created order', o['symbol'], o['side'], o['position_side'], o['qty'],
o['price']], n=True)
if o['order_id'] not in {x['order_id'] for x in self.open_orders}:
self.open_orders.append(o)
else:
print_(['error creating order b', o, oc], n=True)
self.dump_log({'log_type': 'create_order', 'data': o})
except Exception as e:
print_(['error creating order c', oc, c.exception(), e], n=True)
self.dump_log({'log_type': 'create_order', 'data': {'result': str(c.exception()),
'error': repr(e), 'data': oc}})
return created_orders
finally:
self.ts_released['create_orders'] = time()
async def cancel_orders(self, orders_to_cancel: [dict]) -> [dict]:
if not orders_to_cancel:
return
if self.ts_locked['cancel_orders'] > self.ts_released['cancel_orders']:
return
self.ts_locked['cancel_orders'] = time()
try:
deletions = []
for oc in orders_to_cancel:
try:
deletions.append((oc, asyncio.create_task(self.execute_cancellation(oc))))
except Exception as e:
print_(['error cancelling order a', oc, e])
cancelled_orders = []
for oc, c in deletions:
try:
o = await c
cancelled_orders.append(o)
if 'order_id' in o:
print_(['cancelled order', o['symbol'], o['side'], o['position_side'], o['qty'],
o['price']], n=True)
self.open_orders = [oo for oo in self.open_orders if oo['order_id'] != o['order_id']]
else:
print_(['error cancelling order', o], n=True)
self.dump_log({'log_type': 'cancel_order', 'data': o})
except Exception as e:
print_(['error cancelling order b', oc, c.exception(), e], n=True)
self.dump_log({'log_type': 'cancel_order', 'data': {'result': str(c.exception()),
'error': repr(e), 'data': oc}})
return cancelled_orders
finally:
self.ts_released['cancel_orders'] = time()
def stop(self, signum=None, frame=None) -> None:
print("\nStopping passivbot, please wait...")
try:
self.stop_websocket = True
self.user_stream_task.cancel()
self.market_stream_task.cancel()
except Exception as e:
print(f"An error occurred during shutdown: {e}")
def pause(self) -> None:
self.process_websocket_ticks = False
def resume(self) -> None:
self.process_websocket_ticks = True
def calc_orders(self):
balance = self.position['wallet_balance']
long_psize = self.position['long']['size']
long_pprice = self.position['long']['price']
shrt_psize = self.position['shrt']['size']
shrt_pprice = self.position['shrt']['price']
if self.hedge_mode:
do_long = self.do_long or long_psize != 0.0
do_shrt = self.do_shrt or shrt_psize != 0.0
else:
no_pos = long_psize == 0.0 and shrt_psize == 0.0
do_long = (no_pos and self.do_long) or long_psize != 0.0
do_shrt = (no_pos and self.do_shrt) or shrt_psize != 0.0
self.xk['do_long'] = do_long
self.xk['do_shrt'] = do_shrt
orders = []
if self.long_mode == 'panic':
if long_psize != 0.0:
orders.append({'side': 'sell', 'position_side': 'long', 'qty': abs(long_psize),
'price': float(self.ob[1]), 'type': 'limit', 'reduce_only': True, 'custom_id': 'long_panic_close'})
else:
long_entries = calc_long_entry_grid(
balance, long_psize, long_pprice, self.ob[0], self.xk['inverse'], self.xk['do_long'],
self.xk['qty_step'], self.xk['price_step'], self.xk['min_qty'], self.xk['min_cost'],
self.xk['c_mult'], self.xk['grid_span'][0], self.xk['pbr_limit'][0], self.xk['max_n_entry_orders'][0],
self.xk['initial_qty_pct'][0], self.xk['eprice_pprice_diff'][0], self.xk['secondary_pbr_allocation'][0],
self.xk['secondary_pprice_diff'][0], self.xk['eprice_exp_base'][0]
)
long_closes = calc_long_close_grid(balance,
long_psize, long_pprice, self.ob[1], self.xk['spot'], self.xk['inverse'], self.xk['qty_step'],
self.xk['price_step'], self.xk['min_qty'], self.xk['min_cost'], self.xk['c_mult'], self.xk['pbr_limit'][0],
self.xk['initial_qty_pct'][0], self.xk['min_markup'][0], self.xk['markup_range'][0],
self.xk['n_close_orders'][0]
)
orders += [{'side': 'buy', 'position_side': 'long', 'qty': abs(float(o[0])),
'price': float(o[1]), 'type': 'limit', 'reduce_only': False,
'custom_id': o[2]} for o in long_entries if o[0] > 0.0]
orders += [{'side': 'sell', 'position_side': 'long', 'qty': abs(float(o[0])),
'price': float(o[1]), 'type': 'limit', 'reduce_only': True,
'custom_id': o[2]} for o in long_closes if o[0] < 0.0]
if self.shrt_mode == 'panic':
if shrt_psize != 0.0:
orders.append({'side': 'buy', 'position_side': 'shrt', 'qty': abs(shrt_psize),
'price': float(self.ob[0]), 'type': 'limit', 'reduce_only': True, 'custom_id': 'shrt_panic_close'})
else:
shrt_entries = calc_shrt_entry_grid(
balance, shrt_psize, shrt_pprice, self.ob[1], self.xk['inverse'], self.xk['do_shrt'],
self.xk['qty_step'], self.xk['price_step'], self.xk['min_qty'], self.xk['min_cost'],
self.xk['c_mult'], self.xk['grid_span'][1], self.xk['pbr_limit'][1], self.xk['max_n_entry_orders'][1],
self.xk['initial_qty_pct'][1], self.xk['eprice_pprice_diff'][1], self.xk['secondary_pbr_allocation'][1],
self.xk['secondary_pprice_diff'][1], self.xk['eprice_exp_base'][1]
)
shrt_closes = calc_shrt_close_grid(balance,
shrt_psize, shrt_pprice, self.ob[0], self.xk['spot'], self.xk['inverse'], self.xk['qty_step'],
self.xk['price_step'], self.xk['min_qty'], self.xk['min_cost'], self.xk['c_mult'], self.xk['pbr_limit'][1],
self.xk['initial_qty_pct'][1], self.xk['min_markup'][1], self.xk['markup_range'][1],
self.xk['n_close_orders'][1]
)
orders += [{'side': 'sell', 'position_side': 'shrt', 'qty': abs(float(o[0])),
'price': float(o[1]), 'type': 'limit', 'reduce_only': False,
'custom_id': o[2]} for o in shrt_entries if o[0] < 0.0]
orders += [{'side': 'buy', 'position_side': 'shrt', 'qty': abs(float(o[0])),
'price': float(o[1]), 'type': 'limit', 'reduce_only': True,
'custom_id': o[2]} for o in shrt_closes if o[0] > 0.0]
return sorted(orders, key=lambda x: calc_diff(x['price'], self.price))
async def cancel_and_create(self):
if self.ts_locked["cancel_and_create"] > self.ts_released["cancel_and_create"]:
return
if any(self.error_halt.values()):
print_([f'warning: error in rest api fetch {self.error_halt}, halting order creations/cancellations'])
return
self.ts_locked["cancel_and_create"] = time()
try:
to_cancel_, to_create_ = filter_orders(
self.open_orders,
self.calc_orders(),
keys=["side", "position_side", "qty", "price"],
)
to_cancel, to_create = [], []
for elm in to_cancel_:
if elm["position_side"] == "long":
if self.long_mode == "tp_only":
if elm["side"] == "sell":
to_cancel.append(elm)
elif self.long_mode != "manual":
to_cancel.append(elm)
if elm["position_side"] == "shrt":
if self.shrt_mode == "tp_only":
if elm["side"] == "buy":
to_cancel.append(elm)
elif self.shrt_mode != "manual":
to_cancel.append(elm)
for elm in to_create_:
if elm["position_side"] == "long":
if self.long_mode == "tp_only":
if elm["side"] == "sell":
to_create.append(elm)
elif self.long_mode != "manual":
to_create.append(elm)
if elm["position_side"] == "shrt":
if self.shrt_mode == "tp_only":
if elm["side"] == "buy":
to_create.append(elm)
elif self.shrt_mode != "manual":
to_create.append(elm)
to_cancel = sorted(to_cancel, key=lambda x: calc_diff(x["price"], self.price))
to_create = sorted(to_create, key=lambda x: calc_diff(x["price"], self.price))
results = []
if to_cancel:
# to avoid building backlog, cancel n+1 orders, create n orders
results.append(
asyncio.create_task(
self.cancel_orders(to_cancel[: self.n_orders_per_execution + 1])
)
)
await asyncio.sleep(
0.01
) # sleep 10 ms between sending cancellations and sending creations
if to_create:
results.append(
await self.create_orders(to_create[: self.n_orders_per_execution])
)
if any(results):
print()
await asyncio.sleep(
self.delay_between_executions
) # sleep before releasing lock
return results
finally:
self.ts_released["cancel_and_create"] = time()
async def on_market_stream_event(self, ticks: [dict]):
if ticks:
for tick in ticks:
if tick['is_buyer_maker']:
self.ob[0] = tick['price']
else:
self.ob[1] = tick['price']
self.price = ticks[-1]['price']
now = time()
if now - self.ts_released['force_update'] > self.force_update_interval:
self.ts_released['force_update'] = now
# force update pos and open orders thru rest API every 30 sec
await asyncio.gather(self.update_position(), self.update_open_orders())
if now - self.ts_released['print'] >= 0.5:
self.update_output_information()
if now - self.heartbeat_ts > 60 * 60:
# print heartbeat once an hour
print_(['heartbeat\n'], n=True)
self.heartbeat_ts = time()
await self.cancel_and_create()
async def on_user_stream_events(self, events: Union[List[Dict], List]) -> None:
if type(events) == list:
for event in events:
await self.on_user_stream_event(event)
else:
await self.on_user_stream_event(events)
async def on_user_stream_event(self, event: dict) -> None:
try:
pos_change = False
if 'wallet_balance' in event:
self.position['wallet_balance'] = self.adjust_wallet_balance(event['wallet_balance'])
pos_change = True
if 'long_psize' in event:
self.position['long']['size'] = event['long_psize']
self.position['long']['price'] = event['long_pprice']
self.position = self.add_pbrs_to_pos(self.position)
pos_change = True
if 'shrt_psize' in event:
self.position['shrt']['size'] = event['shrt_psize']
self.position['shrt']['price'] = event['shrt_pprice']
self.position = self.add_pbrs_to_pos(self.position)
pos_change = True
if 'new_open_order' in event:
if event['new_open_order']['order_id'] not in {x['order_id'] for x in self.open_orders}:
self.open_orders.append(event['new_open_order'])
if 'deleted_order_id' in event:
self.open_orders = [oo for oo in self.open_orders if oo['order_id'] != event['deleted_order_id']]
if 'partially_filled' in event:
await self.update_open_orders()
if pos_change:
self.position['equity'] = self.position['wallet_balance'] + \
calc_upnl(self.position['long']['size'], self.position['long']['price'],
self.position['shrt']['size'], self.position['shrt']['price'],
self.price, self.inverse, self.c_mult)
await asyncio.sleep(0.01) # sleep 10 ms to catch both pos update and open orders update
await self.cancel_and_create()
except Exception as e:
print(['error handling user stream event', e])
traceback.print_exc()
def update_output_information(self):
self.ts_released['print'] = time()
line = f"{self.symbol} "
if self.position['long']['size'] != 0.0:
line += f"l {self.position['long']['size']} @ "
line += f"{round_(self.position['long']['price'], self.price_step)}, "
long_closes = sorted([o for o in self.open_orders if o['side'] == 'sell'
and o['position_side'] == 'long'], key=lambda x: x['price'])
long_entries = sorted([o for o in self.open_orders if o['side'] == 'buy'
and o['position_side'] == 'long'], key=lambda x: x['price'])
leqty, leprice = (long_entries[-1]['qty'], long_entries[-1]['price']) if long_entries else (0.0, 0.0)
lcqty, lcprice = (long_closes[0]['qty'], long_closes[0]['price']) if long_closes else (0.0, 0.0)
line += f"e {leqty} @ {leprice}, c {lcqty} @ {lcprice} "
if self.position['shrt']['size'] != 0.0:
shrt_closes = sorted([o for o in self.open_orders if o['side'] == 'buy'
and o['position_side'] == 'shrt'], key=lambda x: x['price'])
shrt_entries = sorted([o for o in self.open_orders if o['side'] == 'sell'
and o['position_side'] == 'shrt'], key=lambda x: x['price'])
seqty, seprice = (shrt_entries[0]['qty'], shrt_entries[0]['price']) if shrt_entries else (0.0, 0.0)
scqty, scprice = (shrt_closes[-1]['qty'], shrt_closes[-1]['price']) if shrt_closes else (0.0, 0.0)
line += f"s {self.position['shrt']['size']} @ "
line += f"{round_(self.position['shrt']['price'], self.price_step)}, "
line += f"e {seqty} @ {seprice}, c {scqty} @ {scprice} "
if self.position['long']['size'] > abs(self.position['shrt']['size']):
liq_price = self.position['long']['liquidation_price']
else:
liq_price = self.position['shrt']['liquidation_price']
line += f"|| last {self.price} "
line += f"lpprc diff {calc_diff(self.position['long']['price'], self.price):.3f} "
line += f"spprc diff {calc_diff(self.position['shrt']['price'], self.price):.3f} "
line += f"liq {round_dynamic(liq_price, 5)} "
line += f"lpbr {self.position['long']['pbr']:.3f} "
line += f"spbr {self.position['shrt']['pbr']:.3f} "
line += f"bal {round_dynamic(self.position['wallet_balance'], 5)} "
line += f"eq {round_dynamic(self.position['equity'], 5)} "
print_([line], r=True)
def flush_stuck_locks(self, timeout: float = 5.0) -> None:
now = time()
for key in self.ts_locked:
if self.ts_locked[key] > self.ts_released[key]:
if now - self.ts_locked[key] > timeout:
print('flushing stuck lock', key)
self.ts_released[key] = now
async def start_websocket(self) -> None:
self.stop_websocket = False
self.process_websocket_ticks = True
await asyncio.gather(self.update_position(), self.update_open_orders())
await self.init_exchange_config()
await self.init_order_book()
self.user_stream_task = asyncio.create_task(self.start_websocket_user_stream())
self.market_stream_task = asyncio.create_task(self.start_websocket_market_stream())
await asyncio.gather(self.user_stream_task, self.market_stream_task)
async def beat_heart_user_stream(self) -> None:
pass
async def init_user_stream(self) -> None:
pass
async def start_websocket_user_stream(self) -> None:
await self.init_user_stream()
asyncio.create_task(self.beat_heart_user_stream())
print_(['url', self.endpoints['websocket_user']])
async with websockets.connect(self.endpoints['websocket_user']) as ws:
self.ws = ws
await self.subscribe_to_user_stream(ws)
async for msg in ws:
if msg is None:
continue
try:
if self.stop_websocket:
break
asyncio.create_task(self.on_user_stream_events(self.standardize_user_stream_event(json.loads(msg))))
except Exception as e:
print(['error in websocket user stream', e])
traceback.print_exc()
async def start_websocket_market_stream(self) -> None:
k = 1
async with websockets.connect(self.endpoints['websocket_market']) as ws:
await self.subscribe_to_market_stream(ws)
async for msg in ws:
if msg is None:
continue
try:
if self.stop_websocket:
break
ticks = self.standardize_market_stream_event(json.loads(msg))
if self.process_websocket_ticks:
asyncio.create_task(self.on_market_stream_event(ticks))
if k % 10 == 0:
self.flush_stuck_locks()
k = 1
k += 1
except Exception as e:
if 'success' not in msg:
print('error in websocket', e, msg)
async def subscribe_to_market_stream(self, ws):
pass
async def subscribe_to_user_stream(self, ws):
pass
async def start_bot(bot):
while not bot.stop_websocket:
try:
await bot.start_websocket()
except Exception as e:
print('Websocket connection has been lost, attempting to reinitialize the bot...', e)
traceback.print_exc()
await asyncio.sleep(10)
async def main() -> None:
parser = argparse.ArgumentParser(prog='passivbot', description='run passivbot')
parser.add_argument('user', type=str, help='user/account_name defined in api-keys.json')
parser.add_argument('symbol', type=str, help='symbol to trade')
parser.add_argument('live_config_path', type=str, help='live config to use')
parser.add_argument('-m', '--market_type', type=str, required=False, dest='market_type', default=None,
help='specify whether spot or futures (default), overriding value from backtest config')
parser.add_argument('-gs', '--graceful_stop', action='store_true',
help='if true, disable long and short')
parser.add_argument(
"-lw",
"--long_wallet_exposure_limit",
"--long-wallet-exposure-limit",
type=float,
required=False,
dest="long_wallet_exposure_limit",
default=None,
help="specify long wallet exposure limit, overriding value from live config",
)
parser.add_argument(
"-sw",
"--short_wallet_exposure_limit",
"--short-wallet-exposure-limit",
type=float,
required=False,
dest="short_wallet_exposure_limit",
default=None,
help="specify short wallet exposure limit, overriding value from live config",
)
parser.add_argument(
"-sm",
"--short_mode",
"--short-mode",
type=str,
required=False,
dest="short_mode",
default=None,
help="specify one of following short modes: [n (normal), m (manual), gs (graceful_stop), p (panic), t (tp_only)]"
)
parser.add_argument(
"-lm",
"--long_mode",
"--long-mode",
type=str,
required=False,
dest="long_mode",
default=None,
help="specify one of following long modes: [n (normal), m (manual), gs (graceful_stop), p (panic), t (tp_only)]"
)
parser.add_argument('-ab', '--assigned_balance', type=float, required=False, dest='assigned_balance', default=None,
help='add assigned_balance to live config')
args = parser.parse_args()
try:
accounts = json.load(open('api-keys.json'))
except Exception as e:
print(e, 'failed to load api-keys.json file')
return
try:
account = accounts[args.user]
except Exception as e:
print('unrecognized account name', args.user, e)
return
try:
config = load_live_config(args.live_config_path)
except Exception as e:
print(e, 'failed to load config', args.live_config_path)
return
config['user'] = args.user
config['exchange'] = account['exchange']
config['symbol'] = args.symbol
config['live_config_path'] = args.live_config_path
config['market_type'] = args.market_type if args.market_type is not None else 'futures'
if args.assigned_balance is not None:
print(f'\nassigned balance set to {args.assigned_balance}\n')
config['assigned_balance'] = args.assigned_balance
if args.long_mode is None:
if not config['long']['enabled']:
config['long_mode'] = 'manual'
else:
if args.long_mode in ['gs', 'graceful_stop', 'graceful-stop']:
print('\n\nlong graceful stop enabled; will not make new entries once existing positions are closed\n')
config['long']['enabled'] = config['do_long'] = False
elif args.long_mode in ['m', 'manual']:
print('\n\nlong manual mode enabled; will neither cancel nor create long orders')
config['long_mode'] = 'manual'
elif args.long_mode in ['n', 'normal']:
print('\n\nlong normal mode')
config['long']['enabled'] = config['do_long'] = True
elif args.long_mode in ['p', 'panic']:
print('\nlong panic mode enabled')
config['long_mode'] = 'panic'
config['long']['enabled'] = config['do_long'] = False
elif args.long_mode.lower() in ['t', 'tp_only', 'tp-only']:
print('\nlong tp only mode enabled')
config['long_mode'] = 'tp_only'
if args.short_mode is None:
if not config['shrt']['enabled']:
config['shrt_mode'] = 'manual'
else:
if args.short_mode in ['gs', 'graceful_stop', 'graceful-stop']:
print('\n\nshrt graceful stop enabled; will not make new entries once existing positions are closed\n')
config['shrt']['enabled'] = config['do_shrt'] = False
elif args.short_mode in ['m', 'manual']:
print('\n\nshrt manual mode enabled; will neither cancel nor create shrt orders')
config['shrt_mode'] = 'manual'
elif args.short_mode in ['n', 'normal']:
print('\n\nshrt normal mode')
config['shrt']['enabled'] = config['do_shrt'] = True
elif args.short_mode in ['p', 'panic']:
print('\nshort panic mode enabled')
config['shrt_mode'] = 'panic'
config['shrt']['enabled'] = config['do_shrt'] = False
elif args.short_mode.lower() in ['t', 'tp_only', 'tp-only']:
print('\nshort tp only mode enabled')
config['shrt_mode'] = 'tp_only'
if args.graceful_stop:
print('\n\ngraceful stop enabled for both long and short; will not make new entries once existing positions are closed\n')
config['long']['enabled'] = config['do_long'] = False
config['shrt']['enabled'] = config['do_shrt'] = False
config['long_mode'] = None
config['shrt_mode'] = None
if args.long_wallet_exposure_limit is not None:
print(
f"overriding long wallet exposure limit ({config['long']['pbr_limit']}) "
f"with new value: {args.long_wallet_exposure_limit}"
)
config["long"]["pbr_limit"] = args.long_wallet_exposure_limit
if args.short_wallet_exposure_limit is not None:
print(
f"overriding short wallet exposure limit ({config['shrt']['pbr_limit']}) "
f"with new value: {args.short_wallet_exposure_limit}"
)
config["shrt"]["pbr_limit"] = args.short_wallet_exposure_limit
if 'spot' in config['market_type']:
config = spotify_config(config)
if account['exchange'] == 'binance':
if 'spot' in config['market_type']:
from procedures import create_binance_bot_spot
bot = await create_binance_bot_spot(config)
else:
from procedures import create_binance_bot
bot = await create_binance_bot(config)
elif account['exchange'] == 'bybit':
from procedures import create_bybit_bot
bot = await create_bybit_bot(config)
else:
raise Exception('unknown exchange', account['exchange'])
print('using config')
pprint.pprint(denumpyize(config))
signal.signal(signal.SIGINT, bot.stop)
signal.signal(signal.SIGTERM, bot.stop)
await start_bot(bot)
await bot.session.close()
if __name__ == '__main__':
try:
asyncio.run(main())
except Exception as e:
print(f'\nThere was an error starting the bot: {e}')
traceback.print_exc()
finally:
print('\nPassivbot was stopped succesfully')
os._exit(0)