A question about BarIndex()

Hi community.

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.

Thanks

Hello kgalea,

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.

barindex

You can query that quote at a special array position for example:

zC = C[1397];

https://www.amibroker.com/guide/h_understandafl.html

Best regards,
Peter

5 Likes

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:

AddColumn(BarIndex(), "BarIndex", 1.0 );
Filter = 1;

Yes as manual says it returns "zero-based bar number".
33


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

etc.

2 Likes

Hi, Just wanted to say thank you to everyone who took the time to post. I appreciate it :grin:

1 Like

Thanks Tomasz, this article you linked me to was amazing. I feel alot more empowered now

Cheers

Thanks for taking the time to explain Barindex to me. What you wrote has helped heaps

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.