DataSeries Class
Previous Topic  Next Topic 

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.


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 null values which simply means that you do not want to store a value for the current bar. Mathematically, you can correctly assign a value of zero however if the DataSeries was the primary DataSeries of an indicator whose values would be used for plotting, you may NOT want a zero value plotted. Meaning, you want a zero value for proper calculations but not a zero value for chart visualization. The Reset() method allows you to reset the current bar's DataSeries value to a zero for calculation purposes but NinjaScript would ignore this value when it plotted it on a chart.



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. Internally, a dummy value does exist which is the current bar close however, if you wanted to check to see if it was a valid value that you set, you can check the following method.


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

    if (CurrentBar > 0)
        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[0] - Low[0]);

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