Series<t>

View as Markdown

Definition

A Series\<t\> is a special generic type of data structure that can be constructed with any chosen data type and holds a series of values equal to 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 Series<double> object with 200 historical values of data, one for each bar. Series<double> objects can be used as input data for all indicator methods.

By default NinjaTrader limits the number of values stored for Series\<t\> objects to 256 from the current bar being processed. 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 Series\<t\> object so that it stores all values instead through the use of the MaximumBarsLookBack property.

Constructors

ConstructorDescription
Series\<t\>(ninjaScriptBase)Creates a Series\<t\> object synchronized to the primary data series of the provided NinjaScript
Series\<t\>(ninjaScriptBase, maximumBarsLookBack)Creates a Series\<t\> object synchronized to the primary data series of the provided NinjaScript. This constructor also allows controlling the Series\<t\>’s MaximumBarsLookBack
Series\<t\>(bars)Creates a Series\<t\> object synchronized to the provided Bars object, for Multi Time Frame scripts, this could be given from BarsArray
Series\<t\>(bars, maximumBarsLookBack)Creates a Series\<t\> object synchronized to the provided Bars object, for Multi Time Frame scripts, this could be given from BarsArray. While this constructor allows controlling the Series\<t\>’s MaximumBarsLookBack, it is forced to MaximumBarsLookBack.Infinite

Parameters

ParameterDescription
ninjaScriptBaseThe NinjaScript object used to create the Series
barsThe Bars object used to create the Series
maximumBarsLookBackA MaximumBarsLookBack value used for memory performance

Methods and Properties

Method/PropertyDescription
GetValueAt()Returns the underlying input value at a specified bar index value.
IsValidDataPoint()Determines if the specified input is set at a barsAgo value relative to the current bar.
Reset()Resets the internal marker which is used for IsValidDataPoint() back to false.
CountThe total number of bars or data points.

Creating Series<t> Objects

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

Custom Series\<t\> objects will hold the number of values specified by the MaximumBarsLookBack property when the custom series object is instantiated.

To create a Series\<t\> object:

  1. Determine the data type of the Series\<t\> object you wish to create. This could be double, bool, int, string or any other object type you want.
  2. Define a variable of type Series\<t\> that will hold a Series\<t\> object. This example will create “myDoubleSeries” as a Series<double>.
  3. In the OnStateChange() method, in the State.DataLoaded create a new Series\<t\> object and assign it to the “myDoubleSeries” variable.
1private Series<double> myDoubleSeries; // Define a Series`<t>` variable. In this instance we want it
2 // as a double so we created a Series<double> variable.
3
4private Series<double> mySecondaryDoubleSeries; // Define a Series`<t>` variable. In this instance we want it
5 // as a double so we created a Series<double> variable.
6
7// Create a Series object and assign it to the variable
8protected override void OnStateChange()
9{
10 if (State == State.Configure)
11 {
12 // Add a secondary data series to sync our Secondary Series<double>
13 AddDataSeries(BarsPeriodType.Minute, 1);
14 }
15 else if (State == State.DataLoaded)
16 {
17 // "this" refers to the NinjaScript object itself. This syncs the Series object to historical data bars
18 // MaximumBarsLookBack determines how many values the Series<double> will have access to
19 myDoubleSeries = new Series<double>(this, MaximumBarsLookBack.Infinite);
20
21 // "BarsArray[1]" refers to the first data series added to the script with AddDataSeries.
22 mySecondaryDoubleSeries = new Series<double>(BarsArray[1]);
23 }
24}

Setting Values

You can set the value for the current bar being evaluated by choosing a “barsAgo” value of “0” or, for historical bars, by choosing a “barsAgo” value that represents the number of bars ago that you want the value to be stored at.

1protected override void OnBarUpdate()
2{
3 myDoubleSeries[0] = Close[0];
4}

The “barsAgo” value is only guaranteed to be in sync with the recent current bar during core data event methods, such as OnBarUpdate(), OnMarketUpdate(), and during strategy related order events such as OnOrderUpdate(), OnExecutionUpdate(), OnPositionUpdate(). For scenarios where you may need to set a value outside of a core data/order event, such as OnRender() or a custom event, you must first synchronize the “barsAgo” pointer via the TriggerCustomEvent() method.

Checking for Valid Values

It is possible that you may use a Series\<t\> object but decide not to set a value for a specific bar. However, you should not try to access a Series\<t\> value that has not been set. Internally, a dummy value does exist, 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. Please see IsValidDataPoint() for more information.

Calling IsValidDataPoint() will only work on a MaximumBarsLookBackInfinite series. Attempting to check IsValidDataPoint() on a MaximumBarsLookBack256 series will throw an error. Please check the Log tab of the Control Center.

Getting Values

You can access Series\<t\> object values using the syntax Series<t>[int barsAgo] where barsAgo represents the data value n (number of bars ago).

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

Alternatively, you can access a value at an absolute bar index using the GetValueAt() method.

In most cases, you will access the historical price series using a core data event handler such as OnBarUpdate(). For more advanced developers, you may find situations where you wish to access historical price series outside of the core data event methods, such as OnRender(), or your own custom event. In these advanced scenarios, you may run into situations where the “barsAgo” pointer is not in sync with the current bar, and may result in errors when trying to obtain this information. In those cases, please use the Bars.Get…() methods with the absolute bar index, e.g., GetValueAt().

Methods that Accept ISeries<t> as Arguments

All indicator methods accept ISeries\<double\> objects as arguments. Carrying from the prior examples, let’s print out the 10 period simple moving average of range.

1protected override void OnBarUpdate()
2{
3 // Calculate the range of the current bar and set the value
4 myDoubleSeries[0] = (High[0] - Low[0]);
5
6 // Print the current 10 period SMA of range
7 Print("Value is " + SMA(myDoubleSeries, 10)[0]);
8}