0

目前我有一个非常手动的过程来从 Binance 返回一对历史烛台数据,例如 AVAXUSDT

我希望能够从 Binance 获取以 USDT(或其他)为基础货币的多对组合的烛台数据,例如 AVAXUSDT、LUNAUSDT、ADAUSDT 等。

这是我返回单对的脚本的一部分

candlesticks = client.get_historical_klines("AVAXUSDT", Client.KLINE_INTERVAL_12HOUR, "1 Jan, 2019", "18 Sep, 2021")

我怎样才能得到很多对?

4

1 回答 1

1

一个可能的解决方案:

import binance
import time

# tqdm not necessary, is only used for showing progress during klines iteration
from tqdm import tqdm

def multiple_klines(quote_currency: str) -> dict:
    klines = {}

    # You don't need API key/secret for this type of requets
    client = binance.Client()

    # Extract trading pairs from exchange information
    exchange_info = client.get_exchange_info()
    symbols = [x['symbol'] for x in exchange_info['symbols']]

    # Filter list
    selected_symbols = list(filter(lambda x: x.endswith(quote_currency), symbols))

    # Iterate over filtered list of trading pairs
    for symbol in tqdm(selected_symbols):
        klines[symbol] = client.get_historical_klines(
            symbol, client.KLINE_INTERVAL_12HOUR,
            "1 Jan, 2019", "18 Sep, 2021"
        )

        # Prevent exceeding rate limit:
        if int(client.response.headers['x-mbx-used-weight-1m']) > 1_000:
            print('Pausing for 30 seconds...')
            time.sleep(30)

    return klines

klines_dict = multiple_klines('USDT')

笔记:

  • 此类调用无需身份验证(API 密钥/秘密)。但请注意速率限制(每分钟允许调用 API)。
  • 您为 klines 调用选择的时间跨度和间隔不会包含从开始到结束的所有数据,因为API仅返回 500 个“行”(默认情况下,最多 1000 个)。您选择的时间跨度是 992 天,因此有 1984 个“行”,间隔为 12 小时。
  • 返回dict包含交易对(例如BTCUSDT)作为键和 klines 数据作为值的 a。
  • Klines 数据返回为nested list. 您可以在此处找到文档。
于 2021-10-05T14:19:24.600 回答