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-11-07 08:00:00 74837.1 75125 74717.1 75111 45.515
2024-11-07 09:00:00 75122 75149 74904.2 74951.7 39.724
2024-11-07 10:00:00 74951.8 75025.2 74811 75025.2 11.703
2024-11-07 11:00:00 75025.2 75025.2 74800 74800 80.038
2024-11-07 12:00:00 74800.1 75125 74800 74987.8 54.549
2024-11-07 13:00:00 74987.8 75079.7 74849.9 74931.1 53.157

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-11-07 08:00:00 0.723 0.277 2.608
2024-11-07 09:00:00 0.723 0.276 2.617
2024-11-07 10:00:00 0.719 0.281 2.555
2024-11-07 11:00:00 0.722 0.278 2.6
2024-11-07 12:00:00 0.715 0.285 2.506
2024-11-07 13:00:00 0.713 0.287 2.484

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
    
  }
)
#>                      open  high   low close  volume
#> 2024-10-13 00:00:00 63217 63299 63209 63294  3.0940
#> 2024-10-13 00:05:00 63294 63306 63235 63242  5.9610
#> 2024-10-13 00:10:00 63242 63254 63214 63214  0.3485
#> 2024-10-13 00:15:00 63214 63236 63206 63206  0.4509
#> 2024-10-13 00:20:00 63206 63206 63059 63190 30.0984
#> 2024-10-13 00:25:00 63190 63252 63190 63252  1.1521
#> 2024-10-13 00:30:00 63252 63308 63252 63265  8.6116
#> 2024-10-13 00:35:00 63265 63265 63230 63256  0.6772
#> 2024-10-13 00:40:00 63256 63302 63256 63286  0.4704
#> 2024-10-13 00:45:00 63286 63301 63256 63277  1.1825
#>                 ...                                
#> 2024-10-19 21:50:00 68321 68321 68260 68260  2.0300
#> 2024-10-19 21:55:00 68260 68261 68212 68212  2.8811
#> 2024-10-19 22:00:00 68212 68286 68208 68211  9.1740
#> 2024-10-19 22:05:00 68211 68263 68195 68246  2.6223
#> 2024-10-19 22:10:00 68246 68287 68228 68234  2.8729
#> 2024-10-19 22:15:00 68234 68266 68215 68243  1.5885
#> 2024-10-19 22:20:00 68243 68291 68243 68264  3.0150
#> 2024-10-19 22:25:00 68264 68300 68253 68281  7.9790
#> 2024-10-19 22:30:00 68281 68325 68258 68325  7.1780
#> 2024-10-19 22:35:00 68325 68350 68307 68339  4.6664

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] 4000

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