You can do various things with cryptocurrency price data, such as creating your own forecasting models, illustrating historical prices, or performing chart analysis. But first, you need to get hold of the data. Nowadays, several APIs provide access to cryptocurrency price data. One of the most important and trusted sources is Coinmarketcap.com. The website offers multiple API endpoints that provide access to historical price data for various cryptocurrencies like Bitcoin, Ethereum, etc. In addition, Coinmarketap offers meta information about coins and crypto exchanges including, listing and halving dates, circulating supply, or the current market cap. This tutorial gives an overview of the coinmarketcap API and shows how to use the API with Python. We will request historical price data and store them in a local SQLite database using the Pewee SQL framework.
The rest of this article is a Python hands-on tutorial: First, we lay the groundwork for working with the Coinmarketcap API by signing up for a free API key. Subsequently, we store this in a YAML file and use it to query bitcoin quotes from the Coinmarketcap API endpoint. Our goal is to store the data with Peewee in an SQLite DB. We will first define a schema to store the data. Finally, we will learn how to query the obtained rates from our local database and use them later, for example, to train a price prediction model.
The Coinmarketcap API
CoinMarketCap is one of the world’s most referenced crypto asset price tracking websites. The website provides high-quality and accurate information to private and business users so they can draw their own informed conclusions. In April 2020, CoinMarketCap was acquired by Binance – the largest cryptocurrency exchange in the world.
Generally, all relevant coins are listed on Coinmarketcap – however, some are scam coins and coins whose further development was abandoned long ago. The CoinMarketCap API is a set of powerful RESTful JSON endpoints. According to the website, these APIs are designed to meet the mission-critical needs of application developers, data scientists, and enterprise platforms.
Let’s look at some of the available REST endpoints.
Endpoints
Coinmarketcap offers several API endpoints for various crypto-related data, including prices, exchanges, and other information.
A big advantage of the Coinmarketcap API is that the data goes way back. For example, the historical price data for Bitcoin ranges back to 2013.
Acquiring an API Key from the Developer Portal
All requests to the coinmarketcap API require authentication with an API key. Authentication can be achieved in two ways:
Send the API key in the custom header of the HTTP request with the attribute ‘X-CMC_PRO_API_KEY’ (Prefered).
Provide the key in the URL path.
Before requesting data from the coinmarketcap API, you must acquire your API key by registering on the coinmarketcap API developer portal. You will choose from one of several pricing plans during the registration process. The Free basic plan provides access to most data and is sufficient for this tutorial. However, it has severe limitations, such as a limited number of requests you can make daily and monthly. After the registration, you can log in to your account and display the API key in the “Overview” section.
Each call to the Coinmarketcap API consumes API credits. The number of calls and data we can retrieve via API is limited to 100 credits for the free plan. You can see how many of your daily and monthly credits are still available on the overview page.
It would be best never to store an API key directly in the code for security reasons. A better practice is to import and access the API key from a separate YAML file. Create the file with the name “api_config_coinmarketcap.yml” and insert your API key into this file as follow:
api_key: “your coinmarketcap api key”
Obtaining Bitcoin Prices via the Coinmarketcap API
In the following, we write some Python code to send requests to the Coinmarketcap API at regular intervals. Our API requests obtain cryptocurrency prices for the number one in the market capitalization ranking. Each request to the Coinmarketcap API consumes credits. Therefore, we aim to store the price data in a local SQLite database. We can then later retrieve it from there without consuming credits. To manage the SQLite DB, we use the Pewee framework.
Before beginning the coding part, please ensure you have set up your Python 3 environment and required packages. If you don’t have a Python environment available yet, you can followthe steps in this articleto set up the Anaconda Python environment.
In addition, we will usePeewee, a lightweight Object-Relational-Mapper (ORM) framework that lets us interact with SQLite in an object-oriented and more convenient way.
You can install packages using console commands:
pip install <package name>
conda install <package name>(if you are using the anaconda packet manager)
Step #1 Create a Relational Model in SQLite
We begin by defining a relational data model in SQlite using Pewee. We will then use this model to persist the crypto price data from the API and make them available for later reuse. Our model will contain two tables:
Run: A table where we will store an ID and a timestamp makes identifying past runs easier. We start querying data from the API, we will add a new runid to this table.
The second table is called price and contains the price quotes for a predetermined interval. We will store the price quotes in this table and reference the runid via a foreign key.
Running the code below creates empty tables.
from peewee import *from datetime import date, timedelta, datetimeimport numpy as np import pandas as pdimport seaborn as snsimport timeimport jsonimport yamlfrom requests import Request, Sessionfrom requests.exceptions import ConnectionError, Timeout, TooManyRedirectsfrom pandas.plotting import register_matplotlib_convertersimport matplotlib.dates as mdatesimport matplotlib.pyplot as plt# persist informationdb = SqliteDatabase('assets.db')class BaseModel(Model): class Meta: database = dbclass Run(BaseModel): # This model uses the "assets.db" database. timestamp = DateTimeField(unique=True) symbol = CharField() id = AutoField()class Price(BaseModel): # This model uses the "assets.db" database. datetime = DateTimeField(unique=True) volume_24 = FloatField() price = FloatField() runid = ForeignKeyField(Run, backref='prices') # By default, Peewee includes an IF NOT EXISTS clause when creating tables. def create_tables(): with db: db.create_tables([Price]) db.create_tables([Run])def drop_tables(): with db: db.drop_tables([Price]) db.drop_tables([Run]) create_tables()#drop_tables()
If you want to drop the tables again, you can call our custom drop_tables function.
Step #2 Streaming Price Quotes
Now that we have created the relational data model, we can define a request to the Coinmarketcap API and initiate the stream of price data. For this purpose, we will call the coinmarketcap REST endpoint “cryptocurrency/listings/latest.” This endpoint returns a JSON response that contains the latest price data for a specific number of cryptocurrencies based on their market capitalization rank. The response contains the following information for each cryptocurrency included:
Price value
The timestamp
The trading volume
The cost of the request in credits
Before querying the API, we need to define basic API parameters:
Conversion_Currency: The currency in which the API will return the prices.
Start: The position in the market cap ranking from which the API will include cryptocurrencies in the result.
Limit: The number of cryptocurrencies for which the API will return prices based on the start position in the current market cap ranking.
We will select USD as “conversion_currency,” set the limit, and start to “one.” As a result, the API will return only the price of Bitcoin. We use a query interval of 10 seconds. You can change the interval, but be aware that you will deplete your free credits quickly if you request data by the second. We will therefore limit the requests to 200. You can increase the timeframe to retrieve price quotes for more extended periods.
We will store and load our API key from a YAML file. Be sure to replace the api_key_path in the code below with the path where you placed the api_config_coinmarketcap.yml file. Once you have made this adjustment, you can start the API requests by running the code below.
symbol='BTCUSD'query_interval = 10 # in secondsquery_number = 100 # the number of queries after which the API stops# load the API key from our local filewith open(r'API Keys/api_config_coinmarketcap.yml') as file: apikey = yaml.load(file, Loader=yaml.FullLoader)['api_key']# Define some essential API parameters# Coinmarketcap API for latest market ticker quotes and averages for cryptocurrencies and exchanges.# https://coinmarketcap.com/api/documentation/v1/#operation/getV1CryptocurrencyListingsLatesturl = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'parameters = { 'start':'1', 'limit':'1', 'convert':'USD'}headers = { 'Accepts': 'application/json', 'X-CMC_PRO_API_KEY': apikey,}session = Session()session.headers.update(headers)# create a new run and save it to our local SQLite DBrun_timestamp = datetime.now()run = Run(symbol=symbol, timestamp = run_timestamp)run.save()current_run_id = run.id print(f'run_id: {current_run_id} - timestamp: {run_timestamp} - interval: {query_interval} - number of queries: {query_number}')# query the coinmarketcap API every x secondsfor s in range(0, query_number): try: response = session.get(url, params=parameters) data = json.loads(response.text) #print(data) # response - quote data_quote = data['data'][0]['quote']['USD'] price = np.round(data_quote['price'], 1) # the price volume_24 = np.round(data_quote['volume_24h'], 1) # the volume in the last 24 hours # response - status data_status = data['status'] api_timestamp = data_status['timestamp'] # the timestamp of the pricepoint api_credits = data_status['credit_count'] # the number of credits spent on the last request # create a new pricepoint and save it to the SQLite db new_pricepoint = Price(datetime=api_timestamp, price=price, volume_24=volume_24, runid=current_run_id) id = new_pricepoint.save() # display what we have just saved print(f'request number: {s} - added {price} at {api_timestamp} - 24 hour volume: {volume_24} - credits used: {api_credits}') except (ConnectionError, Timeout, TooManyRedirects) as e: print(e) time.sleep(query_interval)print('finished')
Step #3 Querying the Data from Our SQL Table
We now have the price data persisted in our local SQLite DB. From there, we can query the data using the peewee “select” SQL command. For example, we will query the run table and retrieve the number of requests made to the coinmarketcap API.
Next, select the price quotes for this specific run-id and display them in a line plot.
query = Price.select().where(Price.runid==current_run_id)df_prices = pd.DataFrame(list(query.dicts()))print(df_prices)register_matplotlib_converters()fig, ax = plt.subplots(figsize=(10, 8))plt.title('BTC prices from the coinmarketcap API', fontsize=14)datetimes = pd.to_datetime(df_prices['datetime'])sns.lineplot(data=df_prices, x=datetimes, y="price")#ax.set_xlim([df_prices['datetime'].min(),df_prices['datetime'].max()])#ax.xaxis.set_major_locator(mdates.MinuteLocator())plt.xticks(rotation=75)
And voila, the line plot shows the last data received from the API. We now have stored this data in our SQLite database.
Summary
This article gave a quick overview of the Coinmarketcap API – one of the most referenced APIs in the crypto space. You have learned to request historical price data from a Coinmarketcap API endpoint with Python. We wrote a short Python script that queries one of several REST endpoints (cryptocurrency/listings/latest) in regular intervals. To ensure that we can later reuse the requested data, we stored the response from the API in an SQLite database using the Pewee framework.
I am always happy to receive feedback from my readers. So if the tutorial helped you, or if you have any comments, please write them in the comments.
Sources and Further Reading
If you are not sure yet, if the coinmarketcap API is for you, consider these other ways of obtaining cryptocurrency price data:
Working with the Coinbase API
Using the Yahoo Finance API to obtain Cryptocurrency Price Data
The links above to Amazon are affiliate links. By buying through these links, you support the Relataly.com blog and help to cover the hosting costs. Using the links does not affect the price.
For more insights on stock-market prediction, check out the following Python tutorials:
Stock Market Prediction using Multivariate Time Series and Recurrent Neural Networks in Python
Stock-Market prediction using Neural Networks for Multi-Output Regression in Python
Stock Market Prediction using Univariate Time Series Models based on Recurrent Neural Networks with Python
Stock Market Prediction – Adjusting Time Series Prediction Intervals in Python
Feature Engineering for Multivariate Time Series Prediction with Python
Building a Twitter Bot for Crypto Trading Signals using Python and Gate.io
Florian Follonier
Hi, I am Florian, a Zurich-based consultant for AI and Data. Since the completion of my Ph.D. in 2017, I have been working on the design and implementation of ML use cases in the Swiss financial sector. I started this blog in 2020 with the goal in mind to share my experiences and create a place where you can find key concepts of machine learning and materials that will allow you to kick-start your own Python projects.
This will open a From Web setup box. In the HTTP request header parameters(optional) second box enter your API Key and press OK. Excel will connect to the API and open up the Power Query window. The data is returned in JSON format and we must now extract the data.
A:CoinMarketCap is committed to always providing the crypto community with a robust free API through our free Basic tier. Even on free Basic our users can benefits from enterprise grade infrastructure, documentation, and flexibility.
CoinMarketCap is commonly known to be the go-to for checking cryptocurrencies and token prices. CoinMarketCap provides API levels for individual users and businesses. The free plan has limits on the number of API calls you can make per month.
If you are an investor, web scraping CoinMarketCap can be your first step towards success. Web scraping automates the process of extracting data from CoinMarketCap and stores the data in a structured format for further analysis. It allows you to keep your head in the game and stay ahead in the market.
Simply run $ python scrape.py to scrape all currencies on coinmarketcap. Since the list grows longer everyday, the number of scraped currencies can be limited by specifying a minimum market cap, e.g.: $ python scrape.py 1000000 .
On Coinmarketcap all prices are calculated by the volume-weighted average of all the prices from different exchanges. Bear in mind that it is important to monitor the circulating supply of a cryptocurrency - not the total supply.
Getting live Cryptocurrency data in Excel is very easy: you have to install our Add-in and then request from among the crypto symbols we offer. You can read the full excel tutorial to get started.
That's because they are not looking at the same data. CoinGecko, which is headquartered in Singapore, aggregates price data from 529 crypto exchanges while U.S.-based CoinMarketcap collects data from 438 crypto exchanges, according to each company at publishing time.
Yes, it is accurate in a sense because CoinMarketCap is the world's most-referenced price-tracking website for cryptoassets, and is used by tens of millions of people to great effect.
Cryptocurrency portfolio tracker allows you to track the total amount and value of your cryptocurrencies across all wallets, exchanges, platforms, and blockchains in real-time. ... List of Top Crypto Portfolio Tracker Apps
Scrape crypto websites and retrieve data on various coins and IPOs. Collect data such as: current rate, market cap, 24H change, week high and low, returns, all time high, total supply, max supply, and more.
If a user successfully completes the quiz, they will be rewarded for their newfound knowledge with 1inch. This CoinMarketCap Earn campaign has a reward pool of 50,000 1INCH tokens. Successful users will receive 3 1INCH (about $5) per user until the rewards pool runs out.
Binance API is considered to be one of the best crypto-to-crypto exchanges. The Binance API is the digital platform that supports many popular cryptocurrencies like Dogecoin, Harmony, Ethereum, Bitcoin, and others.
Even though it's completely legal to scrape publicly available data, there are two types of information that you should be cautious about. These are: Copyrighted data. Personal information.
Web scraping is completely legal if you scrape data publicly available on the internet. But some kinds of data are protected by international regulations, so be careful scraping personal data, intellectual property, or confidential data.
In its second ruling on Monday, the Ninth Circuit reaffirmed its original decision and found that scraping data that is publicly accessible on the internet is not a violation of the Computer Fraud and Abuse Act, or CFAA, which governs what constitutes computer hacking under U.S. law.
Step 1: Setup the CoinMarketCap Web Request in Excel
Under the Data tab in the ribbon, select Get Data > From Other Sources > From Web. Next, we need to input the URL we want to use and setup our API key as a header item to authenticate with CoinMarketCap. Make sure the Advanced radio button is selected.
Well, they are! Listings are always free. You can apply here to add your cryptoasset to CoinMarketCap. CoinMarketCap also does not sanction any service that assists in listing any cryptoasset project or exchange.
There are more than 100 alternatives to CoinMarketCap, not only websites but also apps for a variety of platforms, including Android, iPhone, Windows and Mac. The best alternative is Cryptowatch, which is free. Other great sites and apps similar to CoinMarketCap are Coingecko, FTX, Good Crypto and wallmine.
CoinMarketCap reports on the trading activities of thousands of markets but does not directly sell any cryptocurrency. The best way to find where to buy is by looking on the markets section for the cryptocurrency. For example, to find where to buy Bitcoin, you can look at the markets section for Bitcoin.
There is no ownership relationship between CoinMarketCap and Binance.com. Binance is the global blockchain company behind the world's largest digital asset exchange by trading volume and users, serving a greater mission to accelerate cryptocurrency adoption with 25+ products, projects and initiatives.
DIA will initially provide the top 50 crypto assets and the top 25 DeFi assets. In its standard format. DIA provides the option to customise the setup by increasing the data frequency as well as the breadth and depth of asset data as required by any specific use case.
It is because the higher the market cap of a cryptocurrency, the more dominant it is considered to be in the market. Cryptocurrency market capitalization is an indicator that measures the total value of a cryptocurrency. The stock market cap is calculated by multiplying the share price by times the shares outstanding.
TradingView. TradingView is by far the most popular charting and technical analysis tool for traders of all markets. In recent years, they have pushed to integrate their tool set with the most popular cryptocurrency exchanges and the results are impressive.
CoinMarketCap is the more popular website out of the two. It has a lot more traffic than CoinGecko and is generally considered to be the more authoritative site. However, CoinGecko has a number of features that make it superior to CoinMarketCap.
CoinGecko offers a cleaner user interface and it is not owned by a centralized cryptocurrency exchange. CoinMarketCap is owned by the group in charge of the Binance exchange. Though it is a bit more cluttered, CoinMarketCap offers more short-form content and links to videos about trending topics.
CoinMarketCap. CoinMarketCap is one of the world's most-trusted cryptocurrency price-trackers. The company also offers a portfolio tracking product to investors. Though the platform is limited in its functionality, it can be a good option for traders on a budget.
While some users report a positive experience using CoinMarketCap, according to many of the 286 customer reviews on trustpilot.com which only averaged 1.4 out of 5 stars, CoinMarketCap is seen as one of the least trustworthy crypto listing services in the industry.
Coinbase has the second-highest exchange rank on CoinMarketCap — second only to the behemoth Binance. It is also in the top 10 in terms of liquidity, boasting $462 billion in quarterly trading volume as of the second quarter of 2021 — about 16.5 times greater than what it was a year earlier.
Unlike screen scrapers, our no-code platform exports data directly from CoinMarketCap's Official API so you can download extracted data seamlessly. This means no breaking code, getting blocked, overpriced proxies or incorrect data.
Go to https://www.coingecko.com/en/api and scroll about halfway down. There, you'll see the types of requests you can make. Since my application needs to bring in some data from CoinGecko, I want to make a GET request. That's exactly what CoinGecko enables.
The data will be downloaded via an API from coinmarketcap.com. Once you've set up the API, it becomes a breeze to pull crypto prices and data into Excel, in just a matter of seconds.
The CoinMarketCap API is a suite of high-performance RESTful JSON endpoints that are specifically designed to meet the mission-critical demands of application developers, data scientists, and enterprise business platforms.
Binance API is a method that allows you to connect to the Binance servers using several programming languages. With it, you can automate your trading and make HTTP requests to send and receive data. Here we access Binance API using Python with requests module.
CoinGecko provides a fundamental analysis of the crypto market. In addition to tracking price, volume and market capitalisation, CoinGecko tracks community growth, open-source code development, major events and on-chain metrics.
You can build an API and then sell it. For example, the first 1000 requests made each month might be free. However, for each additional request after that, you can charge something like $0.001. Therefore, using APIs allows you to save time, money, and resources while also allowing you to monetize your work!
An API app usually costs $5,199 to build. However, the total cost can be as low as $2,600 or as high as $7,799. An API app with a low number of features (also known as a "minimum viable product", or MVP) will be more affordable than an app that includes all intended functionality.
Introduction: My name is Wyatt Volkman LLD, I am a handsome, rich, comfortable, lively, zealous, graceful, gifted person who loves writing and wants to share my knowledge and understanding with you.
We notice you're using an ad blocker
Without advertising income, we can't keep making this site awesome for you.