KucoinSpotCancelOrder Class for KuCoin Spot Trading Order Cancellation
Source:R/KucoinSpotCancelOrder.R
KucoinSpotCancelOrder.Rd
The KucoinSpotCancelOrder
class provides an asynchronous interface for canceling spot trading orders on KuCoin.
It leverages the coro
package for non-blocking HTTP requests, returning promises that resolve to data.table
objects.
This class supports canceling individual orders by orderId
or clientOid
, partially canceling an order, canceling all orders
for a specific symbol, and canceling all orders across all symbols.
Details
Purpose and Scope
This class focuses exclusively on order cancellation operations in the KuCoin Spot trading system, including:
Individual Cancellation: Cancel a single order by
orderId
orclientOid
.Partial Cancellation: Reduce the quantity of a specific order.
Bulk Cancellation: Cancel all orders for a symbol or across all symbols.
API Endpoint
Not applicable (class-level documentation; see individual methods for specific endpoints).
Usage
Utilised by traders and developers to programmatically cancel Spot trading orders on KuCoin. The class is initialized with API credentials,
automatically loaded via get_api_keys()
if not provided, and a base URL from get_base_url()
. For detailed endpoint information,
parameters, and response schemas, refer to the official KuCoin API Documentation.
Methods
initialize(keys, base_url): Initialises the object with API credentials and base URL.
cancel_order_by_order_id(orderId, symbol): Cancels an order by its system-generated
orderId
.cancel_order_by_client_oid(clientOid, symbol): Cancels an order by its client-assigned
clientOid
.cancel_partial_order(orderId, symbol, cancelSize): Partially cancels an order by
orderId
with a specified quantity.cancel_all_orders_by_symbol(symbol): Cancels all orders for a specific symbol.
cancel_all_orders(): Cancels all spot orders across all symbols.
Public fields
keys
List containing KuCoin API keys (
api_key
,api_secret
,api_passphrase
,key_version
).base_url
Character string representing the base URL for KuCoin API endpoints. Initialise a New KucoinSpotCancelOrder Object
Description
Initialises a
KucoinSpotCancelOrder
object with API credentials and a base URL for canceling Spot trading orders asynchronously. If not provided, credentials are sourced fromget_api_keys()
and the base URL fromget_base_url()
.Workflow Overview
Credential Assignment: Sets
self$keys
to the provided or default API keys.URL Assignment: Sets
self$base_url
to the provided or default base URL.
Automated Trading Usage
Cancellation Hub: Use this as the central object for all cancellation operations in your trading bot, streamlining order termination logic.
Secure Setup: Load credentials via
get_api_keys()
from a secure source (e.g., environment variables or a vault), ensuring safe deployment.Integration: Pair with querying classes (e.g.,
KucoinSpotOrderQueries
) to verify cancellations and maintain order state consistency.
Methods
Method new()
Usage
KucoinSpotCancelOrder$new(keys = get_api_keys(), base_url = get_base_url())
Arguments
keys
List containing API configuration parameters from
get_api_keys()
, including:api_key
: Character string; your KuCoin API key.api_secret
: Character string; your KuCoin API secret.api_passphrase
: Character string; your KuCoin API passphrase.key_version
: Character string; API key version (e.g.,"2"
). Defaults toget_api_keys()
.
base_url
Character string representing the base URL for the API. Defaults to
get_base_url()
.
Returns
A new instance of the KucoinSpotCancelOrder
class.
Cancel Order By OrderId
Description
Cancels a spot order by its system-generated orderId
via a DELETE request to /api/v1/hf/orders/{orderId}
.
Calls cancel_order_by_order_id_impl
.
Workflow Overview
Validation: Ensures
orderId
andsymbol
are valid.URL: Constructs endpoint with
orderId
andsymbol
.Authentication: Generates headers.
API Call: Sends DELETE request.
Response: Returns the canceled
orderId
.
Automated Trading Usage
Targeted Cancellation: Cancel specific orders when market conditions shift (e.g., cancel a buy order if price spikes), using
orderId
from placement logs.Status Verification: Post-cancellation, confirm with a query class (e.g.,
get_order_by_order_id
) to ensure completion before re-ordering.Error Handling: If cancellation fails (e.g., already filled), log the
orderId
and retry or adjust strategy based on status.
Method cancel_order_by_order_id()
Arguments
orderId
Character string; system-generated order ID (e.g., "671124f9365ccb00073debd4"). Required.
symbol
Character string; trading pair (e.g., "BTC-USDT"). Required.
Returns
Promise resolving to a data.table
with:
orderId
(character): The canceled order ID.
Description
Cancels a spot order by its client-assigned clientOid
via a DELETE request to /api/v1/hf/orders/client-order/{clientOid}
.
Calls cancel_order_by_client_oid_impl
.
Workflow Overview
Validation: Ensures
clientOid
andsymbol
are valid.URL: Constructs endpoint with
clientOid
andsymbol
.Authentication: Generates headers.
API Call: Sends DELETE request.
Response: Returns the canceled
clientOid
.
API Endpoint
DELETE https://api.kucoin.com/api/v1/hf/orders/client-order/{clientOid}?symbol={symbol}
Automated Trading Usage
Custom Tracking: Cancel orders by
clientOid
tied to your system’s identifiers (e.g., "STRAT1_001"), avoidingorderId
mapping.Event-Driven: Trigger cancellation based on signals (e.g., volatility spike), using stored
clientOid
values from your database.Confirmation: Verify with a query class (e.g.,
get_order_by_client_oid
) to update order status in real-time.
Method cancel_order_by_client_oid()
Arguments
clientOid
Character string; client-assigned order ID (e.g., "5c52e11203aa677f33e493fb"). Required.
symbol
Character string; trading pair (e.g., "BTC-USDT"). Required.
Returns
Promise resolving to a data.table
with:
clientOid
(character): The canceled client order ID.
Description
Partially cancels a spot order by its orderId
with a specified quantity via a DELETE request to /api/v1/hf/orders/cancel/{orderId}
.
Calls cancel_partial_order_impl
.
Workflow Overview
Validation: Ensures
orderId
,symbol
, andcancelSize
are valid.URL: Constructs endpoint with
orderId
,symbol
, andcancelSize
.Authentication: Generates headers.
API Call: Sends DELETE request.
Response: Returns the
orderId
andcancelSize
.
API Endpoint
DELETE https://api.kucoin.com/api/v1/hf/orders/cancel/{orderId}?symbol={symbol}&cancelSize={cancelSize}
Automated Trading Usage
Position Adjustment: Reduce exposure (e.g., cancel 0.01 BTC of a 0.05 BTC order) when risk thresholds are approached, using dynamic
cancelSize
calculations.Liquidity Management: Partially cancel large orders to free capital based on market depth updates, maintaining some market presence.
Post-Cancel Check: Use a query class to confirm remaining
size
viaget_order_by_order_id
, ensuring the adjustment took effect.
Method cancel_partial_order()
Arguments
orderId
Character string; system-generated order ID (e.g., "6711f73c1ef16c000717bb31"). Required.
symbol
Character string; trading pair (e.g., "BTC-USDT"). Required.
cancelSize
Character string; quantity to cancel (e.g., "0.00001"). Required.
Returns
Promise resolving to a data.table
with:
orderId
(character): The partially canceled order ID.cancelSize
(character): The canceled quantity.
Description
Cancels all spot orders for a specified symbol via a DELETE request to /api/v1/hf/orders
.
Calls cancel_all_orders_by_symbol_impl
.
Workflow Overview
Validation: Ensures
symbol
is valid.URL: Constructs endpoint with
symbol
.Authentication: Generates headers.
API Call: Sends DELETE request.
Response: Returns a success indicator.
Automated Trading Usage
Market Exit: Clear all orders for a symbol (e.g., "BTC-USDT") during strategy shifts or market closeouts, triggered by price thresholds.
Risk Mitigation: Cancel all orders for a volatile symbol if risk metrics (e.g., VaR) exceed limits, using real-time data.
Post-Cancel Audit: Verify with
get_open_orders
from a query class to ensure no orders remain, logging any discrepancies.
Method cancel_all_orders_by_symbol()
Returns
Promise resolving to a data.table
with:
result
(character): "success" if the request is accepted.
Description
Cancels all spot orders across all symbols via a DELETE request to /api/v1/hf/orders/cancelAll
.
Calls cancel_all_orders_impl
.
Workflow Overview
URL: Constructs endpoint with no parameters.
Authentication: Generates headers.
API Call: Sends DELETE request.
Response: Returns successful and failed symbols.
Automated Trading Usage
Emergency Stop: Execute during critical events (e.g., account breaches, market crashes) to halt all trading, triggered by risk alerts.
Portfolio Reset: Clear all orders before a major strategy overhaul, ensuring a clean slate for new setups.
Failure Analysis: Inspect
failedSymbols
to identify cancellation issues (e.g., already filled orders), logging errors for debugging.
Method cancel_all_orders()
Examples
if (FALSE) { # \dontrun{
# Comprehensive example demonstrating key methods
main_async <- coro::async(function() {
# Initialise the class
cancellations <- KucoinSpotCancelOrder$new()
# Cancel an order by orderId
canceled_order <- await(cancellations$cancel_order_by_order_id("671124f9365ccb00073debd4", "BTC-USDT"))
print("Canceled by OrderId:"); print(canceled_order)
# Cancel an order by clientOid
canceled_client <- await(cancellations$cancel_order_by_client_oid("5c52e11203aa677f33e493fb", "BTC-USDT"))
print("Canceled by ClientOid:"); print(canceled_client)
# Partially cancel an order
partial_cancel <- await(cancellations$cancel_partial_order("6711f73c1ef16c000717bb31", "BTC-USDT", "0.00001"))
print("Partial Cancellation:"); print(partial_cancel)
# Cancel all orders for a symbol
symbol_cancel <- await(cancellations$cancel_all_orders_by_symbol("BTC-USDT"))
print("All Orders for Symbol Canceled:"); print(symbol_cancel)
# Cancel all orders across all symbols
all_cancel <- await(cancellations$cancel_all_orders())
print("All Orders Canceled:"); print(all_cancel)
})
main_async()
while (!later::loop_empty()) later::run_now()
} # }