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

 

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?

 

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.

 

May someone help me? I want to find the position of the highest bar in the range of nearest 80 bars. I have tried to code below, however, I do not get the right result. May someone helps me with what wrong with my code and how to correct it to find out how many bars from the current to the highest point?


int y2bar= iHighest(NULL,PERIOD_H1,MODE_HIGH,80,1);// find the highest bar

double y2= iHigh(NULL,PERIOD_H1,y2bar);// value of the highest bar


int x2= Bars(NULL,PERIOD_H1,Time[y2bar],Time[0]);// count how many bars from current to the  highest

Or using iBarShift(NULL,0,iTime(NULL,0,y2bar));


Both codes turn wrong result

 
Dang Thanh:

May someone help me? I want to find the position of the highest bar in the range of nearest 80 bars. I have tried to code below, however, I do not get the right result. May someone helps me with what wrong with my code and how to correct it to find out how many bars from the current to the highest point?


int y2bar= iHighest(NULL,PERIOD_H1,MODE_HIGH,80,1);// find the highest bar

double y2= iHigh(NULL,PERIOD_H1,y2bar);// value of the highest bar


int x2= Bars(NULL,PERIOD_H1,Time[y2bar],Time[0]);// count how many bars from current to the  highest

Or using iBarShift(NULL,0,iTime(NULL,0,y2bar));


Both codes turn wrong result

Both codes seem ok on my chart. In what way is the result wrong?

Perhaps you are not on H1?  Also...

iBarShift(NULL,0,iTime(NULL,0,y2bar));
Reason: