exchange.CreateConditionOrder
exchange.CreateConditionOrder() function is used to create a conditional order. A conditional order is an order type that automatically executes when specific trigger conditions are met.
exchange.CreateConditionOrder(symbol, side, amount, condition)
exchange.CreateConditionOrder(symbol, side, amount, condition, ...args)Examples
-
Create a Take Profit order (TP): Automatically sell when the price rises to the target price.
javascriptfunction main() { // Create TP order: When BTC_USDT price rises to 65000, sell 0.01 BTC at price 65000 var condition = { ConditionType: ORDER_CONDITION_TYPE_TP, // Take Profit order TpTriggerPrice: 65000, // Trigger price TpOrderPrice: 65000 // Execution price, can also be set to -1 for market order } var id = exchange.CreateConditionOrder("BTC_USDT", "sell", 0.01, condition) Log("TP order Id:", id) }pythondef main(): # Create TP order: When BTC_USDT price rises to 65000, sell 0.01 BTC at price 65000 condition = { "ConditionType": ORDER_CONDITION_TYPE_TP, # Take Profit order "TpTriggerPrice": 65000, # Trigger price "TpOrderPrice": 65000 # Execution price, can also be set to -1 for market order } id = exchange.CreateConditionOrder("BTC_USDT", "sell", 0.01, condition) Log("TP order Id:", id)c++void main() { // Create TP order: When BTC_USDT price rises to 65000, sell 0.01 BTC at price 65000 OrderCondition condition = {.ConditionType = ORDER_CONDITION_TYPE_TP, .TpTriggerPrice = 65000, .TpOrderPrice = 65000}; auto id = exchange.CreateConditionOrder("BTC_USDT", "sell", 0.01, condition); Log("TP order Id:", id); } -
Create a Stop Loss order (SL): Automatically sell when the price drops to the stop loss price.
javascriptfunction main() { // Create SL order: When BTC_USDT price drops 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 market order } var id = exchange.CreateConditionOrder("BTC_USDT", "sell", 0.01, condition) Log("SL order Id:", id) }pythondef main(): # Create SL order: When BTC_USDT price drops 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 market order } id = exchange.CreateConditionOrder("BTC_USDT", "sell", 0.01, condition) Log("SL order Id:", id)c++void main() { // Create SL order: When BTC_USDT price drops to 58000, sell 0.01 BTC at market price OrderCondition condition = {.ConditionType = ORDER_CONDITION_TYPE_SL, .SlTriggerPrice = 58000, .SlOrderPrice = -1}; auto id = exchange.CreateConditionOrder("BTC_USDT", "sell", 0.01, condition); Log("SL order Id:", id); } -
Create OCO order: Set take-profit and stop-loss simultaneously. When either one is triggered, the other is automatically canceled.
javascriptfunction main() { // Create OCO order: take-profit price 65000, stop-loss price 58000 var condition = { ConditionType: ORDER_CONDITION_TYPE_OCO, // OCO order TpTriggerPrice: 65000, // Take-profit trigger price TpOrderPrice: 65000, // Take-profit execution price SlTriggerPrice: 58000, // Stop-loss trigger price SlOrderPrice: 58000 // Stop-loss execution price } var id = exchange.CreateConditionOrder("BTC_USDT", "sell", 0.01, condition) Log("OCO order Id:", id) }pythondef main(): # Create OCO order: take-profit price 65000, stop-loss price 58000 condition = { "ConditionType": ORDER_CONDITION_TYPE_OCO, # OCO order "TpTriggerPrice": 65000, # Take-profit trigger price "TpOrderPrice": 65000, # Take-profit execution price "SlTriggerPrice": 58000, # Stop-loss trigger price "SlOrderPrice": 58000 # Stop-loss execution price } id = exchange.CreateConditionOrder("BTC_USDT", "sell", 0.01, condition) Log("OCO order Id:", id)c++void main() { // Create OCO order: take-profit price 65000, stop-loss price 58000 OrderCondition condition = {.ConditionType = ORDER_CONDITION_TYPE_OCO, .TpTriggerPrice = 65000, .TpOrderPrice = 65000, .SlTriggerPrice = 58000, .SlOrderPrice = 58000}; auto id = exchange.CreateConditionOrder("BTC_USDT", "sell", 0.01, condition); Log("OCO order Id:", id); } -
Create a condition order using additional parameters (option) to pass exchange-specific parameters.
javascriptfunction main() { // Pass option parameter using JSON format var option = { "type": "TRAILING_STOP_MARKET", "activatePrice": "300", "callbackRate": "0.1" } var sideWithOption = "buy;" + JSON.stringify(option) var condition = { ConditionType: ORDER_CONDITION_TYPE_TP, TpTriggerPrice: 77, TpOrderPrice: 71 } var id = exchange.CreateConditionOrder("SOL_USDT.swap", sideWithOption, 1, condition) Log("Condition Order Id:", id) Sleep(2000) Log(exchange.GetConditionOrder(id)) }pythonimport json def main(): # Pass option parameter using JSON format option = { "type": "TRAILING_STOP_MARKET", "activatePrice": "300", "callbackRate": "0.1" } sideWithOption = "buy;" + json.dumps(option) condition = { "ConditionType": ORDER_CONDITION_TYPE_TP, "TpTriggerPrice": 77, "TpOrderPrice": 71 } id = exchange.CreateConditionOrder("SOL_USDT.swap", sideWithOption, 1, condition) Log("Condition Order Id:", id) Sleep(2000) Log(exchange.GetConditionOrder(id))c++void main() { // Pass option parameter using JSON format json option = R"({ "type": "TRAILING_STOP_MARKET", "activatePrice": "300", "callbackRate": "0.1" })"_json; string sideWithOption = "buy;" + option.dump(); OrderCondition condition = {.ConditionType = ORDER_CONDITION_TYPE_TP, .TpTriggerPrice = 77, .TpOrderPrice = 71}; auto id = exchange.CreateConditionOrder("SOL_USDT.swap", sideWithOption, 1, condition); Log("Condition Order Id:", id); Sleep(2000); Log(exchange.GetConditionOrder(id)); }
Returns
| Type | Description |
string / null value | Returns the conditional order ID when the conditional order is created successfully, returns null value when creation fails. The conditional order ID format is similar to regular order ID, consisting of the exchange instrument code and the exchange's original conditional order ID, separated by a comma. |
Arguments
| Name | Type | Required | Description |
symbol | string | Yes | The parameter When calling the When calling the When calling the |
side | string | Yes | The parameter For spot exchange objects, the optional values for the For futures exchange objects, the optional values for the Supports additional parameters (option): Additional parameters can be passed through the For example: Additional parameters are used to pass exchange-specific parameters (such as order type, time-in-force rules, etc.), and the specific supported parameters depend on the exchange API. |
amount | number | Yes | The parameter |
condition | object | Yes | The parameter
|
arg | string / number / bool / object / array / any (any type supported by the platform) | No | Extended parameters, can output additional information to the log of this conditional order. Multiple |
See Also
Condition exchange.CancelConditionOrder exchange.GetConditionOrder exchange.GetConditionOrders exchange.ModifyConditionOrder
Remarks
Support for conditional orders depends on the specific exchange; some exchanges may not support conditional order functionality.
Conditional orders do not occupy account funds before being triggered; funds are only occupied when the order is actually placed after triggering.
Different exchanges may have varying levels of support and specific parameters for conditional orders. Please refer to the corresponding exchange's API documentation before use.
Supports passing additional parameters (option) through the side parameter for exchange-specific parameters. Additional parameters need to be merged with the side parameter in the format "side;{JSON object}" (recommended) or "side;key=value&key=value" (URL encoded format). For example: "buy;{\"type\":\"TRAILING_STOP_MARKET\"}".
Different exchanges support different option parameters; the specific supported parameters depend on the exchange's API documentation. Common parameters include: order type (type), time in force (timeInForce), activation price (activatePrice), callback rate (callbackRate), etc.
When using option parameters, the amount and condition parameters still need to be provided. If certain parameters in the exchange API have been passed through option, these basic parameters may be overridden by the corresponding parameters in option; the specific behavior depends on the exchange API implementation.