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

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 kempotrader, Today, 08:56 AM
          0 responses
          6 views
          0 likes
          Last Post kempotrader  
          Started by kempotrader, Today, 08:54 AM
          0 responses
          4 views
          0 likes
          Last Post kempotrader  
          Started by mmenigma, Today, 08:54 AM
          0 responses
          2 views
          0 likes
          Last Post mmenigma  
          Started by halgo_boulder, Today, 08:44 AM
          0 responses
          1 view
          0 likes
          Last Post halgo_boulder  
          Started by drewski1980, Today, 08:24 AM
          0 responses
          4 views
          0 likes
          Last Post drewski1980  
          Working...
          X