How can I get all coins data from bybit by using one websocket request?
All I found is https://bybit-exchange.github.io/docs/v5/websocket/public/kline that returns one coin by one message. Here is how I do it. Please help
class ByBit:
ssl_context = ssl.SSLContext()
def __init__(self):
self.coins_data = {}
async def auth(self, websocket: WebSocketClientProtocol):
api_key = cnf.BUYBUT_API_KEY
api_secret = cnf.BUYBUT_API_SECRET
expires = int((time.time() + 1) * 1000)
signature = str(hmac.new(
bytes(api_secret, "utf-8"),
bytes(f"GET/realtime{expires}", "utf-8"), digestmod="sha256"
).hexdigest())
await websocket.send(json.dumps({
"op": "auth",
"args": [api_key, expires, signature]
}))
async def subscribe_on_ticker(self, websocket: WebSocketClientProtocol, tickers: list):
await websocket.send(json.dumps({
"op": "subscribe",
"args": tickers
}))
async def subscribe(self, websocket: WebSocket, tickers: list, interval=30):
uri = "wss://stream.bybit.com/v5/public/linear"
async with websockets.connect(uri, ssl=self.ssl_context) as ws:
# Subscribe to the required channel to receive real-time data
await self.auth(ws)
# await self.subscribe_on_ticker(ws, ["kline.30.BTCUSDT", "kline.30.ETHUSDT"])
await self.subscribe_on_ticker(ws, [f"kline.{interval}.{ticker}" for ticker in tickers])
while True:
response = await ws.recv()
message = json.loads(response)
for ticker in message.get("data", []):
symbol = message.get("topic").replace(f"kline.{interval}.", "")
buy_price = ticker["open"]
sell_price = ticker["open"]
self.update_coin_data(symbol, buy_price, sell_price)
await websocket.send_json(self.coins_data)
I want to get all available coins by one websocket connection ( as binance websocket does)
New contributor
python_dev1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.