MQL4 Pending Order Parameters
Learn in this complete MQL4 Pending Order Parameters article how to program the EA to place pending stop orders and pending limit orders.
Pending Stop Order
A buy stop order is placed above the current price, while a sell stop order is placed below the current price. The trader expects that the price will rise to the buy stop level or fall to the sell stop level and continue in that direction in profit.
In MQL4, there are two parameters that must be changed within the OrderSend() function to adjust it for stop orders, as we see in the following buy stop example:
As you can see, the first and third parameters have been changed: the first parameter, representing the type of order, has been changed to OP_BUYSTOP, and the third parameter, representing price, now indicates Ask+PriceLevel instead of Ask. We want a plus sign (+) here because we will be adding pips to the ask price to represent a level above the market in which we want to enter into with the buy stop.
It is easy enough to create an external variable called
This external variable can be later modified, but the default of 20 means that I am expecting the market to reach up 20 pips before I enter into my trade: Ask + PriceLevel (defaulted as 20 my extern variable) * Point.
Let us examine an example of a sell stop:
As you can see once again, only the first and third parameters are different from the standard sell market order:
the first parameter, representing the type of order, has been changed to OP_SELLSTOP, and the third parameter, representing price, now indicates Bid-PriceLevel instead of Bid.
We want a negative sign (-) here after Bid because we will be subtracting pips from the Bid price to represent a level below the market in which we want to enter into with the sell stop.
Pending Limit Order
Limit Orders are the opposite of the stop order. A buy limit order is placed below the current price, while the sell limit order is placed above the current price. The trader expects that the price will lower to the buy limit level, or rise to the sell limit level, trigger the order, then reverse direction for profit.
Here is an example of a buy limit order:
Again, only the first and third parameters are modified. We use OP_BUYLIMIT to indicate a buy limit order. We then indicate that the pending price must be less than the current price by modifying the price parameter: we use Ask – PriceLevel * Point, in order to represent that we want to take the trade if market drops, minus (-), to the the PriceLevel, defaulted to 20 pips below the current Ask price.
Here is an example of a Sell Limit Order:
As you can see once more, the first parameter of order type has been modified to OP_SELL, and the third parameter of price has been modified to indicate that the entry price rests at the PriceLevel, defaulted to 20 pips above the current Bid price.
Expiration Parameter (second to last)
Suppose you want to put in an expiration for the pending buy stop order. This expiration time is located in the second to last parameter, and it needs to be in seconds.
For instance, suppose I had an EA that worked on an hourly chart, and I wanted it to expire in 6 hours. Well then, I would have to work how many seconds is in 6 hours, something like this: 60*60*6 = 21600
That order will then expire in 6 hours.
Sometimes I like to optimize this expiration time, and thus I like to put it in an extern variable. Suppose I want to optimize the expiration in hours.
Then I create an extern variable called PendingExpirationHours and a double that calculates the hours:
OrderSend(), OP_BUYSTOP, Lots, Ask + PriceLevel * Point, Slippage, Bid-StopLoss *Point, Bid+TakeProfit*Point, “EAName”, MagicNumber, TimeCurrent()+expiration, Blue)
Now, if I want to, I can go ahead and optimize PendingExpirationHours from 0-50, with a step of 1, to see which hour works best for an expiration time of the pending order.