In C#, square brackets represent a way to access values stored within an array. NinjaScript comes with quite a few arrays that we call DataSeries objects which could be accessed with square brackets. DataSeries objects are linked to the underlying bars series in that they hold the same number of values as the number of bars on a chart. For example, to get the close price one bar ago, you would use Close[1] since the value of 1 within the square brackets represents the number of bars ago whose value you wish to reference. As another example, to get the high three bars ago, you would use High[3].
double close1 = Close[1]; // gets the close price one bar ago double high3 = High[3]; // gets the high of three bars ago double low = Low; // results in compile error. Low is an array, and can't be accessed directly. It should be Low[n Bars ago].
double SMA_current = SMA(14)[0]; // gets the current value of the SMA double SMA_1 = SMA(14)[1]; // gets the SMA value one bar ago double SMA_value = SMA(14); // results in compile error. SMA(14) is a DataSeries and the variable SMA_value of type double can't hold a DataSeries.
CrossAbove(IDataSeries series1, double value, int lookBackPeriod) CrossAbove(IDataSeries series1, IDataSeries series2, int lookBackPeriod)
if (CrossAbove(SMA(14), SMA(28), 1)) // works fine if (CrossAbove(SMA(14), 1000, 1)) // works fine, this uses a double for the second argument. See the above overload. if (CrossAbove(SMA(14)[0], SMA(28)[0], 1)) // compile error: SMA(14)[0] is a double, not a DataSeries if (CrossAbove(SMA(14), SMA(28)[0], 1)) // would work fine with a DataSeries as first argument and a double as the second argument
Comment