Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Wrong Time from Times[] function or a complete misunderstanding.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Wrong Time from Times[] function or a complete misunderstanding.

    Debugging through my strategy and I noticed something about the "Times[]" function that doesn't make sense. If the current bar (last bar in the collection) is 0 and it counts backwards, shouldn't the date times be getting older instead of newer? Is there something additional I should be doing? Perhaps I misunderstood the paradigm, is the 0 bar index the 1st bar in the collection instead of the last? Maybe I'm doing this completely wrong, any assistance is appreciated.

    Times[1] is the hourly series added to the strategy.

    AddDataSeries(Data.BarsPeriodType.Minute, 60);

    UPDATE: This is bizarre, the following pic is from a strategy debugging session run on an hourly chart with the hourly series added. You'll notice that my above assumption is correct about the current bar. But, look at the times, they aren't even in the same year. Perhaps I should re-sync the Times function, but how? Thanks.

    Click image for larger version

Name:	Wrong Times.PNG
Views:	157
Size:	137.0 KB
ID:	1114947

    Attached Files
    Last edited by Herrwolf1; 08-21-2020, 11:02 AM.

    #2
    Hello Herrwolf1, thanks for your post.

    The GetValueAt method takes an absolute bar index, and the Times[][] array takes a barsAgo value, so the two are different from each other. To get the latest value with GetValueAt, you would pass in the CurrentBar property, while getting the most recent value from the Times[][] array, you would give it 0 as in 0 bars ago.

    Please let me know if I can assist any further.
    Chris L.NinjaTrader Customer Service

    Comment


      #3

      Originally posted by Herrwolf1 View Post
      Debugging through my strategy and I noticed something about the "Times[]" function that doesn't make sense.
      "Times[]" is not a function.

      Don't use those words, it is completely wrong to think of "Times[]" as a function.

      Time[] (no s) is a an object of type ISeries<DateTime>.
      Times[] (note the s) is an array of ISeries<DateTime> objects indexed by the BarsInProgress.

      Originally posted by Herrwolf1 View Post
      If the current bar (last bar in the collection) is 0 and it counts backwards, shouldn't the date times be getting older instead of newer?
      That is correct.

      The timestamp of each newly closed bar is always greater than or equal to the previous bar.

      Or, to say it in your words,
      If Time[0] is time T then Time[1] will always be older than (or equal to) time T.

      Originally posted by Herrwolf1 View Post
      Is there something additional I should be doing? Perhaps I misunderstood the paradigm, is the 0 bar index the 1st bar in the collection instead of the last? Maybe I'm doing this completely wrong, any assistance is appreciated.
      I think we found your confusion.

      There are two zero's in play -- it is absolutely critical that you be able to understand
      these differences I'm about to explain.

      The '0' in Time[0] is called the 'BarsAgo index'.

      It is not an array index. Ok, it's sorta like an array index, but the BarsAgo index always
      starts at the end of the array, not at the beginning like an array.

      Remember -- Time[0] is accessing a series, not an array.

      Even though series and arrays are indexed the same, the '[0]' index in a series does
      not mean the same thing as the '[0]' index in an array.

      But still, they are similar, so let's drill down on that.

      Let's consider an array data structure, with 6 elements, indexed like this,

      0 1 2 3 4 5

      The first element in the array is considered the oldest and it is on the far left.
      If you add a new element to the array, it is added on the right, and this new
      element becomes the new youngest element. Standard array stuff, right?

      Contrast that with a series with 6 elements, indexed like this,

      5 4 3 2 1 0

      The first element in the series is still the oldest, and it is still on the far left.
      When a new element is added to the series, it is also added on the right, and
      it too, becomes the youngest element.

      The difference is how they are accessed -- they have different index models.

      In the array, a[0] means the far left (aka oldest) very first element.
      In the series, s[0] means the far right (aka youngest) very last element.

      In an array, a[0] never changes, it always references the same value at the
      same position in the array.

      In the series, s[0] is a moving target, it adjusts automatically to always reference
      the most recently added element (aka, the most recently closed bar).

      See the difference?
      A series is like a backwards array.

      Ok, now consider the 'BarsAgo index' idea vs the 'Absolute index' idea.

      Here's where the implementation details matter a bit. A series is being stored
      as an array under the hood, and to access the array elements in that series you
      use the GetValueAt() method -- which takes an absolute bar index value.

      That is, let's say there are 100 bars on the chart, numbered from 0 to 99.
      Time[0] accesses the 99th bar (aka the last bar).
      Time.GetValueAt(0) accesses the 0th bar (aka the first bar).

      When the bar 101 closes, we have numbered bars 0 through 100,
      Time[0] accesses the 100th bar (aka, the new last bar).
      Time.GetValueAt(0) still accesses the first bar, nothing has changed here.

      Originally posted by Herrwolf1 View Post
      Times[1] is the hourly series added to the strategy.

      AddDataSeries(Data.BarsPeriodType.Minute, 60);

      UPDATE: This is bizarre, the following pic is from a strategy debugging session run on an hourly chart with the hourly series added. You'll notice that my above assumption is correct about the current bar. But, look at the times, they aren't even in the same year. Perhaps I should re-sync the Times function, but how? Thanks.
      The output in the attached picture is completely correct.
      Can you now see why?

      When you use GetValueAt() you are using a method to access the elements of
      the series as if it was an array so you pass the absolute bar index, and this
      allows you to walk the elements from oldest to youngest -- where index 0 is
      the oldest of all elements in the series.

      If you use the BarsAgo index, then index 0 is the youngest of all elements
      in the array.

      Note how '0' can mean different ends of the same data structure:
      Time[0] is using the BarsAgo index -- this is a series access.
      Time.GetValueAt(0) is using the Absolute Bar index -- this is an array access.

      Now let's consider your hourly series,

      Times[1][0] is using BarsAgo index 0 -- this is series order.
      Times[1].GetValueAt(0) is using the Absolute Bar index 0 -- this is array order.

      Times[1][29] is using BarsAgo index 29 -- the 29th most recently closed bar.
      Times[1].GetValueAt(29) is using the Absolute Bar index 29 - the 29th bar in the array.

      The idea of BarsAgo is critical -- the 29th 'bar ago' -- get it?

      BarsAgo index is the convenient series access -- from the right.
      Absolute index is like an array access -- from the left.

      Beware: BarsAgo index != Absolute index.

      Make sense?
      Last edited by NinjaTrader_ChrisL; 08-21-2020, 01:59 PM. Reason: Please keep responses courteous

      Comment


        #4
        Originally posted by bltdavid View Post
        "Times[]" is not a function.

        Don't use those words, it is completely wrong to think of "Times[]" as a function.

        Time[] (no s) is a an object of type ISeries<DateTime>.
        Times[] (note the s) is an array of ISeries<DateTime> objects indexed by the BarsInProgress.
        Yep, words matter.

        So, let me harp on myself a bit.

        Time[] is a series access.
        Times[] is an array access.
        Times[][] is a series access.

        So, "Times[] is an array" is the correct wording.

        Like I said, the series access using '[]' looks the same as array
        access using '[]' -- but they have very different index models.

        Comment


          #5
          Perfect. I certainly appreciate it. The documentation for "GetValueAt()" does indicate the absolute bar index and now it makes sense. Thank you!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by arvidvanstaey, Today, 02:19 PM
          4 responses
          11 views
          0 likes
          Last Post arvidvanstaey  
          Started by samish18, 04-17-2024, 08:57 AM
          16 responses
          60 views
          0 likes
          Last Post samish18  
          Started by jordanq2, Today, 03:10 PM
          2 responses
          9 views
          0 likes
          Last Post jordanq2  
          Started by traderqz, Today, 12:06 AM
          10 responses
          18 views
          0 likes
          Last Post traderqz  
          Started by algospoke, 04-17-2024, 06:40 PM
          5 responses
          48 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Working...
          X