Counting bars including weekends - page 3

 
Igor Makanu:

complicated, there is a time between dates in seconds (datetime) - this is enough to calculate the number of whole days in which 24 hours, 60 minutes in each hour and 60 seconds in a minute.... google to help

If we're talking about a twisted way of counting, you can do it this way:

you can, as you suggest, count each bar and find out if there was a weekend between the current and the previous bar.... in general here only the possibility of complicating the task is limited by desire ))))

We should start by assuming that time intervals start and end at 0:0.

 
Dmitry Fedoseev:

We have to start by assuming that the time intervals start and end at 0:0.

in my example this assumption is not needed, but we need to assume that the calculation is within one calendar year

there was a solution in the first post, alternatively there should be another solution in the topic Only useful codes from kimiw, I think I have seen

 
Igor Makanu:

in my example this assumption is not needed, but we need to assume that the calculation is within one calendar year

in the first post was the solution, as an option - there should be another solution in the thread Only useful codes from KimIV, I think I saw

It is not just days that need to be counted, but working days or weekends.

 
Dmitry Fedoseev:

It is not just days that need to be counted, but weekdays or weekends.

It's hard to say, the wording of the problem is ambiguous:

forex2030:

how do you know how many weekends there were between points, e.g. on D1, and then add them to the required bars?

if according to your wording, then copyCopyTime() the day bars and analyze the weekend bars in the loop, but what to do with the weekend bars? - it says that it wants to add them?
 
Igor Makanu:

It's hard to say, the wording of the problem is ambiguous:

...

Where is the ambiguity: "how do you find out how many outputs there were between points on e.g. D1 to add them to the required bars? "

Why this is needed is another question.

 
int WorkingDays(datetime startDate,datetime endDate){

   int d=int((endDate-startDate)/86400);
   
   int m[7,7]={
   {0, 0,1,2,3,4,5}, // воскр
   {0, 1,2,3,4,5,5}, // понед
   {0, 1,2,3,4,4,4}, // вторн
   {0, 1,2,3,3,3,4}, // среда
   {0, 1,2,2,2,3,4}, // четв
   {0, 1,1,1,2,3,4}, // пятн
   {0, 0,0,1,2,3,4}  // субб
   };

   return (d/7)*5+m[(int)TimeDayOfWeek(startDate)][d%7];
}

Seems to have checked, seems fine. Check if you're not too lazy.

 
Dmitry Fedoseev:

Where is the ambiguity: " how do you know how many outputs there were between points on e.g. D1 so you can add them to the bars you want? "

Why would that be necessary is another question.

I think that if we chat for a couple more hours, we will start to evaluate the problem, and how many outputs can be between two bars.... and surely there is always a correspondence that before Monday comes Sunday?

)))))

with correcting the calculation of days by subtracting the datetime , I think this code will work for all cases

input datetime d_start = D'2020.01.01 12:30:27';
input datetime d_stop  = D'2020.02.01';

//+------------------------------------------------------------------+
void OnStart()
{
   const int day_in_sec = 24 * 60 * 60;
   datetime d_corect1 = d_start - d_start % day_in_sec;
   datetime d_corect2 = d_stop - d_stop % day_in_sec;
   int result = (int)((d_corect2 - d_corect1) / day_in_sec);
   printf("всего %i дней ", result);
}
//+------------------------------------------------------------------+
 
forex2030:

By date

To the first point I add bars

Then count the number of bars between these dates.

int  Bars( 
   string           symbol_name,     // имя символа 
   ENUM_TIMEFRAMES  timeframe,       // период 
   datetime         start_time,      // с какой даты 
   datetime         stop_time        // по какую дату 
   );
You get less than the number of bars you set. The difference between the number of bars you set and the number of bars you get is the number of days off. Thus, we obtain the number of bars of any period, even of a minute period. Just carefully check how many bars are returned by this function. Either the two outer bars or one of them will be considered. I do not remember exactly, but it seems both of them are considered.
 
Alexey Viktorov:

Then count the number of bars between these dates.

You get less than the given number of bars. The difference between the given number of bars and the received number of bars is the number of days off. Thus, we obtain the number of bars of any period, even of a minute period. Just carefully check how many bars are returned by this function. Either the two outer bars or one of them will be considered. I do not remember exactly, but it seems both of them are considered.

Talk about the dates for which there are no bars yet.

 

just in case, I'll add an example - it counts how many bars are missing

input datetime d_start = D'2020.01.01 12:30:27';
input datetime d_stop  = D'2020.02.01';

//+------------------------------------------------------------------+
void OnStart()
{
   const int day_in_sec = 24 * 60 * 60;
   datetime d_corect1 = d_start - d_start % day_in_sec;
   datetime d_corect2 = d_stop - d_stop % day_in_sec;
   int result = (int)((d_corect2 - d_corect1) / day_in_sec);
   printf("всего %i дней ", result);
   datetime t_arr[];
   int allbarD1 = CopyTime(_Symbol, PERIOD_D1, d_start, d_stop, t_arr);
   if(allbarD1 < 0)
   {
      printf("Error CopyTime() func № %i", GetLastError());
      return;
   }
   printf("Всего выходных % i дней", result - allbarD1);
}
//+------------------------------------------------------------------+
Reason: