Series<T>
<< Click to Display Table of Contents >> Series<T> |
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. The Series<T> class implements the ISeries<T> interface.
Note: 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. |
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 |
ninjaScriptBase |
The NinjaScript object used to create the Series |
bars |
The Bars object used to create the Series |
maximumBarsLookBack |
A MaximumBarsLookBack value used for memory performance |
Returns the underlying input value at a specified bar index value. |
|
Determines if the specified input is set at a barsAgo value relative to the current bar. |
|
Resets the internal marker which is used for IsValidDataPoint() back to false. |
|
The total number of bars or data points. |
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.
Note: 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
private Series<double> myDoubleSeries; // Define a Series<T> variable. In this instance we want it |
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.
Setting Series<T> values |
---|
protected override void OnBarUpdate() |
Note: 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. |
Warning: Calling IsValidDataPoint() will only work a MaximumBarsLookBackInfinite series. Attempting to check IsValidDataPoint() MaximumBarsLookBack256 series throw an error. Please check the Log tab of the Control Center |
Accessing Series object values |
---|
protected override void OnBarUpdate() |
Alternatively, you can access a value at an absolute bar index using the GetValueAt() method.
Note: In most cases, you will access the historical price series using a core data event handler such as OnBarUpdate(). For more advance 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(). |
Using a custom Series object as indicator input |
---|
protected override void OnBarUpdate() |