binance_depth

Retrieve a snapshot of the order book for a specified trading pair. Refer to this article for the management of an orderbook.

  • Github function here.

Example: Spot (spot)

Get depth data for BTCUSDT in spot market.

Show the code
library(binancer)
library(dplyr)
# import depth data from binance 
depth <- binance_depth(pair = "BTCUSDT", api = "spot", quiet = TRUE)

df_depth <- 
  dplyr::bind_rows(
  tail(dplyr::filter(depth, side == "ASK"), n = 1), # best ask
  head(dplyr::filter(depth, side == "BID"), n = 1)  # best bid
) 

Visualize a structure orderbook from depth data with OrderBook() function:

Show the code
best_ask <- df_depth$price[2]
best_bid <- df_depth$price[1]
OrderBook(depth, 
          min_price = best_bid*0.99, 
          max_price = best_ask*1.01, 
          levels = 10,
          as_datatable = TRUE)

Example: Futures (fapi)

Get depth data for BTCUSDT in USD-m market.

Show the code
library(binancer)
library(dplyr)
# import depth futures usd-m data from binance 
depth_fapi <- binance_depth(pair = "BTCUSDT", api = "fapi", quiet = TRUE)

df_depth <- 
  dplyr::bind_rows(
    tail(dplyr::filter(depth_fapi, side == "ASK"), n = 1), # best ask
    head(dplyr::filter(depth_fapi, side == "BID"), n = 1)  # best bid
) 

Example: Futures (dapi)

Get depth data for BTCUSD_PERP in COIN-m market.

Show the code
library(binancer)
library(dplyr)
# import depth futures coin-m data from binance 
depth_dapi <- binance_depth(pair = "BTCUSD_PERP", api = "dapi", quiet = TRUE)

df_depth <- dplyr::bind_rows(
  tail(dplyr::filter(depth_dapi, side == "ASK"), n = 1), # best ask
  head(dplyr::filter(depth_dapi, side == "BID"), n = 1)  # best bid
)

Example: Options (eapi)

Get depth data for BTC-240628-30000-P in Options market. BTC-240628-30000-P is a Put contract with strike price 30000 and expiry date on 28-06-2024.

Show the code
library(binancer)
library(dplyr)
# import depth futures coin-m data from binance 
depth_eapi <- binance_depth(pair = "BTC-240628-30000-P", api = "eapi")

df_depth <- dplyr::bind_rows(
  tail(dplyr::filter(depth_eapi, side == "ASK"), n = 1), # best ask
  head(dplyr::filter(depth_eapi, side == "BID"), n = 1)  # best bid
)
Back to top

Citation

BibTeX citation:
@online{sartini2024,
  author = {Sartini, Beniamino},
  date = {2024-06-18},
  url = {https://cryptoverser.org/binancer-docs/binance_depth.html},
  langid = {en}
}
For attribution, please cite this work as:
Sartini, Beniamino. 2024. June 18, 2024. https://cryptoverser.org/binancer-docs/binance_depth.html.