Skip to content

Usecase

Serkan Korkmaz edited this page May 26, 2024 · 23 revisions

Overview of cryptocurrency libraries 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. crypto
  2. crypto2
  3. binancer
  4. coinmarketcapr

crypto is probably the oldest R library available, and crypto2 is essentially a fork of it. binancer and coinmarketcapr are similar to these libraries. The most notable difference between these are that binancer uses data.table instead of tibble.

cryptoQuotes differentiates itself in various ways. cryptoQuotes uses xts-objects, and provides a more flexible granularity than, for example, crypto and crypto2 which only supports daily prices. cryptoQuotes supports all available intervals ranging from seconds to months. binancer also supports flexible granularity, but is limited to OHLC-V-data from Binance only. cryptoQuotes supports sentiment data on top of OHLC-V-data, not only from Binance but for all major cryptocurrency exchanges.

crypto2 advertises itself as being fully research compliant in the sense that it also extracts prices of cryptocurrencies that are either de-listed, or dead projects, to avoid survivorship biases in cryptocurrency research. crypto and crypto2 uses webcrawlers to extract the market data, while cryptoQuotes uses public market data endpoints.

The cryptoQuotes library is primarily used 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 and BitMart 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 the cryptoQuotes-package 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-05-21 02:00:00 71432.80 71981.11 69153.24 70153.00 16546.611
2024-05-22 02:00:00 70153.00 70677.47 68896.20 69163.10 15990.402
2024-05-23 02:00:00 69163.10 70096.43 66299.22 67971.36 19022.937
2024-05-24 02:00:00 67971.36 69257.66 66611.44 68563.06 12600.485
2024-05-25 02:00:00 68563.06 69625.00 68502.00 69292.07 5310.579
2024-05-26 02:00:00 69292.07 69318.98 68806.35 69114.53 1385.539

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-05-21 02:00:00 0.5343 0.4657 1.147305
2024-05-22 02:00:00 0.5456 0.4544 1.200704
2024-05-23 02:00:00 0.5516 0.4484 1.230152
2024-05-24 02:00:00 0.5677 0.4323 1.313208
2024-05-25 02:00:00 0.5569 0.4431 1.256827
2024-05-26 02:00:00 0.5636 0.4364 1.291476

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 RMarkdown.

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