Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Highest bar before current bar

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

    Highest bar before current bar

    Hello,

    I would like to know the High of the bars since the beginning of the session before the current bar. When I use the following code:

    Code:
    High[HighestBar(High, Bars.BarsSinceSession)]
    If the current bar is actually the highest since the beginning of the session, I get the current bar high.

    But I dont want this. I want to know the second highest bar. So the highest bar BEFORE the current bar.

    Any ideas?

    #2
    Hello siroki,

    Thank you for your post.

    You could set this to a variable and then call it on the next bar. For example:
    Code:
            #region Variables
    		private double highBeforeCurrentBar = 0;
            #endregion
    
            protected override void Initialize()
            {
    		CalculateOnBarClose = true;	
            }
            
            protected override void OnBarUpdate()
            {
    			// Add one to the value to truly calculate the value,
    			// keep in mind the first value will not be correct
    			// as the initial value is zero.
    			Print(highBeforeCurrentBar+1);
    			highBeforeCurrentBar = High[HighestBar(High, Bars.BarsSinceSession)];
            }

    Comment


      #3
      Great. Thanks for your help. Indeed I have fixed it by creating a variable and updating it once a new higher high has been formed. Awesome!

      Comment


        #4
        hi, I want to know the highest of the past 20 bars, at 'FirstTickOfBar' (with CalculateOnBarClose = false)
        skipping the current bar [0] (from bar [1] to bar [20])
        but I also want to know the index [i] of the bar that has this maximum.

        If I use MAX(High,20) [1] I get the value of the maximum, but it is NOT possible to know the index of this bar.

        If I use HighestBar (IDataSeries series, int period)
        I get the index, and the highest bar,
        but with High [HighestBar (Close, 20)]
        I can not avoid considering also the bar [0] I do not want!

        Is there a way to find maximum and index of the past 20 bars,
        without having to use a FOR loop?

        for (int i = 1; i<20; i++)
        {
        ...
        if (High[i]>High[i+1]) HighMax=High[i];
        ...
        ...
        }

        Comment


          #5
          Hello,

          Thank you for the question.

          Based on your needs of not including the current bar, there would not be a built in method that excludes specific data points. HighestBar would be the best choice if you did not require excluding the current bar.

          For this I believe the best route would be to create your own custom method that only includes the data you need. You would need to use a for loop for this to "loop" through the data as you have provided an example of.

          I look forward to being of further assistance.

          Comment

          Latest Posts

          Collapse

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