BarNumber (Function)
The BarNumber series function assigns a reference number to each bar after MaxBarsBack is satisfied.
Syntax
BarNumber
Returns (Integer)
A positive numeric reference value for each bar on the chart.
Parameters
None
Remarks
MaxBarsBack is the minimum number of referenced historical bars required at the beginning of a chart to begin calculating trading strategies, analysis techniques, and functions. For example, a 10-bar moving average would require a MaxBarsBack setting of 9 to calculate, which is 9 historical bars and the current bar.
Since BarNumber is based on MaxBarsBack, if there are 500 bars in a chart, with a MaxBarsBack setting of 50, the next bar after the 50th bar on the chart moving left to right, will be BarNumber = 1. The last bar on the chart (most recent) will be BarNumber = 451.
BarNumber is often used to identify a particular bar or number of bars because of some special occurrence or situation that you want to test or factor into your analysis.
The BarNumber function is similar to the reserved word CurrentBar. However, CurrentBar does not allow previous bar references: BarNumber[5](of five bars ago) is correct, however, Currentbar[5] is incorrect and does not work.
Examples
Assigns the BarNumber for each bar to Value1, then plots Value1:
Value1 = BarNumber;
Plot1(Value1, "BarNum");
Assigns the BarNumber to Value1 when Condition1 is true, and assigns the number of bars since Condition1 occurred to Value2:
if Condition1 then
Value1 = BarNumber;
if BarNumber > Value1 then
Value2 = CurrentBar – Value1;