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 mmenigma, 01-23-2024, 09:37 AM
          1 response
          85 views
          0 likes
          Last Post Nin8aTrender  
          Started by wbayne333, 02-22-2021, 01:18 PM
          6 responses
          408 views
          0 likes
          Last Post Nin8aTrender  
          Started by gtheaded, 07-03-2020, 03:47 PM
          3 responses
          367 views
          0 likes
          Last Post Nin8aTrender  
          Started by theminster, Today, 03:12 PM
          0 responses
          10 views
          0 likes
          Last Post theminster  
          Started by AyeSir, Today, 03:01 PM
          0 responses
          11 views
          0 likes
          Last Post AyeSir
          by AyeSir
           
          Working...
          X