Python websocket 没有获取所有数据

Python websocket not getting all data

我正在编写一个 Python 机器人来通过 WebSocket 收集 Bitfinex 开仓订单,但我不是一次获取所有数据,而是一个一个地获取。下面是我的代码:

import json

from websocket import create_connection
ws = create_connection("wss://api.bitfinex.com/ws/1")
#ws.connect("wss://api2.bitfinex.com:3000/ws")
ws.send(json.dumps({
"event": "subscribe",
"channel": "book",
"symbol": "tBTCUSD",
"prec": "P1",
"freq": "F1",
"len": "25"
 }))


while True:
result = ws.recv()
result = json.loads(result)
print (result)
ws.close()

首先我得到这个数据:

{'event': 'info', 'version': 1.1, 'platform': {'status': 1}}
{'event': 'subscribed', 'channel': 'book', 'chanId': 9479, 'prec': 'P1', 'freq': 'F1', 'len': '25', 'pair': 'BTCUSD'}
[9479, [[9242, 3, 2.48234792], [9241, 4, 2.14994206], [9240, 7, 18.84], [9239, 4, 0.18569934], [9236, 4, 2.40599999], [9235, 2, 0.84793866], [9234, 3, 0.7], [9232, 1, 0.1], [9230, 5, 17.46637416], [9229, 1, 0.5], [9228, 2, 0.50405148], [9227, 1, 0.05410933], [9226, 3, 6.833112], [9225, 1, 1.2], [9224, 5, 2.44538683], [9222, 6, 8.21999999], [9221, 3, 1.12], [9220, 3, 2.022], [9219, 1, 7.04], [9218, 2, 0.40431586], [9217, 4, 5.68698009], [9216, 4, 2.86999999], [9215, 1, 0.09335208], [9214, 1, 6.1], [9213, 2, 2.02], [9246, 2, -0.042], [9247, 1, -1.079], [9249, 2, -1.20650001], [9250, 8, -42.96401676], [9251, 4, -8.99723363], [9253, 3, -0.02800001], [9254, 2, -0.48926159]

然后就这样一一搞定:

[9479, 9245, 2, 2.01234792]

我如何通过 websocket 立即获得完整的订单簿而不是 API,如下所示:

[9479, [[9242, 3, 2.48234792], [9241, 4, 2.14994206], [9240, 7, 18.84], [9239, 4, 0.18569934], [9236, 4, 2.40599999], [9235, 2, 0.84793866], [9234, 3, 0.7], [9232, 1, 0.1], [9230, 5, 17.46637416], [9229, 1, 0.5], [9228, 2, 0.50405148], [9227, 1, 0.05410933], [9226, 3, 6.833112], [9225, 1, 1.2], [9224, 5, 2.44538683], [9222, 6, 8.21999999], [9221, 3, 1.12], [9220, 3, 2.022], [9219, 1, 7.04], [9218, 2, 0.40431586], [9217, 4, 5.68698009], [9216, 4, 2.86999999], [9215, 1, 0.09335208], [9214, 1, 6.1], [9213, 2, 2.02], [9246, 2, -0.042], [9247, 1, -1.079], [9249, 2, -1.20650001], [9250, 8, -42.96401676], [9251, 4, -8.99723363], [9253, 3, -0.02800001], [9254, 2, -0.48926159], [9255, 2, -2.5], [9256, 4, -10.30405436], [9258, 2, -6.75], [9259, 2, -0.04], [9260, 1, -0.6], [9262, 1, -0.25], [9263, 3, -7.8391], [9264, 1, -1.6], [9265, 2, -0.1606286], [9266, 2, -6.93685322], [9267, 6, -1.87947602], [9268, 4, -17.67527073], [9270, 6, -8.65613095], [9271, 2, -1.75519738], [9272, 6, -2.09804066], [9273, 1, -0.5], [9275, 3, -11.95653655], [9276, 1, -0.75519738]]]

它似乎工作正常。您收到的初始数据是 "full" 当前订单簿;接下来的 "one by one" 条消息是更新。

没办法告诉订阅每次都发送完整的订单簿;那将是对 Bitfinex 带宽的浪费。您必须在 Python 代码中自行应用更新。

顺便说一句,我发现 this client 可能会有用;但是,您仍然需要自己应用更新。

来自官网site

The pair list is continuously growing, to get the an up to date list you can query the symbols this endpoint: https://api.bitfinex.com/v1/symbols

因此您可以使用收到的每个新数据追加和更新列表或使用上面的 api...