【问题标题】:Python: how to get the price of a crypto at a given time in the pastPython:如何在过去给定时间获取加密货币的价格
【发布时间】:2022-01-15 23:06:43
【问题描述】:

我有什么方法可以使用ccxt 提取过去给定时间的加密货币价格?

例子:在2018-01-24 11:20:01时获取币安上的BTC价格

【问题讨论】:

    标签: python binance ccxt


    【解决方案1】:

    你可以在CCXT的binance类上使用fetch_ohlcv方法

    def fetch_ohlcv(self, symbol, timeframe='1m', since=None, limit=None, params={}):


    您需要将日期作为以毫秒为单位的时间戳,并且您只能精确到分钟,因此请去掉秒数,否则您将获得下一分钟的价格

    timestamp = int(datetime.datetime.strptime("2018-01-24 11:20:00", "%Y-%m-%d %H:%M:%S").timestamp() * 1000)
    

    您只能获取BTC与其他货币的价格对比,我们将使用USDT(与美元紧密匹配)作为我们的比较货币,因此我们将在BTC/USDT市场查找BTC的价格

    当我们使用该方法时,我们会将since设置为您的时间戳,但将限制设置为1,这样我们只能得到一个价格

    import ccxt
    from pprint import pprint
    
    print('CCXT Version:', ccxt.__version__)
    
    exchange = ccxt.binance({
        'enableRateLimit': True,
        "apiKey": '...',
        "secret": '...',
    })
    timestamp = int(datetime.datetime.strptime("2018-01-24 11:20:00", "%Y-%m-%d %H:%M:%S").timestamp() * 1000)
    response = exchange.fetch_ohlcv('BTC/USDT', '1m', timestamp, 1)
    pprint(response)
    

    这将返回一根蜡烛的烛台值

    [ 
      1516792860000, // timestamp
      11110, // value at beginning of minute, so the value at exactly "2018-01-24 11:20:01"
      11110.29, // highest value between "2018-01-24 11:20:01" and "2018-01-24 11:20:02"
      11050.91, // lowest value between "2018-01-24 11:20:01" and "2018-01-24 11:20:02"
      11052.27, // value just before "2018-01-24 11:20:02"
      39.882601 // The volume traded during this minute
    ]
    

    【讨论】:

      【解决方案2】:

      【讨论】:

        猜你喜欢
        • 2015-04-28
        • 1970-01-01
        • 2021-08-04
        • 2021-04-08
        • 2023-01-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-15
        相关资源
        最近更新 更多