The exchange.Buy()
function is used to place buy orders. The Buy()
function is a member function of the exchange object {@var/EXCHANGE exchange}. The Buy()
function operates on the exchange account bound to the exchange object exchange
. The purpose of the member functions (methods) of the exchange
object is only related to exchange
, and it will not be repeated after the documentation.
A successful order returns the order Id, a failed order returns a null value.
The attribute Id
of the order {@struct/Order Order} structure of the FMZ platform consists of the exchange product code and the exchange original order ID, separated by English commas. For example, the attribute Id
format of the spot trading pair ETH_USDT
order of the OKX exchange is: ETH-USDT,1547130415509278720
.
When calling the exchange.Buy()
function to place an order, the return value order Id
is consistent with the Id
attribute of the order {@struct/Order Order} structure.
string, null value
exchange.Buy(price, amount) exchange.Buy(price, amount, …args)
The price
parameter is used to set the order price.
price
true
number
The amount
parameter is used to set the order amount.
amount
true
number
Extended parameters that can output accompanying information to this order log, arg
parameters can be passed more than one.
arg
false
string, number, bool, object, array, null and any other type supported by the system
function main() {
var id = exchange.Buy(100, 1);
Log("id:", id);
}
def main():
id = exchange.Buy(100, 1)
Log("id:", id)
void main() {
auto id = exchange.Buy(100, 1);
Log("id:", id);
}
The order number returned by exchange.Buy()
can be used to query the order information and cancel the order.
// The following is an error call
function main() {
exchange.SetContractType("quarter")
// Set the shorting direction
exchange.SetDirection("sell")
// If you place a buy order, an error will be reported, and shorting can only be sold
var id = exchange.Buy(50, 1)
// Set the long direction
exchange.SetDirection("buy")
// If you place a sell order, it will report an error, go long, only buy
var id2 = exchange.Sell(60, 1)
// Set direction to close long positions
exchange.SetDirection("closebuy")
// If you place a buy order, it will report an error, close long, only sell
var id3 = exchange.Buy(-1, 1)
// Set direction to close short positions
exchange.SetDirection("closesell")
// If you place a sell order, it will report an error, close short, only buy
var id4 = exchange.Sell(-1, 1)
}
# The following is an error call
def main():
exchange.SetContractType("quarter")
exchange.SetDirection("sell")
id = exchange.Buy(50, 1)
exchange.SetDirection("buy")
id2 = exchange.Sell(60, 1)
exchange.SetDirection("closebuy")
id3 = exchange.Buy(-1, 1)
exchange.SetDirection("closesell")
id4 = exchange.Sell(-1, 1)
// The following is an error call
void main() {
exchange.SetContractType("quarter");
exchange.SetDirection("sell");
auto id = exchange.Buy(50, 1);
exchange.SetDirection("buy");
auto id2 = exchange.Sell(60, 1);
exchange.SetDirection("closebuy");
auto id3 = exchange.Buy(-1, 1);
exchange.SetDirection("closesell");
auto id4 = exchange.Sell(-1, 1);
}
When placing an order for a cryptocurrency futures contract, care must be taken to ensure that the trade direction is set correctly, as a mismatch between the trade direction and the trade function will result in an error:
direction is sell, invalid order type Buy
direction is buy, invalid order type Sell
direction is closebuy, invalid order type Buy
direction is closesell, invalid order type Sell
// For example, the trading pair: ETH_BTC, place a buy order at the market price
function main() {
// Place a buy order at the market price and buy ETH coins with a value of 0.1 BTC (denominated currency)
exchange.Buy(-1, 0.1)
}
def main():
exchange.Buy(-1, 0.1)
void main() {
exchange.Buy(-1, 0.1);
}
Spot market order.
When placing an order for a futures contract, you must pay attention to whether the trade direction is set correctly, as an error will be reported if the trade direction and the trade function do not match. The order size for cryptocurrency futures contracts is the number of contracts if not specified.
The parameter price
is set to -1
for placing market orders, which requires the exchange’s order placement interface to support market orders. When placing market orders for cryptocurrency spot contracts, the parameter amount
is the amount of money in the currency of the order. When placing market orders for cryptocurrency futures contracts, the amount parameter amount
is the number of contracts. There are a few cryptocurrency exchanges that do not support market order interface during live trading. The order quantity for market buy orders on some spot exchanges is the number of trading coins. Please refer to the Special Instructions for Exchanges in the “User Guide” for details.
If you are using an older version of the docker, the return value of the order Id
of the exchange.Buy()
function may be different from the return value of the order Id
described in the current document.
{@fun/Trade/exchange.Sell exchange.Sell}, {@fun/Futures/exchange.SetContractType exchange.SetContractType}, {@fun/Futures/exchange.SetDirection exchange.SetDirection}
Market exchange.Sell