【问题标题】:get live price in milliseconds (Binance Websocket)以毫秒为单位获取实时价格(Binance Websocket)
【发布时间】:2022-08-11 19:25:43
【问题描述】:

如何更改我的代码,以便每 100 毫秒获取一次信息?

import asyncio
from binance import AsyncClient, BinanceSocketManager


async def main():
    client = await AsyncClient.create()
    bm = BinanceSocketManager(client)
    # start any sockets here, i.e a trade socket
    ts = bm.trade_socket(\'BTCBUSD\')
    # then start receiving messages
    async with ts as tscm:
        while True:
            res = await tscm.recv()
            print(res)

    await client.close_connection()

if __name__ == \"__main__\":

    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

我很感激我能得到的每一个答案,非常感谢!

  • 目前获取信息的时间是多少毫秒?我也认为由于交易所设置的限制,你能得到的最快是 200 ~ 300 ms
  • 似乎我每 2 秒获取一次数据。每 200 毫秒获取一次数据会很棒。你知道我该怎么做吗?
  • 是的,我会在周末为你写点东西。
  • 非常感谢你 。我正在等待尽快

标签: python api websocket bitcoin binance


【解决方案1】:

您询问“如何以毫秒为单位获取实时价格(Binance Websocket)”

以下是 binance 上可用流的列表,其中包含“更新速度”的可用选项:https://github.com/binance/binance-spot-api-docs/blob/master/web-socket-streams.md#detailed-stream-information

据我了解,大多数流类型每秒更新一次(更新速度:1000 毫秒)。

只有深度流每秒可以更新 10 次(1000ms 和 100ms 间隔)

交易流(agg 和 normal)和 Bookticker(个人和所有)是实时的。这意味着,一旦此信息可用,您将收到它...

如果没有交易,您将不会收到任何东西,因为价格没有变化……但是一旦发生交易,就会向您报告。

如果您想知道该资产的当前买卖价格,您可以使用 bookticker,与深度和差异相比,它的数据要少得多。深度流...如果您需要的不仅仅是当前订单的第一个位置,我建议使用本地深度缓存:https://www.lucit.tech/unicorn-binance-local-depth-cache.html

为了获得稳定的 websocket 连接,我建议使用UNICORN Binance WebSocket API,它会捕获大多数异常并在断开连接后自动重新连接,并且使用它的语法很简单:

from unicorn_binance_websocket_api.manager import BinanceWebSocketApiManager


def process_new_receives(stream_data, stream_buffer_name=False):
    print(str(stream_data))


ubwa = BinanceWebSocketApiManager(exchange="binance.com")
ubwa.create_stream('trade',
                   ['ethbtc', 'btcusdt', 'bnbbtc', 'ethbtc'],
                   process_stream_data=process_new_receives)

【讨论】:

    【解决方案2】:

    由于您似乎很匆忙,以下是我使用的,尽管我使用websockets 库进行调用。当我有更多时间来看看我是否可以让调用更快时,我会看看 binance api,但这应该有望实现你想要的。

    您可以通过更改await asyncio.sleep(0.5) 中的睡眠时间来更改请求之间的延迟,但如果您将其设置为低于 0.5 秒,则会触发错误:received 1008 (policy violation) Too many requests; then sent 1008 (policy violation) Too many requests

    import asyncio
    import websockets
    import json
    
    msg = {"method": "SUBSCRIBE", "params":
            [
            "btcusdt@depth"
            ],
            "id": 1
            }
    
    async def call_api():
        async with websockets.connect('wss://stream.binance.com:9443/ws/btcusdt@depth') as ws:
            while True:
                await ws.send(json.dumps(msg))
                response = await asyncio.wait_for(ws.recv(), timeout=2)
                response = json.loads(response)
                print(response)
                await asyncio.sleep(0.5)
    
    asyncio.get_event_loop().run_until_complete(call_api())
    

    【讨论】:

    • 你好,亲爱的,非常感谢你的帮助。代码只运行了大约 2 分钟然后我得到一个错误:/
    • 错误是什么?
    • 这个:websockets.exceptions.ConnectionClosedError: sent 1011 (unexpected error) keepalive ping timeout;没有收到关闭帧
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-05
    • 2013-05-09
    • 1970-01-01
    • 1970-01-01
    • 2013-05-26
    • 1970-01-01
    相关资源
    最近更新 更多