【问题标题】:We are now able to retrieve 1 second historical klines for cryptocurrencies我们现在能够检索加密货币的 1 秒历史 klines
【发布时间】:2022-11-13 07:03:17
【问题描述】:

检索加密货币的免费历史 klines(开盘高低收盘数据)的最小粒度曾是1分钟。

感谢币安,我们现在可以获得 1 秒粒度的历史数据!

【问题讨论】:

    标签: python binance cryptocurrency ohlcv


    【解决方案1】:

    如何获取数据:

    没有币安 REST API:

    from io import BytesIO
    from zipfile import ZipFile
    from urllib.request import urlopen
    
    
    def download_zip(
        coin_pair: str = "BTCUSDT",    # trading pair 
        date: str = "2022-11-11",      # desired day (one can also download a whole month, replace 'daily' with 'monthly' in the url)
        save_to_path: str = r"./tmp"   # path where to save the .csv file
    ):
        url = fr"https://data.binance.vision/data/spot/daily/klines/{coin_pair}/1s/{coin_pair}-1s-{date}.zip"
        with urlopen(url) as zipresp:
            with ZipFile(BytesIO(zipresp.read())) as zfile:
                zfile.extractall(save_to_path)
        
    

    这将从特定日期将所需硬币对的 .csv 文件下载到指定文件夹。

    如果愿意,我们现在可以将文件进一步处理为 pandas.DataFrame,并且只保留日期时间、开盘价、高价、低价、收盘价和成交量列:

    import os
    import pickle
    import numpy as np
    import pandas as pd
    import datetime as dt
    
    
    def process_files(
        path: str = r"./tmp",  # path to where we downloaded the .csv files
    ):
        files = [f for f in os.listdir(path) if f[-4:] == ".csv"]
        for file in files:
            file_path = path + "\" + file
            df = pd.read_csv(file_path, header=None, names=['dateTime', 'open', 'high', 'low', 'close', 'volume', 'closeTime', 'quoteAssetVolume', 'numberOfTrades', 'takerBuyBaseVol', 'takerBuyQuoteVol', 'ignore'])
            df = df[['dateTime', 'open', 'high', 'low', 'close', 'volume']]  # use this line if you are only interested in the ohlcv-data
            df['dateTime'] = df['dateTime'].apply(lambda t: dt.datetime.fromtimestamp(t / 1000.0))
            df.open = df.open.astype(np.float64)
            df.high = df.high.astype(np.float64)
            df.low = df.low.astype(np.float64)
            df.close = df.close.astype(np.float64)
            df.volume = df.volume.astype(np.float64)
            symbol = file.split("-")[0]  # use this and
            df['symbol'] = symbol        # this line, if you want to add a column with the trading pair
            df.set_index('dateTime', inplace=True)
            
            save_path = file_path.replace(".csv", "")
            with open(save_path, "wb") as fd:
                pickle.dump(df, fd)
        # remove the .csv files as they are no longer needed
            
    

    使用币安 REST API

    到目前为止,币安还没有更新他们的 REST API 来检索 1 秒的历史 klines,至少我不能正确地做到这一点。这部分帖子将在得到答案的帮助后更新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-13
      • 2022-08-19
      • 2022-06-23
      • 2021-01-30
      • 2017-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多