BarCount versus BarIndex

The difference between BarCount and BarIndex can be confusing at first. Plotting can help new users ’see the picture’.

Copy and paste the following code into the Formula Editor and save it as P_BarCount before inserting it as a chart.

1
2
3
4
5
6
7
8
9
10
/*P_BarCount*/ 
 
Plot(BarCount,"BarCount",colorBlue); 
 
Plot(BarIndex(),"BarIndex",colorBlack); 
 
Plot(Cum(1),"Cum",colorRed); 
 
BarsVisible = Status("LastVisibleBar") - Status("FirstVisibleBar");
Plot(BarsVisible,"BarsVisible",colorGreen);

Figure 1.

BarCount001

BarCount is a reserved variable. It is a constant equal to the final count of all the bars, starting from one i.e. it is a one based count equivalent to the LastValue of Cum(1).

BarsVisible has been calculated for the purpose of this demo only. It is the last bar number minus the first bar number (using a one based count). In this example BarsVisible  == 22 -1.

BarIndex() is a function that returns a progressive count of the bars starting from zero i.e. it is a zero based count. The LastValue of BarIndex() always equals BarCount -1.

Note: For this example a small database, that contains only 22 bars of data, has been used (this can be confirmed from the status bar record at the bottom of Figure 1). All of the bars are contained in the screen, and there is no need to scroll forward or back, which makes it easier to understand the behavior of BarCount and BarIndex. In practice, BarsVisible varies as the screen is zoomed in and out while Cum and BarIndex dynamically adjust to the current position of the selector line. Don’t be fooled when applying P_BarCount to larger databases as the plots of BarCount, BarIndex and Cum(1) will all dynamically adjust as the charts are scrolled or zoomed, whereas in other cases they will reference the complete database.

Figure 2.

BarCount002

BarCount and BarIndex() are not normally plotted. In other modes BarCount is referenced as a constant, while the BarIndex() number can be referenced, bar by bar, by using the subscript operator [] i.e. array identifier [expression] .

BarIndex subscript examples (from the AFL reference manual):

Close [0] represents the first available Close value.

High [BarCount -1] represents the last available High value.

Note: The BarsVisible code was copied from a YahooGroup discussion on ‘How to determine the number of visible bars on a chart’ at  AmiBrokerYahooGroup message #114546

ATTACHED FORMULA FILE: P_BarCount.afl

  • To download .afl files right click on the link and pick Save Target As. Then enter Program Files/AmiBroker/Formulas/Custom as the path to access them from within AmiBroker > View > Charts. 
  • Written using AmiBroker Standard Edition v4.9 and Yahoo!Finance EOD data.

1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 4.4 out of 5)
Loading ... Loading ...

One Response to “BarCount versus BarIndex”

  1. brian_z
    September 7th, 2007 | 10:15 pm

    BarCount does not represent the number of bars in the DATABASE,
    but in the ARRAYs in the current execution.

    The arrays have DYNAMIC size depending on zoom factor. That’s why BarCount may
    and will vary.

    For example if you have 20 years of data and you are watching just
    the last year
    AmiBroker does not need and does not use all the bars in the database to calculate
    the indicators for the last year.

    It uses the last year only PLUS some previous bars required to “stabilise” indicator
    values.
    The number of extra bars depends on the functions you use in the formula.

    If you want to use truly all bars you need to use SetBarsRequired function:
    http://www.amibroker.com/f?setbarsrequired

    Best regards,
    Tomasz Janeczko
    amibroker.com

    AmiBrokerYahooGroup message #114917 - AmiBrokerYahooGroup - Re: [amibroker] Simple question about BarCount

Leave a reply