OrderOpenTime() Returns Order Closing Time

 

I am currently using the strategy tester, and I couldn't help but notice the OrderOpenTime() function returns the OrderCloseTime() value after the order has been closed. Meaning, while the order is open the OrderOpenTime() produces the right datetime value. No, I am not accidentally printing the OrderCloseTime() without noticing (haha).


At first, I thought this was because the order switched direction due to the closure (i.e closing a sell would produce a buy order with the open time being the close), but I confirmed the OrderType() value does not change. 


Can anyone replicate this issue? If so, is there anyway to resolve this? 

 
TraderTogami: I am currently using the strategy tester, and I couldn't help but notice the OrderOpenTime() function returns the OrderCloseTime() value after the order has been closed. Meaning, while the order is open the OrderOpenTime() produces the right datetime value. No, I am not accidentally printing the OrderCloseTime() without noticing (haha). At first, I thought this was because the order switched direction due to the closure (i.e closing a sell would produce a buy order with the open time being the close), but I confirmed the OrderType() value does not change. Can anyone replicate this issue? If so, is there anyway to resolve this? 

No! I can only conclude that you must be doing something wrong in your code. OrderCloseTime() returns the correct closing time of an order.

You will have to show a sample code, that compiles and recreates the conditions you claim, so that we can analyse and see how you should correct it.

 
Fernando Carreiro #:

No! I can only conclude that you must be doing something wrong in your code. OrderCloseTime() returns the correct closing time of an order.

You will have to show a sample code, that compiles and recreates the conditions you claim, so that we can analyse and see how you should correct it.

OrderCloseTime() is correct, but OrderOpenTime isn't (at least for me). 


here is some simple test code I made, and it recreates this bug-like behavior on my system (I ran this on the M5 timeframe in the strategy tester on the USDCAD utilizing MetaQuotes Data): 


EDIT & Solution: 

1. I also noticed that the ticket is still in the Pool of trades after closure, weird. 

2. Below code works, I was just blind. 

3. Solution, In my original code: I wrote Print(StrToTime(OrderOpenTime())) instead of Print(TimeToStr(OrderOpenTime())). And for some reason, this print the current time (Interesting)


Thanks for sitting through my craziness. 

#define HOURS_IN_A_DAY 24

//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+

int ticket = 0;

datetime open_time = 0;

int OnInit()

  {

//---

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Expert deinitialization function                                 |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

  {

//---

   

  }

//+------------------------------------------------------------------+

//| Expert tick function                                             |

//+------------------------------------------------------------------+

void OnTick()

  {

//---

      if(ticket == 0){

         ticket = OrderSend(Symbol(), OP_SELL, 0.01, Bid, 10, 0, 0, "Test Trade", 1, TimeCurrent() + 10*60, Red);

         open_time = OrderOpenTime();

      }

      if(TimeCurrent() > open_time + 10*60){

         if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES)){

            if(OrderClose(ticket, OrderLots(), Ask, 10, Green)){

       OrderSelect(ticket, SELECT_BY_TICKET, MODE_HISTORY)

               Print("Open Time: ", OrderOpenTime());   

            }

         }

         

      }

   

  }


 

This is my rendition of your code and the results do not show any problem with OrderOpenTime()

#property strict

//Delay close by 10 minutes
#define _DELAY_ 10*60

int ticket = WRONG_VALUE;
datetime open_time = WRONG_VALUE;

int OnInit() {
   return INIT_SUCCEEDED;
};

void OnTick() {
   if( ticket < 0 ) {
      ResetLastError();
      ticket = OrderSend( _Symbol, OP_SELL, 0.01, Bid, 10, 0, 0, "Test Trade", 1, 0, Red );
      if( ticket >= 0 ) {
         ResetLastError();
         if( OrderSelect( ticket, SELECT_BY_TICKET ) ) {
            open_time = OrderOpenTime();
         } else 
            PrintFormat( "Error: %d — Unable to select order", _LastError );
      } else {
         PrintFormat( "Error: %d — Unable to place order", _LastError );
      };
   } else {
      if( ( open_time > 0 ) && ( TimeCurrent() > open_time + _DELAY_ ) ) {
         ResetLastError();
         if( OrderSelect( ticket, SELECT_BY_TICKET ) ) {
            ResetLastError();
            if( OrderClose( ticket, OrderLots(), OrderClosePrice(), 10, Green ) ) {
               ResetLastError();
               if( OrderSelect( ticket, SELECT_BY_TICKET ) ) {
                  Print( "OpenTime:  ", OrderOpenTime(), " (", open_time, ")" );
                  Print( "CloseTime: ", OrderCloseTime() );
                  ExpertRemove();
               } else 
                  PrintFormat( "Error: %d — Unable to select order", _LastError );
            } else {
               PrintFormat( "Error: %d — Unable to close order", _LastError );
            };
         } else 
            PrintFormat( "Error: %d — Unable to select order", _LastError );
      };
   };
};
2022.10.03 12:20:23.675 Expert (Test)\TestOpenTime USDCAD,M5: loaded successfully
2022.10.03 12:20:23.675 Tester: template 'C:\Trading\MetaQuotes\MetaTrader 4\templates\tester.tpl' applied
2022.10.03 12:20:23.683 TestGenerator: spread set to 20
2022.10.03 12:20:25.703 2022.09.28 00:00:00  TestOpenTime test started
2022.10.03 12:20:25.716 2022.09.28 00:05:00  TestOpenTime USDCAD,M5: open #1 sell 0.01 USDCAD at 1.37245 ok
2022.10.03 12:20:25.716 2022.09.28 00:15:10  TestOpenTime USDCAD,M5: close #1 sell 0.01 USDCAD at 1.37245 at price 1.37244
2022.10.03 12:20:25.716 2022.09.28 00:15:10  TestOpenTime USDCAD,M5: OpenTime:  2022.09.28 00:05:00 (2022.09.28 00:05:00)
2022.10.03 12:20:25.716 2022.09.28 00:15:10  TestOpenTime USDCAD,M5: CloseTime: 2022.09.28 00:15:10
2022.10.03 12:20:25.716 2022.09.28 00:15:10  TestOpenTime USDCAD,M5: ExpertRemove function called
2022.10.03 12:20:25.716 USDCAD,M5: 44 tick events (3 bars, 102222 bar states) processed in 0:00:00.015 (total time 0:00:02.031)


 
Fernando Carreiro #:

This is my rendition of your code and the results do not show any problem with OrderOpenTime()


Thank for providing sanity check to me, see edit for further details. 

 
TraderTogami #: 1. I also noticed that the ticket is still in the Pool of trades after closure, weird.

No, it's not! You are selecting by ticket, so the third parameter "MODE_TRADES" is not applicable. That is why in my code I removed it.

As per documentation ...

pool=MODE_TRADES

[in]  Optional order pool index. Used when the selected parameter is SELECT_BY_POS. It can be any of the following values:

MODE_TRADES (default)- order selected from trading pool(opened and pending orders),
MODE_HISTORY - order selected from history pool (closed and canceled order).

 
Also, please note that many OrderXXX() functions are cached and only update when you reselect an order. That is why I reselect after the close.
 
One more, thing ... In your code you are setting an expiration time for the order, but Market Orders don't have expiration time. Just set it to 0.
 
Fernando Carreiro #:
One more, thing ... In your code you are setting an expiration time for the order, but Market Orders don't have expiration time. Just set it to 0.

I noticed that because at first I was thinking I could use that to close the order after X time, but that didn't work of course, just forgot to fully take it out. 

Fernando Carreiro #:

No, it's not! You are selecting by ticket, so the third parameter "MODE_TRADES" is not applicable. That is why in my code I removed it.

As per documentation ...

pool=MODE_TRADES

[in]  Optional order pool index. Used when the selected parameter is SELECT_BY_POS. It can be any of the following values:

MODE_TRADES (default)- order selected from trading pool(opened and pending orders),
MODE_HISTORY - order selected from history pool (closed and canceled order).

This I did not see when I looked, thanks.

 
         ticket = OrderSend(Symbol(), OP_SELL, 0.01, Bid, 10, 0, 0, "Test Trade", 1, TimeCurrent() + 10*60, Red);

         open_time = OrderOpenTime()

MT4: You can not use any Trade Functions until you first select an order.
 
William Roeder #:

MT4: You can not use any Trade Functions until you first select an order.

This is also an error, it was fixed on my end, my apologies. 

Reason: