How can I get the Order Time Setup for a running position?

 

Hey guys,

is there something like ORDER_TIME_SETUP for a running position or how can I get the time when the order was created? I didn't find anything for PositionGetInteger().

Thanks for help!

 
Marbo: Hey guys, is there something like ORDER_TIME_SETUP for a running position or how can I get the time when the order was created? I didn't find anything for PositionGetInteger(). Thanks for help!

POSITION_TIME

Position open time

datetime

POSITION_TIME_MSC

Position opening time in milliseconds since 01.01.1970

long

POSITION_TIME_UPDATE

Position changing time

datetime

POSITION_TIME_UPDATE_MSC

Position changing time in milliseconds since 01.01.1970

long

 
Fernando Carreiro #:

POSITION_TIME

Position open time

datetime

POSITION_TIME_MSC

Position opening time in milliseconds since 01.01.1970

long

POSITION_TIME_UPDATE

Position changing time

datetime

POSITION_TIME_UPDATE_MSC

Position changing time in milliseconds since 01.01.1970

long

Hello Fernando,

I used POSITION_TIME but this gives you the time when the order was filled. I need the time when the order was created. Do you know if it is possible to trace a running position back to its order?

I think there is no way to get the creation time of a position.

 
Marbo #: Hello Fernando, I used POSITION_TIME but this gives you the time when the order was filled. I need the time when the order was created. Do you know if it is possible to trace a running position back to its order? I think there is no way to get the creation time of a position.

Then read the ORDER_TIME_SETUP for the position's order as you already mentioned.

Please note however, that on a "netting" account, a position can have multiple orders for it. In this case, scan the orders, for those that have the corresponding position id ( ORDER_POSITION_ID / POSITION_IDENTIFIER ).

POSITION_IDENTIFIER

Position identifier is a unique number assigned to each re-opened position. It does not change throughout its life cycle and corresponds to the ticket of an order used to open a position.

 

Position identifier is specified in each order (ORDER_POSITION_ID) and deal (DEAL_POSITION_ID) used to open, modify, or close it. Use this property to search for orders and deals related to the position.

 

When reversing a position in netting mode (using a single in/out trade), POSITION_IDENTIFIER does not change. However, POSITION_TICKET is replaced with the ticket of the order that led to the reversal. Position reversal is not provided in hedging mode.

long

ORDER_POSITION_ID

Position identifier that is set to an order as soon as it is executed. Each executed order results in a deal that opens or modifies an already existing position. The identifier of exactly this position is set to the executed order at this moment.

long

Articles

Orders, Positions and Deals in MetaTrader 5

MetaQuotes, 2011.02.01 16:13

Creating a robust trading robot cannot be done without an understanding of the mechanisms of the MetaTrader 5 trading system. The client terminal receives the information about the positions, orders, and deals from the trading server. To handle this data properly using the MQL5, it's necessary to have a good understanding of the interaction between the MQL5-program and the client terminal.


Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
  • www.mql5.com
Position Properties - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Fernando Carreiro #:

Then read the ORDER_TIME_SETUP for the position's order as you already mentioned.

Please note however, that on a "netting" account, a position can have multiple orders for it. In this case, scan the orders, for those that have the corresponding position id ( ORDER_POSITION_ID / POSITION_IDENTIFIER ).

POSITION_IDENTIFIER

Position identifier is a unique number assigned to each re-opened position. It does not change throughout its life cycle and corresponds to the ticket of an order used to open a position.

 

Position identifier is specified in each order (ORDER_POSITION_ID) and deal (DEAL_POSITION_ID) used to open, modify, or close it. Use this property to search for orders and deals related to the position.

 

When reversing a position in netting mode (using a single in/out trade), POSITION_IDENTIFIER does not change. However, POSITION_TICKET is replaced with the ticket of the order that led to the reversal. Position reversal is not provided in hedging mode.

long

ORDER_POSITION_ID

Position identifier that is set to an order as soon as it is executed. Each executed order results in a deal that opens or modifies an already existing position. The identifier of exactly this position is set to the executed order at this moment.

long


Thank you very much (again), Fernando! :)
I will work it out tomorrow.

 
Marbo #:

Thank you very much (again), Fernando! :)
I will work it out tomorrow.

Obviously it's way more complex than I thought. I don't understand the process how to get the order properties from a running position. At the moment I am only trying to do something like this:

ulong ticket=PositionGetTicket(PositionsTotal()-1);
   if (ticket>0) {
      long id=PositionGetInteger(POSITION_IDENTIFIER);
      if (HistoryOrderSelect(id)) {
         Print(OrderGetInteger(ORDER_TIME_SETUP));
      }     
   }

But honestly, I have no idea what I am doing there. I just try some things and hopefully I get a correct order creation time.

I also googled but there is no answer to the question how to get the order properties of a position. :(

 
Marbo #: Obviously it's way more complex than I thought. I don't understand the process how to get the order properties from a running position. At the moment I am only trying to do something like this: But honestly, I have no idea what I am doing there. I just try some things and hopefully I get a correct order creation time. I also googled but there is no answer to the question how to get the order properties of a position. :(

Scan through the orders to find the ones that have their respective position id the same as that of the position you are are working with.

As a short-cut, if there is only one position per order and there have been no modifications to that position (partial closes), then the position ticket and order ticket are one and the same.

 
Marbo:

is there something like ORDER_TIME_SETUP for a running position or how can I get the time when the order was created? I didn't find anything for PositionGetInteger().

Why do you need to know this parameter?

 
Fernando Carreiro #:

Scan through the orders to find the ones that have their respective position id the same as that of the position you are are working with.

As a short-cut, if there is only one position per order and there have been no modifications to that position (partial closes), then the position ticket and order ticket are one and the same.

I think I am doing exactly this. Please have a look:

ulong position_ticket=PositionGetTicket(PositionsTotal()-1);
   Print(PositionGetString(POSITION_SYMBOL));
   if (position_ticket>0) {
      long id=PositionGetInteger(POSITION_IDENTIFIER);
      Print(id);
      HistorySelect(0,TimeCurrent());
      for (int i=HistoryOrdersTotal()-1;i>=0;i--) {
         ulong order_ticket=HistoryOrderGetTicket(i);
         if (order_ticket==position_ticket) {
            Print(order_ticket);
            Print(position_ticket);
            Print(OrderGetInteger(ORDER_TIME_SETUP));
         }
      }
   }

This scripts prints the symbol of the current position, the order ticket and position ticket are correct but the order time setup parameter is always zero. Why??

 
fxsaber #:

Why do you need to know this parameter?

My entry signal is a bullish engulfing pattern. When I see this I place a buy limit. When the order is executed (can be many hours later), I want to know the highest high between my signal (creation time of the buy limit order) and the execution of the order. This highest high should be my take profit and when the order is executed I want to set the take profit according to this highest high.

 
Marbo #: I think I am doing exactly this. Please have a look: This scripts prints the symbol of the current position, the order ticket and position ticket are correct but the order time setup parameter is always zero. Why??

No! As stated in my post #4, you need to compare the ORDER_POSITION_ID property from your orders with the POSITION_IDENTIFIER property from the position.

As for the ORDER_TIME_SETUP property, it is a datetime, but I have never used it before so have no experience with what it usually returns.

Reason: