To GET the candlestick data we can use the function fromJSON from the package RJSONIO.
suppose that we want to see the pair BTC/USDT in intervals of 1 hour, the maximum amount of observations that we can adquire in one request is 1000, so we do:
Candlestick.data <- fromJSON("https://api.binance.com/api/v3/klines?symbol=BTCUSDT&interval=1h&limit=1000")
seeing the data with View(Candlestick.data) we can se that is a list of lists, where each sublist contain all the candlestick data corresponding to 1-hour interval (close price, highest price, volume, number of trades, etc.).
To change the lists into a dataframe we could do:
i <- 1:length(Candlestick.data)
df <- sapply(i, function(i){as.numeric(unlist(Candlestick.data[i]))})
By doing this, each column of df corresponds to the period (1-hour ago, 2-hours ago, and so on) and each row to the different candlestick data. So let's say we want to see all the close prices of the last 1000 hours, as we can see in the documentation , this is the 5th element of each list, so we can do this by df[5,].
If we want the rows to be the periods and the columns to be the candlestick data, we can transpose the matrix with t() like this
df_t <- t(df) and now to get the close prices df_t[,5].
Finally, to make the request easier, we can use the function paste0() and now, in summary, we could do for example:
library(RJSONIO)
crypto_A <- "ETH" # the spot market crypto_A vs crypto_B must exist in Binance
crypto_B <- "BTC"
interval <- "15m" # see all the possible intervals in the documentation
observations <- "1000" # remember that the maximum is 1000
Candlestick.data <- fromJSON(paste0("https://api.binance.com/api/v3/klines?symbol=",crypto_A,crypto_B,"&interval=",interval,"&limit=",observations))
i <- 1:length(Candlestick.data)
df <- sapply(i, function(i){as.numeric(unlist(Candlestick.data[i]))})
df_t <- t(df)
Hope this can help and remember to read the documentation for more details.