OrderOpenPrice function


for example, forex

In this article the OrderOpenPrice() function will be considered. As an example of practical usage of this function we will consider how to place and manage a trailing stop.

Let me remind You that a Trailing Stop is an algorithm of managing the level of a Stop Loss order. After a trailing stop is placed (for example at X pips) the following happens:

  • MetaTrader doesn’t take any actions until a profit of X pips forms in the open position. After that MetaTrader places Stop Loss order at the distance of Х from the current price (in this case at the breakeven level).
  • On performing the first step MetaTrader send a command to change the level of Stop Loss order by the distance of Х pips from the current quote each time when the distance between it and the old level of the order exceeds X pips. As a result the Stop Loss order «trails» to the current price.

We will realize this principle in the MetaQuotes Language 4. We will assume that an open position is selected and we know exactly that this position is open in the instrument to which the expert advisor is attached. Also we will assume that the value of the trailing stop in pips is contained in variable TrailingStop.

   int err;
   if (OrderType() == OP_BUY)
     {
       // long position
       if ((Bid-OrderOpenPrice())>=(TrailingStop*Point))
         {
           // we place Stop Loss
           if (OrderModify(OrderTicket(), OrderOpenPrice(), Bid-TrailingStop*Point,
                                    OrderTakeProfit(), 0))
             Print("#", OrderTicket(),": trailing stop ", Bid-TrailingStop*Point);
           else
             {
              err = GetLastError(); 
              Print("#", OrderTicket(),": trailing stop error ", err);
             }
         }
     }
   else
     {
       // short position
       if ((OrderOpenPrice()-Ask)>=(TrailingStop*Point))
         {
           // we place Stop Loss
           if (OrderModify(OrderTicket(), OrderOpenPrice(), Ask+TrailingStop*Point,
                                    OrderTakeProfit(), 0))
             Print("#", OrderTicket(),": trailing stop ", Ask+TrailingStop*Point);
           else
             {
              err = GetLastError(); 
              Print("#", OrderTicket(),": trailing stop error ", err);
             }
         }
     }

First of all we are interested in a new function OrderOpenPrice():

   double OrderOpenPrice()

This function returns the opening price for an order/position selected by the OrderSelect() function.

In this code several more functions can be unclear for You:

  • OrderTicket() — returns the ticker of an order/position selected by the OrderSelect() function;
  • Bid — Bid of the latest quote in the instrument to which the expert advisor is attached;
  • Ask — Ask of the latest quote in the instrument to which the expert advisor is attached;
  • Point — the size of a point of the current instrument in the currency of the quote (for EURUSD it is 0.0001, for USDJPY it is 0.01 etc.).

Next article: "OrderTicket function"

+7 (495) 710-76-76
© 1998—2008 «Alpari»

close

Your Personal Area

For alpari.classic enter your account number (a letter and 4 figures) and the code word for the Personal Area.

For alpari.micro account: enter your login (6 figures) and the password for MT.

Open an account!Forgotten your password?