繁体   English   中英

Python:当前蜡烛完成时从币安获取数据 -

[英]Python: Get data from Binance when the current candle is finished -

我想在蜡烛完成 5 分钟后从 binance 中提取数据,无论我什么时候运行应用程序,我都尝试了以下方法,但每秒都在拉数据,我也想过要睡 5 分钟但我认为这会导致数据不兼容。

while True:         
      try:
          for ticker in symbols:
               candles = live.getKlines(Client, symbol=ticker, length=48)
               print(candles)
                   

def getKlines(Client, symbol, length):
    headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36',
               "Connection": "keep-alive",
               "Accept-Encoding": "*"
               }
    payload = {'symbol': symbol, 'interval': '5m', 'limit': '200'}
    result = request.get(
        "https://api.binance.com/api/v3/klines?", headers=headers, params=payload)
    candles = pd.DataFrame(eval(result.content))
    candles.columns = ['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Close_Time',
                       'Quote_Volume', 'Trades_Count', 'BUY_VOL', 'BUY_VOL_VAL', 'x']    
    candles.insert(loc=1, column="Symbol", value=symbol)  
    candles["Date"] = pd.to_datetime(candles["Date"], unit="ms")
    candles["Close"] = candles["Close"].astype(float)
    candles["Volume"] = candles["Volume"].astype(float)

    return candles

我也是 xD 我正在谷歌上搜索完全相同的问题的答案^^希望有人能帮助我们^^

如果你仍然需要它,我创建了这个方法:

def get_seconds_to_close(interval):
    candle = get_values_of_current_candle("BTC",interval) ## Here u should use your own method to get the values of the last/current candle (coin doesnt matter here)
    timestamp = candle["time"] ## We use the current candle to get the timestamp aka. opening time of the candle
    if interval=="1m":
        seconds = 60
    elif interval=="5m":
        seconds = 300
    elif interval=="1h":
        seconds = 3600
    elif interval == "4h":
        seconds = 14400
    elif interval == "1d":
        seconds = 24 * 3600
    else:
        print("Interval error!")
        quit()

    current_time = time.time()
    needed_timestamp = timestamp+seconds
    seconds_left = needed_timestamp-current_time
    return seconds_left

它将返回给定时间范围内新蜡烛打开的剩余秒数(在您的情况下为“5m”)。 您可以在循环中将此方法与 time.sleep(seconds_left) 一起使用,以根据您的时间范围在每次打开新蜡烛时提取数据。

您可能想看看 Websocket API 文档。 这是您可以在每 5 分钟蜡烛关闭时收到最新更新(例如,将 websocket 的间隔设置为 5m: https://binance-docs.ZBF215181B5140522135/en457B3D4F6B73.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM