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

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 cre8able, Yesterday, 01:16 PM
            3 responses
            11 views
            0 likes
            Last Post cre8able  
            Started by ChartTourist, Today, 08:22 AM
            0 responses
            6 views
            0 likes
            Last Post ChartTourist  
            Started by LiamTwine, Today, 08:10 AM
            0 responses
            2 views
            0 likes
            Last Post LiamTwine  
            Started by Balage0922, Today, 07:38 AM
            0 responses
            5 views
            0 likes
            Last Post Balage0922  
            Started by JoMoon2024, Today, 06:56 AM
            0 responses
            6 views
            0 likes
            Last Post JoMoon2024  
            Working...
            X