Batch Cancel OCO Orders (Implementation)
Source:R/impl_spottrading_orders_oco.R
cancel_oco_order_batch_impl.Rd
Cancels a batch of OCO (One-Cancels-the-Other) orders on the KuCoin Spot trading system asynchronously by sending a DELETE request to the /api/v3/oco/orders
endpoint.
Returns a data.table
with the IDs of the canceled limit and stop-limit orders associated with the OCO orders.
Usage
cancel_oco_order_batch_impl(
keys = get_api_keys(),
base_url = get_base_url(),
query = list(),
.__coro_env_parent__ = <environment>
)
Arguments
- keys
List; API configuration parameters from
get_api_keys()
. Defaults toget_api_keys()
.- base_url
Character string; base URL for the KuCoin API. Defaults to
get_base_url()
.- query
Named list; optional query parameters for filtering (e.g.,
list(orderIds = "674c388172cf2800072ee746,674c38bdfd8300000795167e", symbol = "BTC-USDT")
).
Value
Promise resolving to a data.table
with one row containing:
cancelledOrderIds
(list): A list of character strings representing the IDs of the canceled limit and stop-limit orders associated with the OCO orders.
Details
What is an OCO Order and Batch Cancellation?
An OCO (One-Cancels-the-Other) order pairs a limit order (e.g., to secure profits) with a stop-limit order (e.g., to cap losses), canceling one when the other executes. Batch cancellation of OCO orders is useful for:
Portfolio Cleanup: Remove all OCO orders for a symbol (e.g., "BTC-USDT") or specific IDs if market conditions no longer favor your strategy.
Risk Adjustment: Cancel multiple OCOs during unexpected volatility to avoid unintended triggers (e.g., canceling BTC-USDT OCOs during a flash crash).
Strategy Overhaul: Reset all OCO orders across trading pairs to implement a new trading plan without manual intervention. For example, if you have OCO orders on BTC-USDT to sell at $55,000 (profit) or $48,000 (stop-loss) but anticipate a breakout, batch canceling them prevents premature execution.
Description
This function cancels multiple OCO orders using optional filters (orderIds
, symbol
) via a DELETE request. If no parameters are provided, all OCO orders are canceled by default. The response includes the IDs of the canceled limit and stop-limit orders.
Workflow
Parameter Validation: Ensures
query
is a named list and validatessymbol
andorderIds
if provided.Request Construction: Builds the endpoint URL with query parameters (
orderIds
,symbol
) usingbuild_query
.Authentication: Generates private API headers using
build_headers()
with the DELETE method and endpoint.API Request: Sends a DELETE request to the KuCoin API with a 3-second timeout via
httr::DELETE
.Response Processing: Parses the response with
process_kucoin_response
, confirms success ("200000"), and converts thecancelledOrderIds
array to adata.table
column as a list.
API Details
Endpoint:
DELETE https://api.kucoin.com/api/v3/oco/orders
Domain: Spot
API Channel: Private
API Permission: Spot
Rate Limit Pool: Spot
Rate Limit Weight: 3
SDK Service: Spot
SDK Sub-Service: Order
SDK Method Name: batchCancelOcoOrders
Official Documentation: KuCoin Batch Cancel OCO Order
Request
Query Parameters
orderIds
: String (optional) - Comma-separated list of OCO order IDs (e.g., "674c388172cf2800072ee746,674c38bdfd8300000795167e"). If omitted, all OCO orders are canceled.symbol
: String (optional) - Trading pair symbol (e.g., "BTC-USDT"). If omitted, OCO orders for all symbols are canceled.
Examples
if (FALSE) { # \dontrun{
library(coro)
library(data.table)
main_async <- coro::async(function() {
# Batch cancel specific OCO orders for BTC-USDT
canceled_orders <- await(cancel_oco_order_batch_impl(
query = list(
orderIds = "674c388172cf2800072ee746,674c38bdfd8300000795167e",
symbol = "BTC-USDT"
)
))
print(canceled_orders)
})
# Run the async function
main_async()
while (!later::loop_empty()) later::run_now()
} # }