exchange.CreateConditionOrder
Creates a conditional order. A conditional order is executed automatically once its trigger condition is met.
exchange.CreateConditionOrder(symbol, side, amount, condition)
exchange.CreateConditionOrder(symbol, side, amount, condition, ...args)Examples
-
Create a take-profit (TP) order that sells automatically once the price rises to the target level.
javascriptfunction main() { // Create a take-profit order: when the BTC_USDT price rises to 65000, sell 0.01 BTC at a price of 65000 var condition = { ConditionType: ORDER_CONDITION_TYPE_TP, // Take-profit order TpTriggerPrice: 65000, // Trigger price TpOrderPrice: 65000 // Order price; can also be set to -1 to place a market order } var id = exchange.CreateConditionOrder("BTC_USDT", "sell", 0.01, condition) Log("TP order Id:", id) }pythondef main(): # Create a take-profit order: when the BTC_USDT price rises to 65000, sell 0.01 BTC at a price of 65000 condition = { "ConditionType": ORDER_CONDITION_TYPE_TP, # Take-profit order "TpTriggerPrice": 65000, # Trigger price "TpOrderPrice": 65000 # Order price; can also be set to -1 to place a market order } id = exchange.CreateConditionOrder("BTC_USDT", "sell", 0.01, condition) Log("TP order Id:", id)rustfn main() { // Create a take-profit order: when the BTC_USDT price rises to 65000, sell 0.01 BTC at a price of 65000 let condition = OrderCondition { ConditionType: ORDER_CONDITION_TYPE_TP, // Take-profit order TpTriggerPrice: 65000.0, // Trigger price TpOrderPrice: 65000.0, // Order price; can also be set to -1 to place a market order ..Default::default() }; let id = exchange.CreateConditionOrder("BTC_USDT", "sell", 0.01, &condition); Log!("TP order Id:", id); } -
Create a stop-loss (SL) order that sells automatically once the price falls to the stop level.
javascriptfunction main() { // Create a stop-loss order: when the BTC_USDT price falls to 58000, sell 0.01 BTC at market price var condition = { ConditionType: ORDER_CONDITION_TYPE_SL, // Stop-loss order SlTriggerPrice: 58000, // Trigger price SlOrderPrice: -1 // -1 means place a market order } var id = exchange.CreateConditionOrder("BTC_USDT", "sell", 0.01, condition) Log("SL order Id:", id) }pythondef main(): # Create a stop-loss order: when the BTC_USDT price falls to 58000, sell 0.01 BTC at market price condition = { "ConditionType": ORDER_CONDITION_TYPE_SL, # Stop-loss order "SlTriggerPrice": 58000, # Trigger price "SlOrderPrice": -1 # -1 means place a market order } id = exchange.CreateConditionOrder("BTC_USDT", "sell", 0.01, condition) Log("SL order Id:", id)rustfn main() { // Create a stop-loss order: when the BTC_USDT price falls to 58000, sell 0.01 BTC at market price let condition = OrderCondition { ConditionType: ORDER_CONDITION_TYPE_SL, // Stop-loss order SlTriggerPrice: 58000.0, // Trigger price SlOrderPrice: -1.0, // -1 means place a market order ..Default::default() }; let id = exchange.CreateConditionOrder("BTC_USDT", "sell", 0.01, &condition); Log!("SL order Id:", id); } -
Creates an OCO order: sets both a take-profit and a stop-loss at the same time. When either side is triggered, the order on the other side is canceled automatically.
javascriptfunction main() { // Create an OCO order: take-profit price 65000, stop-loss price 58000 var condition = { ConditionType: ORDER_CONDITION_TYPE_OCO, // Condition type: OCO order TpTriggerPrice: 65000, // Take-profit trigger price TpOrderPrice: 65000, // Take-profit order price SlTriggerPrice: 58000, // Stop-loss trigger price SlOrderPrice: 58000 // Stop-loss order price } var id = exchange.CreateConditionOrder("BTC_USDT", "sell", 0.01, condition) Log("OCO order Id:", id) }pythondef main(): # Create an OCO order: take-profit price 65000, stop-loss price 58000 condition = { "ConditionType": ORDER_CONDITION_TYPE_OCO, # Condition type: OCO order "TpTriggerPrice": 65000, # Take-profit trigger price "TpOrderPrice": 65000, # Take-profit order price "SlTriggerPrice": 58000, # Stop-loss trigger price "SlOrderPrice": 58000 # Stop-loss order price } id = exchange.CreateConditionOrder("BTC_USDT", "sell", 0.01, condition) Log("OCO order Id:", id)rustfn main() { // Create an OCO order: take-profit price 65000, stop-loss price 58000 let condition = OrderCondition { ConditionType: ORDER_CONDITION_TYPE_OCO, // Condition type: OCO order TpTriggerPrice: 65000.0, // Take-profit trigger price TpOrderPrice: 65000.0, // Take-profit order price SlTriggerPrice: 58000.0, // Stop-loss trigger price SlOrderPrice: 58000.0 // Stop-loss order price }; let id = exchange.CreateConditionOrder("BTC_USDT", "sell", 0.01, &condition); Log!("OCO order Id:", id); } -
Creates a conditional order with an extra argument (option) that passes exchange-specific parameters.
javascriptfunction main() { // Exchange-specific parameters go in the last object argument var option = { "type": "TRAILING_STOP_MARKET", "activatePrice": "300", "callbackRate": "0.1" } var condition = { ConditionType: ORDER_CONDITION_TYPE_TP, TpTriggerPrice: 77, TpOrderPrice: 71 } var id = exchange.CreateConditionOrder("SOL_USDT.swap", "buy", 1, condition, option) Log("Condition Order Id:", id) Sleep(2000) Log(exchange.GetConditionOrder(id)) }pythondef main(): # Exchange-specific parameters go in the last object argument option = { "type": "TRAILING_STOP_MARKET", "activatePrice": "300", "callbackRate": "0.1" } condition = { "ConditionType": ORDER_CONDITION_TYPE_TP, "TpTriggerPrice": 77, "TpOrderPrice": 71 } id = exchange.CreateConditionOrder("SOL_USDT.swap", "buy", 1, condition, option) Log("Condition Order Id:", id) Sleep(2000) Log(exchange.GetConditionOrder(id))rustfn main() { // Rust passes the object argument through the function with the WithOptions suffix let opts = JSONParse(r#"{"type": "TRAILING_STOP_MARKET", "activatePrice": "300", "callbackRate": "0.1"}"#).unwrap(); let condition = OrderCondition { ConditionType: ORDER_CONDITION_TYPE_TP, TpTriggerPrice: 77.0, TpOrderPrice: 71.0, ..Default::default() }; let id = exchange.CreateConditionOrderWithOptions("SOL_USDT.swap", "buy", 1, &condition, &opts).unwrap(); Log!("Condition Order Id:", id); Sleep(2000); Log!(exchange.GetConditionOrder(&id)); }
Returns
| Type | Description |
string / null value | Returns the conditional order Id on success and a null value on failure. A conditional order Id has a similar format to a regular order Id: the exchange's symbol code and the exchange's original conditional order Id, separated by a comma. |
Arguments
| Name | Type | Required | Description |
symbol | string | Yes | The trading symbol of the conditional order, e.g. |
side | string | Yes | The trade direction. For spot: |
amount | number | Yes | The order quantity of the conditional order. Note: for a spot market buy order, the quantity is the amount to spend. On a few spot exchanges, the quantity of a market buy order is the number of coins rather than the amount to spend; see the notes on each exchange's page. For futures exchange objects, the |
condition | object | Yes | The trigger condition and execution price of the conditional order. See |
arg | string / number / bool / object / array / any | No | Extended parameters. You can pass more than one |
See Also
Condition exchange.CancelConditionOrder exchange.GetConditionOrder exchange.GetConditionOrders exchange.ModifyConditionOrder
Remarks
Support for conditional orders depends on the specific exchange; some exchanges do not support conditional orders.
A conditional order does not tie up account funds before it is triggered; the order is actually placed and funds are tied up only after it is triggered.
Exchanges differ in how far they support conditional orders and in the specific parameters they accept. Check the corresponding exchange's API documentation before use.
Exchange-specific parameters (option) go in the last parameter as an object, for example exchange.CreateConditionOrder(symbol, "buy", amount, condition, {type: "TRAILING_STOP_MARKET", activatePrice: "300"}). The keys in the object are passed to the exchange as is. In Rust, use exchange.CreateConditionOrderWithOptions().
The supported option parameters vary by exchange; refer to the exchange's API documentation for details. Common parameters include order type (type), time in force (timeInForce), activation price (activatePrice) and callback rate (callbackRate).
The amount and condition parameters are still required when using option parameters. If some exchange API parameters are already passed through option, the corresponding parameters in option may override these basic parameters. The exact behavior depends on the exchange's API implementation.
Parameters can also be appended after the side parameter (legacy syntax), written as "side;{JSON object}" or "side;key=value&key=value", for example "buy;{\"type\":\"TRAILING_STOP_MARKET\",\"activatePrice\":\"300\"}" or "buy;type=TRAILING_STOP_MARKET&activatePrice=300". The supported parameters depend on the exchange API.
The ConditionType of condition can be OCO, TP or SL from ORDER_CONDITION_TYPE. ORDER_CONDITION_TYPE_GENERIC appears only in query results (for exchange-specific conditional orders such as trailing stops). It cannot be used when creating or modifying a conditional order; passing it raises an error and returns a null value. Every trigger price and execution price required by the selected type must be passed and must not be 0 (OCO needs all four, TP needs the two take-profit prices, SL needs the two stop-loss prices); otherwise an error is raised and a null value is returned. An execution price of -1 means the order is executed as a market order once triggered.
Exchanges that do not support this function: Bithumb, Bitstamp, Coinw, DigiFinex, Futures_DigiFinex, Gemini, HashKey, Korbit, LBank, MEXC, Upbit, WEEX, Zaif, Hyperliquid, Pendle, Uniswap, PancakeSwap, Aerodrome, Velodrome, KyberSwap, OneInch, Jupiter, OKX_DEX, Futures_Polymarket, Futures_Futu, Futures_IB, Exchange, Web3. The call fails with an error and returns null; for all exchanges see Capability Matrix.