Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Time[0] returns the datetime of the oldest bar in the playback chart

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

    Time[0] returns the datetime of the oldest bar in the playback chart

    I'm running a simple strategy in playback mode that includes the line
    Code:
    Print(Time[0].ToString());
    but it prints the datetime value of the oldest bar in the chart instead of the current bar's datetime.

    How do I obtain the current bar's datetime in playback mode?

    #2
    Hello Red_70,

    Thanks for your post.

    Time[0] would return the time of the current bar on a chart when using Playback with Market Replay data.

    See this demonstration video: https://brandonh-ninjatrader.tinytak...N18yMzAzOTMyOQ

    Time: https://ninjatrader.com/support/help...eries_time.htm
    Playback: https://ninjatrader.com/support/help...connection.htm
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Hi Brandon,

      My OnBarUpdate block consists of only two lines:

      Code:
      Print("Time[0] = " + Time[0]);
      Print("Time[1] = " + Time[1]);


      This should print the datetime values of the current bar and the previous bar (2024-03-28 10:23:00 and 2024-03-28 10:22:00) but instead it prints 3/23/2024 8:05:00 AM for the first value and then throws an error for the second value.

      Here's a screenshot of what I'm experiencing:

      Click image for larger version

Name:	NT screenshot.png
Views:	116
Size:	388.0 KB
ID:	1297833


      Am I missing something obvious here?
      Attached Files

      Comment


        #4
        Originally posted by Red_70 View Post
        Am I missing something obvious here?
        Yes.

        On the very first bar, the previous bar does not exist.

        The attempt to access Time[1] on the very first bar
        fails, because anything with BarsAgo index [1] doesn't
        exist yet when there is only 1 bar.

        Just like anything using BarsAgo index [100] fails
        until you have 101 bars.

        Make sense?

        -=o=-

        "Throwing an error for the second" is expected behavior,
        because your code is wrong.

        That is, your code has a bug.

        ​-=o=-

        How to avoid this error?

        Easy.

        Use some guard code, something like this,

        Code:
        if (CurrentBar < 1)
            return;
        
        Print("Time[0] = " + Time[0]);
        Print("Time[1] = " + Time[1]);​
        This kind of guard code is very common at the top of
        OnBarUpdate.

        Good reading here.


        Comment


          #5
          Hello Red_70,

          Thanks for your notes.

          bltdavid is correct and has provided some excellent information on this topic.

          This error message indicates that you are trying to access a BarsAgo value that is not valid.

          A more simple example using one series would be on bar 5 you check for 6 BarsAgo. There are not yet 6 bars so the CurrentBar minus 6 would be a negative number or a non-existent bar.

          A CurrentBar check could be used in your indicator's logic to ensure that a certain number of bars have processed before the indicator begins calculation.

          See the help guide documentation below for more information.

          CurrentBar - https://ninjatrader.com/support/help...currentbar.htm
          ​Make sure you have enough bars - https://ninjatrader.com/support/help...nough_bars.htm
          <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

          Comment


            #6
            Hi bitdavid and Brandon,

            Thanks for your responses. My misunderstanding here was that OnBarUpdate() would apply only to the current bar, and not to every bar on the chart. Now all is clear!

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by NullPointStrategies, Today, 05:17 AM
            0 responses
            53 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            130 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            70 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            44 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            49 views
            0 likes
            Last Post TheRealMorford  
            Working...
            X