Accessing all historical crypto data with specified time interval using Financial Modeling Prep (Python)
Asked Answered
T

1

2

Financial Modeling Prep is a free API that can be used to access various financial metrics such as stock prices and cryptocurrency data. The API documentation outlines how to access data through programming languages like Python. In particular, for cryptocurrency data:

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

One simply needs to generate a unique API key, and data can be accessed by calling a URL. The content of the URL is received and parsed as a JSON, and returned as an object in Python. For example, I can access all historical data of Bitcoin (price, volume, low, high, etc.):

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)

By default, with this URL call the object contains all BTC data (1828 days worth, as of 1/18/21), with a time interval of 1 day. For example, using the Spyder variable explorer:

enter image description here

However, I want to improve my time resolution to 4 hours. The API documentation gives some insight on how to do this - just change url to the following:

url = ("https://financialmodelingprep.com/api/v3/historical-chart/4hour/BTCUSD?apikey=myKey")

The result is data for BTC sampled very 4 hours. However, there are only 200 data points, limiting the range of historical data:

enter image description here

After reviewing the documentation, it is unclear how to specify both the 4 hour interval as well as all historical data (so I'd get 6*1828 = 10968 data points). How can I get all data at the time interval of interest?

Twandatwang answered 18/1, 2021 at 19:33 Comment(0)
M
4

I know this isn't the exact solution you were looking for, but here's another way you can get historical crypto prices from coinmarketcap.com without using an API.

Sept 2022 Update:

This no longer works getting their most recent data, but the updated code should still work to get historical data from 2021 and earlier. Look for the cmcRank key (in camelCase) instead of cmc_rank like before:

import urllib.request, json

# use urllib to get HTML data
url = "https://coinmarketcap.com/historical/20200829/"
contents = urllib.request.urlopen(url)
bytes_str = contents.read()

# decode bytes string
data_str = bytes_str.decode("utf-8")
data_str = data_str.replace('\n', '')
data_str = data_str.replace('\\', '')

# 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["cmcRank"] < 5:
        print(f"\nsymbol: '{item['symbol']}' - name: {item['name']}")
        print (json.dumps(item, indent=4))

To get different data from another date just replace the 20200829 part in the URL with a preferred date (e.g. use 20210110 instead for Jan 10th, 2021).

Moule answered 24/3, 2021 at 0:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.