NinjaScript > Language Reference > Data >

BoolSeries Class

Print this Topic Previous pageReturn to chapter overviewNext page

Definition
A BoolSeries is a special type of data structure that holds a series of bool values and always contains the same number of elements as bars in a chart. See the DataSeries Class for related information.

 

 

Creating BoolSeries Objects

To create a BoolSeries object:

 

1.Define a variable ("myBoolSeries" used in this example) of type BoolSeries that will hold a BoolSeries object
2.In the Initialize() method, create a new BoolSeries object and assign it to the "myBoolSeries" variable

 

#region Variables
private BoolSeries myBoolSeries; // Define a BoolSeries variable
#endregion

 

// Create a BoolSeries object and assign it to the variable
protected override void Initialize()
{
    myBoolSeries = new BoolSeries(this); // "this" refers to the indicator, or strategy
                                        // itself. This syncs the BoolSeries object
                                        // to historical data bars
}

 

* BoolSeries objects can be used on supplementary series in a multi-time frame and instrument strategy. Please see our support forum NinjaScript reference samples section for further information.

 

Note: By default NinjaTrader limits the number of values stored for BoolSeries objects to 256. This drastically improves memory performance by not holding onto old values that are generally not needed. Should you need more values than the last 256 please be sure to create the BoolSeries object so that it stores all values instead through the use of the MaximumBarsLookBack property.

 

#region Variables
private BoolSeries myBoolSeries; // Define a BoolSeries variable
#endregion

 

// Create a BoolSeries object and assign it to the variable
protected override void Initialize()
{

    // MaximumBarsLookBack determines how many values the BoolSeries will have access to
    myBoolSeries = new BoolSeries(this, MaximumBarsLookBack.Infinite);
}

 

Setting Values - BoolSeries.Set() & BoolSeries.Reset()
You can set value (plug in a value) into a BoolSeries object by calling the Set() method.

 

BoolSeries.Set(bool value)

Setting a value on a BoolSeries object is automatically aligned to the current bar being evaluated. This ensures values across all BoolSeries objects are always in sync by the CurrentBar index. The following code samples demonstrates analyzing the close vs. open price and then storing the result in a BoolSeries object.

 

protected override void OnBarUpdate()
{
    // Is the close > than the open
    myBoolSeries.Set(Close[0] > Open[0] ? true : false);
}

 

BoolSeries.Set(int barsAgo, bool value)
You can also set the value for historical bars by including a "barsAgo" value that represents the number of bars ago that you want the bool value to be stored at.

 

Calling the Reset() method is unique and can be very powerful for custom indicator development. BoolSeries objects can hold null values which simply means that you do not want to store a value for the current bar. Reset() will reset the current index value to null.

 

 

Checking for Valid Values
It is possible that you may use a BoolSeries object but decide not to set a value for a specific bar. However, you should not try to access a BoolSeries value that has not been set. Internally, a dummy value does exists, but you want to check to see if it was a valid value that you set before trying to access it for use in your calculations.

 

BoolSeries.ContainsValue(int barsAgo)
Returns a true or false value.
 
 

Getting Values
You can access BoolSeries object values using the syntax BoolSeries[int barsAgo] where barsAgo represents the data value n number of bars ago.

 

protected override void OnBarUpdate()
{
    // Prints the current and last bar value
    Print("The values are " + myBoolSeries[0] + " " + myBoolSeries[1]);
}