Announcement

Collapse
No announcement yet.

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.

    http://ninjatrader.com/support/helpG...highestbar.htm

    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:

        http://ninjatrader.com/support/forum...ead.php?t=7299

        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.

          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;
          }
          Attached Files
          Last edited by NinjaTrader_ChelseaB; 08-13-2020, 04:00 PM.
          Chelsea B.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by jerminedop, Today, 12:18 AM
          0 responses
          5 views
          0 likes
          Last Post jerminedop  
          Started by cnzaxtoil, Yesterday, 11:38 PM
          0 responses
          4 views
          0 likes
          Last Post cnzaxtoil  
          Started by cnzaxtoil, Yesterday, 11:37 PM
          0 responses
          3 views
          0 likes
          Last Post cnzaxtoil  
          Started by sukhob, Yesterday, 04:27 PM
          1 response
          14 views
          0 likes
          Last Post zacharydw00  
          Started by Bmnaiorwsd, Yesterday, 07:52 PM
          0 responses
          4 views
          0 likes
          Last Post Bmnaiorwsd  
          Working...
          X