Coinmarketcap Crypto Data & R Programming

Hi there. In this programming post, I play around with the coinmarketcapr package for working with Coinmarketcap crypto data in R.

Here are the references & documentation links used here:

 

A full detailed post of this can be found here.

 


Pixabay Image Source

Topics


  • Setup of Coinmarketcap API
  • Various Functions In coinmarketcapr
  • Price Conversion Program

 

Setup Of Coinmarketcap API


For this project I use the free service to obtain an API key. The pricing details is found here. For more features, you do need the higher priced subscriptions.

Here is the code snippet for the setup with the API key in R.

# Load packages:
library(coinmarketcapr)

# Setup:

setup(api_key = "<api_key>")
 

 

Various Functions In coinmarketcapr


From R's coinmarketcapr package, I test out various functions in this package. Note that the output was based on code that I ran a few weeks ago from the time of this posting.

In addition, some functions do not work in the free API plan.

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: 

 

Active Cryptos - No Output Shown

# 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)

 

Crypto Map - No Output Shown

# get_crpyto_map() gets all active cryptos supported by platform incl unique id for each crypto

get_crypto_map()

 

Crypto Metadata

With get_crypto_data(), you can obtain static metadata on a given crypto(s). The descrpiton part of the output can be useful.

# 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

 

Get Crypto Quotes

The latest historical market quotes can be obtained with get_crypto_quotes().

# 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 Valid Currencies

Valid fiat currencies recognized by Coinmarketcap can be obtained with the use of get_valid_currencies().

# 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

The one function that stands out for me is get_price_conversion(). You can convert from a crypto/fiat value into a fiat amount.

# 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

 

Functions That Did Not Work For Me

These functions did not work for me. I suspect that it does not work with the free API service. You would have to pay more for more access to these.

  • plot_top_currencies()
  • get_exchange_meta()
  • get_exchange_map()
  • get_crypto_ohlcv(currency = "USD", latest = TRUE, symbol = "BTC")

 


Pixabay Image Source

Price Conversion Program


This section focuses on get_price_conversion() from coinmarketcapr. I create a function that takes inputs and outputs a converted crypto/fiat amount.

The dplyr package in R is like pandas in Python when it comes to data wrangling and working with dataframes. This || operator is the symbol version for or.

The if statements are there to make sure that the user does input a valid crypto that Coinmarketcap recognizes as well a valid fiat currency.

The inputs here is an amount that is converted, a currency that is to be converted and the currency that the user would like after conversion.

 

library(dplyr)

# 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 Calls

Note that the function call outputs occurred when I initially ran the code. It is not the latest data as you read this.

# 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

 


Pixabay Image Source

Thank you for reading.



0
0
0.000
2 comments
avatar

API setup is something that's more technical and not my cup of tea. Looks nice but can't understand more.

0
0
0.000