How long is trade open? Exclude non-trading days

 

In one of my EAs i'm calculating how long a trade is open (in seconds)

I used it to test if order were open for longer than a week. In such case the inclusion of a weekend was no factor. As i'm now thinking of testing for shorter periods - 2 or 3 days - the weekend is a factor.

On Monday noon, I'd like an order that was opened on Friday noon to come up as open for 1 day.

Is there any easy way of doing that? to exclude the weekend?

 
Route206: On Monday noon, I'd like an order that was opened on Friday noon to come up as open for 1 day.

Depending on your broker's timezone, Friday may be a short day, there could also be a short Sunday. So you need to define what you mean by “day.” I.e. 24 hours or noon to noon.
          Time zone - Trading Systems - MQL5 programming forum #2 (2020.11.13)

  1. 24 Hours

    int iOpened        = iBarShift( OrderOpenTime() );
    int elapsedTrading = iOpened * PeriodSeconds();
    int elapsedDays    = elapsedTrading / PeriodSeconds(PERIOD_D1);

  2. X to X

    int elapsedDays=0; int opendate = date( OrderOpenTime() );
    while(--iOpened >= 0){
       int day = date( Time[iOpened] );
       if(day != openDay){ openDay = day; ++elapsedDays; }
    }       // Find bar of the same time one day ago - MQL4 programming forum (2017) 
    if( time( Time[0] ) < time( OrderOpenTime() ) --elapsedDays; // Don't count today.
 

Thanks!

24h would work, but your option 1. is chart dependent with different outcomes (MQL4)

Testing a 4 day old ticket with weekend on an M1 or MN gives Elapsed days of 0. On the W1 it gives 7 and on D1 it gives 3.

Only on the M5, M15, M30, H1 and H4 is it correct with 2.

I think using DayofWeek() might be a solution, in that when selecting tickets older than 3 days, then the test on Monday, Tuesday or Wednesday would be for 5 days (>432000s) and only a test on Thursday and Friday would be for 3 days (>259200s).

 

here is a solution for those that have the same issue - it does NOT use bars or charts, which i think is more straightforward.

int DaysOpen()
  {
   int cal=0; //calenderdays which includes weekends
   int td=0; //tradingdays that has weekends EXCLUDED
   int day=DayOfWeek();
   cal=int((TimeLocal()-time_opening)/86400);
   switch(day)
     {
      case 0:
        {
         while(cal>0)
           {
            if(cal>=7)
              {
               td+=5;
               cal-=7;
              }
            else
              {
               td+=(cal-1);
               cal=0;
              }
           }
         break;
        }
      case 1:
        {
         while(cal>0)
           {
            if(cal>=7)
              {
               td+=5;
               cal-=7;
              }
            else
              {
               td+=(cal-2);
               cal=0;
              }
           }
         break;
        }
      case 2:
        {
         if(cal<=1)
            td=cal;
         if(cal>1)
           {
            td=1;
            cal-=1;
            while(cal>0)
              {
               if(cal>=7)
                 {
                  td+=5;
                  cal-=7;
                 }
               else
                 {
                  td+=(cal-2);
                  cal=0;
                 }
              }
           }
         break;
        }
      case 3:
        {
         if(cal<=2)
            td=cal;
         if(cal>2)
           {
            td=2;
            cal-=2;
            while(cal>0)
              {
               if(cal>=7)
                 {
                  td+=5;
                  cal-=7;
                 }
               else
                 {
                  td+=(cal-2);
                  cal=0;
                 }
              }
           }
         break;
        }
      case 4:
        {
         if(cal<=3)
            td=cal;
         if(cal>3)
           {
            td=3;
            cal-=3;
            while(cal>0)
              {
               if(cal>=7)
                 {
                  td+=5;
                  cal-=7;
                 }
               else
                 {
                  td+=(cal-2);
                  cal=0;
                 }
              }
           }
         break;
        }
      case 5:
        {
         if(cal<=4)
            td=cal;
         if(cal>4)
           {
            td=4;
            cal-=4;
            while(cal>0)
              {
               if(cal>=7)
                 {
                  td+=5;
                  cal-=7;
                 }
               else
                 {
                  td+=(cal-2);
                  cal=0;
                 }
              }
           }
         break;
        }
      case 6:
        {
         if(cal<=5)
            td=cal;
         if(cal>5)
           {
            td=4;
            cal-=5;
            while(cal>0)
              {
               if(cal>=7)
                 {
                  td+=5;
                  cal-=7;
                 }
               else
                 {
                  td+=(cal-2);
                  cal=0;
                 }
              }
           }
         break;
        }
     }
   return(td);
  }