How to get 0 based bar index after TimeFrameSet?

I am trying to calculate weekly data on a daily chart. And I found that the returned value from BarIndex() is not always 0 based, after calling TimeFrameSet(). How could I get the correct weeklyBarsAvailable? It should increment from 0 to 60 during the first 60 weeks of the stock.

_SECTION_BEGIN("Close Ranking");
TimeFrameSet(inWeekly);
weeklyBarIndex = BarIndex();
weeklyBarsAvailable = Min(weeklyBarIndex, 60);
weeklyCloseRank = PercentRank(Close, weeklyBarsAvailable);
TimeFrameRestore();
weeklyBarsIndexExpanded = TimeFrameExpand(weeklyBarIndex, inWeekly, expandLast);
weeklyBarsAvailableExpanded = TimeFrameExpand(weeklyBarsAvailable, inWeekly, expandLast);
weeklyCloseRankExpanded = TimeFrameExpand(weeklyCloseRank, inWeekly, expandLast);
Plot(weeklyBarsIndexExpanded, "weeklyBarsIndexExpanded", colorRed, styleDashed);
Plot(weeklyBarsAvailableExpanded, "weeklyBarsAvailableExpanded", colorPink, styleDashed);
Plot(weeklyCloseRankExpanded, "weeklyCloseRankExpanded", colorYellow, styleDashed);
_SECTION_END();

BarIndex() for AAPG started with 39, then incremented by 1 every week.


BarIndex() for AAPW started from 0, jumped to 26, then incremented by 1 every week.

I would like to see something start from 0 and increment by 1 every week.

hint: if something is 0, 1, 2, 3... incrementally +1 in daily TF, and then you call Compress by TimeFrameSet() in Weekly... you are going to get inWeekly which is 5*inDaily + 1th bar
like, 5, 11, 17... etc
Now, when you do TimeFrameExpand() with expandLast,
this will be 5 5 5 5 5,11 11 11 11 11, 17 17 17 17.... and so on

hope this helps you rewrite your thought.

so this worked for me to get a 0 based number that increments weekly after TimeframeSet(inWeekly)

	bi = BarIndex();
	barChange = bi != Ref(bi, -1);  
	CustomBarIndex = Cum(barChange);