訂閱WebSocket
主網:
現貨:wss://stream.bybit.com/v5/public/spot
USDT, USDC永續 & USDC交割:wss://stream.bybit.com/v5/public/linear
反向合約:wss://stream.bybit.com/v5/public/inverse
期權:wss://stream.bybit.com/v5/public/option
測試網:
現貨:wss://stream-testnet.bybit.com/v5/public/spot
USDT和USDC永續:wss://stream-testnet.bybit.com/v5/public/linear
反向合約:wss://stream-testnet.bybit.com/v5/public/inverse
期權:wss://stream-testnet.bybit.com/v5/public/option
主網:
wss://stream.bybit.com/v5/private
測試網:
wss://stream-testnet.bybit.com/v5/private
主網:
wss://stream.bybit.com/v5/trade
測試網:
wss://stream-testnet.bybit.com/v5/trade
如果您的帳戶是在 www.bybit-tr.com 註冊, 請使用stream.bybit-tr.com
進行主網連接
針對私有頻道和交易, 您可以自定義連接存活時長, 通過增加參數max_active_time
, 最小支持30s
(30秒), 最大支持600s
(10分鐘). 如果您需要按分鐘級別配置, 您也可以傳遞1m
, 2m
等. 例如, wss://stream-testnet.bybit.com/v5/private?max_active_time=1m.
一般來說, 當客戶端與服務端沒有上下行數據, 包括心跳和交易數據下發, 那麼該連接會在最後一條交互後維持10分鐘, 由服務端斷開. 當您由特別的需求時, 可以通過該字段max_active_time
自行控制存活時間.
由於系統每30秒掃描一次, 因此配置的時長可能無法非常精確, 換句話說, 如果您配置了45秒, 然後最後一條數據下發或者心跳發生在2023-08-15 17:27:23
, 您的私有連接可能在2023-08-15 17:28:15
發生斷連.
鑒權
構建連接時,創建鑒權請求。
注意: 如果您正在使用pybit, bybit-api或者其他第三方庫, 您可以忽略此項-因為鑒權已經內建。
{
"req_id": "10001", // 可選項
"op": "auth",
"args": [
"api_key",
1662350400000, //過期時間應當大於當前時間戳
"singature"
]
}
# based on: https://github.com/bybit-exchange/pybit/blob/master/pybit/_http_manager.py
import hmac
import json
import time
import websocket
api_key = ""
api_secret = ""
# Generate expires.
expires = int((time.time() + 1) * 1000)
# Generate signature.
signature = str(hmac.new(
bytes(api_secret, "utf-8"),
bytes(f"GET/realtime{expires}", "utf-8"), digestmod="sha256"
).hexdigest())
ws = websocket.WebSocketApp(
url=url,
...
)
# Authenticate with API.
ws.send(
json.dumps({
"op": "auth",
"args": [api_key, expires, signature]
})
)
鑒權成功的響應示例
{
"success": true,
"ret_msg": "",
"op": "auth",
"conn_id": "cejreaspqfh3sjdnldmg-p"
}
簽名生成的示例可以參考這裡。
由於網絡的複雜性,您可能隨時遇到斷連。請參考以下建議確保您能即時接收到推送:
- 通過發送心跳來維持連接;
- 遇到斷連時,立即重新連接。
IP限頻
- 不要嘗試頻繁地構建連接與斷開連接;
- 不要在5分鐘內構建超過500個連接。
公有頻道訂閱參數限制
不管是期貨、現貨、期權, 對於單個連接, args裡的數組元素長度總和不能超過21,000個字符
- 現貨每次向單一連接僅能發送不超過10個參數的訂閱請求,但單個連接沒有args訂閱限制
- 期權單個連接,至多訂閱2000個args
- 期貨單個連接沒有args限制
如何發送心跳
// req_id is a customised id, which is optional
ws.send(JSON.stringify({"req_id": "100001", "op": "ping"}));
公共頻道接收到pong的響應示例
- 現貨
- 期貨
- 期權
{
"success": true,
"ret_msg": "pong",
"conn_id": "0970e817-426e-429a-a679-ff7f55e0b16a",
"op": "ping"
}
{
"success": true,
"ret_msg": "pong",
"conn_id": "465772b1-7630-4fdc-a492-e003e6f0f260",
"req_id": "",
"op": "ping"
}
{
"args": [
"1672916271846"
],
"op": "pong"
}
私有頻道接收到pong的響應示例
{
"req_id": "test",
"op": "pong",
"args": [
"1675418560633"
],
"conn_id": "cfcb4ocsvfriu23r3er0-1b"
}
為了維持連接,我們推薦您每20秒發送一次心跳。
如何訂閱topic
理解Websocket裡的args
通過傳入args來訂閱指定topic
// 訂閱1檔的orderbook
{
"req_id": "test", // 可選
"op": "subscribe",
"args": [
"orderbook.1.BTCUSDT"
]
}
通過逗號隔開,可以同時訂閱多個topic或者多個symbol
{
"req_id": "test", // 可選
"op": "subscribe",
"args": [
"orderbook.1.BTCUSDT",
"publicTrade.BTCUSDT",
"orderbook.1.ETHUSDT"
]
}
理解如何取消訂閱
您可以通過發送請求來動態地停止訂閱:
{
"op": "unsubscribe",
"args": [
"publicTrade.ETHUSD"
],
"req_id": "customised_id"
}
理解訂閱的響應
訂閱成功後的響應示例
- 私有頻道
- 公有現貨
- 公有期貨
- 公有期權
{
"success": true,
"ret_msg": "",
"op": "subscribe",
"conn_id": "cejreassvfrsfvb9v1a0-2m"
}
{
"success": true,
"ret_msg": "subscribe",
"conn_id": "2324d924-aa4d-45b0-a858-7b8be29ab52b",
"req_id": "10001",
"op": "subscribe"
}
{
"success": true,
"ret_msg": "",
"conn_id": "3cd84cb1-4d06-4a05-930a-2efe5fc70f0f",
"req_id": "",
"op": "subscribe"
}
{
"success": true,
"conn_id": "aa01fbfffe80af37-00000001-000b37b9-7147f432704fd28c-00e1c172",
"data": {
"failTopics": [],
"successTopics": [
"orderbook.100.BTC-6JAN23-18000-C"
]
},
"type": "COMMAND_RESP"
}