-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathstreams.py
More file actions
executable file
·1577 lines (1305 loc) · 58.6 KB
/
streams.py
File metadata and controls
executable file
·1577 lines (1305 loc) · 58.6 KB
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
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import asyncio
import gzip
import json
import logging
import time
from asyncio import sleep
from enum import Enum
from random import random
from socket import gaierror
from typing import Optional, List, Dict, Callable, Any
import websockets as ws
from websockets.exceptions import ConnectionClosedError
from .client import AsyncClient
from .enums import FuturesType
from .exceptions import BinanceWebsocketUnableToConnect
from .enums import ContractType
from .helpers import get_loop
from .threaded_stream import ThreadedApiManager
KEEPALIVE_TIMEOUT = 5 * 60 # 5 minutes
class WSListenerState(Enum):
INITIALISING = 'Initialising'
STREAMING = 'Streaming'
RECONNECTING = 'Reconnecting'
EXITING = 'Exiting'
class BinanceSocketType(str, Enum):
SPOT = 'Spot'
USD_M_FUTURES = 'USD_M_Futures'
COIN_M_FUTURES = 'Coin_M_Futures'
OPTIONS = 'Vanilla_Options'
ACCOUNT = 'Account'
class ReconnectingWebsocket:
MAX_RECONNECTS = 5
MAX_RECONNECT_SECONDS = 60
MIN_RECONNECT_WAIT = 0.1
TIMEOUT = 10
NO_MESSAGE_RECONNECT_TIMEOUT = 60
MAX_QUEUE_SIZE = 100
def __init__(
self, url: str, path: Optional[str] = None, prefix: str = 'ws/', is_binary: bool = False, exit_coro=None, **kwargs
):
self._loop = get_loop()
self._log = logging.getLogger(__name__)
self._path = path
self._url = url
self._exit_coro = exit_coro
self._prefix = prefix
self._reconnects = 0
self._is_binary = is_binary
self._conn = None
self._socket = None
self.ws: Optional[ws.WebSocketClientProtocol] = None # type: ignore
self.ws_state = WSListenerState.INITIALISING
self._queue = asyncio.Queue()
self._handle_read_loop = None
self._ws_kwargs = kwargs
async def __aenter__(self):
await self.connect()
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
if self._exit_coro:
await self._exit_coro(self._path)
self.ws_state = WSListenerState.EXITING
if self.ws:
self.ws.fail_connection()
if self._conn and hasattr(self._conn, 'protocol'):
await self._conn.__aexit__(exc_type, exc_val, exc_tb)
self.ws = None
if not self._handle_read_loop:
self._log.error("CANCEL read_loop")
await self._kill_read_loop()
async def connect(self):
await self._before_connect()
assert self._path
ws_url = self._url + self._prefix + self._path
self._conn = ws.connect(ws_url, close_timeout=0.1, **self._ws_kwargs) # type: ignore
try:
self.ws = await self._conn.__aenter__()
except: # noqa
await self._reconnect()
return
self.ws_state = WSListenerState.STREAMING
self._reconnects = 0
await self._after_connect()
# To manage the "cannot call recv while another coroutine is already waiting for the next message"
if not self._handle_read_loop:
self._handle_read_loop = self._loop.call_soon_threadsafe(asyncio.create_task, self._read_loop())
async def _kill_read_loop(self):
self.ws_state = WSListenerState.EXITING
while self._handle_read_loop:
await sleep(0.1)
async def _before_connect(self):
pass
async def _after_connect(self):
pass
def _handle_message(self, evt):
if self._is_binary:
try:
evt = gzip.decompress(evt)
except (ValueError, OSError):
return None
try:
return json.loads(evt)
except ValueError:
self._log.debug(f'error parsing evt json:{evt}')
return None
async def _read_loop(self):
try:
while True:
try:
while self.ws_state == WSListenerState.RECONNECTING:
await self._run_reconnect()
if self.ws_state == WSListenerState.EXITING:
self._log.debug(f"_read_loop {self._path} break for {self.ws_state}")
break
elif self.ws.state == ws.protocol.State.CLOSING: # type: ignore
await asyncio.sleep(0.1)
continue
elif self.ws.state == ws.protocol.State.CLOSED: # type: ignore
await self._reconnect()
elif self.ws_state == WSListenerState.STREAMING:
assert self.ws
res = await asyncio.wait_for(self.ws.recv(), timeout=self.TIMEOUT)
res = self._handle_message(res)
if res:
if self._queue.qsize() < self.MAX_QUEUE_SIZE:
await self._queue.put(res)
else:
self._log.debug(f"Queue overflow {self.MAX_QUEUE_SIZE}. Message not filled")
await self._queue.put({
'e': 'error',
'm': 'Queue overflow. Message not filled'
})
raise BinanceWebsocketUnableToConnect
except asyncio.TimeoutError:
self._log.debug(f"no message in {self.TIMEOUT} seconds")
# _no_message_received_reconnect
except asyncio.CancelledError as e:
self._log.debug(f"cancelled error {e}")
break
except asyncio.IncompleteReadError as e:
self._log.debug(f"incomplete read error ({e})")
except ConnectionClosedError as e:
self._log.debug(f"connection close error ({e})")
except gaierror as e:
self._log.debug(f"DNS Error ({e})")
except BinanceWebsocketUnableToConnect as e:
self._log.debug(f"BinanceWebsocketUnableToConnect ({e})")
break
except Exception as e:
self._log.debug(f"Unknown exception ({e})")
continue
finally:
self._handle_read_loop = None # Signal the coro is stopped
self._reconnects = 0
async def _run_reconnect(self):
await self.before_reconnect()
if self._reconnects < self.MAX_RECONNECTS:
reconnect_wait = self._get_reconnect_wait(self._reconnects)
self._log.debug(
f"websocket reconnecting. {self.MAX_RECONNECTS - self._reconnects} reconnects left - "
f"waiting {reconnect_wait}"
)
await asyncio.sleep(reconnect_wait)
await self.connect()
else:
self._log.error(f'Max reconnections {self.MAX_RECONNECTS} reached:')
# Signal the error
await self._queue.put({
'e': 'error',
'm': 'Max reconnect retries reached'
})
raise BinanceWebsocketUnableToConnect
async def recv(self):
res = None
while not res:
try:
res = await asyncio.wait_for(self._queue.get(), timeout=self.TIMEOUT)
except asyncio.TimeoutError:
self._log.debug(f"no message in {self.TIMEOUT} seconds")
return res
async def _wait_for_reconnect(self):
while self.ws_state != WSListenerState.STREAMING and self.ws_state != WSListenerState.EXITING:
await sleep(0.1)
def _get_reconnect_wait(self, attempts: int) -> int:
expo = 2 ** attempts
return round(random() * min(self.MAX_RECONNECT_SECONDS, expo - 1) + 1)
async def before_reconnect(self):
if self.ws and self._conn:
await self._conn.__aexit__(None, None, None)
self.ws = None
self._reconnects += 1
def _no_message_received_reconnect(self):
self._log.debug('No message received, reconnecting')
self.ws_state = WSListenerState.RECONNECTING
async def _reconnect(self):
self.ws_state = WSListenerState.RECONNECTING
class KeepAliveWebsocket(ReconnectingWebsocket):
def __init__(
self, client: AsyncClient, url, keepalive_type, prefix='ws/', is_binary=False, exit_coro=None,
user_timeout=None, **kwargs
):
super().__init__(path=None, url=url, prefix=prefix, is_binary=is_binary, exit_coro=exit_coro, **kwargs)
self._keepalive_type = keepalive_type
self._client = client
self._user_timeout = user_timeout or KEEPALIVE_TIMEOUT
self._timer = None
async def __aexit__(self, *args, **kwargs):
if not self._path:
return
if self._timer:
self._timer.cancel()
self._timer = None
await super().__aexit__(*args, **kwargs)
async def _before_connect(self):
if not self._path:
self._path = await self._get_listen_key()
async def _after_connect(self):
self._start_socket_timer()
def _start_socket_timer(self):
self._timer = self._loop.call_later(
self._user_timeout,
lambda: asyncio.create_task(self._keepalive_socket())
)
async def _get_listen_key(self):
if self._keepalive_type == 'user':
listen_key = await self._client.stream_get_listen_key()
elif self._keepalive_type == 'margin': # cross-margin
listen_key = await self._client.margin_stream_get_listen_key()
elif self._keepalive_type == 'futures':
listen_key = await self._client.futures_stream_get_listen_key()
elif self._keepalive_type == 'coin_futures':
listen_key = await self._client.futures_coin_stream_get_listen_key()
else: # isolated margin
# Passing symbol for isolated margin
listen_key = await self._client.isolated_margin_stream_get_listen_key(self._keepalive_type)
return listen_key
async def _keepalive_socket(self):
try:
listen_key = await self._get_listen_key()
if listen_key != self._path:
self._log.debug("listen key changed: reconnect")
self._path = listen_key
await self._reconnect()
else:
self._log.debug("listen key same: keepalive")
if self._keepalive_type == 'user':
await self._client.stream_keepalive(self._path)
elif self._keepalive_type == 'margin': # cross-margin
await self._client.margin_stream_keepalive(self._path)
elif self._keepalive_type == 'futures':
await self._client.futures_stream_keepalive(self._path)
elif self._keepalive_type == 'coin_futures':
await self._client.futures_coin_stream_keepalive(self._path)
else: # isolated margin
# Passing symbol for isolated margin
await self._client.isolated_margin_stream_keepalive(self._keepalive_type, self._path)
except Exception:
pass # Ignore
finally:
self._start_socket_timer()
class BinanceSocketManager:
STREAM_URL = 'wss://stream.binance.{}:9443/'
STREAM_TESTNET_URL = 'wss://testnet.binance.vision/'
FSTREAM_URL = 'wss://fstream.binance.{}/'
FSTREAM_TESTNET_URL = 'wss://stream.binancefuture.com/'
DSTREAM_URL = 'wss://dstream.binance.{}/'
DSTREAM_TESTNET_URL = 'wss://dstream.binancefuture.com/'
VSTREAM_URL = 'wss://vstream.binance.{}/'
VSTREAM_TESTNET_URL = 'wss://testnetws.binanceops.{}/'
WEBSOCKET_DEPTH_5 = '5'
WEBSOCKET_DEPTH_10 = '10'
WEBSOCKET_DEPTH_20 = '20'
def __init__(self, client: AsyncClient, user_timeout=KEEPALIVE_TIMEOUT):
"""Initialise the BinanceSocketManager
:param client: Binance API client
:type client: binance.AsyncClient
"""
self.STREAM_URL = self.STREAM_URL.format(client.tld)
self.FSTREAM_URL = self.FSTREAM_URL.format(client.tld)
self.DSTREAM_URL = self.DSTREAM_URL.format(client.tld)
self.VSTREAM_URL = self.VSTREAM_URL.format(client.tld)
self.VSTREAM_TESTNET_URL = self.VSTREAM_TESTNET_URL.format(client.tld)
self._conns = {}
self._loop = get_loop()
self._client = client
self._user_timeout = user_timeout
self.testnet = self._client.testnet
self.ws_kwargs = {}
def _get_stream_url(self, stream_url: Optional[str] = None):
if stream_url:
return stream_url
stream_url = self.STREAM_URL
if self.testnet:
stream_url = self.STREAM_TESTNET_URL
return stream_url
def _get_socket(
self, path: str, stream_url: Optional[str] = None, prefix: str = 'ws/', is_binary: bool = False,
socket_type: BinanceSocketType = BinanceSocketType.SPOT
) -> ReconnectingWebsocket:
conn_id = f'{socket_type}_{path}'
if conn_id not in self._conns:
self._conns[conn_id] = ReconnectingWebsocket(
path=path,
url=self._get_stream_url(stream_url),
prefix=prefix,
exit_coro=lambda p: self._exit_socket(f'{socket_type}_{p}'),
is_binary=is_binary,
**self.ws_kwargs
)
return self._conns[conn_id]
def _get_account_socket(
self, path: str, stream_url: Optional[str] = None, prefix: str = 'ws/', is_binary: bool = False
):
conn_id = f'{BinanceSocketType.ACCOUNT}_{path}'
if conn_id not in self._conns:
self._conns[conn_id] = KeepAliveWebsocket(
client=self._client,
url=self._get_stream_url(stream_url),
keepalive_type=path,
prefix=prefix,
exit_coro=self._exit_socket,
is_binary=is_binary,
user_timeout=self._user_timeout,
**self.ws_kwargs
)
return self._conns[conn_id]
def _get_futures_socket(self, path: str, futures_type: FuturesType, prefix: str = 'stream?streams='):
socket_type: BinanceSocketType = BinanceSocketType.USD_M_FUTURES
if futures_type == FuturesType.USD_M:
stream_url = self.FSTREAM_URL
if self.testnet:
stream_url = self.FSTREAM_TESTNET_URL
else:
stream_url = self.DSTREAM_URL
if self.testnet:
stream_url = self.DSTREAM_TESTNET_URL
return self._get_socket(path, stream_url, prefix, socket_type=socket_type)
def _get_options_socket(self, path: str, prefix: str = 'ws/'):
stream_url = self.VSTREAM_URL
if self.testnet:
stream_url = self.VSTREAM_TESTNET_URL
return self._get_socket(path, stream_url, prefix, is_binary=True, socket_type=BinanceSocketType.OPTIONS)
async def _exit_socket(self, path: str):
await self._stop_socket(path)
def depth_socket(self, symbol: str, depth: Optional[str] = None, interval: Optional[int] = None):
"""Start a websocket for symbol market depth returning either a diff or a partial book
https://github.com/binance-exchange/binance-official-api-docs/blob/master/web-socket-streams.md#partial-book-depth-streams
:param symbol: required
:type symbol: str
:param depth: optional Number of depth entries to return, default None. If passed returns a partial book instead of a diff
:type depth: str
:param interval: optional interval for updates, default None. If not set, updates happen every second. Must be 0, None (1s) or 100 (100ms)
:type interval: int
:returns: connection key string if successful, False otherwise
Partial Message Format
.. code-block:: python
{
"lastUpdateId": 160, # Last update ID
"bids": [ # Bids to be updated
[
"0.0024", # price level to be updated
"10", # quantity
[] # ignore
]
],
"asks": [ # Asks to be updated
[
"0.0026", # price level to be updated
"100", # quantity
[] # ignore
]
]
}
Diff Message Format
.. code-block:: python
{
"e": "depthUpdate", # Event type
"E": 123456789, # Event time
"s": "BNBBTC", # Symbol
"U": 157, # First update ID in event
"u": 160, # Final update ID in event
"b": [ # Bids to be updated
[
"0.0024", # price level to be updated
"10", # quantity
[] # ignore
]
],
"a": [ # Asks to be updated
[
"0.0026", # price level to be updated
"100", # quantity
[] # ignore
]
]
}
"""
socket_name = symbol.lower() + '@depth'
if depth and depth != '1':
socket_name = f'{socket_name}{depth}'
if interval:
if interval in [0, 100]:
socket_name = f'{socket_name}@{interval}ms'
else:
raise ValueError("Websocket interval value not allowed. Allowed values are [0, 100]")
return self._get_socket(socket_name)
def kline_socket(self, symbol: str, interval=AsyncClient.KLINE_INTERVAL_1MINUTE):
"""Start a websocket for symbol kline data
https://github.com/binance-exchange/binance-official-api-docs/blob/master/web-socket-streams.md#klinecandlestick-streams
:param symbol: required
:type symbol: str
:param interval: Kline interval, default KLINE_INTERVAL_1MINUTE
:type interval: str
:returns: connection key string if successful, False otherwise
Message Format
.. code-block:: python
{
"e": "kline", # event type
"E": 1499404907056, # event time
"s": "ETHBTC", # symbol
"k": {
"t": 1499404860000, # start time of this bar
"T": 1499404919999, # end time of this bar
"s": "ETHBTC", # symbol
"i": "1m", # interval
"f": 77462, # first trade id
"L": 77465, # last trade id
"o": "0.10278577", # open
"c": "0.10278645", # close
"h": "0.10278712", # high
"l": "0.10278518", # low
"v": "17.47929838", # volume
"n": 4, # number of trades
"x": false, # whether this bar is final
"q": "1.79662878", # quote volume
"V": "2.34879839", # volume of active buy
"Q": "0.24142166", # quote volume of active buy
"B": "13279784.01349473" # can be ignored
}
}
"""
path = f'{symbol.lower()}@kline_{interval}'
return self._get_socket(path)
def kline_futures_socket(self, symbol: str, interval=AsyncClient.KLINE_INTERVAL_1MINUTE,
futures_type: FuturesType = FuturesType.USD_M,
contract_type: ContractType = ContractType.PERPETUAL):
"""Start a websocket for symbol kline data for the perpeual futures stream
https://binance-docs.github.io/apidocs/futures/en/#continuous-contract-kline-candlestick-streams
:param symbol: required
:type symbol: str
:param interval: Kline interval, default KLINE_INTERVAL_1MINUTE
:type interval: str
:param futures_type: use USD-M or COIN-M futures default USD-M
:param contract_type: use PERPETUAL or CURRENT_QUARTER or NEXT_QUARTER default PERPETUAL
:returns: connection key string if successful, False otherwise
Message Format
.. code-block:: python
{
"e":"continuous_kline", // Event type
"E":1607443058651, // Event time
"ps":"BTCUSDT", // Pair
"ct":"PERPETUAL" // Contract type
"k":{
"t":1607443020000, // Kline start time
"T":1607443079999, // Kline close time
"i":"1m", // Interval
"f":116467658886, // First trade ID
"L":116468012423, // Last trade ID
"o":"18787.00", // Open price
"c":"18804.04", // Close price
"h":"18804.04", // High price
"l":"18786.54", // Low price
"v":"197.664", // volume
"n": 543, // Number of trades
"x":false, // Is this kline closed?
"q":"3715253.19494", // Quote asset volume
"V":"184.769", // Taker buy volume
"Q":"3472925.84746", //Taker buy quote asset volume
"B":"0" // Ignore
}
}
<pair>_<contractType>@continuousKline_<interval>
"""
path = f'{symbol.lower()}_{contract_type.value}@continuousKline_{interval}'
return self._get_futures_socket(path, prefix='ws/', futures_type=futures_type)
def miniticker_socket(self, update_time: int = 1000):
"""Start a miniticker websocket for all trades
This is not in the official Binance api docs, but this is what
feeds the right column on a ticker page on Binance.
:param update_time: time between callbacks in milliseconds, must be 1000 or greater
:type update_time: int
:returns: connection key string if successful, False otherwise
Message Format
.. code-block:: python
[
{
'e': '24hrMiniTicker', # Event type
'E': 1515906156273, # Event time
's': 'QTUMETH', # Symbol
'c': '0.03836900', # close
'o': '0.03953500', # open
'h': '0.04400000', # high
'l': '0.03756000', # low
'v': '147435.80000000', # volume
'q': '5903.84338533' # quote volume
}
]
"""
return self._get_socket(f'!miniTicker@arr@{update_time}ms')
def trade_socket(self, symbol: str):
"""Start a websocket for symbol trade data
https://github.com/binance-exchange/binance-official-api-docs/blob/master/web-socket-streams.md#trade-streams
:param symbol: required
:type symbol: str
:returns: connection key string if successful, False otherwise
Message Format
.. code-block:: python
{
"e": "trade", # Event type
"E": 123456789, # Event time
"s": "BNBBTC", # Symbol
"t": 12345, # Trade ID
"p": "0.001", # Price
"q": "100", # Quantity
"b": 88, # Buyer order Id
"a": 50, # Seller order Id
"T": 123456785, # Trade time
"m": true, # Is the buyer the market maker?
"M": true # Ignore.
}
"""
return self._get_socket(symbol.lower() + '@trade')
def aggtrade_socket(self, symbol: str):
"""Start a websocket for symbol trade data
https://github.com/binance-exchange/binance-official-api-docs/blob/master/web-socket-streams.md#aggregate-trade-streams
:param symbol: required
:type symbol: str
:returns: connection key string if successful, False otherwise
Message Format
.. code-block:: python
{
"e": "aggTrade", # event type
"E": 1499405254326, # event time
"s": "ETHBTC", # symbol
"a": 70232, # aggregated tradeid
"p": "0.10281118", # price
"q": "8.15632997", # quantity
"f": 77489, # first breakdown trade id
"l": 77489, # last breakdown trade id
"T": 1499405254324, # trade time
"m": false, # whether buyer is a maker
"M": true # can be ignored
}
"""
return self._get_socket(symbol.lower() + '@aggTrade')
def aggtrade_futures_socket(self, symbol: str, futures_type: FuturesType = FuturesType.USD_M):
"""Start a websocket for aggregate symbol trade data for the futures stream
:param symbol: required
:param futures_type: use USD-M or COIN-M futures default USD-M
:returns: connection key string if successful, False otherwise
Message Format
.. code-block:: python
{
"e": "aggTrade", // Event type
"E": 123456789, // Event time
"s": "BTCUSDT", // Symbol
"a": 5933014, // Aggregate trade ID
"p": "0.001", // Price
"q": "100", // Quantity
"f": 100, // First trade ID
"l": 105, // Last trade ID
"T": 123456785, // Trade time
"m": true, // Is the buyer the market maker?
}
"""
return self._get_futures_socket(symbol.lower() + '@aggTrade', futures_type=futures_type)
def symbol_miniticker_socket(self, symbol: str):
"""Start a websocket for a symbol's miniTicker data
https://binance-docs.github.io/apidocs/spot/en/#individual-symbol-mini-ticker-stream
:param symbol: required
:type symbol: str
:returns: connection key string if successful, False otherwise
Message Format
.. code-block:: python
{
"e": "24hrMiniTicker", // Event type
"E": 123456789, // Event time
"s": "BNBBTC", // Symbol
"c": "0.0025", // Close price
"o": "0.0010", // Open price
"h": "0.0025", // High price
"l": "0.0010", // Low price
"v": "10000", // Total traded base asset volume
"q": "18" // Total traded quote asset volume
}
"""
return self._get_socket(symbol.lower() + '@miniTicker')
def symbol_ticker_socket(self, symbol: str):
"""Start a websocket for a symbol's ticker data
https://github.com/binance-exchange/binance-official-api-docs/blob/master/web-socket-streams.md#individual-symbol-ticker-streams
:param symbol: required
:type symbol: str
:returns: connection key string if successful, False otherwise
Message Format
.. code-block:: python
{
"e": "24hrTicker", # Event type
"E": 123456789, # Event time
"s": "BNBBTC", # Symbol
"p": "0.0015", # Price change
"P": "250.00", # Price change percent
"w": "0.0018", # Weighted average price
"x": "0.0009", # Previous day's close price
"c": "0.0025", # Current day's close price
"Q": "10", # Close trade's quantity
"b": "0.0024", # Best bid price
"B": "10", # Bid bid quantity
"a": "0.0026", # Best ask price
"A": "100", # Best ask quantity
"o": "0.0010", # Open price
"h": "0.0025", # High price
"l": "0.0010", # Low price
"v": "10000", # Total traded base asset volume
"q": "18", # Total traded quote asset volume
"O": 0, # Statistics open time
"C": 86400000, # Statistics close time
"F": 0, # First trade ID
"L": 18150, # Last trade Id
"n": 18151 # Total number of trades
}
"""
return self._get_socket(symbol.lower() + '@ticker')
def ticker_socket(self):
"""Start a websocket for all ticker data
By default all markets are included in an array.
https://github.com/binance-exchange/binance-official-api-docs/blob/master/web-socket-streams.md#all-market-tickers-stream
:param coro: callback function to handle messages
:type coro: function
:returns: connection key string if successful, False otherwise
Message Format
.. code-block:: python
[
{
'F': 278610,
'o': '0.07393000',
's': 'BCCBTC',
'C': 1509622420916,
'b': '0.07800800',
'l': '0.07160300',
'h': '0.08199900',
'L': 287722,
'P': '6.694',
'Q': '0.10000000',
'q': '1202.67106335',
'p': '0.00494900',
'O': 1509536020916,
'a': '0.07887800',
'n': 9113,
'B': '1.00000000',
'c': '0.07887900',
'x': '0.07399600',
'w': '0.07639068',
'A': '2.41900000',
'v': '15743.68900000'
}
]
"""
return self._get_socket('!ticker@arr')
def index_price_socket(self, symbol: str, fast: bool = True):
"""Start a websocket for a symbol's futures mark price
https://binance-docs.github.io/apidocs/delivery/en/#index-price-stream
:param symbol: required
:param fast: use faster or 1s default
:returns: connection key string if successful, False otherwise
Message Format
.. code-block:: python
{
"e": "indexPriceUpdate", // Event type
"E": 1591261236000, // Event time
"i": "BTCUSD", // Pair
"p": "9636.57860000", // Index Price
}
"""
stream_name = '@indexPrice@1s' if fast else '@indexPrice'
return self._get_futures_socket(symbol.lower() + stream_name, futures_type=FuturesType.COIN_M)
def futures_depth_socket(self, symbol: str, depth: str = '10', interval: Optional[int] = None, futures_type=FuturesType.USD_M):
"""Subscribe to a futures depth data stream
https://binance-docs.github.io/apidocs/futures/en/#partial-book-depth-streams
:param symbol: required
:type symbol: str
:param depth: optional Number of depth entries to return, default 10.
:type depth: str
:param interval: optional interval for updates, default None. If not set, updates happen every second. Must be None (1s), 250 (250ms), 500 (500ms) or 100 (100ms)
:type interval: int
:param futures_type: use USD-M or COIN-M futures default USD-M
"""
stream_name = symbol.lower() + '@depth' + str(depth)
if interval is not None:
if interval in [250, 500, 100]:
stream_name += f'@{interval}ms'
else:
raise ValueError("Websocket interval value not allowed. Allowed values are [250, 500, 100]")
return self._get_futures_socket(stream_name, futures_type=futures_type)
def symbol_mark_price_socket(self, symbol: str, fast: bool = True, futures_type: FuturesType = FuturesType.USD_M):
"""Start a websocket for a symbol's futures mark price
https://binance-docs.github.io/apidocs/futures/en/#mark-price-stream
:param symbol: required
:param fast: use faster or 1s default
:param futures_type: use USD-M or COIN-M futures default USD-M
:returns: connection key string if successful, False otherwise
Message Format
.. code-block:: python
{
"e": "markPriceUpdate", // Event type
"E": 1562305380000, // Event time
"s": "BTCUSDT", // Symbol
"p": "11185.87786614", // Mark price
"r": "0.00030000", // Funding rate
"T": 1562306400000 // Next funding time
}
"""
stream_name = '@markPrice@1s' if fast else '@markPrice'
return self._get_futures_socket(symbol.lower() + stream_name, futures_type=futures_type)
def all_mark_price_socket(self, fast: bool = True, futures_type: FuturesType = FuturesType.USD_M):
"""Start a websocket for all futures mark price data
By default all symbols are included in an array.
https://binance-docs.github.io/apidocs/futures/en/#mark-price-stream-for-all-market
:param fast: use faster or 1s default
:param futures_type: use USD-M or COIN-M futures default USD-M
:returns: connection key string if successful, False otherwise
Message Format
.. code-block:: python
[
{
"e": "markPriceUpdate", // Event type
"E": 1562305380000, // Event time
"s": "BTCUSDT", // Symbol
"p": "11185.87786614", // Mark price
"r": "0.00030000", // Funding rate
"T": 1562306400000 // Next funding time
}
]
"""
stream_name = '!markPrice@arr@1s' if fast else '!markPrice@arr'
return self._get_futures_socket(stream_name, futures_type=futures_type)
def symbol_ticker_futures_socket(self, symbol: str, futures_type: FuturesType = FuturesType.USD_M):
"""Start a websocket for a symbol's ticker data
By default all markets are included in an array.
https://binance-docs.github.io/apidocs/futures/en/#individual-symbol-book-ticker-streams
:param symbol: required
:param futures_type: use USD-M or COIN-M futures default USD-M
:returns: connection key string if successful, False otherwise
.. code-block:: python
[
{
"u":400900217, // order book updateId
"s":"BNBUSDT", // symbol
"b":"25.35190000", // best bid price
"B":"31.21000000", // best bid qty
"a":"25.36520000", // best ask price
"A":"40.66000000" // best ask qty
}
]
"""
return self._get_futures_socket(symbol.lower() + '@bookTicker', futures_type=futures_type)
def individual_symbol_ticker_futures_socket(self, symbol: str, futures_type: FuturesType = FuturesType.USD_M):
"""Start a futures websocket for a single symbol's ticker data
https://binance-docs.github.io/apidocs/futures/en/#individual-symbol-ticker-streams
:param symbol: required
:type symbol: str
:param futures_type: use USD-M or COIN-M futures default USD-M
:returns: connection key string if successful, False otherwise
.. code-block:: python
{
"e": "24hrTicker", // Event type
"E": 123456789, // Event time
"s": "BTCUSDT", // Symbol
"p": "0.0015", // Price change
}
"""
return self._get_futures_socket(symbol.lower() + '@ticker', futures_type=futures_type)
def all_ticker_futures_socket(self, futures_type: FuturesType = FuturesType.USD_M):
"""Start a websocket for all ticker data
By default all markets are included in an array.
https://binance-docs.github.io/apidocs/futures/en/#all-book-tickers-stream
:param futures_type: use USD-M or COIN-M futures default USD-M
:returns: connection key string if successful, False otherwise
Message Format
.. code-block:: python
[
{
"u":400900217, // order book updateId
"s":"BNBUSDT", // symbol
"b":"25.35190000", // best bid price
"B":"31.21000000", // best bid qty
"a":"25.36520000", // best ask price
"A":"40.66000000" // best ask qty
}
]
"""
return self._get_futures_socket('!bookTicker', futures_type=futures_type)
def symbol_book_ticker_socket(self, symbol: str):
"""Start a websocket for the best bid or ask's price or quantity for a specified symbol.
https://github.com/binance-exchange/binance-official-api-docs/blob/master/web-socket-streams.md#individual-symbol-book-ticker-streams
:param symbol: required
:type symbol: str
:returns: connection key string if successful, False otherwise
Message Format
.. code-block:: python
{
"u":400900217, // order book updateId
"s":"BNBUSDT", // symbol
"b":"25.35190000", // best bid price
"B":"31.21000000", // best bid qty
"a":"25.36520000", // best ask price
"A":"40.66000000" // best ask qty
}
"""
return self._get_socket(symbol.lower() + '@bookTicker')
def book_ticker_socket(self):
"""Start a websocket for the best bid or ask's price or quantity for all symbols.
https://github.com/binance-exchange/binance-official-api-docs/blob/master/web-socket-streams.md#all-book-tickers-stream
:returns: connection key string if successful, False otherwise
Message Format
.. code-block:: python
{
// Same as <symbol>@bookTicker payload
}
"""
return self._get_socket('!bookTicker')
def multiplex_socket(self, streams: List[str]):
"""Start a multiplexed socket using a list of socket names.
User stream sockets can not be included.
Symbols in socket name must be lowercase i.e bnbbtc@aggTrade, neobtc@ticker
Combined stream events are wrapped as follows: {"stream":"<streamName>","data":<rawPayload>}