Indicators: BarDuration

 

BarDuration:

This is a simple indicator to display histogram of custom bars' durations in minutes. Applicable for renko boxes, PnF, equivolume bars, etc.

BarDuration

Author: Stanislav Korotky

 

Thanks for the nice code.

I have a little improvement, as the code is (wrongly) assuming a fixed number of trading sessions per day for all the week days.

   struct Session
   {
      datetime from[], to[];
      Session()
      {
         ZeroMemory(this);
      }
   };

   static Session schedule[7];
   static bool init = false;

   if(!init)
   {
      for(int j = 0; j < 7; j++)
      {
         i = 0;
         while(SymbolInfoSessionQuote(_Symbol, (ENUM_DAY_OF_WEEK)j, i++, from, to))
         {
            ArrayResize(schedule[j].from, i);
            ArrayResize(schedule[j].to, i);
            schedule[j].from[i - 1] = from;
            schedule[j].to[i - 1] = to;
         }
      }
      init = true;
   }

   i = 0;
   while(i < ArraySize(schedule[d].from))
   {
      if(time >= (ulong)schedule[d].from[i] && time < (ulong)schedule[d].to[i])
      {
         return (datetime)(date + schedule[d].to[i]);
      }
      i++;
   }
 
amrali #:

Thanks for the nice code.

I have a little improvement, as the code is (wrongly) assuming a fixed number of trading sessions per day for all the week days.

Thank you for pointing out this nuance, but I think your change is not necessary.

My version allocates schedule array by maximal number of sessions among all days, with zeros in from/to fields for all other days where the later session might be missing. Such "zero" sessions are eliminated from consideration in the latter lookup loop.

 
Yes, I see. Thank you.
Also, in your version ArrayPrint(schedule) works nicely.