Function used to open market or place a pending order. Only accept only order at time, for multiple orders consider to use MT5.MultipleOrders() instead.

The switch for pending order and market order is using fPrice. Any value higher than 0 is considered a pending order.

Some Forex brokers take 200ms to accept orders into market, so take this time on mind. Always check MT5's "Experts" tab for more information.

MT5.SingleOrder(
  sSymbol,
  iCmd,
  fVol,
  fPrice = 0,
  fStop = 0,
  fGain = 0,
  iFillType = 0
)

Arguments

sSymbol

character; symbol for trading.

iCmd

int; operation type:

  • 0: ORDER_TYPE_BUY_LIMIT

  • 1: ORDER_TYPE_SELL_LIMIT

fVol

numeric; number of lots.

fPrice

numeric; order price. WARNING: if fPrice = 0 it will be send as market order.

fStop

numeric; if applicable use any value > 0.

fGain

numeric; if applicable use any value > 0.

iFillType

int; order type filling:

  • 0: ORDER_TYPE_BUY_LIMIT (default)

  • 1: ORDER_TYPE_SELL_LIMIT (Market Order)

  • 2: ORDER_FILLING_IOC

Value

Returns TRUE if the order was successfully sent, FALSE otherwise.

Details

Not all columns need to be filled as MT5.MultipleOrders() request. See examples.

For fPrice = 0, a market order will be sent and it will automatically set iFillType to 1.

For more information about ORDER_TYPE_* see references.

WARNING! INVALID STOPS: take note that buy orders are rejected when order's stop loss are higher than symbol's ask price, and vice versa for sell orders. This is a sanity check made by MT5, not by mt5R. Always check MT5's "Experts" tab for more details when an order is rejected.

References

https://www.mql5.com/en/docs/constants/tradingconstants/orderproperties

https://www.investopedia.com/terms/m/marketorder.asp

See also

Examples

if (FALSE) { ## Sell market order. Using arguments MT5.SingleOrder("EURUSD", iCmd = 1, fVol = 0.01) ## Sell market order, same above. Omitting arguments MT5.SingleOrder("EURUSD", 1, 0.01) ## Buy market order, using stop. fStop need to be lower than symbol's ask price, otherwise the order will be rejected. MT5.SingleOrder("EURUSD", 0, 0.01, fStop = 1.200) ## Last price: 1.2242 18-12-20. 1.200 < 1.2242 = OK ## Sell pending order MT5.SingleOrder("EURUSD", iCmd = 1, fVol = 0.01, fPrice = 1.2334) ## Pending Order MT5.SingleOrder("EURUSD", iCmd = 1, fVol = 0.01, fPrice = 1.18320) ## Sell Pending Order MT5.SingleOrder("EURUSD", iCmd = 0, fVol = 0.01, fPrice = 1.18320, fStop = 1.18200) ## Buy Pending Order with stop MT5.SingleOrder("EURUSD", iCmd = 1, fVol = 0.01, fPrice = 1.18720, fStop = 1.18790) ## Sell Pending Order with stop }