NinjaScript > Educational Resources > Tips > Using [] brackets |
|
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].
Many of NinjaTrader's indicators store their values in plots. Plots are essentially DataSeries objects and to retrieve values from them you need to specify which value you want to access. In most cases, you'd like the current value, so you could use SMA(14)[0], not just SMA(14). SMA(14) is the DataSeries, and you can't access it directly. Using SMA(14)[0] retrieves the part of the DataSeries you're interested in--the most current value.
Most of the time, you need an index value (number in the square brackets), but there are also cases when you need to use the DataSeries instead. CrossAbove() and CrossBelow() are two key examples. If you look at the reference page for CrossAbove(), the two method signatures (overloads) look like this:
This means the first variable must always be a DataSeries object, and the second variable can be either another DataSeries or a double value (100, 70.25, etc). To specify a DataSeries object, you can just leave off the square brackets. For example if(CrossAbove(SMA(14), SMA(28), 1)) checks if the 14 period SMA has crossed above the 28 period SMA within the last bar. if(CrossAbove(SMA(14)[0], SMA(28)[0], 1)) would give you a compile error because it expects a DataSeries as input,not a double value (which is returned when an index is present).
|