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

How to get bar number of MAX value

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

    How to get bar number of MAX value

    Hello
    I have MAX value and i need get its bar number.
    I write function which search this bar on my period but it gives me error:
    Error on calling 'OnCalculateMinMax' method on bar 32845: The calculation results in unrenderable values.
    32845 = CurrentBar if Calculate.OnBarClose and this bar do not included in my period very strange error.
    Last edited by nordseven; 02-24-2017, 02:19 AM.

    #2
    Hello nordseven,

    Thank you for writing into our Support Forums!

    You will not need to create your own function to find the bar number of the highest value, a method called HighestBar() is already provided.

    Please reference the help documentation below on HighestBar() for its complete usage.



    For convenience I will reference the sample code within:

    Code:
    protected override void OnBarUpdate()
    {   
      // store the highest bars ago value
      int highestBarsAgo = HighestBar(High, Bars.BarsSinceNewTradingDay);
      
      //evaluate high price from highest bars ago value
      double highestPrice = High[highestBarsAgo];         
      
      //Printed result:  Highest price of the session: 2095.5 - occurred 24 bars ago
      Print(string.Format("Highest price of the session: {0} - occurred {1} bars ago", highestPrice, highestBarsAgo));           
    }
    If you have any further questions, please don't hesitate to ask.
    JimNinjaTrader Customer Service

    Comment


      #3
      I'm read about HighestBar and LowestBar and found what they don't help me. I can't use indexing like with MIN, MAX functions. MAX(High,period)[barsAgo]. I count not from 0 bar, i count from barsAgo. LowesBar(Low, period)[not support index?]

      Comment


        #4
        Hello nordseven,

        Yes, you are correct. HighestBar() and LowestBar() cannot be indexed like MIN() and MAX() can. In your case you may wish to input a custom Data Series into HighestBar() or LowestBar() to look back a certain period. There is a reference sample that outlines this in an indicator. I will link it below:



        Please let me know if you have any further questions.
        JimNinjaTrader Customer Service

        Comment


          #5
          Hello Community,

          I wasn't able to find a community provided example and this has stumped quite a few developers, so I am providing sample code below, and an example script attached.
          HighestBarInRangeExample_NT8.zip

          This is an alternative method for calculating the highest bar in a range.

          HighestBar() can be used to calculate the highest bar in the most recent period.
          https://ninjatrader.com/support/help...highestbar.htm

          Code:
          protected override void OnBarUpdate()
          {
          if (State == State.Historical && CurrentBar == Count - 2)
          {
          int highestBarAgo = HighestBarAgoInRange(High, 10, 0);
          Print(Time[highestBarAgo]);
          }
          }
          
          // must be called from a data driven method or used with TriggerCustomEvent
          private int HighestBarAgoInRange(ISeries<double> inputSeries, int period, int barsAgo)
          {
          int startBarsAgo = ((period + barsAgo - 1) < CurrentBar) ? (period + barsAgo - 1) : CurrentBar;
          int returnVal = startBarsAgo;
          
          //for (int index = CurrentBar - barsAgo; index < CurrentBar - period - barsAgo; ++index)
          for (int index = startBarsAgo - 1; index >= barsAgo; --index)
          if (inputSeries[index] > inputSeries[returnVal])
          returnVal = index;
          
          return returnVal;
          }
          Attached Files
          Last edited by NinjaTrader_ChelseaB; 05-31-2021, 08:55 AM.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_ChelseaB View Post
            Hello Community,

            I wasn't able to find a community provided example and this has stumped quite a few developers, so I am providing sample code below, and an example script attached.

            This is an alternative method for calculating the highest bar in a range.

            HighestBar() can be used to calculate the highest bar in the most recent period.


            Code:
            protected override void OnBarUpdate()
            {
            if (State == State.Historical && CurrentBar == Count - 2)
            {
            int highestBarAgo = HighestBarAgoInRange(High, 10, 0);
            Print(Time[highestBarAgo]);
            }
            }
            
            // must be called from a data driven method or used with TriggerCustomEvent
            private int HighestBarAgoInRange(ISeries<double> inputSeries, int period, int barsAgo)
            {
            int startBarsAgo = ((period + barsAgo - 1) < CurrentBar) ? (period + barsAgo - 1) : CurrentBar;
            int returnVal = startBarsAgo;
            double currentHigh = inputSeries[startBarsAgo];
            
            //for (int index = CurrentBar - barsAgo; index < CurrentBar - period - barsAgo; ++index)
            for (int index = startBarsAgo - 1; index >= barsAgo; --index)
            if (inputSeries[index] > inputSeries[returnVal])
            returnVal = index;
            
            return returnVal;
            }
            How would you use this realtime to draw lines at range highs as they form?

            Comment


              #7
              Hello ChrisR,

              The code shown in the sample is executing once at the end of historical processing, you would need to remove the historical condition if you wanted to use that method in realtime.
              JesseNinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_ChelseaB View Post
                Hello Community,

                I wasn't able to find a community provided example and this has stumped quite a few developers, so I am providing sample code below, and an example script attached.

                This is an alternative method for calculating the highest bar in a range.

                HighestBar() can be used to calculate the highest bar in the most recent period.
                https://ninjatrader.com/support/help...highestbar.htm

                Code:
                protected override void OnBarUpdate()
                {
                if (State == State.Historical && CurrentBar == Count - 2)
                {
                int highestBarAgo = HighestBarAgoInRange(High, 10, 0);
                Print(Time[highestBarAgo]);
                }
                }
                
                // must be called from a data driven method or used with TriggerCustomEvent
                private int HighestBarAgoInRange(ISeries<double> inputSeries, int period, int barsAgo)
                {
                int startBarsAgo = ((period + barsAgo - 1) < CurrentBar) ? (period + barsAgo - 1) : CurrentBar;
                int returnVal = startBarsAgo;
                
                //for (int index = CurrentBar - barsAgo; index < CurrentBar - period - barsAgo; ++index)
                for (int index = startBarsAgo - 1; index >= barsAgo; --index)
                if (inputSeries[index] > inputSeries[returnVal])
                returnVal = index;
                
                return returnVal;
                }
                This is very interesting Chelsea I have tried something very similar to this but instead of looking for the Highest " High " which is a default ISeries<double> value I am looking for the greatest value returned for a custom method that is being calculated correctly about the "protected override void OnBarUpdate()" section...

                The method returns a fractal type value from a range of about n = 30 to 50 bars (restrictions prevent over optimizing....)

                So if I understand correctly I am expected to work with Method values by calling the method but how do I restrict this to n=30 for example??? The loop shown above works mainly with ISeries<double>


                Thank you so much for the guidance ChelseaB it is appreciated.

                Comment


                  #9
                  Hello DynamicTest,

                  To restrict your custom method to a certain lookback you would need to make a loop similar to what was shown here. The loop is not NinjaScript specific, that is a standard C# for loop which you could implement in your own method to loop over data. The code in this code is using values from a series, if your data was in a different format you would need to adapt the code in your loop to get data from how you stored it.

                  JesseNinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Mestor, 03-10-2023, 01:50 AM
                  16 responses
                  388 views
                  0 likes
                  Last Post z.franck  
                  Started by rtwave, 04-12-2024, 09:30 AM
                  4 responses
                  31 views
                  0 likes
                  Last Post rtwave
                  by rtwave
                   
                  Started by yertle, Yesterday, 08:38 AM
                  7 responses
                  29 views
                  0 likes
                  Last Post yertle
                  by yertle
                   
                  Started by bmartz, 03-12-2024, 06:12 AM
                  2 responses
                  22 views
                  0 likes
                  Last Post bmartz
                  by bmartz
                   
                  Started by funk10101, Today, 12:02 AM
                  0 responses
                  7 views
                  0 likes
                  Last Post funk10101  
                  Working...
                  X