OrderComment function


for example, forex

There is another way to know whether a position was closed by Stop Loss or by Take Profit order. Moreover this way will be simpler than the ways considered previously.

The OrderComment() function will help us in this.

   string OrderComment()

After an order or a position are selected by the OrderSelect() function the call of the OrderComment() function will return comments of this order/position in the form of a line.

Very often server writes useful things in the comment of an order or a position.

  • [sl] — if Stop Loss triggered;
  • [tp] —if Take Profit triggeres;
  • cancelled — if a pending order was cancelled by the client;
  • deleted [no money] — if a pending order must have triggered but was deleted by the server due to the lack of free assets on the account;
  • partial close — if a position was closed partially;
  • close hedge by #ticket — if a locked position was closed fully by another locked position;
  • [number%] — a position was closed by Stop Out (i.e. the server closed the position automatically, when equity became less than 20% of the required margin);
  • deleted [good for day] — a deleted pending order in CFD, the status of which was Day Order;
  • expiration [YYYY.MM.DD HH:MM] — a pending order which was deleted by the server due to the expiration date YYYY.MM.DD HH:MM;
  • split from #ticket — when a position is closed by another position of different volume a new position appeared in the market with the following comment ; #ticket — ticket number of the position with larger volume.

This list may be incomplete, but at the moment we mustn’t care about it. We are interested in the positions and orders in the comments of which there is such a sub line [sl] (position closed by Stop Loss order) or such a sub line [tp] (position closed by Take Profit order):

   //---- select order/position with ticket 77777
   if ( OrderSelect (77777, SELECT_BY_TICKET) == true) 
     {
       // is it a position an d not a pending order?
       if ((OrderType() == OP_BUY) || (OrderType() == OP_SELL))
         {
            // is it a closed not an open position?
            if ( OrderCloseTime() != 0 )
              {
                 if ( StringFind(OrderComment(), "[sl]") != -1 )
                    {
                       // position closed by Stop Loss
                       // ...
                    }
                 else
                    {
                       if ( StringFind(OrderComment(), "[tp]") != -1 )
                         {
                            // position closed by Take Profit
                            // ...
                         }
                       else 
                            Print("Error: position closed not by an order");
                    }
              }
            else
               Print("Error: this is an open not a closed position");
         }
       else
         Print("Error: this isn’t a closed position, it’s a pending order");
     }
  else
    Print("OrderSelect() returned an error - ",GetLastError());

In this example there is a function we don’t know so far — StringFind():

   int StringFind ( string text, string matched_text, int start=0 )

This function seeks for sub line matched_text in line text starting from the start position (if the third parameter isn’t indicated the search starts from the zero position, i.e. from the first element). If such a sub line isn’t found the function returns -1; if it is found it returns the number of position in the line, from which the sought sub line begins.


Next article: "OrderSwap 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?