Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

About Close[]

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

    About Close[]

    Hello,

    If i do Close.GetValueAt(CurrentBar-1), and there is a 1000 barindex in the chart, it will return bar 999. Right?

    But for Close[999] it will return an error message:

    Indicator 'NEWSDBA': Error on calling 'OnRender' method on bar 10037: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

    How can we get a specific barindex?

    TY

    #2
    Hello frankduc,

    Thanks for your post.

    BarsAgo referencing should not be done in OnRender(), such as using the CurrentBar index in OnRender(). This is because OnRender() should only deal with visible bars (looping between ChartBars.FromIndex/ChartBars.ToIndex). Absolute indexes should be used for Close.GetValueAt(), such as Close.GetValueAt(BarIndex). It is best to use BarsArray[0].Count-1 instead of CurrentBar because BarsInProgress could change if you are using a Multi-Timeframe script.

    When using a price series, indicator, or any Series<T> you will have the GetValueAt method which allows getting a value from a specific index. For example, we would first loop through all visible bars on the chart using ChartBars.FromIndex/ChartBars.ToIndex and then call Close.GetValueAt(123) to get the value of bar 123.

    See the help guide documentation below for more information.
    ChartBars.FromIndex: https://ninjatrader.com/support/help..._fromindex.htm
    ChartBars.ToIndex: https://ninjatrader.com/support/help...rs_toindex.htm
    GetValueAt: https://ninjatrader.com/support/help...getvalueat.htm

    Let us know if we may assist further.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Brandon,

      If i want to loop in OBU should i use:

      Code:
      for (int barIndex = TBPrice; barIndex <= ChartBars.ToIndex; barIndex++)
      {
      if (Bars.GetLow(barIndex) < lowPrice0)
      {
      lowPrice0 = Bars.GetLow(barIndex);
      index = barIndex;
      
      
      }
      }
      or
      Code:
      for (int barsAgo = TBPrice; barsAgo <= ChartBars.ToIndex; barsAgo++)
      {
      if ( Low[barsAgo]) < lowPrice0)
      {
      lowPrice0 = Low[barsAgo];
      index = barsAgo;
      
      
      }
      }
      Should i replace ChartBars.ToIndex; by CurrentBar?

      TY

      Comment


        #4
        Hello frankduc,

        Thanks for your note.

        To loop through all the viewable bars on a chart in OnBarUpdate(), you would do something similar to the following. The following code would loop through all the viewable bars on the chart and print out the High value of each bar.

        Code:
        protected override void OnBarUpdate()
        {
            // loop through all of the viewable range of the chart
            for (int barIndex = ChartBars.FromIndex; barIndex <= ChartBars.ToIndex; barIndex++)
            {
                // print the High value for each index within the viewable range
                Print(High.GetValueAt(barIndex));
            }
        }
        Let us know if we may assist further.
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment


          #5
          Brandon,

          But Bars.GetLow(barIndex) will work anyway or should i replace them all by Low.GetValueAt(barIndex)?

          I am getting this error in OBU: Indicator 'NEWSDBAobu': Error on calling 'OnBarUpdate' method on bar 0: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

          I dont get this error in OR.

          TY

          Comment


            #6
            Hello frankduc,

            Thank you for your note.

            This error message indicates that you are trying to access a bar that has not yet processed on the chart.

            A CurrentBar check should be used in your indicator's logic to ensure that a certain number of bars have processed before the indicator begins calculation. A CurrentBar check would look something like this.

            Code:
            if (CurrentBar < 10)
                return;
            This would check to make sure that 10 bars have processed before the indicator begins its calculations. If you have a Multi-Timeframe or Multi-Instrument script, you would use CurrentBars[].

            Also, debugging steps should be taken to determine how the script is behaving and where errors are occurring. Below is a link to a forum post that demonstrates using prints to understand behavior.

            Debugging: https://ninjatrader.com/support/foru...121#post791121

            See the help guide documentation below for more information.
            CurrentBar - https://ninjatrader.com/support/help...currentbar.htm
            CurrentBars - https://ninjatrader.com/support/help...urrentbars.htm
            Make sure you have enough bars - https://ninjatrader.com/support/help...nough_bars.htm

            Let us know if we may assist further.
            <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            576 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            334 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            101 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            553 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            551 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X