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

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.
          JesseNinjaTrader 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