Type/to search
Built-in Functions
Global
Log
Market
Trade
Account
Futures
Exchange
IO
Network
Storage
Threads
exchange.Go
EventLoop
ctx.subscribe
ctx.poll
ctx.order
ctx.cancel
ctx.orderAsync
ctx.cancelAsync
Subscription
threading
Thread
ThreadLock
ThreadEvent
ThreadCondition
ThreadDict
Server
TA
Talib
OS
Structures
Built-in Variables and Constants
Conventions
Exchanges

Asynchronous order cancellation: returns a request ID immediately without waiting for exchange confirmation. The cancellation result is delivered as an acknowledgment event (kind is 20, op is cancel) to the thread that issued the request, and can be retrieved with ctx.poll. Parameters are the same as ctx.cancel.

ctx.cancelAsync(ex, symbol, id)
ctx.cancelAsync(ex, symbol, id, options)

Examples

Place an order asynchronously, issue an asynchronous cancellation after receiving the order acknowledgment, and wait for the cancellation acknowledgment.

javascript
function waitAck(req) { while (true) { var ack = ctx.poll([ctx.STREAM_ORDER_ACK], 10000) if (!ack) { return null } if (ack.kind === 20 && ack.reqId === req) { return ack } } } function main() { var t = exchange.GetTicker("BTC_USDT") if (!t) { return } var req = ctx.orderAsync(exchange, "BTC_USDT", "buy", _N(t.Buy * 0.98, 2), 0.001) if (req instanceof Error) { Log("not sent:", req.message) return } var placed = waitAck(req) if (!placed || !placed.ok) { Log("order failed:", placed ? placed.error : "no ack") return } var creq = ctx.cancelAsync(exchange, "BTC_USDT", placed.id) if (creq instanceof Error) { Log("not sent:", creq.message) return } var canceled = waitAck(creq) Log("cancel", placed.id, canceled ? canceled.status : "no ack", canceled ? canceled.error : "") }
python
def wait_ack(req): while True: ack = ctx.poll([ctx.STREAM_ORDER_ACK], 10000) if ack is None: return None if ack.kind == 20 and ack.reqId == req: return ack def main(): t = exchange.GetTicker("BTC_USDT") if not t: return req = ctx.orderAsync(exchange, "BTC_USDT", "buy", _N(t["Buy"] * 0.98, 2), 0.001) if isinstance(req, CtxError): Log("not sent:", req) return placed = wait_ack(req) if placed is None or not placed.ok: Log("order failed:", placed.error if placed else "no ack") return creq = ctx.cancelAsync(exchange, "BTC_USDT", placed.id) if isinstance(creq, CtxError): Log("not sent:", creq) return canceled = wait_ack(creq) if canceled is None: Log("cancel", placed.id, "no ack") else: Log("cancel", placed.id, canceled.status, canceled.error)
rust
use sdk::ctx::{EventExt, OrderAckView}; fn wait_ack(req: u64) -> Option<OrderAckView> { loop { match ctx::poll(&[ctx::STREAM_ORDER_ACK], Some(10000)) { ctx::Polled::Event(ev) => { if let Some(ack) = ev.order_ack().filter(|a| a.req_id == req) { return Some(ack); } } ctx::Polled::Empty | ctx::Polled::Stopped => return None, _ => {} } } } fn main() { let Ok(t) = exchange.GetTicker("BTC_USDT") else { return }; let px = _N(t.Buy * 0.98, 2); let req = match ctx::order_async(exchange, "BTC_USDT", "buy", px, 0.001, "{}") { Ok(req) => req, Err(e) => { Log!("not sent:", e); return; } }; let Some(placed) = wait_ack(req).filter(|a| a.ok) else { Log!("order failed"); return; }; match ctx::cancel_async(exchange, "BTC_USDT", &placed.id, "{}") { Ok(creq) => match wait_ack(creq) { Some(ack) => Log!("cancel", placed.id, ack.status, ack.error), None => Log!("cancel", placed.id, "no ack"), }, Err(e) => Log!("not sent:", e), } }

Returns

TypeDescription

number / object

The request ID, matching the reqId in the acknowledgment event. If the arguments are invalid, the exchange does not support via set to ws, or more than 256 requests are in flight, an Error object is returned immediately and no acknowledgment event is generated.

Arguments

NameTypeRequiredDescription

ex

object / number

Yes

The account the order belongs to: an exchange object, or the index of that account in the exchanges array, same as ctx.order.

symbol

string

Yes

The trading symbol of the order, e.g. BTC_USDT, BTC_USDT.swap.

id

string

Yes

The ID of the order to cancel, i.e. the order ID returned when the order was placed, or the id in the order report.

options

object

No

Cancellation options {via, timeout}, same as ctx.cancel.

See Also

Remarks

For the fields of the acknowledgment event, see ctx.orderAsync: on successful cancellation, status is ok and id is the ID of the canceled order; if the order cannot be canceled because it has already been filled or is in another terminal state, status is failed and error contains the failure reason.

Order and cancellation requests sent at the same time are not queued behind each other; acknowledgments arrive in order of completion. When canceling an order placed asynchronously, first wait for its order acknowledgment to obtain the order ID, then cancel it.

In Python, the signature is ctx.cancelAsync(ex, symbol, id, opts=None), returning a request ID or a CtxError object. In Rust, it is ctx::cancel_async(ex, symbol: &str, id: &str, opts_json: &str) -> Result<u64, String>; pass "{}" when there are no options, and read the acknowledgment event via ev.order_ack().