NinjaScript > Language Reference > Data >

DataSeries Class

Print this Topic Previous pageReturn to chapter overviewNext page

Definition
A DataSeries is a special type of data structure that holds a series of double values and always contains the same number of elements as bars in a chart. If you have 200 bars loaded in your chart with a moving average plotted, the moving average itself holds a DataSeries object with 200 historical values of data, one for each bar. DataSeries objects can be used as input data for all indicator methods. The DataSeries class implements the IDataSeries interface.

 

 

Creating DataSeries Objects

When creating custom indicators, DataSeries objects are automatically created for you by calling the Add() method and can be subsequently referenced by the Value and/or Values property. However, you may have a requirement to create a DataSeries object to store values that are part of an overall indicator value calculation. This can be done within a custom indicator or strategy.

 

To create a DataSeries object:

 

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

 

#region Variables
private DataSeries myDataSeries; // Define a DataSeries variable
#endregion

 

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

 

* DataSeries 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 DataSeries 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 DataSeries object so that it stores all values instead through the use of the MaximumBarsLookBack property.

 

#region Variables
private DataSeries myDataSeries; // Define a DataSeries variable
#endregion

 

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

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

 

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

 

DataSeries.Set(double value)

Setting a value on a DataSeries object is automatically aligned to the current bar being evaluated. This ensures values across all DataSeries objects are always in sync by the CurrentBar index. The following code samples demonstrates calculating the range of each bar and storing the value in a DataSeries object.

 

protected override void OnBarUpdate()
{
    // Calculate the range of the current bar and set the value
    myDataSeries.Set(High[0] - Low[0]);
}

 

DataSeries.Set(int barsAgo, double 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 double value to be stored at.

 

Calling the Reset() method is unique and can be very powerful for custom indicator development. DataSeries objects can hold dummy values (usually the Input series for the script which generally would be the Close price) which simply means that you do not want to store a value for the current bar. Reset() method allows you to effectively ignore the value of the current bar for plotting purposes. For calculation purposes you will want to use .ContainsValue() to ensure you are not calculating off of any dummy values assigned by the Reset() method.

 

 

Checking for Valid Values
It is possible that you may use a DataSeries object but decide not to set a value for a specific bar. However, you should not try to access a DataSeries 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.

 

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

Getting Values
You can access DataSeries object values using the syntax DataSeries[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 " + myDataSeries[0] + " " + myDataSeries[1]);
}

 

 

Methods that Accept DataSeries as Arguments
All indicator methods accept DataSeries objects as arguments. Carrying from the prior examples, let's print out the 10 period simple moving average of range.

 

protected override void OnBarUpdate()
{
    // Calculate the range of the current bar and set the value
    myDataSeries.Set(High - Low);

 

    // Print the current 10 period SMA of range
    Print("Value is " + SMA(myDataSeries, 10)[0]);
}