[Edit: Unless each assignment is happening in a separate invocation of OnBarUpdate, because then the most recent bar accessed by MyDataSeries[0] is actually a new bar, because OnBarUpdate is called when that new bar closes, so the indexer '[0]' is automatically updated under the hood (for free) to always reference the most recently closed bar.]
[Edit-2: Spoiler alert: The above is a convoluted way of saying "slot". Keep reading, "slot" will be introduced soon, and then come back here later and read again after you've achieved a good feel for "slot".]
But, also, because you get some additional memory management for free. I mean, each and every DataSeries (or any instance of an 'xxxSeries' class) has exactly one value maintained for each bar on your chart. These values are maintained in parallel, such that literally any xxxSeries object accessed with suffix [0] always means the value associated with the current bar, any xxxSeries object accessed with suffix [1] always means the value 1 bar ago, and so on.
[Edit: When NinjaTrader wrote their xxxSeries classes, they effectively have to shift all values in the "array" one position to the left, to make the indexer '[0]' free for the new value from the new bar. That's what I mean by NT's DataSeries memory management: NT makes room for the new value at '[0]' for all xxxSeries objects attached to the chart just before calling OnBarUpdate -- it does this automatically for us.]
[Edit-2: I'm almost positive that no shifting of data actually takes place. I'll cut off my left nut if I'm wrong about this: NT engineers used "indexed properties" for the '[0]' indexer, there is no "shifting" to free up indexer '[0]'. See my additional answers below.]
But the real beauty is a bit more complex, and under the hood. The engs at NinjaTrader made each and every indicator a DataSeries. Think about an EMA, or Stochastics. They all have values that are calculated and stay associated with each bar, right?
Consider that EMA(10)[0] is accessing the value of the EMA(10) for the current bar, and that EMA(10)[10] is accessing the value of EMA(10) exactly 10 bars ago. The suffix of '[0]' and '[10]' are indexers, and this is DataSeries syntax applied to an indicator.
[Edit: Why age? Because it is a better metaphor. Open[3] is the bar 15 minutes ago on 5 minute bars, or 3 days ago for daily bars, but what if each bar is a 500-Tick bar, or a 1000-Volume bar, or a Renko bar? Time as a metaphor breaks down, but age continues to be completely correct in its meaning -- here, age literally means n bars ago.]
I mean, consider that NinjaTrader engs could have designed a single class structure to contain all data for each bar inside it, and then made a DataSeries of these objects, such that each instance of TradingDay stored in the DataSeries would represent a separate bar ... sorta like where your code is heading above.
But this design becomes cumbersome later. Why? Because some new values come along that are also associated with each bar -- and logically they should be stored in this class, right?
NT engs decided that, from the point of view of extensibility, multiple parallel arrays containing just one piece of data were better than a single array of a monolith structure that contained all the data. Why? Because it's easier to manage, and much easier to extend -- so they focused on making sure that Open[0] is treated specially to mean the open price of the most recently closed bar, and EMA(24)[0] means the value of EMA(24) of the most recently closed bar, and so on. The 'parallelness' accessed via the indexer '[0]' is maintained by NinjaTrader, for free.
Let's say you write a MillionDollar indicator that takes 3 arguments, x, y, z, so it would be called like MillionDollar(x, y, z) -- but because every indicator is also a DataSeries, we can also use an indexer to say MillionDollar(x, y, z)[n] to access the MillionDollar(x, y, z) value n bars ago.
Consider that the success of third party product called Bloodhound is predicated (partially) on this DataSeries data structure -- the multiple parallel arrays of simple values VS the single array of large class of values -- the engineers at NinjaTrader made some wise choices.
Comment