OrderCloseTime function


for example, forex

There may be a situation when we selected an open position by mistake . The OrderCloseTime() function will help us to reveal such a case.

   datetime OrderCloseTime()

The function can be called after the order or position was previously selected by the OrderSelect() function.

It returns the date and time of closing of a position or an order:

  • If the position is open it returns 0 (zero).
  • If the position is closed it returns the date and time of closing of the position.
  • If the pending order is deleted it returns the date and time when the order was deleted.
  • If the pending order isn’t deleted it returns 0 (zero).

Proceeding from these rules we need to check the time of closing of the selected position. If it equals to zero this is an open position and we have to report an error:

   //---- select order/position with ticker  77777
   if ( OrderSelect (77777, SELECT_BY_TICKET) == true) 
     {
       // is it a position and not a pending order?
       if ((OrderType() == OP_BUY) || (OrderType() == OP_SELL))
         {
            // is it a closed and not open position?
            if ( OrderCloseTime() != 0 )
              {
                if ( ((OrderClosePrice() <= OrderStopLoss()) && (OrderType() == OP_BUY)) || 
                     ((OrderClosePrice() >= OrderStopLoss()) && (OrderType() == OP_SELL)) )
                   {
                      // position closed by Stop Loss
                      // ...
                   }
                else
                   {
                      // position closed by Take Profit
                      // ...
                   }
              }
            else
               Print("Error: it’s not a closed position it is an open position");
         }
       else
         Print("Error: it’s not a closed position it’s a pending order");
     }
  else
    Print("OrderSelect() вернул ошибку - ",GetLastError());

The fact that it can’t be a pending order we have checked already (refer to the previous article).

Now our example on determining the type of order, by which a position was closed, contains one less mistake, but it isn’t perfect still. For some reason from the first lines of the code we undoubtedly believe in the fact that the position was closed namely by Stop Loss or Take Profit of the order. And what if it isn’t so?


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