How to have multiple WebSocket streams with PyBit Python library

313 Views Asked by At

I have multiple bybit accounts and I want to track orders in real time using websockets. This is my python code. It works when there is only one websocket init

from bot import ws
import asyncio
import os
from pybit.unified_trading import WebSocket
from time import sleep
import json

ws1 = WebSocket(
    testnet=os.getenv('TESTNET'),
    channel_type="private",
    api_key="XXXXXXX",
    api_secret="XXXXXXXXXXXXXXXXXXX",
)
ws2 = WebSocket(
    testnet=os.getenv('TESTNET'),
    channel_type="private",
    api_key="YYYYYYY",
    api_secret="YYYYYYYYYYYYYYYYYYY",
)

def handle_message(message):
    print(message)

ws1.order_stream(callback=handle_message)
ws2.order_stream(callback=handle_message)

but when I run this code it gives me next error

Traceback (most recent call last):
File "C:\Projects\bybot-telegram\main.py", line 31, in <module>
ws1.order_stream(callback=handle_message)
File "C:\Projects\bybot-telegram\venv\Lib\site-packages\pybit\unified_trading.py", line 116, in
order_stream              self.subscribe(topic, callback)
File "C:\Projects\bybot-telegram\venv\Lib\site-packages\pybit\_websocket_stream.py", line 353, in
subscribe               time.sleep(0.1)  
KeyboardInterrupt  

I tried using ws_name attribute but then I get new error

File "C:\Projects\bybot-telegram\main.py", line 8, in <module>
ws1 = WebSocket(
^^^^^^^^^^
File "C:\Projects\bybot-telegram\venv\Lib\site-packages\pybit\unified_trading.py", line 69, in
__init__                   super().__init__(WSS_NAME, **kwargs)
TypeError: _V5WebSocketManager.__init__() got multiple values for argument 'ws_name'
0

There are 0 best solutions below