CurrentBars

View as Markdown

Definition

Holds an array of int values representing the number of the current bar in a Bars object. An int value is added to this array when calling the AddDataSeries() method. Its purpose is to provide access to the CurrentBar of all Bars objects in a multi-instrument or multi-time frame script.

In multi series processing, the CurrentBars starting value will be -1 until all series have processed the first bar.

Property Value

An array of int values.

Warning: This property should NOT be accessed within the OnStateChange() method before the State has reached State.DataLoaded.

Syntax

CurrentBars[int barSeriesIndex]

Examples

Indicator (BarsRequiredToPlot)

1protected override void OnStateChange()
2{
3 if (State == State.Configure)
4 {
5 // Adds a 5-minute Bars object to the script. It will automatically be assigned
6 // a Bars object index of 1 since the primary data the indicator is run against
7 // set by the UI takes the index of 0.
8 AddDataSeries("AAPL", BarsPeriodType.Minute, 5);
9 }
10}
11
12protected override void OnBarUpdate()
13{
14 // Evaluates to make sure we have at least 20 (default value of BarsRequiredToPlot)
15 // or more bars in both Bars objects before continuing.
16 if (CurrentBars[0] < BarsRequiredToPlot || CurrentBars[1] < BarsRequiredToPlot)
17 return;
18
19 // Indicator script logic calculation code...
20
21}

Strategy (BarsRequiredToTrade)

1protected override void OnStateChange()
2{
3 if (State == State.Configure)
4 {
5 // Adds a 5-minute Bars object to the script. It will automatically be assigned
6 // a Bars object index of 1 since the primary data the indicator is run against
7 // set by the UI takes the index of 0.
8 AddDataSeries("AAPL", BarsPeriodType.Minute, 5);
9 }
10}
11
12protected override void OnBarUpdate()
13{
14 // Evaluates to make sure we have at least 20 (default value of BarsRequiredToTrade)
15 // or more bars in both Bars objects before continuing.
16 if (CurrentBars[0] < BarsRequiredToTrade || CurrentBars[1] < BarsRequiredToTrade)
17 return;
18
19 // Strategy script logic calculation code...
20}