any idea how to count the bars when condition satisfied

 

I have a problem regarding counting the number of bars that satisfied certain conditions..

conditions are

1. ema(C,8) cross down ema(C,13) in the previous bar.

2.ema(C,8) cross above ema(C,13) M number of bars back before the cross in 1.

3.ema(C,8) cross down ema(C,13) N number of bars back before the cross in 2.

4. ema(C,8) cross down ema(C,13) P number of bars back before the cross in 3.

the question is how to design a routine that does the counting of bar numbers between crosses.the graph when plotted will look like the MACD histogram.the total cross is

4(four),UP,DOWN,UP and then DOWN.Please enlightened.below are partial codes to implement the problem

                                      int j,k;
                                      for(int i=1;i<=200;i--)//count maximum 200 is randomly chosen to enclosed 4(four) crosses as described above 
                                      {
                                            
                                          
                                          CondArray1[i]=iMA(NULL,0,8,0,MODE_EMA,PRICE_CLOSE,i);
                                          CondArray2[i]=iMA(NULL,0,13,0,MODE_EMA,PRICE_CLOSE,i);

                                         if ( CondArray1[i]>CondArray2[i]) j++; // j will hold the total count of EMA(C,8) above EMA(C,13)
                                         if ( CondArray1[i]<CondArray2[i]) k++; // k will hold the total count of EMA(C,8) below EMA(C,13)
                                     
                                      }
                                      //till here j,k holds the count of bars in first two loops formed by crosses.the problem is to count the number of bars
                                      //in last loop 'separately' from j counter as the cross below condition is repeating again and the first 'if block' will be activated
                                      // which is not desired as I want count of last loop separately

any idea will is welcome pleas
 

correction:

the loop counter i should start from 2 as the latest cross down completed in previous bar

 

:8 another correction:

the last cross-cross number 4 is UP and not down

 

calculate CondArray-s in advance, then you can check if a new loop started by checking for example:

 ( CondArray1[i]>CondArray2[i]) && ( CondArray1[i+1]<=CondArray2[i+1]) 


then you can reset your counter(s).

 
for(int i=1;i<=200;i--)

with this it will never work, write i++ instead of i--

 

that's another correction :D.yeh it ll never work.what a mistake/mistype.....you hv given the crossing condition check but i want the number of bars in the 3(three) loop formed due to 4(four) crosses.

can you clarify how to reset the counter while 'saving' the count of bar numbers in each loop for future use.if ther is another implementation idea other than this,you are highly welcome to share ..thnx

 
MirTD:

that's another correction :D.yeh it ll never work.what a mistake/mistype.....you hv given the crossing condition check but i want the number of bars in the 3(three) loop formed due to 4(four) crosses.

can you clarify how to reset the counter while 'saving' the count of bar numbers in each loop for future use.if ther is another implementation idea other than this,you are highly welcome to share ..thnx


I think of something like this below. It is not tested, maybe you will see up instead of down or bar count +1 higher.



   double CondArray1[], CondArray2[];
   int j,k;
   for(int i=1;i<=200;i++)
   {
      CondArray1[i]=iMA(NULL,0,8,0,MODE_EMA,PRICE_CLOSE,i);
      CondArray2[i]=iMA(NULL,0,13,0,MODE_EMA,PRICE_CLOSE,i);
   }

   int bar_cnt=0;
   int loop_cnt=0;
   for(i=1;i<=(200-1);i++)
   {
      if (( CondArray1[i]>CondArray2[i]) && ( CondArray1[i-1]<=CondArray2[i-1]))
      {
         Print("new loop #",loop_cnt," dir UP. previous loop bars count: ", cnt);
         bar_cnt=0;
         loop_cnt++;
      }
      if (( CondArray1[i]<CondArray2[i]) && ( CondArray1[i-1]>=CondArray2[i-1]))
      {
         Print("new loop #",loop_cnt," dir DOWN. previous loop bars count: ", cnt);
         bar_cnt=0;
         loop_cnt++;
      }

      bar_cnt++;
   }