NinjaScript > Language Reference > Data >

IDataSeries

Print this Topic Previous pageReturn to chapter overviewNext page

Definition
IDataSeries is an interface that is implemented by all NinjaScript classes that manage historical data such as DataSeries, Indicators and other classes. The relevance of this boils down to one practical use which is providing you with the means to write a method that has flexibility in the types of price data arrays it can accept. By specifying a parameter of type IDataSeries, you can then pass in an array of closing prices, an indicator or a user defined data series.

 

The sample code below demonstrates a method named DoubleTheValue that accepts any object that implements the IDataSeries interface as a parameter. This method is then used twice, the first time passing in an array of closing prices and the second time passing in a 20 period simple moving average.

 

private double DoubleTheValue(IDataSeries priceData)
{
    return priceData[0] * 2;
}

 

protected override void OnBarUpdate()
{
    Print(DoubleTheValue(Close));
    Print(DoubleTheValue(SMA(20)));
}

 

 

Tips

1. When working with IDataSeries objects in your code you may come across situations where you are not sure if the value being accessed is a valid value or just a "placeholder" value. To check if you are using valid values for your logic calculations that have been explicitly set, please use .IsValidPlot(int barIdx) to check.

 

protected override void OnBarUpdate()
{

    // Only set our plot if the input is a valid value
    if (Input.IsValidPlot(CurrentBar))
         Plot0.Set(Input[0]);
}