Skip to contents
## load library
library(cryptoQuotes)

The main goal of the {cryptoQuotes} is to bridge the gap between R and the cryptocurrency market data. Its a high-level API-client that connects to major cryptocurrency exchanges and their respective public market data endpoints.

In this article we will focus on price and sentiment data made available by the Kraken exchange.

Cryptocurrency market data

In this section we will focus on market data from the last 24 hours, on the hourly chart.

Open, Highl Low, Close and Volume (OHLC-V) data

To get OHLC-V data the get_quote()-function is the go-to function,

## Get the
## SPOT price of 
## Bitcoin on the hourly
BTC <- get_quote(
  ticker   = "BTCUSD",
  source   = "kraken",
  futures  = FALSE,
  interval = "1h",
  from     = Sys.Date() - 1
)
Hourly Bitcoin OHLC-V data
index open high low close volume
2024-08-16 09:00:00 58376.8 58670.6 58297.3 58639.1 42.023
2024-08-16 10:00:00 58670.6 58677.6 58329.3 58462.8 21.578
2024-08-16 11:00:00 58462.9 58613.3 58277.7 58414.1 23.327
2024-08-16 12:00:00 58414.1 58497.5 57514.7 57865.1 124.597
2024-08-16 13:00:00 57865.1 58864.1 57810.3 58319.6 140.406
2024-08-16 14:00:00 58325.1 58438.2 57953.6 58001.7 23.244

Sentiment data

One sentiment indicator for Bitcoin is the long-short ratio, which can be retrieved using get_lsratio()-function,

## Get the
## long-short ratio of 
## Bitcoin on the hourly
LS_BTC <- get_lsratio(
  ticker   = "PF_XBTUSD",
  source   = "kraken",
  interval = "1h",
  from     = Sys.Date() - 1
)
Hourly Long-Short Ratio on Bitcoin
index long short ls_ratio
2024-08-16 09:00:00 0.727 0.274 2.656
2024-08-16 10:00:00 0.72 0.28 2.57
2024-08-16 11:00:00 0.714 0.286 2.495
2024-08-16 12:00:00 0.713 0.287 2.487
2024-08-16 13:00:00 0.716 0.284 2.52
2024-08-16 14:00:00 0.715 0.285 2.505

Limitations

There is a limit to the amount of market data that can be extracted in one call. The Kraken exchange, for example, has a limit on 5000 rows of data per call in the futures market,

## Get the SPOT
## market for over 
## 2000 rows
tryCatch(
  get_quote(
    ticker   = "PF_XBTUSD",
    source   = "kraken",
    futures  = TRUE,
    interval = "5m",
    from     = Sys.Date() - 25,
    to       = Sys.Date()
  ),
  error = function(error) {
    
    error
    
  }
)
#> <simpleError: lexical error: invalid char in json text.
#>                                        Time period too large, too many
#>                      (right here) ------^
#> >

If you need more data than this, you need to do multiple calls. One such solution is the following,

## 1) create date
## sequence
dates <- seq(
  from       = as.POSIXct(Sys.Date()),
  by         = "-5 mins",
  length.out = 10000
)

## 2) split the sequence
## in multiples of 100
## by assigning numbers
## to each indices of 100
idx <- rep(
  x    = 1:2,
  each = 5000
)

## 3) use the idx to split
## the dates into equal parts
split_dates <- split(
  x = dates,
  f = idx
)

## 4) collect all all
## calls in a list
## using lapply
ohlc <- lapply(
  X   = split_dates,
  FUN = function(dates){
    
    Sys.sleep(1)
    
    cryptoQuotes::get_quote(
      ticker   = "PF_XBTUSD",
      source   = "kraken",
      futures  = TRUE,
      interval = "5m",
      from     = min(dates),
      to       = max(dates)
    )
    
  }
)

## 4.1) rbind all
## elements
nrow(
  ohlc <- do.call(
    what = rbind,
    args = ohlc
  )
)
#> [1] 10000

Note: For an indepth analysis of the various limitations and workarounds please see the {cryptoQuotes} wiki on Github