exchange.CreateOrder
Places an order.
exchange.CreateOrder(symbol, side, price, amount)
exchange.CreateOrder(symbol, side, price, amount, ...args)Examples
-
Both spot and futures exchange objects can place orders with the
exchange.CreateOrder()function.javascriptfunction main() { var id = exchange.CreateOrder("BTC_USDT", "buy", 60000, 0.01) // Order placed by a spot exchange object; the trading pair is BTC_USDT spot // var id = exchange.CreateOrder("BTC_USDT.swap", "buy", 60000, 0.01) // Order placed by a futures exchange object; the contract is the BTC USDT-margined perpetual swap Log("Order Id:", id) }pythondef main(): id = exchange.CreateOrder("BTC_USDT", "buy", 60000, 0.01) # Order placed by a spot exchange object; the trading pair is BTC_USDT spot # id = exchange.CreateOrder("BTC_USDT.swap", "buy", 60000, 0.01) # Order placed by a futures exchange object; the contract is the BTC USDT-margined perpetual swap Log("Order Id:", id)rustfn main() { let id = exchange.CreateOrder("BTC_USDT", "buy", 60000, 0.01); // Order placed by a spot exchange object; the trading pair is BTC_USDT spot // let id = exchange.CreateOrder("BTC_USDT.swap", "buy", 60000, 0.01); // Order placed by a futures exchange object; the contract is the BTC USDT-margined perpetual swap Log!("Order Id:", id); } -
Place an order with exchange-specific parameters passed in the last argument, an object (recommended).
javascriptfunction main() { // selfTradePreventionMode is not an order option, so it is passed through to the exchange unchanged (Binance Futures self-trade prevention mode) var id = exchange.CreateOrder("SOL_USDT.swap", "buy", 150, 1, {selfTradePreventionMode: "EXPIRE_MAKER"}) Log("Order Id:", id) Sleep(2000) Log(exchange.GetOrder(id)) }pythondef main(): # selfTradePreventionMode is not an order option, so it is passed through to the exchange unchanged (Binance Futures self-trade prevention mode) id = exchange.CreateOrder("SOL_USDT.swap", "buy", 150, 1, {"selfTradePreventionMode": "EXPIRE_MAKER"}) Log("Order Id:", id) Sleep(2000) Log(exchange.GetOrder(id))rustfn main() { // In Rust, use the function with the WithOptions suffix to pass the object argument let opts = JSONParse(r#"{"selfTradePreventionMode": "EXPIRE_MAKER"}"#).unwrap(); let id = exchange.CreateOrderWithOptions("SOL_USDT.swap", "buy", 150, 1, &opts).unwrap(); Log!("Order Id:", id); Sleep(2000); Log!(exchange.GetOrder(&id)); } -
When a parameter name clashes with an order option (for example,
typefor Binance trailing stop orders), the parameter can only be passed by appending it after thesideparameter (legacy syntax).javascriptfunction main() { // type clashes with an order option, so it can only be passed by appending it after the side parameter var option = { "type": "TRAILING_STOP_MARKET", "activationPrice": "2300", "callbackRate": "0.1" } var sideWithOption = "buy;" + JSON.stringify(option) var id = exchange.CreateOrder("SOL_USDT.swap", sideWithOption, -1, 1) Log("Order Id:", id) Sleep(2000) Log(exchange.GetOrder(id)) }pythonimport json def main(): # type clashes with an order option, so it can only be passed by appending it after the side parameter option = { "type": "TRAILING_STOP_MARKET", "activationPrice": "2300", "callbackRate": "0.1" } sideWithOption = "buy;" + json.dumps(option) id = exchange.CreateOrder("SOL_USDT.swap", sideWithOption, -1, 1) Log("Order Id:", id) Sleep(2000) Log(exchange.GetOrder(id))rustfn main() { // type clashes with an order option, so it can only be passed by appending it after the side parameter let option = r#"{"type": "TRAILING_STOP_MARKET", "activationPrice": "2300", "callbackRate": "0.1"}"#; let sideWithOption = format!("buy;{}", option); let id = exchange.CreateOrder("SOL_USDT.swap", &sideWithOption, -1, 1).unwrap(); Log!("Order Id:", id); Sleep(2000); Log!(exchange.GetOrder(&id)); }
Returns
| Type | Description |
string / null value | Returns the order Id if the order is placed successfully, or a null value if it fails. The |
Arguments
| Name | Type | Required | Description |
symbol | string | Yes | The trading symbol of the order, e.g. |
side | string | Yes | Trade direction: for spot, |
price | number | Yes | The order price. A price of -1 indicates a market order. |
amount | number | Yes | The order quantity. Note: for a spot market buy order, the quantity is the amount of quote currency to spend; on a few spot exchanges the quantity of a market buy order is the number of coins rather than the amount spent. See the notes on each exchange's page for details. For futures exchange objects, when placing orders with the |
arg | string / number / bool / object / array / any | No | Extended parameters; multiple |
See Also
Remarks
Exchange-specific parameters (options) should go in the last argument, as an object. For example, in exchange.CreateOrder(symbol, "buy", price, amount, {timeInForce: "GTX"}), the keys type, tif, reduceOnly and postOnly are order options. All other keys are passed through to the exchange unchanged. In Rust, use exchange.CreateOrderWithOptions() to pass the object argument.
Each exchange supports its own set of option parameters; refer to the exchange's API documentation for details. Common parameters include order type (type), time in force (timeInForce), activation price (activationPrice), callback rate (callbackRate) and others.
When you use option parameters, you must still provide the price and amount arguments. If the option object also sets the matching exchange API parameters, those values may override the base arguments. The exact behavior depends on the exchange's API implementation.
On on-chain swap exchanges such as Uniswap and Jupiter, an order is swapped at once rather than resting on the order book. For a limit order, price is the worst acceptable execution price, and the on-chain transaction enforces it. If the current quote cannot meet that price, the call returns an error and no transaction is sent. A market order (price set to -1) is protected by the slippage setting, and for a market buy amount is the amount of quote currency to spend. For the order ID format, whether orders can be canceled and the supported extra parameters, see the page for each exchange, such as Uniswap and Jupiter.
You can also append parameters to the side argument (legacy syntax), as "side;{JSON object}" or "side;key=value&key=value" (URL-encoded), for example 'buy;{"type":"TRAILING_STOP_MARKET","activationPrice":"2300"}'. An exchange parameter with the same name as an order option, such as Binance's type, can only be passed with this syntax. If both syntaxes pass a parameter with the same name, the value in side takes precedence. Which parameters are supported depends on the exchange API.
Unsupported exchanges: Web3. Calling this function on these exchanges raises an error and returns a null value. To compare support across exchanges, see the capability matrix.
Exchanges that do not support this function: Web3. The call fails with an error and returns null; for all exchanges see Capability Matrix.