Skip to contents

Places a new order (limit or market) to the KuCoin Spot trading system asynchronously. This function handles both limit and market order types, validates parameters according to KuCoin's specifications, and returns the order ID and client order ID upon successful placement.

Usage

add_order_impl(
  keys = get_api_keys(),
  base_url = get_base_url(),
  type,
  symbol,
  side,
  clientOid = NULL,
  price = NULL,
  size = NULL,
  funds = NULL,
  stp = NULL,
  tags = NULL,
  remark = NULL,
  timeInForce = NULL,
  cancelAfter = NULL,
  postOnly = NULL,
  hidden = NULL,
  iceberg = NULL,
  visibleSize = NULL,
  .__coro_env_parent__ = <environment>
)

Arguments

keys

List; API configuration parameters from get_api_keys(). Defaults to get_api_keys().

base_url

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

type

Character string; order type: "limit" or "market". Required.

symbol

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

side

Character string; order side: "buy" or "sell". Required.

clientOid

Character string; unique client order ID (recommended UUID, max 40 chars). Optional.

price

Character string; price for limit orders (must align with priceIncrement). Required for limit orders.

size

Character string; quantity for limit or market orders (must align with baseIncrement). Required for limit, optional for market.

funds

Character string; funds for market orders (must align with quoteIncrement). Optional for market, mutually exclusive with size.

stp

Character string; self-trade prevention strategy: "CN", "CO", "CB", or "DC". Optional.

tags

Character string; order tag (max 20 ASCII chars). Optional.

remark

Character string; order remarks (max 20 ASCII chars). Optional.

timeInForce

Character string; time-in-force strategy: "GTC", "GTT", "IOC", or "FOK". Optional, defaults to "GTC".

cancelAfter

Integer; cancel after n seconds (for GTT). Optional.

postOnly

Logical; passive order flag (disabled for IOC/FOK). Optional, defaults to FALSE.

hidden

Logical; hide order from order book. Optional, defaults to FALSE.

iceberg

Logical; show only visible portion in iceberg orders. Optional, defaults to FALSE.

visibleSize

Character string; max visible quantity for iceberg orders. Optional.

Value

Promise resolving to a data.table containing:

  • orderId (character): Unique order ID generated by KuCoin.

  • clientOid (character): Client-specified order ID.

Details

Workflow Overview

  1. Parameter Validation: Validates required and optional parameters based on order type (limit/market) using rlang::arg_match0 for enumerated values.

  2. Request Body Construction: Builds a JSON body with mandatory and optional parameters.

  3. Authentication: Generates headers with API credentials using build_headers().

  4. API Request: Sends a POST request to the KuCoin API with a 3-second timeout.

  5. Response Processing: Parses the response, validates success, and returns order details as a data.table.

API Endpoint

POST https://api.kucoin.com/api/v1/hf/orders

Usage

Utilised to place spot trading orders on KuCoin. Supports limit orders (price + size) and market orders (size or funds). Requires sufficient funds in the account and adheres to KuCoin's trading rules (e.g., max 2000 active orders).

Official Documentation

KuCoin Add Order

Examples

if (FALSE) { # \dontrun{
main_async <- coro::async(function() {
  # Place a limit buy order
  order <- await(add_order_impl(
    type = "limit",
    symbol = "BTC-USDT",
    side = "buy",
    price = "50000",
    size = "0.0001",
    clientOid = uuid::UUIDgenerate(),
    remark = "Test limit order"
  ))
  print(order)

  # Place a market buy order with funds
  market_order <- await(add_order_impl(
    type = "market",
    symbol = "BTC-USDT",
    side = "buy",
    funds = "10",
    clientOid = uuid::UUIDgenerate()
  ))
  print(market_order)
})
main_async()
while (!later::loop_empty()) later::run_now()
} # }