Level2 - 市場行情
{
"id":1545910660740,
"type":"subscribe",
"topic":"/market/level2:BTC-USDT",
"response":true
}
Topic: /market/level2:{symbol},{symbol}...
- 推送頻率:
實時推送
- 一個topic中最多支持100個symbol
訂閱此topic可獲取指定交易對Level-2買賣盤數據。訂閱成功後,服務端會推送增量的市場數據給您。
{
"type": "message",
"topic": "/market/level2:BTC-USDT",
"subject": "trade.l2update",
"data": {
"changes": {
"asks": [
[
"18906",//price
"0.00331",//size
"14103845"//sequence
],
[
"18907.3",
"0.58751503",
"14103844"
]
],
"bids": [
[
"18891.9",
"0.15688",
"14103847"
]
]
},
"sequenceEnd": 14103847,
"sequenceStart": 14103844,
"symbol": "BTC-USDT",
"time": 1663747970273//milliseconds
}
}
校準流程:
- 將Websocket推送的Level 2數據緩存在本地;
- 通過REST請求拉取Level 2買賣盤的快照;
- 回放緩存的Level 2數據流;
- 將Level 2數據流應用到快照上,只需滿足 sequenceStart(new)<=sequenceEnd+1(old) 並且 seqenceEnd(new) > sequenceEnd(old),即可認爲是有效的L2增量消息,可以用changes中的數據覆蓋本地數據。changes中每條記錄上的sequence僅表示該價格的數量最後一次修改對應的sequence,不作爲消息連續的判斷依據;
- 根據訂單的價格和數量更新買賣盤。如果價格爲0,忽略這條消息,只更新順序號;如果數量爲0,則需要將該數量對應的訂單價格從買賣盤中移除。如遇其他情況,正常更新買賣盤即可。
Level 2 的Change屬性是一個“price, size, sequence”的字符串值,即:[“價格”,“數量”,“sequence”]。
請注意: size指的是price對應的最新size。當size爲0時,需要將其對應的price從買賣盤中刪除。
示例
以BTC/USDT爲例,假設level 2當前買賣盤數據如下:
步驟1.成功訂閱此topic,您會收到如下買賣盤數據流:
...
"asks":[
["3988.59","3", "16"], // 摒棄 sequence = 16
["3988.61","0", "19"], // 移除 price 爲 3988.61 的數據
["3988.62","8", "15"], // 摒棄 sequence <16
]
"bids":[
["3988.50", "44", "18"] // 更新 price 爲 3988.50 的size
]
"sequenceStart": 19,
"sequenceEnd": 15,
...
TIP
changes中每條記錄上的sequence僅表示該價格的數量最後一次修改對應的sequence,不作爲消息連續的判斷依據;比如當在相同價位有多個數量更新["3988.50", "20", "17"]、["3988.50", "44", "18"],此時只會推送最新的["3988.50", "44", "18"]
步驟2.通過REST請求拉取Level 2
...
"sequence": "16",
"asks":[
["3988.62","8"],// [“價格”,“數量”]
["3988.61","32"],
["3988.60","47"],
["3988.59","3"],
]
"bids":[
["3988.51","56"],
["3988.50","15"],
["3988.49","100"],
["3988.48","10"]
]
...
當前拉取的買賣盤的快照數據如下:
| Price | Size | Side |
|---------|-----|------|
| 3988.62 | 8 | Sell |
| 3988.61 | 32 | Sell |
| 3988.60 | 47 | Sell |
| 3988.59 | 3 | Sell |
| 3988.51 | 56 | Buy |
| 3988.50 | 15 | Buy |
| 3988.49 | 100 | Buy |
| 3988.48 | 10 | Buy |
當前買賣盤快照信息的sequence爲16
,摒棄買賣盤數據流中sequence <= 16的數據,回放sequence爲[18,19]
的數據,更新買賣盤快照信息。現在,您本地的sequence爲19
。
變更:
- 將價格3988.50對應的數量變更爲44(sequence順序號爲18)
- 移除價格爲3988.61的數據(sequence順序號爲19)
變更後,當前買賣盤數據爲最新數據,具體數據如下:
| Price | Size | Side |
|---------|-----|------|
| 3988.62 | 8 | Sell |
| 3988.60 | 47 | Sell |
| 3988.59 | 3 | Sell |
| 3988.51 | 56 | Buy |
| 3988.50 | 44 | Buy |
| 3988.49 | 100 | Buy |
| 3988.48 | 10 | Buy |