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

Cancels an order and returns once the exchange confirms the cancellation. By default, the order is canceled through the exchange's REST API; when the via option is ws, it is canceled through the exchange's WebSocket trading API. It can be called from the main thread and from threads created by threading.Thread(). The version that does not wait for the cancellation result is ctx.cancelAsync.

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

Examples

Cancels an order via WebSocket; prints the reason for the failure when it cannot be canceled (e.g. the order has already been filled).

javascript
function main() { var t = exchange.GetTicker("BTC_USDT") if (!t) { return } var id = ctx.order(exchange, "BTC_USDT", "buy", _N(t.Buy * 0.98, 2), 0.001, {via: "ws"}) if (id instanceof Error) { Log("order failed:", id.message) return } var r = ctx.cancel(exchange, "BTC_USDT", id, {via: "ws", timeout: 3000}) if (r instanceof Error) { Log("cancel failed:", r.message) } else { Log("canceled:", id) } }
python
def main(): t = exchange.GetTicker("BTC_USDT") if not t: return oid = ctx.order(exchange, "BTC_USDT", "buy", _N(t["Buy"] * 0.98, 2), 0.001, {"via": "ws"}) if isinstance(oid, CtxError): Log("order failed:", oid) return r = ctx.cancel(exchange, "BTC_USDT", oid, {"via": "ws", "timeout": 3000}) if isinstance(r, CtxError): Log("cancel failed:", r) else: Log("canceled:", oid)
rust
fn main() { let Ok(t) = exchange.GetTicker("BTC_USDT") else { return }; let px = _N(t.Buy * 0.98, 2); let id = match ctx::order(exchange, "BTC_USDT", "buy", px, 0.001, r#"{"via":"ws"}"#) { Ok(id) => id, Err(e) => { Log!("order failed:", e); return; } }; match ctx::cancel_with(exchange, "BTC_USDT", &id, r#"{"via":"ws","timeout":3000}"#) { Ok(()) => Log!("canceled:", id), Err(e) => Log!("cancel failed:", e), } }

Returns

TypeDescription

undefined / object

Returns nothing when the cancellation succeeds; when the cancellation fails, no exception is thrown and an Error object is returned instead, whose message property contains the reason for the failure.

Arguments

NameTypeRequiredDescription

ex

object / number

Yes

The account the order belongs to: either an exchange object or the index of that account in the exchanges array, the same as in 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 return value of ctx.order, or the id in an order update.

options

object

No

Cancellation options {via, timeout}: via specifies the cancellation channel and takes the value rest (default) or ws; timeout is the timeout for waiting for the exchange's response, in milliseconds (the default value is used when it is omitted or set to 0). No other fields are supported.

See Also

Remarks

If the order has already been filled or has reached another final state and cannot be canceled, this is also treated as a cancellation failure, and an Error object with the reason for the failure is returned. An error is also returned on network failures, authentication failures, or when the exchange does not explicitly confirm the cancellation.

When via is ws, the rules are the same as for ctx.order: it can only be used with exchanges that support WebSocket order placement; if the exchange does not support it, an error is returned directly, with no automatic fallback to the REST API. If the trading connection is unavailable before the request is sent, the order is automatically canceled through the REST API instead.

In Python, the signature is ctx.cancel(ex, symbol, id, opts=None); it returns None on success and a CtxError object on failure (no exception is thrown). In Rust, the signature is ctx::cancel(ex, symbol: &str, id: &str) -> Result<(), String>; to pass options, use ctx::cancel_with(ex, symbol, id, opts_json: &str), where the options are passed as a JSON string, e.g. r#"{"via":"ws"}"#.