In this article I will demonstrate how to get Bitcoin prices directly
in R without using advanced API calls. We will use the
library cryptoQuotes to achieve this.
## load library
library(cryptoQuotes)
This library uses httr, xts and
zoo under the hood to get the Bitcoin prices in
R. It supports most major exchanges like Binance, Bitmart,
Kucoin and Kraken.
To get the latest Bitcoin price into R I use the
function getQuote, and pass the relevant parameters.
## load bitcoin
## spot sprices
btc <- getQuote(
ticker = 'BTCUSDT',
source = 'binance',
interval = '1h',
futures = FALSE
)
If we use the head function, we can see what the
function outputs, to understand it contents.
## head btc
head(
btc
)
## Open High Low Close Volume
## 2023-11-23 11:00:00 37461.36 37464.98 37159.95 37370.00 2265.1737
## 2023-11-23 12:00:00 37370.00 37530.00 37261.10 37303.87 1254.6933
## 2023-11-23 13:00:00 37303.87 37393.94 37221.75 37238.00 928.5663
## 2023-11-23 14:00:00 37238.00 37300.00 37123.77 37204.11 1258.8151
## 2023-11-23 15:00:00 37204.10 37245.52 36870.00 36951.34 2323.6408
## 2023-11-23 16:00:00 36951.34 37170.98 36917.00 37084.00 1288.8021
It contains the latest hourly OHLC prices from 2023-11-23 11:00:00 to 2023-12-14 06:00:00 denominated in USDT.
The library works out of the box with quantmod, such
that we can chart the prices with candlesticks and any indicator
supported by quantmod and TTR.
## charting with
## quantmod
quantmod::chartSeries(
btc,
name = 'BTCUSDT',
theme = quantmod::chartTheme('white')
)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
The cryptoQuotes library also supports interactive
charting, and uses plotly under the hood. You can easily
make a candlestick chart with, for example, bollinger bands.
## charting with
## plotly and cryptoQuotes
chart(
chart = kline(
btc
) %>% addBBands()
)