Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from pybit.unified_trading import WebSocket
- from time import sleep
- import json
- ws = WebSocket(
- testnet=False,
- channel_type="spot",
- )
- file = open('bybit.logs', 'w')
- file2 = open('bybit_depth.logs', 'w')
- def handle_trades(message):
- renamed_fields = {'T': 'time', 'p': 'price', 'v': 'size', 'S': 'side'}
- for i in range(len(message['data'])):
- file.write(json.dumps({renamed_fields[field]: message['data'][i][field] for field in renamed_fields}))
- file.write('\n')
- file.flush()
- def handle_message(message):
- depth = {}
- depth['ts'] = message['ts']
- depth['bids'] = message['data']['b']
- depth['asks'] = message['data']['a']
- if message['type'] != 'snapshot':
- print('Not snapshot!')
- file2.write(json.dumps(depth))
- file2.write('\n')
- file2.flush()
- ws.trade_stream(
- symbol="DICEUSDT",
- callback=handle_trades
- )
- ws.orderbook_stream(
- depth=50,
- symbol="DICEUSDT",
- callback=handle_message
- )
- while True:
- sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement