gpt4 book ai didi

python - 如何在一个请求中从 Binance API 获取所有(或多个)对的历史 klines?

转载 作者:行者123 更新时间:2023-12-03 17:18:03 27 4
gpt4 key购买 nike

我有一个交易多对(30-40)的交易机器人。它使用之前的 5m 蜡烛作为价格输入。因此,我一一获得所有对的 5m 历史记录。目前,完整的周期大约需要 10 分钟,所以 500 万支蜡烛每 10 米更新一次,这是不好的。
关于如何加快速度的任何想法?

最佳答案

我认为最适合您的选择是 websocket 连接。您不能每次接收一次 kline 数据,例如。 5 分钟,但您可以收到蜡烛的每一个变化,就像您在图表中看到的那样。 Binance API 仅提供此功能,但结合 websocket 连接,它会非常快,而不是 10 分钟。
收到数据后,您只需指定蜡烛关闭的时间,您可以从 json 数据('t' 和 'T')中的时间戳来完成。 [documentation here]
您必须安装 websockets 库。

pip install websockets
这是一些示例代码,它是如何工作的。
import asyncio
import websockets


async def candle_stick_data():
url = "wss://stream.binance.com:9443/ws/" #steam address
first_pair = 'bnbbtc@kline_1m' #first pair
async with websockets.connect(url+first_pair) as sock:
pairs = '{"method": "SUBSCRIBE", "params": ["xrpbtc@kline_1m","ethbtc@kline_1m" ], "id": 1}' #other pairs

await sock.send(pairs)
print(f"> {pairs}")
while True:
resp = await sock.recv()
print(f"< {resp}")

asyncio.get_event_loop().run_until_complete(candle_stick_data())
输出:
< {"e":"kline","E":1599828802835,"s":"XRPBTC","k":{"t":1599828780000,"T":1599828839999,"s":"XRPBTC","i":"1m","f":76140140,"L":76140145,"o":"0.00002346","c":"0.00002346","h":"0.00002346","l":"0.00002345","v":"700.00000000","n":6,"x":false,"q":"0.01641578","V":"78.00000000","Q":"0.00182988","B":"0"}}
< {"e":"kline","E":1599828804297,"s":"BNBBTC","k":{"t":1599828780000,"T":1599828839999,"s":"BNBBTC","i":"1m","f":87599856,"L":87599935,"o":"0.00229400","c":"0.00229610","h":"0.00229710","l":"0.00229400","v":"417.88000000","n":80,"x":false,"q":"0.95933156","V":"406.63000000","Q":"0.93351653","B":"0"}}
< {"e":"kline","E":1599828804853,"s":"ETHBTC","k":{"t":1599828780000,"T":1599828839999,"s":"ETHBTC","i":"1m","f":193235180,"L":193235214,"o":"0.03551300","c":"0.03551700","h":"0.03551800","l":"0.03551300","v":"21.52300000","n":35,"x":false,"q":"0.76437246","V":"11.53400000","Q":"0.40962829","B":"0"}}
< {"e":"kline","E":1599828806303,"s":"BNBBTC","k":{"t":1599828780000,"T":1599828839999,"s":"BNBBTC","i":"1m","f":87599856,"L":87599938,"o":"0.00229400","c":"0.00229620","h":"0.00229710","l":"0.00229400","v":"420.34000000","n":83,"x":false,"q":"0.96497998","V":"406.63000000","Q":"0.93351653","B":"0"}}

关于python - 如何在一个请求中从 Binance API 获取所有(或多个)对的历史 klines?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63515267/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com