Skip to contents

Retrieves partial orderbook depth data (20 or 100 levels) for a specified trading symbol from the KuCoin API asynchronously.

Usage

get_part_orderbook_impl(
  base_url = get_base_url(),
  symbol,
  size,
  .__coro_env_parent__ = <environment>
)

Arguments

base_url

Character string; base URL for the KuCoin API. Defaults to get_base_url().

symbol

Character string; trading symbol (e.g., "BTC-USDT").

size

Integer; orderbook depth (20 or 100).

Value

Promise resolving to a data.table containing:

  • timestamp (POSIXct): Snapshot timestamp in UTC.

  • time_ms (integer): Snapshot timestamp in milliseconds.

  • sequence (character): Orderbook update sequence.

  • side (character): Order side ("bid" or "ask").

  • price (character): Aggregated price level.

  • size (character): Aggregated size at that price.

Details

Workflow Overview

  1. Input Validation: Ensures size is 20 or 100, aborting if invalid.

  2. Query Construction: Builds a query string with the symbol parameter using build_query().

  3. URL Assembly: Combines base_url, /api/v1/market/orderbook/level2_{size}, and the query string.

  4. HTTP Request: Sends a GET request with a 10-second timeout via httr::GET().

  5. Response Processing: Validates the response with process_kucoin_response() and extracts "data".

  6. Data Conversion: Converts bids and asks into separate data.tables, adds side, combines them, and appends snapshot fields.

API Endpoint

GET https://api.kucoin.com/api/v1/market/orderbook/level2_{size}

Usage

Utilised to obtain a snapshot of the orderbook for a trading symbol, showing aggregated bid and ask levels.

Official Documentation

KuCoin Get Part OrderBook

Examples

if (FALSE) { # \dontrun{
main_async <- coro::async(function() {
  orderbook_20 <- await(get_part_orderbook_impl(symbol = "BTC-USDT", size = 20))
  print(orderbook_20)
  orderbook_100 <- await(get_part_orderbook_impl(symbol = "BTC-USDT", size = 100))
  print(orderbook_100)
})
main_async()
while (!later::loop_empty()) later::run_now()
} # }