BarIndex() Question

My AFL code is below which needs to find the bar at which the expression occurs.
bi = BarIndex();
Period = 50;
FirstHighest = HHV(H, Period);
FirstLowest = LLV(L, Period);

FirstHighestBarIndex = ValueWhen(H == FirstHighest, bi,1);
FirstLowestBarIndex = ValueWhen(L == FirstLowest, bi,1);

Filter = 1;
AddColumn(FirstHighest, "FirstHighest", 1.2 );
AddColumn(FirstLowest, "FirstLowest", 1.2 );
AddColumn(FirstHighestBarIndex, "FirstHighestBarIndex", 1.0 );
AddColumn(FirstLowestBarIndex, "FirstLowestBarIndex", 1.0 );

The explorer resuls is as below.
Untitled

The question is barIndex (FirstHighestBarIndex) results. It gives the results above 7000, but the total number of historical bars available for the given Symbol is only around 400.

Instead of incorrect assumptions, add the code:

AddColumn( BarCount, "BarCount" );

Also your code is incorrect because it does not give you single "first" index but array of indices and certainly not first, but last when condition was met.

To get better understanding of what is happening in your code and how functions work, use advice given here: How do I debug my formula?

2 Likes