Binance WebSocket订购簿-每次更改深度

Binance WebSocket订购簿-每次更改深度,websocket,algorithmic-trading,trading,cryptoapi,binance,Websocket,Algorithmic Trading,Trading,Cryptoapi,Binance,下面是一个python脚本,它通过Biance的Websocket API()订阅订单簿信息 在两个请求中(btcusdt@depth和btcusdt@depth@100ms),每个json负载以不同的深度进行流传输。 请说明这可能是什么原因?我做错什么了吗?或者他们可能会有一些特定的标准,比如一本订单的深度是多少 import json import websocket socket='wss://stream.binance.com:9443/ws' def on_open(self):

下面是一个python脚本,它通过Biance的Websocket API()订阅订单簿信息

在两个请求中(
btcusdt@depth
btcusdt@depth@100ms),每个json负载以不同的深度进行流传输。
请说明这可能是什么原因?我做错什么了吗?或者他们可能会有一些特定的标准,比如一本订单的深度是多少

import json
import websocket

socket='wss://stream.binance.com:9443/ws'

def on_open(self):
    print("opened")
    subscribe_message = {
        "method": "SUBSCRIBE",
        "params":
        [
         "btcusdt@depth@100ms"
         ],
        "id": 1
        }

    ws.send(json.dumps(subscribe_message))

def on_message(self, message):
    print("received a message")

    ###### depths of bid/ask ######
    d = json.loads(message)
    for k, v in d.items():
        if k == "b":
            print(f"bid depth : {len(v)}")
        if k == "a":
            print(f"ask depth : {len(v)}")

def on_close(self):
    print("closed connection")

ws = websocket.WebSocketApp(socket,
                            on_open=on_open,
                            on_message=on_message,
                            on_close=on_close)

ws.run_forever()
btcusdt@depth@100毫秒 btcusdt@depth
您的代码读取最后100毫秒或1000毫秒的差异长度(未指定时间范围时的默认值)。也就是说,远程API只发送差异,而不是完整列表

预计差异的长度会有所不同


例如:

订单簿有2个出价和2个询问:

  • 要价1.02,金额10
  • 要价1.01,金额10
  • 投标价格0.99,金额10
  • 投标价格0.98,金额10
在这段时间内,会增加一个出价,并更新一个提问。因此,消息返回:

"b": [
    [ // added new bid
        0.97,
        10
    ]
],
"a": [
    [ // updated existing ask
        1.01,
        20
    ]
]
您的代码将此消息读取为

bid depth: 1
ask depth: 1
在另一个时间段内,更新两个投标

"b": [
    [ // updated existing bid
        0.98,
        20
    ],
    [ // updated existing bid
        0.99,
        20
    ]
],
"a": [] // no changes
所以你的代码读作

bid depth: 2
ask depth: 0
"b": [
    [ // updated existing bid
        0.98,
        20
    ],
    [ // updated existing bid
        0.99,
        20
    ]
],
"a": [] // no changes
bid depth: 2
ask depth: 0