For If Else "How many consecutive days has this been on my report"

Trying to write a For If Else loop to indicate how many consecutive days a symbol/ticker has appeared on my exploration report. I absolutely suck at if in AFL. IIF is easy, but I can't comprehend how to use "if" to accomplish what I want. I know what I have isn't correct, coding is not my profession so please be gentle. Condescension won't help, but coaching will :slight_smile:

What I'm trying to say here is if Filter == 1, tell me how many consecutive days this symbol has been the 20-day high. I'm trying to shift the range back by one day for every time it goes through the loop. This won't surprise you, but it doesn't work. I'm stuck and I need community help. Thank in advance!

high20 = HHV(Close, 20);
high55 = HHV(Close, 55);
	
V_high = IIf(Volume > 400000, 1, 0);
buysignal = IIf(Close == high20 OR Close == high55, 1, 0);


Filter = IIf(buysignal == 1 AND V_high == 1, 1, 0);

for( i = 1; i < BarCount; i++ )
{
  if( Filter[i] >= 1)
  {
    StartPeriod = 21 + i;
    EndPeriod = 1 + i;
    
    // Calculate the highest close price for the specific period
    HighPastClose = HHV(Ref(Close, -EndPeriod), StartPeriod - EndPeriod + 1);
    
    YesterdayClose = Ref(Close, -EndPeriod);
    
    consecutive = IIf(YesterdayClose == HighPastClose, i + 1, 1);
   }
  else
  {
    consecutive = 1;
  }
}


  
   
h20today = IIf(Close == high20, True, False);
h55today = IIf(Close == high55, True, False);
gc = IIf(Cross(MA(Close,50), MA(Close,200)), 1, 0);
M_factor = ATR(20) / Close;
V_factor = Volume / (Sum(Volume,20)/20);
pct_change = ((Close-Ref(Close, -2))/Close) * 100;


AddColumn(h20today, "High 20");
AddColumn(h55today, "High 55");
AddColumn(gc, "GC");
AddColumn(C,"Close");
AddColumn(consecutive, "Days on Report");
AddColumn(pct_change, "% Change");
AddColumn(HHV(Close, 20), "20-Day High");
AddColumn(HHV(Close, 55), "55-Day High");
AddColumn(MA(Close, 50), "SMA 50");
AddColumn(MA(Close, 200), "SMA 200");
AddColumn(ATR(20),"ATR 20");
AddColumn(M_factor, "M Factor");
AddColumn(Volume, "Volume");
AddColumn(V_factor, "Vol. 20 Factor");
AddColumn(ADX(20), "ADX 20");




You don't need loops for that.

Consecutive = BarsSince( HHVBars( Close, 20 ) > 0 );

Plot( Consecutive, "Consecutive HHV", colorRed ); 

Thank you very much for the help! I really appreciate it