· 4 years ago · Sep 09, 2021, 01:18 PM
1from binance.client import Client
2from binance.websockets import BinanceSocketManager
3from twisted.internet import reactor
4import time, configparser
5
6# Get creds
7config = configparser.ConfigParser()
8config.read_file(open('creds.cfg'))
9api_key = config.get('BINANCE', 'access_key')
10secret_key = config.get('BINANCE', 'secret_key')
11
12client = Client(api_key, secret_key)
13
14symbol = "btcusdt"
15price = None
16
17def streaming_data_process(msg):
18 #Function to process the received messages or errors
19 if msg['e'] == 'error':
20 print(msg)
21 # close and restart the socket
22 bm.stop_socket(conn_key)
23 reactor.stop()
24 bm.start()
25 else:
26 global price
27 price = float(msg['p'])
28
29# Starting the WebSocket
30bm = BinanceSocketManager(client)
31conn_key = bm.start_aggtrade_socket(f'{symbol}', streaming_data_process)
32bm.start()
33i = 0
34
35while True:
36 currentPrice = price
37 time.sleep(1)
38 i += 1
39 print(f"{currentPrice} ({i})")