Open In App

Get Real-Time Crypto Currencies Price Using R and Binance API

Last Updated : 26 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

 In this article, we are going to see how to get the real-time price of cryptocurrencies using Binance API  in R Programming Language.

Binance API

Binance API is a method that allows you to connect to the Binance servers using several programming languages. With it, you can automate your trading and make HTTP requests to send and receive data. Here we access Binance API using R with httr2 package. We will be sending requests to Binance API and extracting the real-time price of the required cryptocurrency in JSON format.

Get Crypto Price Using R And Binance API

We will be using the library httr2 to make requests and store the data. httr2 handles everything, including JSON formats. You can install the library as follows.

R
# install httr2
install.packages('httr2', dependencies = TRUE)

Performing Binance API requests:

R
# 1) perform GET-request
# for latest BTCUSDT ticker price
response <- httr2::req_perform(
  req = httr2::request(
    'https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT'
  )
)

# 2) binance returns
# a json object which can 
# be extacted directly
response <- httr2::resp_body_json(
  resp = response
)

# 3) extract price
# and symbol
price <- as.numeric(
  response$price
)
symbol <- response$symbol

# 4) print the results
# with appropriate labelling
print(
  paste(
    symbol, 'price is', price
  )
)

Output:

R
[1] "BTCUSDT price is 43913.58"


Get Crypto Prices Using R Without httr2

If you are not comfortable with API requests, R has some built-in libraries that handles these things for you. One example of such is the cryptoQuotes package, which provides access to latest crypto prices and volumes without having to do the requests yourself.

We can use the getQuotes function to get the prices of any pair listed on major exchanges. Below is an example,

Getting Crypto Prices Using cryptoQuotes:

R
# 1) get latest
# BTC prices using
# getQuote
tail(
  btc_price <- cryptoQuotes::getQuote(
    ticker   = 'BTCUSDT',
    source   = 'binance',
    futures  = FALSE,
    interval = '1d'
  )
)

Output:

R
               Open     High      Low    Close   Volume
2023-12-17 42278.02 42424.07 41252.00 41374.65 27722.11
2023-12-18 41374.64 42757.81 40542.93 42657.80 46734.09
2023-12-19 42657.80 43497.00 41811.10 42275.99 40927.86
2023-12-20 42275.99 44283.00 42206.00 43668.93 48710.29
2023-12-21 43668.92 44242.35 43286.72 43861.80 34624.29
2023-12-22 43861.79 44398.26 43412.54 43939.81 27436.58


All extracted prices can charted using candlesticks and various indicators, such as Bollinger Bands. Here is an example using getQuote and chart from the package to chart the Ethereum daily price,

R
# 1) get latest
# ethereum prices
# on 15 minute intervals
eth_price <- cryptoQuotes::getQuote(
  ticker   = 'ETHUSDT',
  source   = 'binance',
  futures  = FALSE,
  interval = '1d'
)

# 2) chart prices with
# candlesticks and 
# add Bollinger Bands
cryptoQuotes::chart(
  chart = cryptoQuotes::kline(quote = eth_price) %>% 
    cryptoQuotes::addVolume() %>% 
    cryptoQuotes::addBBands(),
  slider = FALSE
)

Output:



geeks

Daily Ethereum Prices with Candlesticks and Bollinger Bands



The cryptoQuotes package supports Spot and Futures market in all available intervals, and can be installed using the following code,

R
## install cryptoQuotes
install.packages('cryptoQuotes', dependencies = TRUE)





Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads