My bar count attempt, have i written to much code just to count bars?

5105

I was trying to find a way of counting bars only after a condition is true. I have coded this so it can recognise any timeframe and adjust to suit.

```void OnTick()
{
// Version 1
bool periodm1 = false;
bool periodm5 = false;
bool periodm15 = false;
bool periodm30 = false;
bool periodh1 = false;
bool periodh4 = false;
bool periodd1 = false;

if(Period() == PERIOD_M1) periodm1 = true;
if(Period() == PERIOD_M5) periodm5 = true;
if(Period() == PERIOD_M15) periodm15 = true;
if(Period() == PERIOD_M30) periodm30 = true;
if(Period() == PERIOD_H1) periodh1 = true;
if(Period() == PERIOD_H4) periodh4 = true;
if(Period() == PERIOD_D1) periodd1 = true;

int hrCount = Hour();
int minCount = Minute(); // Counts 0,1,2,3, as each minute passes

//+------------------------------------------------------------------+
// I use 4 objects to count each bar.
// secCount1 Counts each minute using a second count  On tick its 0,1,4,6,  0,1,3,5,7,11 etc on each minute though not every minute
// secCount2 counts every tick    0,1,2,3,4,5,6,7,8,etc
// minCount catches each time minCount gets to 5
// barCount is used to do the main count used in statements
//+------------------------------------------------------------------+

static int barCount = 0;
int secCount1 = Seconds();
static int secCount2 = Seconds();
secCount2++;

if(periodm1 && secCount1 == 0) secCount2 = 0;

if(periodm5 && secCount1 == 0 && (minCount==0 || minCount==5 || minCount==10
|| minCount==15 || minCount==20 || minCount==25 || minCount==30 || minCount==35
|| minCount==40 || minCount==45 || minCount==50 || minCount==55)) secCount2 = 0;

if(periodm15 && secCount1 == 0 && (minCount==0 || minCount==15 || minCount==30
|| minCount==45)) secCount2 = 0;

if(periodm30 && secCount1 == 0  && (minCount==0 || minCount==30)) secCount2 = 0;

if(periodh1 && secCount1 == 0 && minCount == 0) secCount2 = 0;

if(periodh4 && secCount1 == 0 && minCount == 0 && (hrCount == 0 || hrCount == 4
|| hrCount == 8 || hrCount == 16 || hrCount == 20)) secCount2 = 0;

if(periodd1 && secCount1 == 0 && minCount == 0 && hrCount == 1) secCount2 = 0;

// Use this statment in main controling statement (Here ive used it to only count if hours are between 8am and 9am
if(hrCount >= 8 && hrCount < 9 && secCount2 == 0)
{
barCount++;
Print(barCount);
}

}```

In this code my condition is meant to count each bar between 8am and 9am each day on the 5m chart.

I am happy with this code as it does the job okay but have i written too much code for such a simple task? I couldnt figure out how i might use predefined Bars variable because it only counts all bars on chart when i wanted to only count when a condition is true?

2320

Unless you are expecting no activity during an entire 5mins, there are usually 12 M5 bars in an Hour.

However, you can also use "iBarShift()" to get an index for a start date/time and as well as an index for end date/time. The difference of the two indices will give you the number of bars in between those two dates/times.