I'll say sorry in advance. I am a newb...I have tried finding an answer to this question. I'm sure it has been answered, but here in the forum, or in the help files or on Google and I have tried looking, but I'm still confused. So thanks for your time.
I don't understand the purpose of Barindex().
I know that every array has a numerical index for each array item. Is this what bar index returns, the index list of an array?
And what is its practical application?
Again, real sorry. I know there are hundreds of examples. Once I get the essence of what it does and how it does it, I'm sure it will all make sense.
barindex is the "id or index number" for the exact "bar position" in an array. The index depends on the number of bars in that array. First index number of an array item is 0.
Highest index number is the most current quotation stored in your database.
You can query that quote at a special array position for example:
As written here 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?
You can find how given function works using Exploration:
Yes as manual says it returns "zero-based bar number".
Barindex() is an array function itself. So it does not return single number but array of bar numbers. BarCount on the other hand is not an array but gives total number of bars (it is type number).
BTW, also read about QuickAFL article of KB (in regards to subset of all available data bars)
Note: following one is different in versions since AB 5.30:
"It is also worth noting that when QuickAFL is used, BarIndex() function does NOT represent elements of the AFL array, but rather the indexes of ENTIRE quotation array."
since that article is of year 2008 (so before changes been made in AB 5.30 of 2010, see manual picture above). So since AB 5.30 it (Barindex()) always starts from zero (also if QuickAFL is turned on).
See last three pictures of this post showing the difference between before and since AB 5.30.
For example you may iterate just visible number of bars.
bi = Barindex();// array
fvb = FirstVisibleValue( bi );// number
lvb = LastVisibleValue( bi );// number
for( i = fvb; i <= lvb; i++ ) {
// do something
}
Getting bar number at start or end of analysis range
bi = BarIndex();
fbr = Status("firstbarinrange");// flagging first bar of range - returns true or false
startbar = LastValue(ValueWhen(fbr, bi));// get first bar of analysis range (this is single element)
Enter every n-th bar
Buy = BarIndex() % 10 == 0; // buy every 10-th bar