Submit an Order with MQL4 OrderSend

OrderSend()  is the function in MQL4 that allows you to submit orders from Metatrader to your Broker.

When you create an Expert Advisor in many cases, probably the majority, you want it to trade for you. We will see in this article how to submit an order using the MQL4 language and the function OrderSend() .

OrderSend MQL4 Function

As mentioned before, the MQL4 OrderSend()  function allows you to submit orders from Metatrader to your Broker.

OrderSend needs several arguments, we can see them in more detail

MQL4 OrderSend Parameters

It is worth to spend some time learning more about some of the arguments, this will allow to avoid future errors, in bold the required ones:

  • symbol is simply the instrument that you want to trade, for example EURUSD, usually in an EA you will use Symbol() to trade with the current instrument
  • cmd is the type of order to submit and can be OP_BUY or OP_SELL for market buy and sell orders, OP_BUYLIMIT, OP_SELLLIMIT, OP_BUYSTOP, OP_SELLSTOP are also available for limit and stop orders
  • volume is the position size, as suggested in the article Calculate Position Size, use rules of risk management
  • price, stoploss and takeprofit are indeed the prices you want to open, set stop loss and take profit, important to remember with these prices that the open price must be the current price and that stop loss and take profit cannot be too close to the open price or the order will not be accepted. These prices must also be normalized to the number of decimals accepted by the broker, which is identifiable with the native variable Digits , failing to normalize may cause the order to not be accepted
  • slippage is the acceptable difference between the open price requested by the client and the actual price on the server, for example a slippage of 3 pips allows an order submitted at a price of 1.1980 to be accepted if the price on the server differs of max 3 pips, so between 1.1983 and 1.1977.
  • comment can be a comment or a note on the order
  • magic is a number that you can assign to the order to identify it, for example you want to use the same magic number for all the orders opened by a specific EA in a currency pair, this will allow you to identify between all your orders which ones are executed by the EA
  • expiration is the date and time to keep a pending order valid, when the order is expired this will not be executed
  • arrow_color is the color of the arrow to draw in the chart when the order is submitted

OrderSend()  is defined as an integer function meaning it return an int value, this value is the ticket number for the order if this is submitted successfully or -1 if the order submission fails. You can use GetLastError()  to see what went wrong.

MQL4 OrderSend Example

We can see a simple example with a script, in the script we have already included the function to normalize the digits from Pips Normalization and Calculate Position Size

Running the script on the EURUSD results in

metatrader4-mql4-ordersend-submitorder-1

This article is only an introduction about how to submit an order with MetaTrader and MQL4 OrderSend Function.

When you will create your own Expert Advisor you will realise that there is more to be considered and to be checked before submitting an order.

We will see some more advanced code in other articles and Expert Advisors, for the moment I would suggest to experiment some basic code on a Demo Account.

You can download a script to submit new orders from the download section or here.

Or you can take a look at the MT4 EA Template that I created that has most of the code required to create an EA, and some examples.

Please leave a comment if you have any feedback or doubt and remember to like us on Social Media!

We use cookies to ensure that we give you the best experience on our website.