Series<t>
Series<t>
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
Parameters
Methods and Properties
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:
- 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. - Define a variable of type Series
\<t\>that will hold a Series\<t\>object. This example will create “myDoubleSeries” as a Series<double>. - In the OnStateChange() method, in the State.DataLoaded create a new Series
\<t\>object and assign it to the “myDoubleSeries” variable.
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.
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).
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.

