Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Time elapsed per bar for Range bars

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

    Time elapsed per bar for Range bars

    I am trying to develop an indicator that will show how long it has taken each bar to fully draw. It seems pretty straight forward but the code below isn't working for me. The result on the 25 Range is one value (-0.1) for every bar. On a 50 Range the value is -.832. Where am I going wrong?

    Code:
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public class RangePM : Indicator
        {
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Name = "RangePM";
                    Calculate = Calculate.OnBarClose;
                    IsSuspendedWhileInactive = true;
                    IsOverlay = false;
    
                    AddPlot(Brushes.Goldenrod, "RangePM");
                }
    
            }
    
            protected override void OnBarUpdate()
            {
                DateTime currentBarTime = Bars.GetTime(0);
                DateTime previousBarTime = Bars.GetTime(1);
                double currentBarDuration = (currentBarTime - previousBarTime).TotalSeconds;
    
                Values[0][0] = currentBarDuration;
            }
        }
    }​

    #2
    You are using GetTime() incorrectly.

    An 'absolute bar' index is not the same as a 'bars ago' index.

    It's a matter of the '0th' index starting at the far left of the
    chart vs the far right of the chart.

    GetTime(0) is the very first bar of the chart, on the far left.
    GetTime(1) is the second bar of the chart.

    Your code is wrong.

    You're wanting the most recently closed bar and the prior bar,
    these bars are on the far right of the chart.

    Try this,

    double currentBarDuration = (Time[0] - Time[1]).TotalSeconds;

    Make sense?

    PS:
    Many times you'll find 'bars ago' indexes used with square brackets,
    because these are usually indexes for a data series.

    But 'absolute bar' indexes tend to be with parentheses, because these
    are passed as arguments into functions.

    Knowing the difference between the two is absolutely critical.

    Last edited by bltdavid; 10-04-2023, 12:40 PM.

    Comment


      #3
      And you'll need to add the guard code,

      Code:
      if (CurrentBar < 1)
          return;
      to the the top of OnBarUpdate, to protect
      against accessing Time[1] when this value
      doesn't exist.

      Huh?
      On the very first bar of the chart, when that
      very first bar closes, there is no previous bar yet.
      And so, on the very first bar, accessing Time[1]
      generates an error -- because, duh, there is no
      previous bar on the very first bar, ie, there is no
      previous slot at the '1' index on the very first bar.

      Comment


        #4
        That's perfect! Just what I was looking for. I know just enough to get myself into trouble. Thanks for taking the time to teach me. Have a great day!

        Comment


          #5
          Is there anyway to get this in 00:00 format? and display overlay in the lower left corner of the data box?

          Comment


            #6
            Hello Fbraun378​,

            DateTime.Subtract will return a timespan if you are not wanting a number of seconds.
            Returns the value that results from subtracting the specified time or duration from the value of this instance.


            This timespan can use the datetime format strings.
            Learn to use custom date and time format strings to convert DateTime or DateTimeOffset values into text representations, or to parse strings for dates & times.


            Print(Time[0].Subtract(Time[1]).ToString("H:mm:ss"));
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            636 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            366 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            107 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            568 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            571 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X