Average Position Holding Time - page 2

 
Luca De Andrea #:
Anyone with this formula for the OnTester? Could be really useful.

Testing Statistics (https://www.mql5.com/en/docs/constants/environment_state/statistics) doesn't provide yet this parameter that we can see in the Tester Report: Average Position Holding Time

Up, anyone already test it ? 

It's very useful i think

 
Dua Yong Rew #:

correct me if I'm wrong here

So I need to declare global variables and store the position holding time(s) everytime my trade closed in On_Tick() and used it in custom max (on_Tester) later?

Can metaquotes give us a simpler solution? Should I write to ServiceDesk to appeal for this statistic? 

are you already test it with custom OnTester sir ? 

Thanks in advance

 

It's been 10 years

//+------------------------------------------------------------------+
//| Tester function                                                  |
//+------------------------------------------------------------------+
double OnTester()
  {
//---

   int      PosCount = 0;

   long     PosHoldTime = 0;

   if(!HistorySelect(0, TimeCurrent()))
   {
      Print("Failed to select history");

      return 0;
   }

   for(int i = HistoryDealsTotal() - 1; i >= 0; i--)
   {
      ulong DealTicket = HistoryDealGetTicket(i);

      if(DealTicket > 0)
      {
         // Check if the deal is a closing deal
         if(HistoryDealGetInteger(DealTicket, DEAL_ENTRY) == DEAL_ENTRY_OUT)
         {
            // Get the corresponding position ticket
            ulong PositionTicket = HistoryDealGetInteger(DealTicket, DEAL_POSITION_ID);

            // Find the opening deal for this position
            for(int j = i - 1; j >= 0; j--)
            {
               ulong PrevDealTicket = HistoryDealGetTicket(j);

               if(HistoryDealGetInteger(PrevDealTicket, DEAL_POSITION_ID) == PositionTicket &&
                  HistoryDealGetInteger(PrevDealTicket, DEAL_ENTRY) == DEAL_ENTRY_IN)
                  {
                     long  OpenTime  = HistoryDealGetInteger(PrevDealTicket, DEAL_TIME);
                     long  CloseTime = HistoryDealGetInteger(DealTicket, DEAL_TIME);

                     // Calculate holding time for this position
                     PosHoldTime += CloseTime - OpenTime;
                     PosCount++;

                     break; // Stop searching for the opening deal once it's found
                  }
            }
         }
      }
   }

   double   AverageTime = (PosCount > 0) ? ((double) PosHoldTime / 60 / PosCount) : 0;

//---
   return(AverageTime);
  }
 
Dua Yong Rew #:
long  OpenTime  = HistoryDealGetInteger(PrevDealTicket, DEAL_TIME);                      long  CloseTime = HistoryDealGetInteger(DealTicket, DEAL_TIME);
long  OpenTime  = HistoryDealGetInteger(PrevDealTicket, DEAL_TIME);
                     long  CloseTime = HistoryDealGetInteger(DealTicket, DEAL_TIME);


use ulong