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
2025-01-12 23:00:00 93870.2 94450 93870.2 94449.9 30.004
2025-01-13 94450 95808.2 94407.5 95286.4 89.981
2025-01-13 01:00:00 95286.4 95331.5 94100 94122 53.878
2025-01-13 02:00:00 94122 94416.3 93918.8 94152.2 24.186
2025-01-13 03:00:00 94152.3 94347.4 93967.9 94210 7.296
2025-01-13 04:00:00 94213.1 94779.2 94153.2 94503.2 27.56

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
2025-01-12 23:00:00 0.744 0.256 2.906
2025-01-13 0.745 0.255 2.923
2025-01-13 01:00:00 0.743 0.257 2.891
2025-01-13 02:00:00 0.742 0.258 2.873
2025-01-13 03:00:00 0.743 0.257 2.897
2025-01-13 04:00:00 0.745 0.254 2.929

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-12-19 00:00:00 100163 100670 100100 100578 130.2710
#> 2024-12-19 00:05:00 100578 100784 100395 100565  41.3488
#> 2024-12-19 00:10:00 100565 100832 100556 100764  24.1706
#> 2024-12-19 00:15:00 100764 100960 100659 100761  28.3330
#> 2024-12-19 00:20:00 100761 100958 100729 100889  54.2844
#> 2024-12-19 00:25:00 100889 100970 100727 100885  14.0346
#> 2024-12-19 00:30:00 100885 100931 100809 100875   7.6012
#> 2024-12-19 00:35:00 100875 100899 100571 100572  15.4080
#> 2024-12-19 00:40:00 100572 100595 100334 100380  29.8328
#> 2024-12-19 00:45:00 100380 100789 100360 100721  30.2716
#>                 ...                                     
#> 2024-12-25 21:50:00  98344  98584  98323  98547  21.4235
#> 2024-12-25 21:55:00  98547  98547  98362  98469   6.2682
#> 2024-12-25 22:00:00  98469  98570  98416  98428  10.6190
#> 2024-12-25 22:05:00  98428  98608  98415  98603  10.6527
#> 2024-12-25 22:10:00  98603  98605  98388  98428   6.7483
#> 2024-12-25 22:15:00  98428  98549  98428  98481   7.3383
#> 2024-12-25 22:20:00  98481  98664  98481  98650   9.3217
#> 2024-12-25 22:25:00  98650  98690  98611  98690   3.0372
#> 2024-12-25 22:30:00  98690  98991  98690  98975  12.0965
#> 2024-12-25 22:35:00  98975  99038  98833  98951   3.5715

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