Broker API HubMenu

MQL4 OrderSend()

functionOrderSend()MQL4Sending orders

Opens a market order or places a pending order. The page calls it the main function for doing either.

Risk warning: CFDs and spread bets are complex instruments and come with a high risk of losing money rapidly due to leverage. You can lose money on these products, and each provider publishes its own loss figure under FCA rules. You should consider whether you understand how they work and whether you can afford to take the high risk of losing your money. Automated and API trading can amplify losses through bugs, connectivity failures and runaway orders.

Independent and unofficialdocs.mql4.comchecked

This is an MQL4 function, not an HTTP endpoint. It has no method, no path and no address to send a request to: it runs inside the MetaTrader 4 terminal, in a program written in MQL4.

This function can place a real order on whichever account the terminal is logged in to, demo or live. The page says a market order can be opened only at the latest Bid, for a sell, or Ask, for a buy, that prices have to be normalised to the symbol's digits, that stop levels cannot sit closer to the market than the distance MarketInfo() returns for MODE_STOPLEVEL, and that some trade servers switch off expiry times or cap the number of open and pending orders.

Signature

The form of the call on the vendor's page, with the vendor's comments taken out.

int OrderSend(
  string symbol,
  int cmd,
  double volume,
  double price,
  int slippage,
  double stoploss,
  double takeprofit,
  string comment=NULL,
  int magic=0,
  datetime expiration=0,
  color arrow_color=clrNONE
)

Arguments

NameTypeDescription
symbolrequiredstringThe symbol to trade.
cmdrequiredintThe kind of operation, as one of the trade operation values, for example OP_BUY or OP_SELL.
volumerequireddoubleThe number of lots.
pricerequireddoubleThe order price.
slippagerequiredintThe most price slippage to accept on a buy or sell order.
stoplossrequireddoubleThe stop loss level.
takeprofitrequireddoubleThe take profit level.
commentstringA comment on the order. The page says the server can change its last part. Default: NULL.
magicintThe order's magic number, which a program can use as its own identifier. Default: 0.
expirationdatetimeWhen the order expires. For pending orders only. Default: 0.
arrow_colorcolorThe colour of the opening arrow on the chart. Left out or set to CLR_NONE, no arrow is drawn. Default: clrNONE.

Return value

Type
int
What comes back
The ticket number the trade server gave the order, or -1 on failure. The page points to GetLastError() for the reason.

Sample

Written by this site from the signature and arguments recorded on this page. It has not been run against a terminal, a server or an account, and the values in it are placeholders. It holds no credential and needs none: the program runs inside a terminal that is already logged in to an account. Nothing is entered on this site.

// Script. Run it only on a demo account: it sends a market order.
void OnStart()
  {
   int ticket=OrderSend(Symbol(),OP_BUY,0.1,Ask,3,0,0,"sample",123456,0,clrNONE);
   if(ticket<0)
      Print("OrderSend failed with error #",GetLastError());
   else
      Print("OrderSend placed order #",ticket);
  }

Return codes documented for this function

The page says the function returns -1 on failure and that GetLastError() gives the code. It names four of those codes in its notes, 129, 130, 147 and 148, and describes 148 as returned by the trade server. They are among MetaTrader 4's trade server return codes, each of which recorded here is below. docs.mql4.comchecked