Skip to content

Usecase

Serkan Korkmaz edited this page Jul 5, 2024 · 25 revisions

Overview of cryptocurrency packages in R

At the time of writing there are a few other R packages that extracts cryptocurrency market data in R. These include, but not limited to, the following,

  1. {quantmod}
  2. {crypto2}
  3. {binancer}
  4. {coinmarketcapr}
  5. {crypto}

{crypto} is probably the oldest R package available but has been archived by CRAN, and is no longer actively maintained. The package has been forked and {crypto2} has now taken its place. {binancer} and {coinmarketcapr}, especially the latter, is similar to {crypto2} as they are both Coinmarketcap API-clients; the only difference is that {coinmarketcapr} uses {data.table} whereas {crypto2} uses {tibble}. Of these packages only {crypto2} is actively maintained.

{quantmod} and {crypto2} are more comparable in terms of retrieval of cryptocurrency market data, as both packages utilises the Coinmarketcap API; {crypto2} provides access via a webscraper, while {quantmod} uses Yahoo Finance who provides cryptocurrency market data via Coinmarketcap too. These packages, in terms of data retrieval, differs in efficiency - {quantmod} retrieves the data in fractions of what {crypto2} does, and delivers the data as {xts}-objects. {crypto2} advertises itself as being fully research compliant, as it provides access to delisted cryptocurrency projects and thus minimizes survivorship biases; and to the best of our knowledge, {quantmod} does so too.

Note

It is blasphemous to compare {quantmod} to any other financial R package of the same class. We fully acknowledge the superiority of {quantmod} in all aspects and are providing this discussion for research purposes. If you, as an end-user or developer, find this discussion biased please reach out.

{cryptoQuotes} differs from {quantmod} and {crypto2} in various ways. Firstly, {cryptoQuotes} retrieves the cryptocurrency market data directly from the cryptocurrency exchanges; in that sense it is more similar to {binancer}, but with a bigger arsenal of exchanges. Secondly, {cryptoQuotes} provides a higher granularity level than {crypto2} and {quantmod} who only provide hourly to weekly data; {cryptoQuotes} provides the granularity from seconds to months. Thirdly, {cryptoQuotes} uses interactive financial charts via {plotly}, whereas {quantmod} uses static base R plots. Fourthly, {cryptoQuotes} provides access to cryptocurrency sentiment data.

{cryptoQuotes} is primarily intended for developing and evaluating trading strategies, and analyzing the cryptocurrency market in general. It is not limited to major cryptocurrencies as the included cryptocurrency exchanges includes the likes KuCoin, BitMart and Huobi (HTX) where your average dog-themed cryptocurrency project is listed alongside the various ElonCumCoin-projects.

Cryptocurrency Market Data in R

OHLC-V Data

The main usecase of {cryptoQuotes} is the available get_-functions. Below is a basic example of retrieving the daily price of Bitcoin.

## 1) get BTC
## on the daily
BTC <- cryptoQuotes::get_quote(
  ticker  = "BTCUSDT",
  source  = "bybit",
  futures = FALSE,
  interval = "1d",
  from = Sys.Date() - 100
)

All the returned objects are {xts}-objects, with the following format,

index open high low close volume
2024-06-30 02:00:00 60979.90 63068.91 60707.31 62772.83 9159.041
2024-07-01 02:00:00 62772.83 63863.88 62527.77 62893.02 17146.217
2024-07-02 02:00:00 62893.02 63295.86 61810.50 62131.02 12553.436
2024-07-03 02:00:00 62131.02 62286.36 59387.59 60211.10 19925.700
2024-07-04 02:00:00 60211.10 60503.22 56752.13 57056.28 25290.405
2024-07-05 02:00:00 57056.28 57555.00 53345.94 56592.85 21194.082

Sentiment Data

## 1) get daily
## Long-Short Ratio of
## BTC on the daily chart
BTC_LSR <- cryptoQuotes::get_lsratio(
  ticker   = "BTCUSDT",
  source   = "bybit",
  interval = "1d",
  from     = Sys.Date() - 100
)
index long short ls_ratio
2024-06-30 02:00:00 0.5726 0.4274 1.339729
2024-07-01 02:00:00 0.5732 0.4268 1.343018
2024-07-02 02:00:00 0.5675 0.4325 1.312139
2024-07-03 02:00:00 0.5758 0.4242 1.357379
2024-07-04 02:00:00 0.5839 0.4161 1.403268
2024-07-05 02:00:00 0.5811 0.4189 1.387205

Interactive Charts

Note

Github restricts interactivity via GFM, and all the {plotly}-charts are therefore static images. All interactivity is maintained using scripts, quarto and/or R Markdown.

The returned market data can be charted and examined as follows,

Dark Themed

cryptoQuotes::chart(
  ticker = BTC,
  main = cryptoQuotes::kline(),
  indicator = list(
    cryptoQuotes::sma(n = 7),
    cryptoQuotes::sma(n = 14),
    cryptoQuotes::sma(n = 21)
  ),
  sub = list(
    cryptoQuotes::volume(),
    cryptoQuotes::macd(),
    cryptoQuotes::lsr(BTC_LSR)
  )
)
Charting cryptocurrency market data in R

Light Themed

cryptoQuotes::chart(
  ticker = BTC,
  main = cryptoQuotes::kline(),
  indicator = list(
    cryptoQuotes::sma(n = 7),
    cryptoQuotes::sma(n = 14),
    cryptoQuotes::sma(n = 21)
  ),
  sub = list(
    cryptoQuotes::volume(),
    cryptoQuotes::macd(),
    cryptoQuotes::lsr(BTC_LSR)
  ),
  options = list(
    dark = FALSE
  )
)
Charting cryptocurrency market data in R

Color Deficient

cryptoQuotes::chart(
  ticker = BTC,
  main = cryptoQuotes::kline(),
  indicator = list(
    cryptoQuotes::sma(n = 7),
    cryptoQuotes::sma(n = 14),
    cryptoQuotes::sma(n = 21)
  ),
  sub = list(
    cryptoQuotes::volume(),
    cryptoQuotes::macd(),
    cryptoQuotes::lsr(BTC_LSR)
  ),
  options = list(
    dark = FALSE,
    deficiency = TRUE
  )
)
Charting cryptocurrency market data in R