Type/to search
Built-in Functions
Global
Version
Sleep
IsVirtual
Mail
Mail_Go
SetErrorFilter
GetPid
GetLastError
GetCommand
GetMeta
Dial
HttpQuery
HttpQuery_Go
Encode
UnixNano
Unix
GetOS
MD5
DBExec
UUID
EventLoop
__Serve
_G
_D
_N
_C
_Cross
JSON.parse
JSON.stringify
SetChannelData
GetChannelData
Log
Market
Trade
Account
Futures
NetSettings
Threads
threading
Thread
getThread
mainThread
currentThread
Lock
Condition
Event
Dict
pending
Thread
ThreadLock
ThreadEvent
ThreadCondition
ThreadDict
Web3
TA
Talib
talib.CDL2CROWS
talib.CDL3BLACKCROWS
talib.CDL3INSIDE
talib.CDL3LINESTRIKE
talib.CDL3OUTSIDE
talib.CDL3STARSINSOUTH
talib.CDL3WHITESOLDIERS
talib.CDLABANDONEDBABY
talib.CDLADVANCEBLOCK
talib.CDLBELTHOLD
talib.CDLBREAKAWAY
talib.CDLCLOSINGMARUBOZU
talib.CDLCONCEALBABYSWALL
talib.CDLCOUNTERATTACK
talib.CDLDARKCLOUDCOVER
talib.CDLDOJI
talib.CDLDOJISTAR
talib.CDLDRAGONFLYDOJI
talib.CDLENGULFING
talib.CDLEVENINGDOJISTAR
talib.CDLEVENINGSTAR
talib.CDLGAPSIDESIDEWHITE
talib.CDLGRAVESTONEDOJI
talib.CDLHAMMER
talib.CDLHANGINGMAN
talib.CDLHARAMI
talib.CDLHARAMICROSS
talib.CDLHIGHWAVE
talib.CDLHIKKAKE
talib.CDLHIKKAKEMOD
talib.CDLHOMINGPIGEON
talib.CDLIDENTICAL3CROWS
talib.CDLINNECK
talib.CDLINVERTEDHAMMER
talib.CDLKICKING
talib.CDLKICKINGBYLENGTH
talib.CDLLADDERBOTTOM
talib.CDLLONGLEGGEDDOJI
talib.CDLLONGLINE
talib.CDLMARUBOZU
talib.CDLMATCHINGLOW
talib.CDLMATHOLD
talib.CDLMORNINGDOJISTAR
talib.CDLMORNINGSTAR
talib.CDLONNECK
talib.CDLPIERCING
talib.CDLRICKSHAWMAN
talib.CDLRISEFALL3METHODS
talib.CDLSEPARATINGLINES
talib.CDLSHOOTINGSTAR
talib.CDLSHORTLINE
talib.CDLSPINNINGTOP
talib.CDLSTALLEDPATTERN
talib.CDLSTICKSANDWICH
talib.CDLTAKURI
talib.CDLTASUKIGAP
talib.CDLTHRUSTING
talib.CDLTRISTAR
talib.CDLUNIQUE3RIVER
talib.CDLUPSIDEGAP2CROWS
talib.CDLXSIDEGAP3METHODS
talib.AD
talib.ADOSC
talib.OBV
talib.ACOS
talib.ASIN
talib.ATAN
talib.CEIL
talib.COS
talib.COSH
talib.EXP
talib.FLOOR
talib.LN
talib.LOG10
talib.SIN
talib.SINH
talib.SQRT
talib.TAN
talib.TANH
talib.MAX
talib.MAXINDEX
talib.MIN
talib.MININDEX
talib.MINMAX
talib.MINMAXINDEX
talib.SUM
talib.HT_DCPERIOD
talib.HT_DCPHASE
talib.HT_PHASOR
talib.HT_SINE
talib.HT_TRENDMODE
talib.ATR
talib.NATR
talib.TRANGE
talib.BBANDS
talib.DEMA
talib.EMA
talib.HT_TRENDLINE
talib.KAMA
talib.MA
talib.MAMA
talib.MIDPOINT
talib.MIDPRICE
talib.SAR
talib.SAREXT
talib.SMA
talib.T3
talib.TEMA
talib.TRIMA
talib.WMA
talib.LINEARREG
talib.LINEARREG_ANGLE
talib.LINEARREG_INTERCEPT
talib.LINEARREG_SLOPE
talib.STDDEV
talib.TSF
talib.VAR
talib.ADX
talib.ADXR
talib.APO
talib.AROON
talib.AROONOSC
talib.BOP
talib.CCI
talib.CMO
talib.DX
talib.MACD
talib.MACDEXT
talib.MACDFIX
talib.MFI
talib.MINUS_DI
talib.MINUS_DM
talib.MOM
talib.PLUS_DI
talib.PLUS_DM
talib.PPO
talib.ROC
talib.ROCP
talib.ROCR
talib.ROCR100
talib.RSI
talib.STOCH
talib.STOCHF
talib.STOCHRSI
talib.TRIX
talib.ULTOSC
talib.WILLR
talib.AVGPRICE
talib.MEDPRICE
talib.TYPPRICE
talib.WCLPRICE
OS
Structures
Built-in Variables

The exchange.Buy() function is used to place a buy order. The Buy() function is a member function of the exchange object 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, which will not be repeated in the rest of this document.

exchange.Buy(price, amount)
exchange.Buy(price, amount, ...args)

Examples

  • The order number returned by exchange.Buy() can be used to query order information and cancel orders.

    javascript
    function main() { var id = exchange.Buy(100, 1); Log("id:", id); }
    python
    def main(): id = exchange.Buy(100, 1) Log("id:", id)
    rust
    fn main() { let id = exchange.Buy(100, 1).unwrap(); Log!("id:", id); }
    c++
    void main() { auto id = exchange.Buy(100, 1); Log("id:", id); }
  • When placing an order for a cryptocurrency futures contract, you must pay attention to whether the trading direction is set correctly. If the trading direction does not match the trading function, an error will be reported:

    ```log

    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

    ```

    javascript
    // The following are incorrect calls function main() { exchange.SetContractType("quarter") // Set the short direction exchange.SetDirection("sell") // Placing a buy order will report an error; shorting can only sell var id = exchange.Buy(50, 1) // Set the long direction exchange.SetDirection("buy") // Placing a sell order will report an error; going long can only buy var id2 = exchange.Sell(60, 1) // Set the close-long direction exchange.SetDirection("closebuy") // Placing a buy order will report an error; closing long can only sell var id3 = exchange.Buy(-1, 1) // Set the close-short direction exchange.SetDirection("closesell") // Placing a sell order will report an error; closing short can only buy var id4 = exchange.Sell(-1, 1) }
    python
    # The following are incorrect calls 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)
    rust
    // The following are incorrect calls fn main() { let _ = exchange.SetContractType("quarter"); // Set the short direction let _ = exchange.SetDirection("sell"); // Placing a buy order will report an error; shorting can only sell let id = exchange.Buy(50, 1); // Set the long direction let _ = exchange.SetDirection("buy"); // Placing a sell order will report an error; going long can only buy let id2 = exchange.Sell(60, 1); // Set the close-long direction let _ = exchange.SetDirection("closebuy"); // Placing a buy order will report an error; closing long can only sell let id3 = exchange.Buy(-1, 1); // Set the close-short direction let _ = exchange.SetDirection("closesell"); // Placing a sell order will report an error; closing short can only buy let id4 = exchange.Sell(-1, 1); }
    c++
    // The following are incorrect calls 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); }
  • Spot market order.

    javascript
    // For example, trading pair: ETH_BTC, market order buy function main() { // Place a market order to buy, buying ETH worth 0.1 BTC (quote currency) exchange.Buy(-1, 0.1) }
    python
    def main(): exchange.Buy(-1, 0.1)
    rust
    // For example, trading pair: ETH_BTC, market order buy fn main() { // Place a market order to buy, buying ETH worth 0.1 BTC (quote currency) let _ = exchange.Buy(-1, 0.1); }
    c++
    void main() { exchange.Buy(-1, 0.1); }

Returns

TypeDescription

string / null value

Returns the order Id if the order is placed successfully, and returns a null value if the order fails. The Id attribute of the order Order structure on the FMZ platform consists of the exchange symbol code and the exchange's original order Id, separated by an English comma. For example, the Id attribute format of an order for the OKX exchange spot trading pair ETH_USDT is: ETH-USDT,1547130415509278720. When calling the exchange.Buy() function to place an order, the returned order Id is consistent with the Id attribute of the order Order structure.

Arguments

NameTypeRequiredDescription

price

number

Yes

The price parameter is used to set the order price.

amount

number

Yes

The amount parameter is used to set the order amount.

arg

string / number / bool / object / array / any (any type supported by the platform)

No

Extension parameter used to output accompanying information to this order log. Multiple arg parameters can be passed in.

See Also

exchange.Sell exchange.SetContractType exchange.SetDirection exchange.IO (API rate limit control; the Buy function is affected by the CreateOrder rate limit setting)

Remarks

When placing an order for a futures contract, you must pay attention to whether the trading direction is set correctly. If the trading direction does not match the trading function, an error will be reported. Unless otherwise specified, the order amount on cryptocurrency futures contract exchanges is denominated in number of contracts.

When the price parameter is set to -1, it is used to place a market order. This feature requires the exchange's order placement interface to support market orders. When placing a buy order for cryptocurrency spot in the form of a market order, the order amount parameter amount is the amount denominated in the quote currency. When placing an order for a cryptocurrency futures contract in the form of a market order, the unit of the order amount parameter amount is number of contracts. In live trading, a few cryptocurrency exchanges do not support the market order interface. For a few spot exchanges, the order amount of a market buy order is the number of trading coins. For details, please refer to the Exchange Special Notes in the "User Guide".

If you are using an older version of the docker, the order Id returned by the exchange.Buy() function may differ from the return value order Id described in the current document.

It should be noted that the order placement interfaces of the following three exchanges are relatively special. For spot market buy orders, the order amount is the number of coins rather than the amount.

  • AscendEx

  • BitMEX

  • Bitfinex