I had been trying to get cryptocurrency market data for some time, and the truth is that it took me a long time to find a way to import them in a clear way in R.
After researching, I think I found a fairly easy way to do it for those of us who use this language and would like to share it. thus, the next person with the same problem won't have to deal with as much. I also think it would be good if others share their way of doing it, if there is an easier or faster way it would help to us who use R.
For this task we have to use Binance API, see the documentation and how to obtain other market data here.
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.