KucoinFuturesTrading: Futures Order Management
Source:R/KucoinFuturesTrading.R
KucoinFuturesTrading.RdKucoinFuturesTrading: Futures Order Management
KucoinFuturesTrading: Futures Order Management
Details
Provides methods for placing, cancelling, and querying futures orders on KuCoin. Supports limit and market orders, stop orders, batch operations, and Dead Connection Protection (DCP). Inherits from KucoinBase.
Purpose and Scope
Order Placement: Place single or batch limit/market futures orders with configurable leverage, margin mode, and position side.
Order Cancellation: Cancel individual orders by system ID or client OID, or cancel all open/stop orders at once.
Order Queries: Retrieve order details, paginated order lists, recent closed orders, and stop orders.
Trade History: Query fill records (paginated or recent) with fee and liquidity details.
Risk Management: Configure Dead Connection Protection (DCP) to auto-cancel orders on connectivity loss.
Usage
All methods require authentication with Futures trading permission enabled.
Use add_order_test() to validate order parameters before placing real orders.
For automated trading, configure DCP via set_dcp() to protect against
unintended open positions during bot failures.
Endpoints Covered
| Method | Endpoint | HTTP |
| add_order | POST /api/v1/orders | POST |
| add_order_test | POST /api/v1/orders/test | POST |
| add_order_batch | POST /api/v1/orders/multi | POST |
| cancel_order_by_id | DELETE /api/v1/orders/{orderId} | DELETE |
| cancel_order_by_client_oid | DELETE /api/v1/orders/client-order/{clientOid} | DELETE |
| cancel_all | DELETE /api/v1/orders | DELETE |
| cancel_all_stop_orders | DELETE /api/v1/stopOrders | DELETE |
| get_order_by_id | GET /api/v1/orders/{orderId} | GET |
| get_order_by_client_oid | GET /api/v1/orders/byClientOid | GET |
| get_order_list | GET /api/v1/orders | GET |
| get_recent_closed_orders | GET /api/v1/recentDoneOrders | GET |
| get_stop_orders | GET /api/v1/stopOrders | GET |
| get_fills | GET /api/v1/fills | GET |
| get_recent_fills | GET /api/v1/recentFills | GET |
| get_open_order_value | GET /api/v1/openOrderStatistics | GET |
| set_dcp | POST /api/v1/orders/dead-cancel-all | POST |
| get_dcp | GET /api/v1/orders/dead-cancel-all/query | GET |
Super class
kucoin::KucoinBase -> KucoinFuturesTrading
Methods
Method new()
Create a new KucoinFuturesTrading instance.
Usage
KucoinFuturesTrading$new(
keys = get_api_keys(),
base_url = get_futures_base_url(),
async = FALSE,
time_source = c("local", "server")
)Arguments
keysList; API credentials from
get_api_keys().base_urlCharacter; Futures API base URL. Defaults to
get_futures_base_url().asyncLogical; if TRUE, methods return promises.
time_sourceCharacter;
"local"or"server".
Method add_order()
Place a Futures Order
Places a new futures order (limit or market) on KuCoin. Supports isolated and cross margin modes, one-way and hedge position modes, and optional reduce-only constraints.
Workflow
Build Body: Constructs JSON body with all non-NULL order parameters.
Request: Authenticated POST to the futures order placement endpoint.
Parsing: Returns
data.tablewith the system-assigned order ID and client OID.
Automated Trading Usage
Leverage Control: Set
leverageto manage risk exposure per order.Position Modes: Use
positionSide = "LONG"or"SHORT"in hedge mode to manage both directions simultaneously.Reduce-Only: Set
reduceOnly = TRUEto ensure the order only closes existing positions, preventing accidental position increases.
curl
curl --location --request POST 'https://api-futures.kucoin.com/api/v1/orders' \
--header 'Content-Type: application/json' \
--header 'KC-API-KEY: your-api-key' \
--header 'KC-API-SIGN: your-signature' \
--header 'KC-API-TIMESTAMP: 1729176273859' \
--header 'KC-API-PASSPHRASE: your-passphrase' \
--header 'KC-API-KEY-VERSION: 2' \
--data-raw '{
"clientOid": "my-order-001",
"symbol": "XBTUSDTM",
"side": "buy",
"type": "limit",
"leverage": 5,
"size": 1,
"price": "50000",
"marginMode": "ISOLATED",
"positionSide": "BOTH",
"timeInForce": "GTC"
}'Usage
KucoinFuturesTrading$add_order(
clientOid,
symbol,
side,
type,
leverage,
size,
price = NULL,
marginMode = "ISOLATED",
positionSide = "BOTH",
timeInForce = NULL,
reduceOnly = NULL,
remark = NULL,
...
)Arguments
clientOidCharacter; unique client order ID.
symbolCharacter; futures symbol (e.g.,
"XBTUSDTM").sideCharacter;
"buy"or"sell".typeCharacter;
"limit"or"market".leverageInteger; leverage multiplier.
sizeInteger; order quantity (number of contracts).
priceCharacter or NULL; price (required for limit orders).
marginModeCharacter;
"ISOLATED"or"CROSS". Default"ISOLATED".positionSideCharacter;
"BOTH"for one-way mode,"LONG"or"SHORT"for hedge mode. Default"BOTH".timeInForceCharacter or NULL; e.g.,
"GTC","IOC","FOK".reduceOnlyLogical or NULL; if TRUE, order only reduces position.
remarkCharacter or NULL; order notes.
...Additional order parameters.
Returns
A single-row data.table (or promise<data.table> if constructed with async = TRUE) with columns:
order_id(character): System-assigned order ID.client_oid(character): Client-provided order ID.
Examples
\dontrun{
ft <- KucoinFuturesTrading$new()
# Place a limit buy order
result <- ft$add_order(
clientOid = "my-order-001",
symbol = "XBTUSDTM",
side = "buy",
type = "limit",
leverage = 5,
size = 1,
price = "50000"
)
print(result$order_id)
# Place a market sell order
result <- ft$add_order(
clientOid = "my-order-002",
symbol = "XBTUSDTM",
side = "sell",
type = "market",
leverage = 10,
size = 2,
reduceOnly = TRUE
)
}
Method add_order_test()
Place a Test Futures Order (Dry Run)
Validates order parameters without placing a real order. The API performs all validation checks (balance, symbol, price, etc.) and returns a simulated order ID. Useful for pre-flight checks in automated systems.
Workflow
Build Body: Constructs JSON body with all non-NULL order parameters (identical to
add_order()).Request: Authenticated POST to the test endpoint; no real order is placed.
Parsing: Returns
data.tablewith a simulated order ID and client OID.
Automated Trading Usage
Pre-Flight Validation: Validate order parameters at bot startup to confirm API keys have trading permissions and symbols are correct.
Parameter Testing: Test edge-case parameters (extreme leverage, unusual sizes) without risking capital.
Integration Testing: Use in CI/CD pipelines to verify order construction logic.
curl
curl --location --request POST 'https://api-futures.kucoin.com/api/v1/orders/test' \
--header 'Content-Type: application/json' \
--header 'KC-API-KEY: your-api-key' \
--header 'KC-API-SIGN: your-signature' \
--header 'KC-API-TIMESTAMP: 1729176273859' \
--header 'KC-API-PASSPHRASE: your-passphrase' \
--header 'KC-API-KEY-VERSION: 2' \
--data-raw '{
"clientOid": "test-001",
"symbol": "XBTUSDTM",
"side": "buy",
"type": "limit",
"leverage": 5,
"size": 1,
"price": "50000",
"marginMode": "ISOLATED",
"positionSide": "BOTH"
}'Usage
KucoinFuturesTrading$add_order_test(
clientOid,
symbol,
side,
type,
leverage,
size,
price = NULL,
marginMode = "ISOLATED",
positionSide = "BOTH",
timeInForce = NULL,
reduceOnly = NULL,
remark = NULL,
...
)Arguments
clientOidCharacter; unique client order ID.
symbolCharacter; futures symbol (e.g.,
"XBTUSDTM").sideCharacter;
"buy"or"sell".typeCharacter;
"limit"or"market".leverageInteger; leverage multiplier.
sizeInteger; order quantity (number of contracts).
priceCharacter or NULL; price (required for limit orders).
marginModeCharacter;
"ISOLATED"or"CROSS". Default"ISOLATED".positionSideCharacter;
"BOTH"for one-way mode,"LONG"or"SHORT"for hedge mode. Default"BOTH".timeInForceCharacter or NULL; e.g.,
"GTC","IOC","FOK".reduceOnlyLogical or NULL; if TRUE, order only reduces position.
remarkCharacter or NULL; order notes.
...Additional order parameters.
Method add_order_batch()
Batch Place Futures Orders
Places multiple futures orders in a single API request. Each order in the
batch uses the same parameter structure as add_order(). The API processes
orders independently; partial failures are possible (some orders may succeed
while others fail).
Workflow
Build Body: Accepts a list of order lists, each containing order parameters.
Request: Authenticated POST to the batch order endpoint.
Parsing: Returns
data.tablewith one row per order result.
Automated Trading Usage
Grid Trading: Place multiple limit orders at different price levels in a single request.
Hedging: Submit simultaneous long and short orders across different symbols.
Efficiency: Reduce API call overhead by batching orders instead of sending them individually.
curl
curl --location --request POST 'https://api-futures.kucoin.com/api/v1/orders/multi' \
--header 'Content-Type: application/json' \
--header 'KC-API-KEY: your-api-key' \
--header 'KC-API-SIGN: your-signature' \
--header 'KC-API-TIMESTAMP: 1729176273859' \
--header 'KC-API-PASSPHRASE: your-passphrase' \
--header 'KC-API-KEY-VERSION: 2' \
--data-raw '[
{
"clientOid": "batch-001",
"symbol": "XBTUSDTM",
"side": "buy",
"type": "limit",
"leverage": 5,
"size": 1,
"price": "49000"
},
{
"clientOid": "batch-002",
"symbol": "XBTUSDTM",
"side": "buy",
"type": "limit",
"leverage": 5,
"size": 1,
"price": "48000"
}
]'Arguments
ordersList of order lists, each with the same fields as
add_order()(i.e.,clientOid,symbol,side,type,leverage,size, and optionalprice,marginMode,positionSide, etc.).
Returns
A data.table (or promise<data.table> if constructed with async = TRUE) with one row per order result:
order_id(character): System-assigned order ID.client_oid(character): Client-provided order ID.symbol(character): Futures symbol.code(character): Per-order status code ("200000"for success).msg(character): Per-order status message.
Examples
\dontrun{
ft <- KucoinFuturesTrading$new()
orders <- list(
list(clientOid = "b1", symbol = "XBTUSDTM", side = "buy",
type = "limit", leverage = 5, size = 1, price = "49000"),
list(clientOid = "b2", symbol = "XBTUSDTM", side = "buy",
type = "limit", leverage = 5, size = 1, price = "48000")
)
results <- ft$add_order_batch(orders)
print(results[, .(order_id, client_oid, code)])
}
Method cancel_order_by_id()
Cancel Order by Order ID
Cancels an open futures order using the system-assigned order ID. If the order has already been filled or cancelled, the API returns an error.
Workflow
Request: Authenticated DELETE with the order ID in the URL path.
Parsing: Returns
data.tablewith the list of cancelled order IDs.
Automated Trading Usage
Stale Order Cleanup: Cancel orders that have not filled within a timeout window.
Position Exit: Cancel pending limit orders before placing a market close to avoid double fills.
curl
curl --location --request DELETE \
'https://api-futures.kucoin.com/api/v1/orders/234125150956625920' \
--header 'KC-API-KEY: your-api-key' \
--header 'KC-API-SIGN: your-signature' \
--header 'KC-API-TIMESTAMP: 1729176273859' \
--header 'KC-API-PASSPHRASE: your-passphrase' \
--header 'KC-API-KEY-VERSION: 2'Method cancel_order_by_client_oid()
Cancel Order by Client Order ID
Cancels an open futures order using the client-provided order ID. Requires the symbol to disambiguate in case client OIDs are reused across symbols.
Workflow
Request: Authenticated DELETE with the client OID in the URL path and
symbolas a query parameter.Parsing: Returns
data.tablewith the cancelled client order ID.
Automated Trading Usage
Idempotent Cancellation: Use client OIDs to cancel orders without needing to store system order IDs.
Consistent State: Cancel by the same ID used to place the order for easier state management in trading bots.
curl
curl --location --request DELETE \
'https://api-futures.kucoin.com/api/v1/orders/client-order/my-order-001?symbol=XBTUSDTM' \
--header 'KC-API-KEY: your-api-key' \
--header 'KC-API-SIGN: your-signature' \
--header 'KC-API-TIMESTAMP: 1729176273859' \
--header 'KC-API-PASSPHRASE: your-passphrase' \
--header 'KC-API-KEY-VERSION: 2'Arguments
clientOidCharacter; the client order ID to cancel.
symbolCharacter; futures symbol (e.g.,
"XBTUSDTM").
Method cancel_all()
Cancel All Orders
Cancels all open futures orders, optionally filtered by symbol. This is a
bulk operation that cancels limit and market orders (but not stop orders;
use cancel_all_stop_orders() for those).
Workflow
Request: Authenticated DELETE with optional
symbolquery parameter.Parsing: Returns
data.tablewith the list of all cancelled order IDs.
Automated Trading Usage
Emergency Stop: Cancel all open orders as part of a kill-switch or panic button.
Strategy Reset: Clear all pending orders before deploying a new trading strategy.
Symbol-Scoped Cleanup: Pass
symbolto cancel only orders for a specific contract without affecting other positions.
curl
curl --location --request DELETE \
'https://api-futures.kucoin.com/api/v1/orders?symbol=XBTUSDTM' \
--header 'KC-API-KEY: your-api-key' \
--header 'KC-API-SIGN: your-signature' \
--header 'KC-API-TIMESTAMP: 1729176273859' \
--header 'KC-API-PASSPHRASE: your-passphrase' \
--header 'KC-API-KEY-VERSION: 2'Arguments
symbolCharacter or NULL; filter by futures symbol. When NULL, cancels all open orders across all symbols.
Method cancel_all_stop_orders()
Cancel All Stop Orders
Cancels all untriggered stop orders, optionally filtered by symbol. This
only affects stop orders that have not yet been triggered; already-triggered
stop orders become regular orders and must be cancelled via cancel_all().
Workflow
Request: Authenticated DELETE to the stop orders endpoint with optional
symbolquery parameter.Parsing: Returns
data.tablewith the list of all cancelled stop order IDs.
Automated Trading Usage
Strategy Teardown: Remove all pending stop-loss and take-profit orders when a strategy is deactivated.
Recalibration: Cancel existing stop orders before placing updated ones at new price levels.
curl
curl --location --request DELETE \
'https://api-futures.kucoin.com/api/v1/stopOrders?symbol=XBTUSDTM' \
--header 'KC-API-KEY: your-api-key' \
--header 'KC-API-SIGN: your-signature' \
--header 'KC-API-TIMESTAMP: 1729176273859' \
--header 'KC-API-PASSPHRASE: your-passphrase' \
--header 'KC-API-KEY-VERSION: 2'Arguments
symbolCharacter or NULL; filter by futures symbol. When NULL, cancels all untriggered stop orders across all symbols.
Method get_order_by_id()
Get Order by Order ID
Retrieves full details of a single futures order by its system-assigned order ID. Returns order status, fill details, timestamps, and all configuration parameters.
Workflow
Request: Authenticated GET with the order ID in the URL path.
Parsing: Returns a single-row
data.tablewith all order fields.Timestamp Conversion: Coerces
created_atandupdated_atfrom milliseconds to POSIXct.
Automated Trading Usage
Order Tracking: Poll order status to determine when a limit order has been filled.
Fill Verification: Check
deal_sizeanddeal_valueto confirm partial or complete fills.Audit Trail: Log full order details for post-trade analysis.
curl
curl --location --request GET \
'https://api-futures.kucoin.com/api/v1/orders/234125150956625920' \
--header 'KC-API-KEY: your-api-key' \
--header 'KC-API-SIGN: your-signature' \
--header 'KC-API-TIMESTAMP: 1729176273859' \
--header 'KC-API-PASSPHRASE: your-passphrase' \
--header 'KC-API-KEY-VERSION: 2'JSON Response
{
"code": "200000",
"data": {
"id": "234125150956625920",
"symbol": "XBTUSDTM",
"type": "limit",
"side": "buy",
"price": "50000",
"size": 1,
"value": "0.00002",
"dealValue": "0",
"dealSize": 0,
"stp": "",
"stop": "",
"stopPriceType": "",
"stopTriggered": false,
"stopPrice": null,
"timeInForce": "GTC",
"postOnly": false,
"hidden": false,
"iceberg": false,
"leverage": "5",
"forceHold": false,
"closeOrder": false,
"visibleSize": null,
"clientOid": "my-order-001",
"remark": null,
"tags": null,
"isActive": true,
"cancelExist": false,
"createdAt": 1729577515473,
"updatedAt": 1729577515473,
"endAt": null,
"orderTime": 1729577515473000000,
"settleCurrency": "USDT",
"marginMode": "ISOLATED",
"avgDealPrice": "0",
"filledSize": 0,
"filledValue": "0",
"status": "open",
"reduceOnly": false
}
}Returns
A single-row data.table (or promise<data.table> if constructed with async = TRUE) with columns:
id(character): Order ID.symbol(character): Contract symbol.type(character): Order type ("limit"or"market").side(character):"buy"or"sell".price(character): Order price.size(integer): Order size in contracts.leverage(character): Leverage multiplier.margin_mode(character):"ISOLATED"or"CROSS".status(character): Order status (e.g.,"open","done").created_at(POSIXct): Order creation time (coerced from milliseconds).updated_at(POSIXct): Last update time (coerced from milliseconds).client_oid(character): Client-provided order ID.
Method get_order_by_client_oid()
Get Order by Client Order ID
Retrieves full details of a single futures order by its client-provided order ID. Useful when the system order ID was not stored at placement time.
Workflow
Request: Authenticated GET with
clientOidas a query parameter.Parsing: Returns a single-row
data.tablewith all order fields.Timestamp Conversion: Coerces
created_atandupdated_atfrom milliseconds to POSIXct.
Automated Trading Usage
Stateless Lookups: Retrieve order status using only the client OID without needing to track system IDs.
Idempotency Checks: Verify if a previously placed order is still active before re-submitting.
curl
curl --location --request GET \
'https://api-futures.kucoin.com/api/v1/orders/byClientOid?clientOid=my-order-001' \
--header 'KC-API-KEY: your-api-key' \
--header 'KC-API-SIGN: your-signature' \
--header 'KC-API-TIMESTAMP: 1729176273859' \
--header 'KC-API-PASSPHRASE: your-passphrase' \
--header 'KC-API-KEY-VERSION: 2'JSON Response
{
"code": "200000",
"data": {
"id": "234125150956625920",
"symbol": "XBTUSDTM",
"type": "limit",
"side": "buy",
"price": "50000",
"size": 1,
"value": "0.00002",
"dealValue": "0",
"dealSize": 0,
"stp": "",
"stop": "",
"stopPriceType": "",
"stopTriggered": false,
"stopPrice": null,
"timeInForce": "GTC",
"postOnly": false,
"hidden": false,
"iceberg": false,
"leverage": "5",
"forceHold": false,
"closeOrder": false,
"visibleSize": null,
"clientOid": "my-order-001",
"remark": null,
"tags": null,
"isActive": true,
"cancelExist": false,
"createdAt": 1729577515473,
"updatedAt": 1729577515473,
"endAt": null,
"orderTime": 1729577515473000000,
"settleCurrency": "USDT",
"marginMode": "ISOLATED",
"avgDealPrice": "0",
"filledSize": 0,
"filledValue": "0",
"status": "open",
"reduceOnly": false
}
}Method get_order_list()
Get Order List
Retrieves a paginated list of futures orders with optional filtering by
status, symbol, side, type, and time range. Use status = "active" for
open orders or status = "done" for closed/cancelled orders.
Workflow
Pagination: Uses
private$.paginate()to fetch all pages of order records.Flattening: Combines all pages into a single
data.tableviaflatten_pages().Timestamp Conversion: Coerces
created_atandupdated_atfrom milliseconds to POSIXct.
Automated Trading Usage
Open Order Monitoring: Query
status = "active"to track all pending orders and detect stale ones.Historical Analysis: Query
status = "done"withstartAt/endAtfor trade performance review.Symbol Filtering: Pass
symbolto retrieve orders for a specific contract only.
curl
curl --location --request GET \
'https://api-futures.kucoin.com/api/v1/orders?status=active&symbol=XBTUSDTM¤tPage=1&pageSize=50' \
--header 'KC-API-KEY: your-api-key' \
--header 'KC-API-SIGN: your-signature' \
--header 'KC-API-TIMESTAMP: 1729176273859' \
--header 'KC-API-PASSPHRASE: your-passphrase' \
--header 'KC-API-KEY-VERSION: 2'JSON Response
{
"code": "200000",
"data": {
"currentPage": 1,
"pageSize": 50,
"totalNum": 1,
"totalPage": 1,
"items": [
{
"id": "234125150956625920",
"symbol": "XBTUSDTM",
"type": "limit",
"side": "buy",
"price": "50000",
"size": 1,
"value": "0.00002",
"dealValue": "0",
"dealSize": 0,
"stp": "",
"stop": "",
"stopPriceType": "",
"stopTriggered": false,
"stopPrice": null,
"timeInForce": "GTC",
"postOnly": false,
"hidden": false,
"iceberg": false,
"leverage": "5",
"forceHold": false,
"closeOrder": false,
"visibleSize": null,
"clientOid": "my-order-001",
"remark": null,
"tags": null,
"isActive": true,
"cancelExist": false,
"createdAt": 1729577515473,
"updatedAt": 1729577515473,
"endAt": null,
"orderTime": 1729577515473000000,
"settleCurrency": "USDT",
"marginMode": "ISOLATED",
"avgDealPrice": "0",
"filledSize": 0,
"filledValue": "0",
"status": "open",
"reduceOnly": false
}
]
}
}Usage
KucoinFuturesTrading$get_order_list(query = list())Arguments
queryNamed list; query parameters. Use
status = "active"for open orders,status = "done"for closed orders. Optional:symbol,side,type,startAt,endAt.
Returns
A data.table (or promise<data.table> if constructed with async = TRUE) with order records; same columns as get_order_by_id().
Returns an empty data.table if no orders match the filters.
Examples
\dontrun{
ft <- KucoinFuturesTrading$new()
# Get all active orders for XBTUSDTM
active <- ft$get_order_list(query = list(status = "active", symbol = "XBTUSDTM"))
print(active[, .(id, side, price, size, status)])
# Get completed orders from the last 7 days
now_ms <- as.integer(as.numeric(Sys.time()) * 1000)
done <- ft$get_order_list(query = list(
status = "done",
startAt = now_ms - 7 * 86400000L,
endAt = now_ms
))
}
Method get_recent_closed_orders()
Get Recent Closed Orders
Retrieves the most recently closed (filled or cancelled) futures orders from the last 24 hours. This is a convenience endpoint that does not require pagination – it returns up to 1000 records in a single response.
Workflow
Request: Authenticated GET with optional
symbolquery parameter.Parsing: Returns
data.tablewith all recently closed order records.Timestamp Conversion: Coerces
created_atandupdated_atfrom milliseconds to POSIXct.
Automated Trading Usage
Quick Fill Check: Rapidly check which orders completed recently without paginating through the full order list.
Slippage Analysis: Compare
avg_deal_priceagainst the originalpricefor recently filled limit orders.Periodic Sync: Poll this endpoint at intervals to update local order state.
curl
curl --location --request GET \
'https://api-futures.kucoin.com/api/v1/recentDoneOrders?symbol=XBTUSDTM' \
--header 'KC-API-KEY: your-api-key' \
--header 'KC-API-SIGN: your-signature' \
--header 'KC-API-TIMESTAMP: 1729176273859' \
--header 'KC-API-PASSPHRASE: your-passphrase' \
--header 'KC-API-KEY-VERSION: 2'JSON Response
{
"code": "200000",
"data": [
{
"id": "234125150956625920",
"symbol": "XBTUSDTM",
"type": "limit",
"side": "buy",
"price": "50000",
"size": 1,
"value": "0.00002",
"dealValue": "0.00002",
"dealSize": 1,
"stp": "",
"stop": "",
"stopPriceType": "",
"stopTriggered": false,
"stopPrice": null,
"timeInForce": "GTC",
"postOnly": false,
"hidden": false,
"iceberg": false,
"leverage": "5",
"forceHold": false,
"closeOrder": false,
"visibleSize": null,
"clientOid": "my-order-001",
"remark": null,
"tags": null,
"isActive": false,
"cancelExist": false,
"createdAt": 1729577515473,
"updatedAt": 1729577815473,
"endAt": 1729577815473,
"orderTime": 1729577515473000000,
"settleCurrency": "USDT",
"marginMode": "ISOLATED",
"avgDealPrice": "50100",
"filledSize": 1,
"filledValue": "0.00002",
"status": "done",
"reduceOnly": false
}
]
}Method get_stop_orders()
Get Stop Orders List
Retrieves a paginated list of untriggered stop orders. Stop orders are conditional orders that become active when a trigger price is reached. Once triggered, they appear in the regular order list.
Workflow
Pagination: Uses
private$.paginate()to fetch all pages of stop order records.Flattening: Combines all pages into a single
data.tableviaflatten_pages().Timestamp Conversion: Coerces
created_atfrom milliseconds to POSIXct.
Automated Trading Usage
Stop-Loss Audit: Verify that stop-loss orders are correctly placed and have not been accidentally cancelled.
Risk Dashboard: Monitor all pending stop orders to assess worst-case risk exposure.
Strategy Sync: Compare existing stop orders against the strategy's intended levels and correct any drift.
curl
curl --location --request GET \
'https://api-futures.kucoin.com/api/v1/stopOrders?symbol=XBTUSDTM¤tPage=1&pageSize=50' \
--header 'KC-API-KEY: your-api-key' \
--header 'KC-API-SIGN: your-signature' \
--header 'KC-API-TIMESTAMP: 1729176273859' \
--header 'KC-API-PASSPHRASE: your-passphrase' \
--header 'KC-API-KEY-VERSION: 2'JSON Response
{
"code": "200000",
"data": {
"currentPage": 1,
"pageSize": 50,
"totalNum": 1,
"totalPage": 1,
"items": [
{
"id": "234125150956625940",
"symbol": "XBTUSDTM",
"type": "limit",
"side": "sell",
"price": "55000",
"size": 1,
"stop": "up",
"stopPriceType": "TP",
"stopTriggered": false,
"stopPrice": "54000",
"timeInForce": "GTC",
"leverage": "5",
"clientOid": "stop-001",
"isActive": true,
"createdAt": 1729577515473,
"marginMode": "ISOLATED",
"status": "open",
"reduceOnly": true
}
]
}
}Usage
KucoinFuturesTrading$get_stop_orders(query = list())Method get_fills()
Get Trade History (Fills)
Retrieves paginated fill (trade execution) history. Each fill represents a partial or complete execution of an order. Includes fee details, liquidity type (maker/taker), and precise trade timestamps.
Workflow
Pagination: Uses
private$.paginate()to fetch all pages of fill records.Flattening: Combines all pages into a single
data.tableviaflatten_pages().Timestamp Conversion: Coerces
trade_timefrom nanoseconds andcreated_atfrom milliseconds to POSIXct.
Automated Trading Usage
PnL Calculation: Aggregate fill prices and sizes to compute realized profit/loss per position.
Fee Tracking: Sum fees across fills for accurate cost accounting.
Maker/Taker Analysis: Monitor
liquidityfield to optimize order placement for fee savings.
curl
curl --location --request GET \
'https://api-futures.kucoin.com/api/v1/fills?symbol=XBTUSDTM¤tPage=1&pageSize=50' \
--header 'KC-API-KEY: your-api-key' \
--header 'KC-API-SIGN: your-signature' \
--header 'KC-API-TIMESTAMP: 1729176273859' \
--header 'KC-API-PASSPHRASE: your-passphrase' \
--header 'KC-API-KEY-VERSION: 2'JSON Response
{
"code": "200000",
"data": {
"currentPage": 1,
"pageSize": 50,
"totalNum": 2,
"totalPage": 1,
"items": [
{
"symbol": "XBTUSDTM",
"tradeId": "5ce24c1f0c19fc3c2ebc1b1a",
"orderId": "234125150956625920",
"side": "buy",
"liquidity": "taker",
"forceTaker": false,
"price": "50100.5",
"size": 1,
"value": "50.1005",
"feeRate": "0.0006",
"fixFee": "0",
"feeCurrency": "USDT",
"stop": "",
"fee": "0.0300603",
"orderType": "limit",
"tradeType": "trade",
"createdAt": 1729577515473,
"settleCurrency": "USDT",
"openFeePay": "0.0300603",
"closeFeePay": "0",
"tradeTime": 1729577515473000000,
"marginMode": "ISOLATED"
},
{
"symbol": "XBTUSDTM",
"tradeId": "5ce24c1f0c19fc3c2ebc1b1b",
"orderId": "234125150956625921",
"side": "sell",
"liquidity": "maker",
"forceTaker": false,
"price": "51200.0",
"size": 1,
"value": "51.2",
"feeRate": "0.0002",
"fixFee": "0",
"feeCurrency": "USDT",
"stop": "",
"fee": "0.01024",
"orderType": "limit",
"tradeType": "trade",
"createdAt": 1729577815473,
"settleCurrency": "USDT",
"openFeePay": "0",
"closeFeePay": "0.01024",
"tradeTime": 1729577815473000000,
"marginMode": "ISOLATED"
}
]
}
}Usage
KucoinFuturesTrading$get_fills(query = list())Arguments
queryNamed list; query parameters. Optional:
orderId,symbol,side,type,startAt,endAt.
Returns
A data.table (or promise<data.table> if constructed with async = TRUE) with columns:
symbol(character): Contract symbol.trade_id(character): Unique trade identifier.order_id(character): Associated order ID.side(character):"buy"or"sell".liquidity(character):"taker"or"maker".price(character): Fill price.size(integer): Fill size in contracts.value(character): Fill value in settlement currency.fee(character): Fee charged.fee_currency(character): Fee currency.fee_rate(character): Fee rate applied.trade_time(POSIXct): Trade timestamp (coerced from nanoseconds).created_at(POSIXct): Record creation time (coerced from milliseconds).
Returns an empty data.table if no fills match the filters.
Examples
\dontrun{
ft <- KucoinFuturesTrading$new()
# Get all fills for XBTUSDTM
fills <- ft$get_fills(query = list(symbol = "XBTUSDTM"))
print(fills[, .(trade_id, side, price, size, fee, trade_time)])
# Get fills for a specific order
order_fills <- ft$get_fills(query = list(orderId = "234125150956625920"))
}
Method get_recent_fills()
Get Recent Fills
Retrieves the most recent fills (last 24 hours, max 1000 records) without pagination. This is a convenience endpoint for quickly checking recent trade executions.
Workflow
Request: Authenticated GET with optional
symbolquery parameter.Parsing: Returns
data.tablewith all recent fill records.Timestamp Conversion: Coerces
trade_timefrom nanoseconds andcreated_atfrom milliseconds to POSIXct.
Automated Trading Usage
Real-Time Monitoring: Poll recent fills to quickly detect new executions without paginating.
Position Updates: Use fill data to update local position tracking in real time.
Latency Checks: Compare
trade_timeagainst order placement time to measure execution speed.
curl
curl --location --request GET \
'https://api-futures.kucoin.com/api/v1/recentFills?symbol=XBTUSDTM' \
--header 'KC-API-KEY: your-api-key' \
--header 'KC-API-SIGN: your-signature' \
--header 'KC-API-TIMESTAMP: 1729176273859' \
--header 'KC-API-PASSPHRASE: your-passphrase' \
--header 'KC-API-KEY-VERSION: 2'JSON Response
{
"code": "200000",
"data": [
{
"symbol": "XBTUSDTM",
"tradeId": "5ce24c1f0c19fc3c2ebc1b1a",
"orderId": "234125150956625920",
"side": "buy",
"liquidity": "taker",
"forceTaker": false,
"price": "50100.5",
"size": 1,
"value": "50.1005",
"feeRate": "0.0006",
"fixFee": "0",
"feeCurrency": "USDT",
"stop": "",
"fee": "0.0300603",
"orderType": "limit",
"tradeType": "trade",
"createdAt": 1729577515473,
"settleCurrency": "USDT",
"openFeePay": "0.0300603",
"closeFeePay": "0",
"tradeTime": 1729577515473000000,
"marginMode": "ISOLATED"
}
]
}Method get_open_order_value()
Get Open Order Value Statistics
Retrieves aggregate statistics about open orders for a specific futures symbol, including total buy/sell quantity and cost. Useful for assessing margin usage and exposure from pending orders.
Workflow
Request: Authenticated GET with
symbolas a required query parameter.Parsing: Returns a single-row
data.tablewith aggregate order statistics.
Automated Trading Usage
Margin Check: Verify available margin before placing new orders by checking existing order costs.
Exposure Monitoring: Track total buy vs sell open order quantities for risk assessment.
Order Capacity: Determine remaining order capacity based on current open order value.
curl
curl --location --request GET \
'https://api-futures.kucoin.com/api/v1/openOrderStatistics?symbol=XBTUSDTM' \
--header 'KC-API-KEY: your-api-key' \
--header 'KC-API-SIGN: your-signature' \
--header 'KC-API-TIMESTAMP: 1729176273859' \
--header 'KC-API-PASSPHRASE: your-passphrase' \
--header 'KC-API-KEY-VERSION: 2'Returns
A single-row data.table (or promise<data.table> if constructed with async = TRUE) with columns:
open_order_buy_qty(integer): Total buy order quantity.open_order_sell_qty(integer): Total sell order quantity.open_order_buy_cost(character): Total buy order cost.open_order_sell_cost(character): Total sell order cost.settle_currency(character): Settlement currency.
Method set_dcp()
Set Dead Connection Protection (DCP)
Configures a dead-man's switch that auto-cancels open futures orders if the client stops sending heartbeat requests within the specified timeout. This protects against scenarios where the trading bot crashes or loses connectivity, preventing orders from remaining open indefinitely.
Workflow
Build Body: Constructs JSON body with
timeout(required) and optionalsymbol.Request: Authenticated POST to the DCP endpoint.
Parsing: Returns
data.tablewith the configured timeout and applicable symbols.
Automated Trading Usage
Safety Net: Set DCP at bot startup so orders are cancelled if the bot crashes.
Heartbeating: Call
set_dcp()periodically (e.g., every 30s withtimeout = 60) to refresh the timer.Disable DCP: Pass
timeout = -1to disable the dead-man's switch.Symbol Scoping: Use
symbolto restrict DCP to specific contracts.
curl
curl --location --request POST 'https://api-futures.kucoin.com/api/v1/orders/dead-cancel-all' \
--header 'Content-Type: application/json' \
--header 'KC-API-KEY: your-api-key' \
--header 'KC-API-SIGN: your-signature' \
--header 'KC-API-TIMESTAMP: 1729176273859' \
--header 'KC-API-PASSPHRASE: your-passphrase' \
--header 'KC-API-KEY-VERSION: 2' \
--data-raw '{"timeout": 60, "symbol": "XBTUSDTM"}'Arguments
timeoutInteger; timeout in seconds. The DCP will cancel all applicable orders if no heartbeat (re-call of
set_dcp()) is received within this period. Use-1to disable DCP.symbolCharacter or NULL; restrict DCP to a specific futures symbol. When NULL, DCP applies to all symbols.
Method get_dcp()
Get Dead Connection Protection (DCP) Settings
Retrieves the current DCP configuration, including the active timeout value, applicable symbols, and the server time of the query. Use this to verify that DCP is correctly configured.
Workflow
Request: Authenticated GET with optional
symbolquery parameter.Parsing: Returns a single-row
data.tablewith the current DCP settings.
Automated Trading Usage
Health Check: Periodically query DCP settings to confirm the safety net is active.
Timeout Verification: Confirm the timeout value matches expectations after calling
set_dcp().
curl
curl --location --request GET \
'https://api-futures.kucoin.com/api/v1/orders/dead-cancel-all/query?symbol=XBTUSDTM' \
--header 'KC-API-KEY: your-api-key' \
--header 'KC-API-SIGN: your-signature' \
--header 'KC-API-TIMESTAMP: 1729176273859' \
--header 'KC-API-PASSPHRASE: your-passphrase' \
--header 'KC-API-KEY-VERSION: 2'Arguments
symbolCharacter or NULL; query DCP settings for a specific futures symbol. When NULL, returns the global DCP configuration.
Examples
if (FALSE) { # \dontrun{
# Synchronous
futures_trading <- KucoinFuturesTrading$new()
# Place a test order (dry run)
result <- futures_trading$add_order_test(
clientOid = "test-001",
symbol = "XBTUSDTM",
side = "buy",
type = "limit",
leverage = 5,
size = 1,
price = "50000"
)
# Cancel an order
futures_trading$cancel_order_by_id("order-id-here")
# Asynchronous
futures_async <- KucoinFuturesTrading$new(async = TRUE)
main <- coro::async(function() {
orders <- await(futures_async$get_order_list(query = list(status = "active")))
print(orders)
})
main()
while (!later::loop_empty()) later::run_now()
} # }
## ------------------------------------------------
## Method `KucoinFuturesTrading$add_order`
## ------------------------------------------------
if (FALSE) { # \dontrun{
ft <- KucoinFuturesTrading$new()
# Place a limit buy order
result <- ft$add_order(
clientOid = "my-order-001",
symbol = "XBTUSDTM",
side = "buy",
type = "limit",
leverage = 5,
size = 1,
price = "50000"
)
print(result$order_id)
# Place a market sell order
result <- ft$add_order(
clientOid = "my-order-002",
symbol = "XBTUSDTM",
side = "sell",
type = "market",
leverage = 10,
size = 2,
reduceOnly = TRUE
)
} # }
## ------------------------------------------------
## Method `KucoinFuturesTrading$add_order_test`
## ------------------------------------------------
if (FALSE) { # \dontrun{
ft <- KucoinFuturesTrading$new()
# Validate a limit order without placing it
result <- ft$add_order_test(
clientOid = "test-001",
symbol = "XBTUSDTM",
side = "buy",
type = "limit",
leverage = 5,
size = 1,
price = "50000"
)
print(result)
} # }
## ------------------------------------------------
## Method `KucoinFuturesTrading$add_order_batch`
## ------------------------------------------------
if (FALSE) { # \dontrun{
ft <- KucoinFuturesTrading$new()
orders <- list(
list(clientOid = "b1", symbol = "XBTUSDTM", side = "buy",
type = "limit", leverage = 5, size = 1, price = "49000"),
list(clientOid = "b2", symbol = "XBTUSDTM", side = "buy",
type = "limit", leverage = 5, size = 1, price = "48000")
)
results <- ft$add_order_batch(orders)
print(results[, .(order_id, client_oid, code)])
} # }
## ------------------------------------------------
## Method `KucoinFuturesTrading$cancel_order_by_id`
## ------------------------------------------------
if (FALSE) { # \dontrun{
ft <- KucoinFuturesTrading$new()
result <- ft$cancel_order_by_id("234125150956625920")
print(result$cancelled_order_ids)
} # }
## ------------------------------------------------
## Method `KucoinFuturesTrading$cancel_order_by_client_oid`
## ------------------------------------------------
if (FALSE) { # \dontrun{
ft <- KucoinFuturesTrading$new()
result <- ft$cancel_order_by_client_oid("my-order-001", symbol = "XBTUSDTM")
print(result$client_oid)
} # }
## ------------------------------------------------
## Method `KucoinFuturesTrading$cancel_all`
## ------------------------------------------------
if (FALSE) { # \dontrun{
ft <- KucoinFuturesTrading$new()
# Cancel all open orders for XBTUSDTM
result <- ft$cancel_all(symbol = "XBTUSDTM")
print(result$cancelled_order_ids)
# Cancel all open orders across all symbols
result <- ft$cancel_all()
} # }
## ------------------------------------------------
## Method `KucoinFuturesTrading$cancel_all_stop_orders`
## ------------------------------------------------
if (FALSE) { # \dontrun{
ft <- KucoinFuturesTrading$new()
# Cancel all stop orders for XBTUSDTM
result <- ft$cancel_all_stop_orders(symbol = "XBTUSDTM")
print(result$cancelled_order_ids)
# Cancel all stop orders across all symbols
result <- ft$cancel_all_stop_orders()
} # }
## ------------------------------------------------
## Method `KucoinFuturesTrading$get_order_by_id`
## ------------------------------------------------
if (FALSE) { # \dontrun{
ft <- KucoinFuturesTrading$new()
order <- ft$get_order_by_id("234125150956625920")
print(order[, .(id, symbol, side, price, size, status)])
} # }
## ------------------------------------------------
## Method `KucoinFuturesTrading$get_order_by_client_oid`
## ------------------------------------------------
if (FALSE) { # \dontrun{
ft <- KucoinFuturesTrading$new()
order <- ft$get_order_by_client_oid("my-order-001")
print(order[, .(id, symbol, side, price, size, status)])
} # }
## ------------------------------------------------
## Method `KucoinFuturesTrading$get_order_list`
## ------------------------------------------------
if (FALSE) { # \dontrun{
ft <- KucoinFuturesTrading$new()
# Get all active orders for XBTUSDTM
active <- ft$get_order_list(query = list(status = "active", symbol = "XBTUSDTM"))
print(active[, .(id, side, price, size, status)])
# Get completed orders from the last 7 days
now_ms <- as.integer(as.numeric(Sys.time()) * 1000)
done <- ft$get_order_list(query = list(
status = "done",
startAt = now_ms - 7 * 86400000L,
endAt = now_ms
))
} # }
## ------------------------------------------------
## Method `KucoinFuturesTrading$get_recent_closed_orders`
## ------------------------------------------------
if (FALSE) { # \dontrun{
ft <- KucoinFuturesTrading$new()
# Get recently closed orders for XBTUSDTM
recent <- ft$get_recent_closed_orders(symbol = "XBTUSDTM")
print(recent[, .(id, side, price, status, created_at)])
# Get all recently closed orders
all_recent <- ft$get_recent_closed_orders()
} # }
## ------------------------------------------------
## Method `KucoinFuturesTrading$get_stop_orders`
## ------------------------------------------------
if (FALSE) { # \dontrun{
ft <- KucoinFuturesTrading$new()
# Get all untriggered stop orders for XBTUSDTM
stops <- ft$get_stop_orders(query = list(symbol = "XBTUSDTM"))
print(stops[, .(id, side, stop_price, price, status)])
# Get all untriggered stop orders
all_stops <- ft$get_stop_orders()
} # }
## ------------------------------------------------
## Method `KucoinFuturesTrading$get_fills`
## ------------------------------------------------
if (FALSE) { # \dontrun{
ft <- KucoinFuturesTrading$new()
# Get all fills for XBTUSDTM
fills <- ft$get_fills(query = list(symbol = "XBTUSDTM"))
print(fills[, .(trade_id, side, price, size, fee, trade_time)])
# Get fills for a specific order
order_fills <- ft$get_fills(query = list(orderId = "234125150956625920"))
} # }
## ------------------------------------------------
## Method `KucoinFuturesTrading$get_recent_fills`
## ------------------------------------------------
if (FALSE) { # \dontrun{
ft <- KucoinFuturesTrading$new()
# Get recent fills for XBTUSDTM
recent <- ft$get_recent_fills(symbol = "XBTUSDTM")
print(recent[, .(trade_id, side, price, size, fee, trade_time)])
# Get all recent fills
all_recent <- ft$get_recent_fills()
} # }
## ------------------------------------------------
## Method `KucoinFuturesTrading$get_open_order_value`
## ------------------------------------------------
if (FALSE) { # \dontrun{
ft <- KucoinFuturesTrading$new()
stats <- ft$get_open_order_value(symbol = "XBTUSDTM")
print(stats[, .(open_order_buy_qty, open_order_sell_qty, settle_currency)])
} # }
## ------------------------------------------------
## Method `KucoinFuturesTrading$set_dcp`
## ------------------------------------------------
if (FALSE) { # \dontrun{
ft <- KucoinFuturesTrading$new()
# Enable DCP with 60-second timeout for XBTUSDTM
result <- ft$set_dcp(timeout = 60, symbol = "XBTUSDTM")
print(result)
# Disable DCP
ft$set_dcp(timeout = -1)
} # }
## ------------------------------------------------
## Method `KucoinFuturesTrading$get_dcp`
## ------------------------------------------------
if (FALSE) { # \dontrun{
ft <- KucoinFuturesTrading$new()
# Check DCP settings for XBTUSDTM
dcp <- ft$get_dcp(symbol = "XBTUSDTM")
print(dcp[, .(timeout, symbols)])
# Check global DCP settings
dcp_global <- ft$get_dcp()
} # }