Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

HighestBar 1 period before this period

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

    HighestBar 1 period before this period

    Hi,
    I've a question that have been asked since yr2008.

    HighestBar(High, 20)[1]

    My desire: get the Highest Bar 1 period before this period from the last 20 periods.

    I knew that add [1] after the HighestBar is not workable. Any other substitute method can be done with the same result?

    If I just code HighestBar(High, 20) and if the current bar is the Highest, the result will be "0". But I want to get the highest bar exclude current bar.

    Thanks!!

    #2
    Hello Manlp,

    Thank you for your post and welcome to the NinjaTrader Support Forum!

    You would set the value on the current bar and then call it on the next bar be fore setting the value again. Alternatively you could also a DataSeries to store the value and then call the barsAgo int of 1 to call the previous value, which would exclude the current value.

    Below is an example of the first solution. Please let me know if you have any questions.
    Code:
            #region Variables
            private int highestBarOfLast20 = 0;
            #endregion
    
            protected override void Initialize()
            {
    			CalculateOnBarClose = true;
            }
    
            protected override void OnBarUpdate()
            {
    			if(CurrentBar <= 20)
    				return;
    			
    			// Print the last bar's value.
    			Print(highestBarOfLast20);
    			
    			// Run HighestBar for current bar.
    			highestBarOfLast20 = HighestBar(High, 20);
            }

    Comment


      #3
      Thanks Patrick!

      I almost get the result I want...but I don't know why the result is 1 bar less than actual figure.

      For example, the highest bar is "6" barago before current period but the print result show "5". I used your example code.

      Comment


        #4
        Hello Manlp,

        Thank you for your response.

        Good point, I did not notice that. That occurs as the value is from one bar ago, so we just need to add a one to the value:
        Code:
        Print(highestBarOfLast20+1);

        Comment


          #5
          Hi Patrick,
          Thanks for your quick responses and I can get the result I want!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          558 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          324 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          101 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          545 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          547 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X