-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathwebsocket_stream.py
44 lines (31 loc) · 1007 Bytes
/
websocket_stream.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
from time import sleep
from binance import ThreadedWebsocketManager
btc_price = {'error':False}
def btc_trade_history(msg):
''' define how to process incoming WebSocket messages '''
if msg['e'] != 'error':
print(msg['c'])
btc_price['last'] = msg['c']
btc_price['bid'] = msg['b']
btc_price['last'] = msg['a']
btc_price['error'] = False
else:
btc_price['error'] = True
# init and start the WebSocket
bsm = ThreadedWebsocketManager()
bsm.start()
# subscribe to a stream
bsm.start_symbol_ticker_socket(callback=btc_trade_history, symbol='BTCUSDT')
# put script to sleep to allow WebSocket to run for a while
# this is just for example purposes
sleep(2)
# add a second stream
bsm.start_symbol_ticker_socket(callback=btc_trade_history, symbol='ETHUSDT')
# put script to sleep to allow WebSocket to run for a while
# this is just for example purposes
sleep(2)
# stop websocket
bsm.stop()
sleep(2)
# display more info about the various websocket streams
help(ThreadedWebsocketManager)