-
Notifications
You must be signed in to change notification settings - Fork 2
/
pybit.spot_changed_code.py
832 lines (689 loc) · 24.7 KB
/
pybit.spot_changed_code.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
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
## Path to view the pybit library files - /usr/local/lib/python3.10/dist-packages/pybit
## File name as saved in the server - spot.py
from ._http_manager import _HTTPManager
from ._websocket_stream import _SpotWebSocketManager
from ._websocket_stream import SPOT
from . import _helpers
from concurrent.futures import ThreadPoolExecutor
ws_name = SPOT
PUBLIC_V1_WSS = "wss://{SUBDOMAIN}.{DOMAIN}.com/spot/quote/ws/v1"
PUBLIC_V2_WSS = "wss://{SUBDOMAIN}.{DOMAIN}.com/spot/quote/ws/v2"
PRIVATE_WSS = "wss://{SUBDOMAIN}.{DOMAIN}.com/spot/ws"
class HTTP(_HTTPManager):
def orderbook(self, **kwargs):
"""
Get the orderbook.
:param kwargs: See
https://bybit-exchange.github.io/docs/spot/#t-orderbook.
:returns: Request results as dictionary.
"""
suffix = "/spot/quote/v1/depth"
return self._submit_request(
method="GET",
path=self.endpoint + suffix,
query=kwargs
)
def merged_orderbook(self, **kwargs):
"""
Get the merged orderbook.
:param kwargs: See
https://bybit-exchange.github.io/docs/spot/#t-mergedorderbook.
:returns: Request results as dictionary.
"""
return self._submit_request(
method="GET",
path=self.endpoint + "/spot/quote/v1/depth/merged",
query=kwargs
)
def query_kline(self, **kwargs):
"""
Get kline.
:param kwargs: See
https://bybit-exchange.github.io/docs/spot/#t-querykline.
:returns: Request results as dictionary.
"""
# Replace query param "from_time" since "from" keyword is reserved.
# Temporary workaround until Bybit updates official request params
if "from_time" in kwargs:
kwargs["from"] = kwargs.pop("from_time")
suffix = "/spot/quote/v1/kline"
return self._submit_request(
method="GET",
path=self.endpoint + suffix,
query=kwargs
)
def latest_information_for_symbol(self, **kwargs):
"""
Get the latest information for symbol.
:param kwargs: See
https://bybit-exchange.github.io/docs/spot/#t-latestsymbolinfo.
:returns: Request results as dictionary.
"""
suffix = "/spot/quote/v1/ticker/24hr"
return self._submit_request(
method="GET",
path=self.endpoint + suffix,
query=kwargs
)
def last_traded_price(self, **kwargs):
"""
Get the last traded price.
:param kwargs: See
https://bybit-exchange.github.io/docs/spot/#t-lasttradedprice.
:returns: Request results as dictionary.
"""
return self._submit_request(
method="GET",
path=self.endpoint + "/spot/quote/v1/ticker/price",
query=kwargs
)
def best_bid_ask_price(self, **kwargs):
"""
Get the best bid/ask price.
:param kwargs: See
https://bybit-exchange.github.io/docs/spot/#t-bestbidask.
:returns: Request results as dictionary.
"""
return self._submit_request(
method="GET",
path=self.endpoint + "/spot/quote/v1/ticker/book_ticker",
query=kwargs
)
def public_trading_records(self, **kwargs):
"""
Get recent trades. You can find a complete history of trades on Bybit
at https://public.bybit.com/.
:param kwargs: See
https://bybit-exchange.github.io/docs/spot/#t-publictradingrecords.
:returns: Request results as dictionary.
"""
# Replace query param "from_id" since "from" keyword is reserved.
# Temporary workaround until Bybit updates official request params
if "from_id" in kwargs:
kwargs["from"] = kwargs.pop("from_id")
suffix = "/spot/quote/v1/trades"
return self._submit_request(
method="GET",
path=self.endpoint + suffix,
query=kwargs
)
def query_symbol(self):
"""
Get symbol info.
:returns: Request results as dictionary.
"""
suffix = "/spot/v1/symbols"
return self._submit_request(
method="GET",
path=self.endpoint + suffix
)
def place_active_order(self, **kwargs):
"""
Places an active order. For more information, see
https://bybit-exchange.github.io/docs/spot/#t-activeorders.
:param kwargs: See
https://bybit-exchange.github.io/docs/spot/#t-activeorders.
:returns: Request results as dictionary.
"""
suffix = "/spot/v3/private/order"
return self._submit_request_new(
method="POST",
path=self.endpoint + suffix,
query=kwargs,
auth=True
)
def place_active_order_bulk(self, orders: list, max_in_parallel=10):
"""
Places multiple active orders in bulk using multithreading. For more
information on place_active_order, see
https://bybit-exchange.github.io/docs/spot/#t-activeorders.
:param list orders: A list of orders and their parameters.
:param max_in_parallel: The number of requests to be sent in parallel.
Note that you are limited to 50 requests per second.
:returns: Future request result dictionaries as a list.
"""
with ThreadPoolExecutor(max_workers=max_in_parallel) as executor:
executions = [
executor.submit(
self.place_active_order,
**order
) for order in orders
]
executor.shutdown()
return [execution.result() for execution in executions]
def get_active_order(self, **kwargs):
"""
Gets an active order. For more information, see
https://bybit-exchange.github.io/docs/spot/#t-getactive.
:param kwargs: See
https://bybit-exchange.github.io/docs/spot/#t-getactive.
:returns: Request results as dictionary.
"""
suffix = "/spot/v3/private/order"
return self._submit_request_new(
method="GET",
path=self.endpoint + suffix,
query=kwargs,
auth=True
)
def cancel_active_order(self, **kwargs):
"""
Cancels an active order. For more information, see
https://bybit-exchange.github.io/docs/spot/#t-cancelactive.
:param kwargs: See
https://bybit-exchange.github.io/docs/spot/#t-cancelactive.
:returns: Request results as dictionary.
"""
suffix = "/spot/v3/private/cancel-order"
return self._submit_request_cancel(
method="POST",
path=self.endpoint + suffix,
query=kwargs,
auth=True
)
def fast_cancel_active_order(self, **kwargs):
"""
Fast cancels an active order.
:param kwargs: See
https://bybit-exchange.github.io/docs/spot/#t-fastcancelactiveorder.
:returns: Request results as dictionary.
"""
return self._submit_request(
method="DELETE",
path=self.endpoint + "/spot/v1/order/fast",
query=kwargs,
auth=True
)
def cancel_active_order_bulk(self, orders: list, max_in_parallel=10):
"""
Cancels multiple active orders in bulk using multithreading. For more
information on cancel_active_order, see
https://bybit-exchange.github.io/docs/spot/#t-activeorders.
:param list orders: A list of orders and their parameters.
:param max_in_parallel: The number of requests to be sent in parallel.
Note that you are limited to 50 requests per second.
:returns: Future request result dictionaries as a list.
"""
with ThreadPoolExecutor(max_workers=max_in_parallel) as executor:
executions = [
executor.submit(
self.cancel_active_order,
**order
) for order in orders
]
executor.shutdown()
return [execution.result() for execution in executions]
def batch_cancel_active_order(self, **kwargs):
"""
Batch cancels active orders.
:param kwargs: See
https://bybit-exchange.github.io/docs/spot/#t-batchcancelactiveorder.
:returns: Request results as dictionary.
"""
return self._submit_request(
method="DELETE",
path=self.endpoint + "/spot/v3/private/cancel-orders",
query=kwargs,
auth=True
)
def batch_fast_cancel_active_order(self, **kwargs):
"""
Batch fast cancels active orders.
:param kwargs: See
https://bybit-exchange.github.io/docs/spot/#t-batchfastcancelactiveorder.
:returns: Request results as dictionary.
"""
return self._submit_request_new(
method="DELETE",
path=self.endpoint + "/spot/order/batch-fast-cancel",
query=kwargs,
auth=True
)
def batch_cancel_active_order_by_ids(self, **kwargs):
"""
Batch cancels active order by ids.
:param kwargs: See
https://bybit-exchange.github.io/docs/spot/#t-batchcancelactiveorderbyids.
:returns: Request results as dictionary.
"""
return self._submit_request_new(
method="DELETE",
path=self.endpoint + "/spot/v3/private/cancel-orders-by-ids",
query=kwargs,
auth=True
)
def query_active_order(self, **kwargs):
"""
Query real-time active order information.
:param kwargs: See
https://bybit-exchange.github.io/docs/spot/#t-queryactive.
:returns: Request results as dictionary.
"""
suffix = "/spot/v3/private/open-orders"
return self._submit_request_new(
method="GET",
path=self.endpoint + suffix,
query=kwargs,
auth=True
)
def user_trade_records(self, **kwargs):
"""
Get user's trading records. The results are ordered in ascending order
(the first item is the oldest).
:param kwargs: See
https://bybit-exchange.github.io/docs/spot/#t-usertraderecords.
:returns: Request results as dictionary.
"""
suffix = "/spot/v1/myTrades"
return self._submit_request(
method="GET",
path=self.endpoint + suffix,
query=kwargs,
auth=True
)
def get_wallet_balance(self, **kwargs):
"""
Get wallet balance info.
:param kwargs: See
https://bybit-exchange.github.io/docs/spot/#t-balance.
:returns: Request results as dictionary.
"""
suffix = "/spot/v1/account"
return self._submit_request(
method="GET",
path=self.endpoint + suffix,
query=kwargs,
auth=True
)
def server_time(self):
"""
Get Bybit server time.
:returns: Request results as dictionary.
"""
suffix = "/spot/v1/time"
return self._submit_request(
method="GET",
path=self.endpoint + suffix
)
def borrow_margin_loan(self, **kwargs):
"""
For more information, see
https://bybit-exchange.github.io/docs/spot/?python--pybit#t-borrowmarginloan
:param kwargs: See
https://bybit-exchange.github.io/docs/spot/?python--pybit#t-borrowmarginloan
:returns: Request result as integer.
"""
suffix = "/spot/v1/cross-margin/loan"
return self._submit_request(
method="POST",
path=self.endpoint + suffix,
query=kwargs,
auth=True
)
def repay_margin_loan(self, **kwargs):
"""
For more information, see
https://bybit-exchange.github.io/docs/spot/?python--pybit#t-repaymarginloan
:param kwargs: See
https://bybit-exchange.github.io/docs/spot/?python--pybit#t-repaymarginloan
:returns: Request result as integer.
"""
suffix = "/spot/v1/cross-margin/repay"
return self._submit_request(
method="POST",
path=self.endpoint + suffix,
query=kwargs,
auth=True
)
def query_borrowing_info(self, **kwargs):
"""
For more information, see
https://bybit-exchange.github.io/docs/spot/?python--pybit#t-queryborrowinginfo
:param kwargs: See
https://bybit-exchange.github.io/docs/spot/?python--pybit#t-queryborrowinginfo
:returns: Request results as dictionary.
"""
suffix = "/spot/v1/cross-margin/order"
return self._submit_request(
method="GET",
path=self.endpoint + suffix,
query=kwargs,
auth=True
)
def query_account_info(self, **kwargs):
"""
For more information, see
https://bybit-exchange.github.io/docs/spot/?python--pybit#t-queryaccountinfo
:param kwargs: See
https://bybit-exchange.github.io/docs/spot/?python--pybit#t-queryaccountinfo
:returns: Request results as dictionary.
"""
suffix = "/spot/v1/cross-margin/accounts/balance"
return self._submit_request(
method="GET",
path=self.endpoint + suffix,
query=kwargs,
auth=True
)
def query_interest_quota(self, **kwargs):
"""
For more information, see
https://bybit-exchange.github.io/docs/spot/?python--pybit#t-queryinterestquota
:param kwargs: See
https://bybit-exchange.github.io/docs/spot/?python--pybit#t-queryinterestquota
:returns: Request results as dictionary.
"""
suffix = "/spot/v1/cross-margin/loan-info"
return self._submit_request(
method="GET",
path=self.endpoint + suffix,
query=kwargs,
auth=True
)
def query_repayment_history(self, **kwargs):
"""
For more information, see
https://bybit-exchange.github.io/docs/spot/?python--pybit#t-queryrepaymenthistory
:param kwargs: See
https://bybit-exchange.github.io/docs/spot/?python--pybit#t-queryrepaymenthistory
:returns: Request results as dictionary.
"""
suffix = "/spot/v1/cross-margin/repay/history"
return self._submit_request(
method="GET",
path=self.endpoint + suffix,
query=kwargs,
auth=True
)
def lt_asset_info(self, **kwargs):
"""
For more information, see
https://bybit-exchange.github.io/docs/spot/#t-ltinfo
:param kwargs: See
https://bybit-exchange.github.io/docs/spot/#t-ltinfo
:returns: Request results as Dictionary.
"""
suffix = "/spot/lt/v1/info"
return self._submit_request(
method="GET",
path=self.endpoint + suffix,
query=kwargs
)
def lt_market_info(self, **kwargs):
"""
For more information, see
https://bybit-exchange.github.io/docs/spot/#t-ltquotereference
:param kwargs: See
https://bybit-exchange.github.io/docs/spot/#t-ltquotereference
:returns: Request results as Dictionary.
"""
suffix = "/spot/lt/v1/reference"
return self._submit_request(
method="GET",
path=self.endpoint + suffix,
query=kwargs,
auth=True
)
def lt_purchase(self, **kwargs):
"""
For more information, see
https://bybit-exchange.github.io/docs/spot/#t-ltpurchase
:param kwargs: See
https://bybit-exchange.github.io/docs/spot/#t-ltpurchase
:returns: Request results as Dictionary.
"""
suffix = "/spot/lt/v1/purchase"
return self._submit_request(
method="POST",
path=self.endpoint + suffix,
query=kwargs,
auth=True
)
def lt_redeem(self, **kwargs):
"""
For more information, see
https://bybit-exchange.github.io/docs/spot/#t-ltredeem
:param kwargs: See
https://bybit-exchange.github.io/docs/spot/#t-ltredeem
:returns: Request results as Dictionary.
"""
suffix = "/spot/lt/v1/redeem"
return self._submit_request(
method="POST",
path=self.endpoint + suffix,
query=kwargs,
auth=True
)
def lt_purchase_redemption_history(self, **kwargs):
"""
For more information, see
https://bybit-exchange.github.io/docs/spot/#t-ltrecord
:param kwargs: See
https://bybit-exchange.github.io/docs/spot/#t-ltrecord
:returns: Request results as Dictionary.
"""
suffix = "/spot/lt/v1/record"
return self._submit_request(
method="GET",
path=self.endpoint + suffix,
query=kwargs,
auth=True
)
class WebSocket(_SpotWebSocketManager):
def __init__(self, **kwargs):
super().__init__(ws_name, **kwargs)
self.ws_public_v1 = None
self.ws_public_v2 = None
self.ws_private = None
self.active_connections = []
self.kwargs = kwargs
self.public_kwargs = _helpers.make_public_kwargs(self.kwargs)
def is_connected(self):
return self._are_connections_connected(self.active_connections)
def _ws_public_v1_subscribe(self, topic, callback):
if not self.ws_public_v1:
self.ws_public_v1 = _SpotWebSocketManager(
ws_name, **self.public_kwargs)
self.ws_public_v1._connect(PUBLIC_V1_WSS)
self.active_connections.append(self.ws_public_v1)
self.ws_public_v1.subscribe(topic, callback)
def _ws_public_v2_subscribe(self, topic, callback):
if not self.ws_public_v2:
self.ws_public_v2 = _SpotWebSocketManager(
ws_name, **self.public_kwargs)
self.ws_public_v2._connect(PUBLIC_V2_WSS)
self.active_connections.append(self.ws_public_v2)
self.ws_public_v2.subscribe(topic, callback)
def _ws_private_subscribe(self, topic, callback):
if not self.ws_private:
self.ws_private = _SpotWebSocketManager(
ws_name, **self.kwargs)
self.ws_private._connect(PRIVATE_WSS)
self.active_connections.append(self.ws_private)
self.ws_private.subscribe(topic, callback)
def custom_topic_stream(self, topic, callback, wss_url):
subscribe = _helpers.identify_ws_method(
wss_url,
{
PUBLIC_V1_WSS: self._ws_public_v1_subscribe,
PUBLIC_V2_WSS: self._ws_public_v2_subscribe,
PRIVATE_WSS: self._ws_private_subscribe
})
subscribe(topic, callback)
def trade_v1_stream(self, callback, symbol):
"""
https://bybit-exchange.github.io/docs/spot/#t-websockettrade
"""
topic = \
{
"topic": "trade",
"event": "sub",
"symbol": symbol,
"params": {
"binary": False
}
}
self._ws_public_v1_subscribe(topic, callback)
def realtimes_v1_stream(self, callback, symbol):
"""
https://bybit-exchange.github.io/docs/spot/#t-websocketrealtimes
"""
topic = \
{
"topic": "realtimes",
"event": "sub",
"symbol": symbol,
"params": {
"binary": False
}
}
self._ws_public_v1_subscribe(topic, callback)
def kline_v1_stream(self, callback, symbol, interval):
"""
https://bybit-exchange.github.io/docs/spot/#t-websocketkline
"""
topic = \
{
"topic": "kline_{}".format(str(interval)),
"event": "sub",
"symbol": symbol,
"params": {
"binary": False
}
}
self._ws_public_v1_subscribe(topic, callback)
def depth_v1_stream(self, callback, symbol):
"""
https://bybit-exchange.github.io/docs/spot/#t-websocketdepth
"""
topic = \
{
"topic": "depth",
"event": "sub",
"symbol": symbol,
"params": {
"binary": False
}
}
self._ws_public_v1_subscribe(topic, callback)
def merged_depth_v1_stream(self, callback, symbol, dump_scale):
"""
https://bybit-exchange.github.io/docs/spot/#t-websocketmergeddepth
"""
topic = \
{
"topic": "mergedDepth",
"event": "sub",
"symbol": symbol,
"params": {
"dumpScale": int(dump_scale),
"binary": False
}
}
self._ws_public_v1_subscribe(topic, callback)
def diff_depth_v1_stream(self, callback, symbol):
"""
This topic always returns messages in the "snapshot" format for a
simplified user experience. pybit processes the delta/snapshot
messages for you. Read the Bybit API documentation for more information.
https://bybit-exchange.github.io/docs/spot/#t-websocketdiffdepth
"""
topic = \
{
"topic": "diffDepth",
"event": "sub",
"symbol": symbol,
"params": {
"binary": False
}
}
self._ws_public_v1_subscribe(topic, callback)
def depth_v2_stream(self, callback, symbol):
"""
https://bybit-exchange.github.io/docs/spot/#t-websocketv2depth
"""
topic = \
{
"topic": "depth",
"event": "sub",
"params": {
"symbol": symbol,
"binary": False
}
}
self._ws_public_v2_subscribe(topic, callback)
def kline_v2_stream(self, callback, symbol, interval):
"""
https://bybit-exchange.github.io/docs/spot/#t-websocketv2kline
"""
topic = \
{
"topic": "kline",
"event": "sub",
"params": {
"symbol": symbol,
"klineType": interval,
"binary": False
}
}
self._ws_public_v2_subscribe(topic, callback)
def trade_v2_stream(self, callback, symbol):
"""
https://bybit-exchange.github.io/docs/spot/#t-websocketv2trade
"""
topic = \
{
"topic": "trade",
"event": "sub",
"params": {
"symbol": symbol,
"binary": False
}
}
self._ws_public_v2_subscribe(topic, callback)
def book_ticker_v2_stream(self, callback, symbol):
"""
https://bybit-exchange.github.io/docs/spot/#t-websocketv2bookticker
"""
topic = \
{
"topic": "bookTicker",
"event": "sub",
"params": {
"symbol": symbol,
"binary": False
}
}
self._ws_public_v2_subscribe(topic, callback)
def realtimes_v2_stream(self, callback, symbol):
"""
https://bybit-exchange.github.io/docs/spot/#t-websocketv2realtimes
"""
topic = \
{
"topic": "realtimes",
"event": "sub",
"params": {
"symbol": symbol,
"binary": False
}
}
self._ws_public_v2_subscribe(topic, callback)
def outbound_account_info_stream(self, callback):
"""
https://bybit-exchange.github.io/docs/spot/#t-outboundaccountinfo
"""
topic = "outboundAccountInfo"
self._ws_private_subscribe(topic=topic, callback=callback)
def execution_report_stream(self, callback):
"""
https://bybit-exchange.github.io/docs/spot/#t-executionreport
"""
topic = "executionReport"
self._ws_private_subscribe(topic=topic, callback=callback)
def ticket_info_stream(self, callback):
"""
https://bybit-exchange.github.io/docs/spot/#t-ticketinfo
"""
topic = "ticketInfo"
self._ws_private_subscribe(topic=topic, callback=callback)