Orderbook subscribing not working for UNIUSDT
Closed this issue · 4 comments
Nice project !
When running this minimal example, there is no output, according to the api docs I would expect to see output:
from pybit import WebSocket
from time import sleep
ws = WebSocket(
endpoint='wss://stream.bybit.com/realtime',
subscriptions=['orderBookL2_25.UNIUSDT'],
)
while True:
for j in ws.fetch('orderBookL2_25.UNIUSDT'):
print(j)
sleep(0.1)
Could you clarify on what I might be doing wrong ? Otherwise I think there is a bug with getting the orderbook for UNIUSDT,
Api docs : https://bybit-exchange.github.io/docs/inverse/#t-websocketorderbook25
Hey there,
I'll check on this first thing tomorrow. I was planning to do an entire overhaul of the Websocket
object by the end of this week anyways.
I've ran the example provided by https://github.com/verata-veritatis/pybit/blob/master/examples/websocket_example.py, it seems like the very first command to get order book of BTCUSD is also not working.
# Import the WebSocket object from pybit.
from pybit import WebSocket
# Define your endpoint URLs and subscriptions.
endpoint_public = 'wss://stream.bybit.com/realtime_public'
endpoint_private = 'wss://stream.bybit.com/realtime_private'
subs = [
'orderBookL2_25.BTCUSD',
'instrument_info.100ms.BTCUSD',
'instrument_info.100ms.ETHUSD'
]
# Connect without authentication!
ws_unauth = WebSocket(endpoint_public, subscriptions=subs)
# Let's fetch the orderbook for BTCUSD.
print(
ws_unauth.fetch('orderBookL2_25.BTCUSD')
)
Observed output:
{}
Seriously this observation has created impact if websocket is really working for PyBit. I also looked at the issue list, it seems like your comment to look into them were few months back. I wonder if PyBit has serious issue to deal with ByBit's websocket?
Also, my impression for websocket is primarily intended for "streaming" purpose instead of "fetching" or "polling" where HTTP couldn't achieve. Could you comment if there is a plan to support websocket "streaming" as well?
I am a bit disappointed that this is getting zero attention and is from March 25 but no news since.
I asked myself every question @viper7882 did. example code doesn't work, seems outdated too and yes, websocket support is supposed to provide streaming like code structure - one shouldn't be polling (even though behind the scenes it's what ultimately happens)
I am revamping the websocket system away from polling, take a look at the branch restructure_wss. There is an example file there to show how it works.
You're right, the current websocket example is misleading. The problem lies with the current websocket fetch method, because if we add the while
loop for each example, the rest of the code cannot run, and I could never figure out a way to show this functionality in a way which makes sense in an real usage scenario. The reason fetch()
usually returns {}
on the first call is because the websocket client hasn't received any orderbook updates yet, and fetch()
doesn't wait for this to happen (you need to poll for it). As I am redesigning the websocket right now I won't update the existing example as it will be updated once the redesign is done, but for now you can use code like
from pybit import WebSocket
subs = [
"orderBookL2_25.BTCUSD"
]
ws = WebSocket(
"wss://stream-testnet.bybit.com/realtime",
subscriptions=subs
)
while True:
data = ws.fetch(subs[0])
if data:
print(data)
From the official doc: https://bybit-exchange.github.io/docs/inverse/#t-websocketorderbook25