Bar Counting

 

I would like to count bars since an event occurs. I used a flag showing the time of the event and I insert it on the formula iBarShift. I don’t know where my mistake is, but the result is always the same, and don’t start with 0, 1, 2… I think that is missing a looping between new data.

Thanks

       
      if(Close[i+1]<=BuyTrailingStop[i+1] && Close[i]>BuyTrailingStop[i])
         {
            UP_Trend_Beggining[i]=Time[i]; // Buy Flag
         }
      else
         {  
            UP_Trend_Beggining[i]=0; 
         }
      if(Close[i+1]>=BuyTrailingStop[i+1] && Close[i]<BuyTrailingStop[i])
         {
            DOWN_Trend_Beggining[i]=Time[i]; // Sell Flag
         }
      else
         {     
            DOWN_Trend_Beggining[i]=0; 
         }
        
//-------------------------------------------------------------------------------------------------------//
//                
//------------------------------------------------------------------------------------------------------        
      int UP_Shift=iBarShift(NULL,0,UP_Trend_Beggining[i]);
      int DOWN_Shift=iBarShift(NULL,0,DOWN_Trend_Beggining[i]);
      
      if(Close[i]>BuyTrailingStop[i])
         {
            UP_Trend[i]=UP_Shift;
         }

      if(Close[i]<BuyTrailingStop[i])
         {
            DOWN_Trend[i]=DOWN_Shift;; 
         }
        
        
      
   
//----
 

I tryed in another way, but it caused a bug on my PC (lol).

      if(UP_Trend_Beggining[i]==1)
      {
         while(DOWN_Trend_Beggining[i]==0)
         {
            UpShift++;
         }
            if(Close[i]>BuyTrailingStop[i])
            {
               UP_Trend[i]=UpShift;
            }
         
      }
      
      if(DOWN_Trend_Beggining[i]==1)
      {
         while(UP_Trend_Beggining[i]==0)
         {
            DownShift++;
         }
            if(Close[i]<BuyTrailingStop[i])
            {
               DOWN_Trend[i]=DownShift;
            }
         
      }   
 
rodrigosm:

I would like to count bars since an event occurs. I used a flag showing the time of the event and I insert it on the formula iBarShift.

 if(Close[i+1]<=BuyTrailingStop[i+1] && Close[i]>BuyTrailingStop[i])
         {
            // UP_Trend_Beggining[i]=Time[i]; // Buy Flag
            // The number of bars since this event is: i
         }
 
WHRoeder:


Thanks, it worked well... but now I don't known what is the function of iBarShift.... I thought it was to count periods since a specified time!

int iBarShift( string symbol, int timeframe, datetime time, bool exact=false) 

Search for bar by open time. The function returns bar shift with the open time specified.

Reason: