Type/to search
Built-in Functions
Global
Log
Market
Trade
Account
Futures
Exchange
IO
Network
Storage
Threads
TA
Talib
OS
Structures
Built-in Variables and Constants
Conventions
Exchanges

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.

    javascript
    function 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) }
    python
    def 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)
    rust
    fn 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).

    javascript
    function 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)) }
    python
    def 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))
    rust
    fn 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, type for Binance trailing stop orders), the parameter can only be passed by appending it after the side parameter (legacy syntax).

    javascript
    function 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)) }
    python
    import 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))
    rust
    fn 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

TypeDescription

string / null value

Returns the order Id if the order is placed successfully, or a null value if it fails. The Id attribute of the FMZ platform's order Order structure consists of the exchange's symbol code and the exchange's original order Id, separated by a comma. For example, for an order on the OKX spot trading pair ETH_USDT, the Id attribute has the format ETH-USDT,1547130415509278720.
When an order is placed by calling exchange.CreateOrder(symbol, side, price, amount), the returned order Id matches the Id attribute of the order Order structure.

Arguments

NameTypeRequiredDescription

symbol

string

Yes

The trading symbol of the order, e.g. BTC_USDT on a spot exchange object or BTC_USDT.swap on a futures exchange object. For the notation (including options), see Symbol Codes.

side

string

Yes

Trade direction: for spot, buy (buy) or sell (sell); for futures, buy (open long), sell (open short), closebuy (close long) or closesell (close short). Exchange-specific parameters can be appended; see the remarks.

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 CreateOrder()/Buy()/Sell() functions, the amount parameter is the number of contracts unless otherwise specified.

arg

string / number / bool / object / array / any

No

Extended parameters; multiple arg parameters can be passed. String, number and similar parameters are written to the log entry for this order (multiple parameters are joined with spaces). The last object parameter is not written to the log but serves as order options. Supported keys include: type (only market or limit, to explicitly specify a market or limit order), tif, reduceOnly and postOnly. Any other keys are passed to the exchange as exchange-specific parameters, equivalent to appending "side;{JSON对象}" to the side parameter (for duplicate keys, the value in side takes precedence), e.g. {"slippage":0.01}.

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.