首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Financial Modeling Prep (Python)以指定的时间间隔访问所有历史密码数据

使用Financial Modeling Prep (Python)以指定的时间间隔访问所有历史密码数据
EN

Stack Overflow用户
提问于 2021-01-19 03:33:31
回答 1查看 1.4K关注 0票数 1

Financial Modeling Prep是一个免费的API,可用于访问各种财务指标,如股票价格和加密货币数据。API文档概述了如何通过Python等编程语言访问数据。特别是,对于加密货币数据:

https://financialmodelingprep.com/developer/docs/#Historical-Cryptocurrencies-Price

只需生成一个唯一的API密钥,就可以通过调用URL来访问数据。URL的内容被接收并解析为JSON,然后在Python中作为对象返回。例如,我可以访问比特币的所有历史数据(价格、成交量、低、高等):

代码语言:javascript
复制
try:
# For Python 3.0 and later
    from urllib.request import urlopen
except ImportError:
# Fall back to Python 2's urllib2
    from urllib2 import urlopen

import json

def get_jsonparsed_data(url):

    response = urlopen(url)
    data = response.read().decode("utf-8")
    return json.loads(data)

url = ("https://financialmodelingprep.com/api/v3/historical-price-full/crypto/BTCUSD?apikey=myKey")

myData = get_jsonparsed_data(url)

默认情况下,使用此URL调用时,对象包含所有BTC数据(截至1/18/21,为1828天),时间间隔为1天。例如,使用Spyder变量资源管理器:

但是,我想将我的时间分辨率提高到4小时。API文档提供了一些关于如何做到这一点的见解-只需将url更改为以下内容:

代码语言:javascript
复制
url = ("https://financialmodelingprep.com/api/v3/historical-chart/4hour/BTCUSD?apikey=myKey")

结果是每4小时采样一次的BTC数据。然而,只有200个数据点,限制了历史数据的范围:

在查看文档后,不清楚如何指定4小时间隔以及所有历史数据(因此我得到了6*1828 = 10968个数据点)。如何获取感兴趣的时间间隔内的所有数据?

EN

回答 1

Stack Overflow用户

发布于 2021-03-24 08:59:42

我知道这不是你想要的解决方案,但这里有另一种方法,你可以从coinmarketcap.com获得历史加密价格,而不使用API:

代码语言:javascript
复制
# use urllib to get HTML data
url = "https://coinmarketcap.com/historical/20201206/"
contents = urllib.request.urlopen(url)
bytes_str = contents.read()

# decode bytes string
data_str = bytes_str.decode("utf-8")

# crop the raw JSON string out of the website HTML
start_str = '"listingHistorical":{"data":'
start = data_str.find(start_str)+len(start_str)
end = data_str.find(',"page":1,"sort":""')
cropped_str = data_str[start:end]

# create a Python list from JSON string
data_list = json.loads(cropped_str)
print ("total cryptos:", len(data_list))

# iterate over the list of crypto dicts
for i, item in enumerate(data_list):

    # pretty print all cryptos with a high rank
    if item["cmc_rank"] < 30:
        print (json.dumps(item, indent=4))

要从另一个日期获取不同的数据,只需将URL中的20201206部分替换为首选日期(例如,使用20210110代替2021年1月10日)。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65780978

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档