Advertisement
den4ik2003

Untitled

Apr 10th, 2024
802
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | None | 0 0
  1. from pybit.unified_trading import WebSocket
  2. from time import sleep
  3. import json
  4.  
  5. ws = WebSocket(
  6.     testnet=False,
  7.     channel_type="spot",
  8. )
  9.  
  10. file = open('bybit.logs', 'w')
  11. file2 = open('bybit_depth.logs', 'w')
  12.  
  13. def handle_trades(message):
  14.     renamed_fields = {'T': 'time', 'p': 'price', 'v': 'size', 'S': 'side'}
  15.  
  16.     for i in range(len(message['data'])):
  17.         file.write(json.dumps({renamed_fields[field]: message['data'][i][field] for field in renamed_fields}))
  18.         file.write('\n')
  19.         file.flush()
  20.        
  21.  
  22. def handle_message(message):
  23.     depth = {}
  24.     depth['ts'] = message['ts']
  25.     depth['bids'] = message['data']['b']
  26.     depth['asks'] = message['data']['a']
  27.    
  28.     if message['type'] != 'snapshot':
  29.         print('Not snapshot!')
  30.        
  31.     file2.write(json.dumps(depth))
  32.     file2.write('\n')
  33.     file2.flush()
  34.    
  35.  
  36. ws.trade_stream(
  37.     symbol="DICEUSDT",
  38.     callback=handle_trades
  39. )
  40.    
  41. ws.orderbook_stream(
  42.     depth=50,
  43.     symbol="DICEUSDT",
  44.     callback=handle_message
  45. )
  46.  
  47.  
  48. while True:
  49.     sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement