Hi there. In this page, I go over working with Coinmarketcap cryptocurrency data in R. The main library is coinmarketcapr
. For installation you use install.packages("coinmarketcapr")
.
Documentation/References:
To be able to access the Coinmarketcap API, you do need an API key. Registration to Coinmarketcap is free if you choose the basic free subscription. The pricing link is here.
Under the free subscription you do not get full features so some of the functions presented here will not work. Paying more per month will allow for more access to more market data endpoints and for commercial use.
Once an API key is obtained, start with loading the coinmarketcapr
package and using setup()
with the API key.
# Load packages:
library(coinmarketcapr)
# Setup:
setup(api_key = "<api_key>")
This section deals with various functions in the coinmarketcapr
package. I wanted to explore this package.
Get API Info
# Obtain global market cap details into a dataframe:
# Obtaining API info
# Returns API Key details and usage stats, outputs dataframe with API key info.
get_api_info()
## plan_credit_limit_daily plan_credit_limit_daily_reset
## 1: 333 In 3 hours, 54 minutes
## plan_credit_limit_daily_reset_timestamp plan_credit_limit_monthly
## 1: 2021-08-07 10000
## plan_credit_limit_monthly_reset plan_credit_limit_monthly_reset_timestamp
## 1: In 25 days, 3 hours, 54 minutes 2021-09-01
## plan_rate_limit_minute usage_current_minute_requests_made
## 1: 30 0
## usage_current_minute_requests_left usage_current_day_credits_used
## 1: 30 9
## usage_current_day_credits_left usage_current_month_credits_used
## 1: 324 210
## usage_current_month_credits_left
## 1: 9790
Active Cryptos With Latest Market Data
# Obtaining active Cryptocurrencies with latest market data, currency is USD as default
# Output is a big dataframe.
crypto_listings <- get_crypto_listings(currency = "USD", latest = TRUE)
head(crypto_listings)
## id name symbol slug num_market_pairs date_added
## 1: 1 Bitcoin BTC bitcoin 8938 2013-04-28
## 2: 1027 Ethereum ETH ethereum 5640 2015-08-07
## 3: 825 Tether USDT tether 16451 2015-02-25
## 4: 1839 Binance Coin BNB binance-coin 538 2017-07-25
## 5: 2010 Cardano ADA cardano 283 2017-10-01
## 6: 52 XRP XRP xrp 627 2013-08-04
## tags
## 1: mineable,pow,sha-256,store-of-value,state-channels,coinbase-ventures-portfolio,...
## 2: mineable,pow,smart-contracts,ethereum,coinbase-ventures-portfolio,three-arrows-capital-portfolio,...
## 3: payments,stablecoin,stablecoin-asset-backed,avalanche-ecosystem,solana-ecosystem
## 4: marketplace,centralized-exchange,payments,binance-smart-chain,alameda-research-portfolio,multicoin-capital-portfolio
## 5: mineable,dpos,pos,platform,research,smart-contracts,...
## 6: medium-of-exchange,enterprise-solutions,binance-chain,arrington-xrp-capital,galaxy-digital-portfolio,a16z-portfolio,...
## max_supply circulating_supply total_supply cmc_rank last_updated
## 1: 2.10000e+07 18778193 18778193 1 2021-08-06 20:04:00
## 2: NA 116996930 116996930 2 2021-08-06 20:04:00
## 3: NA 62001013281 63468847060 3 2021-08-06 20:03:00
## 4: 1.68137e+08 168137036 168137036 4 2021-08-06 20:03:00
## 5: 4.50000e+10 32112443766 32959337588 5 2021-08-06 20:03:00
## 6: 1.00000e+11 46363275151 99990326429 6 2021-08-06 20:04:00
## platform_id platform_name platform_symbol platform_slug
## 1: NA <NA> <NA> <NA>
## 2: NA <NA> <NA> <NA>
## 3: 1027 Ethereum ETH ethereum
## 4: NA <NA> <NA> <NA>
## 5: NA <NA> <NA> <NA>
## 6: NA <NA> <NA> <NA>
## platform_token_address USD_price USD_volume_24h
## 1: <NA> 4.292174e+04 37962715007
## 2: <NA> 2.936934e+03 27054730531
## 3: 0xdac17f958d2ee523a2206206994597c13d831ec7 1.000166e+00 69002699960
## 4: <NA> 3.431555e+02 1580943461
## 5: <NA> 1.410246e+00 1864507561
## 6: <NA> 7.514224e-01 2461340210
## USD_percent_change_1h USD_percent_change_24h USD_percent_change_7d
## 1: 0.17564716 4.44514165 7.8248419
## 2: 0.58505076 4.60431927 23.3565910
## 3: -0.00072023 -0.01605977 -0.0153554
## 4: -0.73375983 1.51556217 8.9369488
## 5: -0.03516782 1.74553905 10.0795191
## 6: 0.48498386 2.80193874 1.3809539
## USD_percent_change_30d USD_percent_change_60d USD_percent_change_90d
## 1: 24.01043338 20.35837583 -27.25721803
## 2: 24.16916020 7.73737170 -23.06513775
## 3: 0.00764765 -0.03749172 0.02686356
## 4: 1.68572186 -11.71996224 -47.45190688
## 5: -0.43556725 -15.95053604 -12.66593211
## 6: 12.74767104 -19.19466608 -52.28860499
## USD_market_cap USD_last_updated
## 1: 805992716235 2021-08-06 20:04:00
## 2: 343612232405 2021-08-06 20:04:00
## 3: 62011332480 2021-08-06 20:03:00
## 4: 57697143145 2021-08-06 20:03:00
## 5: 45286455833 2021-08-06 20:03:00
## 6: 34838405264 2021-08-06 20:04:00
Crypto Map
It appears that the information from get_crypto_map()
is a subset from get_crypto_listings()
.
# get_crpyto_map() gets all active cryptos supported by platform incl unique id for each crypto
get_crypto_map()
## id name symbol slug rank is_active
## 1: 1 Bitcoin BTC bitcoin 1 1
## 2: 2 Litecoin LTC litecoin 15 1
## 3: 3 Namecoin NMC namecoin 686 1
## 4: 4 Terracoin TRC terracoin 1828 1
## 5: 5 Peercoin PPC peercoin 638 1
## ---
## 5943: 11173 BabySun BabySun babysun 2943 1
## 5944: 11174 Dirty Doge DDOGE dirty-doge 3073 1
## 5945: 11175 SafeVault VAULT safevault 3189 1
## 5946: 11178 Wrapped LUNA Token WLUNA wrapped-luna-token 2804 1
## 5947: 11180 Rewards Token REWARDS rewards-token 3180 1
## first_historical_data last_historical_data platform_id
## 1: 2013-04-28 18:47:00 2021-08-06 19:59:00 NA
## 2: 2013-04-28 18:47:00 2021-08-06 19:59:00 NA
## 3: 2013-04-28 18:47:00 2021-08-06 19:59:00 NA
## 4: 2013-04-28 18:47:00 2021-08-06 19:59:00 NA
## 5: 2013-04-28 18:47:00 2021-08-06 19:59:00 NA
## ---
## 5943: 2021-08-05 19:48:00 2021-08-06 19:58:00 1839
## 5944: 2021-08-05 23:15:00 2021-08-06 19:50:00 1839
## 5945: 2021-08-06 11:03:00 2021-08-06 19:58:00 1839
## 5946: 2021-08-06 16:00:00 2021-08-06 19:55:00 1027
## 5947: 2021-08-06 19:57:00 2021-08-06 19:57:00 1839
## platform_name platform_symbol platform_slug
## 1: <NA> <NA> <NA>
## 2: <NA> <NA> <NA>
## 3: <NA> <NA> <NA>
## 4: <NA> <NA> <NA>
## 5: <NA> <NA> <NA>
## ---
## 5943: Binance Smart Chain BNB binance-coin
## 5944: Binance Smart Chain BNB binance-coin
## 5945: Binance Smart Chain BNB binance-coin
## 5946: Ethereum ETH ethereum
## 5947: Binance Smart Chain BNB binance-coin
## platform_token_address
## 1: <NA>
## 2: <NA>
## 3: <NA>
## 4: <NA>
## 5: <NA>
## ---
## 5943: 0xca40882bec9B7e77401652E6d0bfeC9C168BB56B
## 5944: 0x37f963a1a358308a0daac0f8ebeb23ca18cf00b9
## 5945: 0xe2e6e66551E5062Acd56925B48bBa981696CcCC2
## 5946: 0xd2877702675e6ceb975b4a1dff9fb7baf4c91ea9
## 5947: 0x7B5B3BC6890c1dE78FeDE739C1b02D2e51B1b58c
Crypto Market Pairs
Using get_crypto_marketpairs()
did not work for me. It is supposed to get a list of active market pairs that Coinmarketcap has given a crypto. I tried get_crypto_marketpairs(slug = "bitcoin")
and got no results. I think it does not work under the free API subscription.
If you put BTC, you can get something like BTCUSDT, BTCETH and so on as market pairs.
Get Crypto Metadata
Using get_crypto_meta()
will retrieve static metadata for one or more cryptos. The description part for the crypto gives insights on what the crypto is about.
# Get Crypto Metadata:
# Bitcoin:
get_crypto_meta(symbol = "BTC")
## id name symbol category
## 1: 1 Bitcoin BTC coin
## description
## 1: Bitcoin (BTC) is a cryptocurrency . Users are able to generate BTC through the process of mining. Bitcoin has a current supply of 18,778,193. The last known price of Bitcoin is 42,941.26536699 USD and is up 5.23 over the last 24 hours. It is currently trading on 8938 active market(s) with $37,999,772,955.16 traded over the last 24 hours. More information can be found at https://bitcoin.org/.
## slug logo subreddit
## 1: bitcoin https://s2.coinmarketcap.com/static/img/coins/64x64/1.png bitcoin
## notice tags tag_names tag_groups _https___bitcoin_org__ platform
## 1: mineable Mineable OTHER https://bitcoin.org/ NA
## date_added twitter_username is_hidden
## 1: 2013-04-28 0
# DAG is Constellation
get_crypto_meta(symbol = "DAG")
## id name symbol category
## 1: 2868 Constellation DAG coin
## description
## 1: Constellation (DAG) is a cryptocurrency launched in 2018. Constellation has a current supply of 3,711,998,690.36 with 1,266,911,931.0440834 in circulation. The last known price of Constellation is 0.21477047 USD and is down -6.11 over the last 24 hours. It is currently trading on 9 active market(s) with $4,170,418.70 traded over the last 24 hours. More information can be found at https://www.constellationnetwork.io.
## slug logo
## 1: constellation https://s2.coinmarketcap.com/static/img/coins/64x64/2868.png
## subreddit notice tags tag_names tag_groups
## 1: constellation dag DAG CONSENSUS_ALGORITHM
## _https___www_constellationnetwork_io_ platform date_added twitter_username
## 1: https://www.constellationnetwork.io NA 2018-06-20 Conste11ation
## is_hidden
## 1: 0
Open, High, Low, Close, Volume (OHLCV)
I tried obtaining OHLCV price data on Bitcoin with get_crypto_ohlcv(currency = "USD", latest = TRUE, symbol = "BTC")
but it did not work.
I think you need the startup plan from Coinmarketcap. If access was granted I would use the price data to produce candlestick charts with plotly in R.
Get Crypto Quotes
Using get_crypto_quotes()
retrieves the latest/historical market quotes based on the crypto inputs.
# Get Crypto Quotes:
get_crypto_quotes(symbol = c("BTC", "ETH"))
## id name symbol slug num_market_pairs date_added tags
## 1: 1 Bitcoin BTC bitcoin 8938 2013-04-28 mineable
## 2: 1027 Ethereum ETH ethereum 5640 2015-08-07 mineable
## max_supply circulating_supply total_supply is_active platform cmc_rank
## 1: 21000000 18778193 18778193 1 NA 1
## 2: NA 116996930 116996930 1 NA 2
## is_fiat last_updated quote
## 1: 0 2021-08-06 20:04:00 <list[10]>
## 2: 0 2021-08-06 20:04:00 <list[10]>
Get Exchange Map
I am not entirely sure on this get_exchange_map()
. It did not work for me. In the documentation .pdf it mentions about returning a paginated list of all crypto exchanges by Coinmarketcap ID.
Get Exchange Meta
Using get_exchange_meta()
will return metadata for one or more crypto exchanges. Information includes launch date, official website, URL and social links. The function get_exchange_meta(slug = "binance")
did not work for me as I think it does not work for the free API service.
Get Global Marketcap
For extracting the global market cap of the market, use get_global_marketcap()
. The marketcap for a crypto is the total volume multiplied by the price. Global market cap would be the total marketcaps for all cryptos.
# Global marketcap:
get_global_marketcap(currency = "USD", latest = TRUE)
## active_cryptocurrencies total_cryptocurrencies active_market_pairs
## 1: 5961 11178 44830
## active_exchanges total_exchanges eth_dominance btc_dominance
## 1: 391 1434 19.65323 46.20175
## eth_dominance_yesterday btc_dominance_yesterday
## 1: 19.60705 45.81098
## eth_dominance_24h_percentage_change btc_dominance_24h_percentage_change
## 1: 0.04618065 0.3907733
## defi_volume_24h defi_volume_24h_reported defi_market_cap
## 1: 11476690878 11476690878 90651854245
## defi_24h_percentage_change stablecoin_volume_24h
## 1: -13.88324 79305501649
## stablecoin_volume_24h_reported stablecoin_market_cap
## 1: 79305501649 113712198551
## stablecoin_24h_percentage_change derivatives_volume_24h
## 1: 2.536051 1.615e+11
## derivatives_volume_24h_reported derivatives_24h_percentage_change
## 1: 1.615e+11 -11.19311
## USD_total_market_cap USD_total_volume_24h USD_total_volume_24h_reported
## 1: 1.743026e+12 99814443527 99814443527
## USD_altcoin_volume_24h USD_altcoin_volume_24h_reported
## 1: 61812295978 61812295978
## USD_altcoin_market_cap USD_defi_volume_24h USD_defi_volume_24h_reported
## 1: 937717316855 11476690878 11476690878
## USD_defi_24h_percentage_change USD_defi_market_cap USD_stablecoin_volume_24h
## 1: -13.88324 90651854245 79305501649
## USD_stablecoin_volume_24h_reported USD_stablecoin_24h_percentage_change
## 1: 79305501649 2.536051
## USD_stablecoin_market_cap USD_derivatives_volume_24h
## 1: 113712198551 1.615e+11
## USD_derivatives_volume_24h_reported USD_derivatives_24h_percentage_change
## 1: 1.615e+11 -11.19311
## USD_last_updated USD_total_market_cap_yesterday
## 1: 2021-08-06 20:04:11 1.666489e+12
## USD_total_volume_24h_yesterday
## 1: 98558407207
## USD_total_market_cap_yesterday_percentage_change
## 1: 4.592696
## USD_total_volume_24h_yesterday_percentage_change last_updated
## 1: 1.274408 2021-08-06 20:04:11
Get Valid Currencies
Use get_valid_currencies()
to determine which currencies are supported by CoinMarketCap. This can help with the next function after this which is for price conversions.
# Valid (fiat) currencies:
get_valid_currencies()
## [1] "AUD" "BRL" "CAD" "CHF" "CLP" "CNY" "CZK" "DKK" "EUR" "GBP" "HKD" "HUF"
## [13] "IDR" "ILS" "INR" "JPY" "KRW" "MXN" "MYR" "NOK" "NZD" "PHP" "PKR" "PLN"
## [25] "RUB" "SEK" "SGD" "THB" "TRY" "TWD" "ZAR" "USD"
Price Conversion
I think this function is one of the most useful functions in the coinmarketcapr
package. You can convert from a crypto to fiat value with get_price_conversion()
. You can also do crypto to crypto as shown in the third example.
# Convert 0.06 Bitcoin into USD
get_price_conversion(amount = 0.06, symbol = "BTC", convert = "USD")
## id symbol name amount last_updated USD_price USD_last_updated
## 1: 1 BTC Bitcoin 0.06 2021-08-06 20:04:00 2575.304 2021-08-06 20:04:00
# From 5 Ether to Canadian Dollars
get_price_conversion(amount = 5, symbol = "ETH", convert = "CAD")
## id symbol name amount last_updated CAD_price
## 1: 1027 ETH Ethereum 5 2021-08-06 20:04:00 18442.28
## CAD_last_updated
## 1: 2021-08-06 20:04:00
# 1 ETH to BTC (Bitcoin):
get_price_conversion(amount = 1, symbol = "ETH", convert = "BTC")
## id symbol name amount last_updated BTC_price
## 1: 1027 ETH Ethereum 1 2021-08-06 20:04:00 0.06842532
## BTC_last_updated
## 1: 2021-08-06 20:04:00
Plot Top Currencies
To plot the top currencies you can use plot_top_currencies()
. This did not work for me in the free API service.
With the use of the get_price_conversion()
from coinmarketcapr
, I can create a program that takes inputs and output a requested crypto/fiat currency. The program will convert an amount in crypto only and convert that to an amount based in another crypto or fiat currency.
This price conversion program is an extension on get_price_conversion()
. I add some checks to let the user know if the crypto ticker or fiat currency abbreviation is invalid. Valid tickers/abbreviations will produce an output.
The dplyr
library from R is used for the checks in the if statement. dplyr
in R is like pandas
in Python for working with dataframes. In Python, I can use the or
keyword but in R I need this ||
operator for or.
The first if statement checks if the user inputted a valid crypto ticker for the from_currency
. Assuming that the first if statement is passed, the second nested if statement checks if the to_currency()
input is a valid crypto ticker or fiat currency.
The %in%
keyword helps check if the input does match Coinmarketcap’s valid fiat currency or crypto.
If the inputs pass the if statements and comes out as true, the function will convert the crypto amount from one to another.
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
# Buliding upon the price conversion function from the library.
# From_currency is crypto and to_currency
crypto_price_converter <- function(amt_to_convert = 1, from_currency = "BTC", to_currency = "USD"){
# Obtain valid fiat currencies:
valid_fiat <- get_valid_currencies()
# Valid Coinmarketcap crypto tickers from get_crypto_map()
cmc_cryptos <- get_crypto_map()$symbol
# Check if inputted currencies are in Coinmarketcap's crypto & fiat currency lists
if (from_currency %in% cmc_cryptos){
if((to_currency %in% cmc_cryptos) || (to_currency %in% valid_fiat)){
return(get_price_conversion(amount = amt_to_convert, symbol = from_currency, convert = to_currency))
} else{
print("Invalid converted currency. Please enter a valid crypto/fiat ticker.")
}
} else {
print("Please enter in a valid crypto ticker.")
}
}
# Function Call 1 - 1 Bitcoin to USD
crypto_price_converter(amt_to_convert = 1, from_currency = "BTC", to_currency = "USD")
## id symbol name amount last_updated USD_price USD_last_updated
## 1: 1 BTC Bitcoin 1 2021-08-06 20:04:00 42921.74 2021-08-06 20:04:00
# Function Call 2 - 10 ETH To CAD
crypto_price_converter(amt_to_convert = 10, from_currency = "ETH", to_currency = "CAD")
## id symbol name amount last_updated CAD_price
## 1: 1027 ETH Ethereum 10 2021-08-06 20:04:00 36884.57
## CAD_last_updated
## 1: 2021-08-06 20:04:00
# Function Call 3 - 200 Solana (SOL) in terms of BTC
crypto_price_converter(amt_to_convert = 200, from_currency = "SOL", to_currency = "BTC")
## id symbol name amount last_updated BTC_price BTC_last_updated
## 1: 5426 SOL Solana 200 2021-08-06 20:04:00 0.1860791 2021-08-06 20:04:00