Duration of a Trade. Time a trade is open. ORDER_TIME_DONE_MSC.

 

Good day, 

I have an EA to perform an Ordermodify function after the Trade has been opened for a certain Period. 

The logic is simple:

Step 1: Enter a trade

Step 2: If the trade is open for longer than Time "T1" but less than "T2", perform Ordermodify and reduce the TP.

Step 3: If the trade is open for longer than Time "T2" but less than "T3", perform another Ordermodify and reduce TP again.

Step 4: IF the trade is open for longer than "T3" , close all trades. 

============================================================================================================

I have tried to use this within a loop.

long  time_done_msc=HistoryOrderGetInteger(PositionGetTicket(i),ORDER_TIME_DONE_MSC);

to get the time of the trade, it does not work. 

int HR=3600000, Pip=10000;     ///    HR - 1 hour in Milliseconds
int T1=14, T2=25, T3=40;
int TP=25, TP2=15, TP3=7;


void LongTrades() {           for (int i=PositionsTotal()-1; i>=0; i--)
                              {  int ticket=PositionGetTicket(i);
                                       
                              long now = (long) TimeCurrent() * 1000;
                              long postime = PositionGetInteger(POSITION_TIME_MSC);
                              long elapsedmsc = now - postime;
                              
                              long  time_done_msc=HistoryOrderGetInteger(PositionGetTicket(i),ORDER_TIME_DONE_MSC);

                              double VarTP=PositionGetDouble(POSITION_TP); double VarOP=PositionGetDouble(POSITION_PRICE_OPEN);

                              if ( (VarTP==VarOP+TP2/Pip) | (VarTP==VarOP-TP2/Pip ) )  Clash=1;    //OrderMod1 already done
                              if ( (VarTP==VarOP+TP3/Pip) | (VarTP==VarOP-TP3/Pip ) )  Clash=1;    //OrderMod2 already done
                                               
                              if (elapsedmsc > (T1*HR) && elapsedmsc < (T2*HR) && Clash!=1 )  //========== if trade is longer T1  but less than T2, order modify    
                                          {
                                          Print("*****         Time is : ",elapsedmsc/HR," Hours,  so we are Performing MOD1");
                                          Print("Trade Dir: ", PositionGetInteger(POSITION_TYPE), " //  Entry Point :",PositionGetDouble(POSITION_PRICE_OPEN), " //  TP:",PositionGetDouble(POSITION_TP) );
                       
                                          if (PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_BUY)   TradeFunc.PositionModify(ticket,NULL,PositionGetDouble(POSITION_PRICE_OPEN)+TP2/Pip);     
                                          if (PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_SELL)  TradeFunc.PositionModify(ticket,NULL,PositionGetDouble(POSITION_PRICE_OPEN)-TP2/Pip);                                    
                                          }
                                  
                              if (elapsedmsc > (T2*HR) && elapsedmsc < (T3*HR) && Clash!=1 ) //=============== if trade is longer than T2 but less than T3.
                                       {  Print("*****         Time is : ",elapsedmsc/HR," Hours,  so we are Performing MOD2");
                                          Print("Trade Dir: ", PositionGetInteger(POSITION_TYPE), " //  Entry Point :",PositionGetDouble(POSITION_PRICE_OPEN), " //  TP:",PositionGetDouble(POSITION_TP) );
                       
                                          if (PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_BUY)   TradeFunc.PositionModify(ticket,NULL,PositionGetDouble(POSITION_PRICE_OPEN)+TP3/Pip);     
                                          if (PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_SELL)  TradeFunc.PositionModify(ticket,NULL,PositionGetDouble(POSITION_PRICE_OPEN)-TP3/Pip);                                       
                                       }
                                                                          
                              if (elapsedmsc>(T3*HR) && PositionGetDouble(POSITION_PROFIT)>0) //=============== if trade is longer T3, order close at next profitability
                                 {  Print("*****         Time is : ",elapsedmsc/HR," Hours,  so we are Closing the ticket once its profitable");
                                    Print("Trade Dir: ", PositionGetInteger(POSITION_TYPE), " //  Entry Point :",PositionGetDouble(POSITION_PRICE_OPEN), " //  TP:",PositionGetDouble(POSITION_TP) );
                 
                                    TradeFunc.PositionClose(ticket);     }
                              }    
                 }

 
Chijioke Chukwunonso Iloabachie:

Good day, 

I have an EA to perform an Ordermodify function after the Trade has been opened for a certain Period. 

The logic is simple:

Step 1: Enter a trade

Step 2: If the trade is open for longer than Time "T1" but less than "T2", perform Ordermodify and reduce the TP.

Step 3: If the trade is open for longer than Time "T2" but less than "T3", perform another Ordermodify and reduce TP again.

Step 4: IF the trade is open for longer than "T3" , close all trades. 

============================================================================================================

I have tried to use this within a loop.

to get the time of the trade, it does not work. 


It looks like you're mixing position functions with order functions which won't work.

Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles

Orders, Positions and Deals in MetaTrader 5
Orders, Positions and Deals in MetaTrader 5
  • www.mql5.com
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.
 
Ryan L Johnson #:

It looks like you're mixing position functions with order functions which won't work.

Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles

isnt 

HistoryOrderGetInteger(PositionGetTicket(i),ORDER_TIME_DONE_MSC);

the correct way?

 
Chijioke Chukwunonso Iloabachie #:


isnt : 

 the correct way?

I use:

PositionGetInteger(POSITION_IDENTIFIER)

The documentation that I posted explains the differences.

PositionGetInteger(POSITION_TIME)
Orders are pending and not executed. History orders are in a separate pool from positions.
 
Ryan L Johnson #:
PositionGetInteger(POSITION_TIME
PositionGetInteger(POSITION_TIME

this worked. 


Thank you so much. 

 
Chijioke Chukwunonso Iloabachie #:

this worked. 


Thank you so much. 

int      timeElapsed = (int)(TimeCurrent() - PositionGetInteger(POSITION_TIME));

May be this one helps you

 
Anil Varma #:

May be this one helps you

Best practice is to convert datetime to a ulong (https://www.mql5.com/en/forum/338238/page4#comment_22674823).
SymbolInfoTick() doubt - How to convert Ulong milliseconds from a date to time?
SymbolInfoTick() doubt - How to convert Ulong milliseconds from a date to time?
  • 2021.06.03
  • Fernando Carreiro
  • www.mql5.com
The question is, how get the ulong milliseconds from a date. Convert 2021/06/03 22:30 to milliseconds. I've used copyticksrange with a little range of time and the result has been exactly what i was expecting