Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

IndexOutOfBounds exception on Value[0] at bar 15

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

    IndexOutOfBounds exception on Value[0] at bar 15

    Hi
    I get an IndexOutOfBounds Exception on the Value[0] = direction statement in the code below. CurrentBar = 15.
    Why is there an issues in assigning a value to the Values series at this point ?

    Code:
    protected override void OnBarUpdate()
    {
    
    if(CurrentBar<15)
        return;
    
    int direction = 0; // 1 is green/up, -1 is red/down, 0 is neutral
    
    ma1 = GetMovingAverageValue(Close, period1);
    ma2 = GetMovingAverageValue(Open, period2);
    ma3 = GetMovingAverageValue(Close, period3);
    
    double max = Math.Max(Math.Max(ma1, ma2), ma3);
    double min = Math.Min(Math.Min(ma1, ma2), ma3);
    
    double dif = max - min;
    
    double filter = ATR(14)[0] * (40/100);
    
    if(ma1 > ma2 && dif > filter)
    {
        direction = 1;
        BarBrush = Brushes.Green;
    }
    else if(ma2 > ma1 && dif > filter)
    {
        direction = -1;
        BarBrush = Brushes.Red;
    }
    else
    {
        direction = 0;
        BarBrush = Brushes.Gray;
    }
    Value[0] = direction;
    }

    #2
    Hello SunSorgenfrei,

    Thanks for your post.

    This error message means an invalid index was used somewhere in your script.

    Indexing errors can also occur when using an invalid index with arrays or collections.

    Below are public links to 3rd party educational sites on arrays and index out of range errors.
    https://www.dotnetperls.com/array

    Debugging prints would need to be added to the script that prints out all the values/indexes used in your script to determine exactly what index is being accessed.

    Once you know the index your script is trying to access, you could add a CurrentBars condition to your script to make sure that a certain number of bars is processed.

    Below is a link to a forum post that demonstrates how to use prints to understand behavior.

    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 me know if I 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
      Hi again
      I know what an Index out of Bounce means.
      I have been debugging this in Visual Studio and the Indicator fails on the
      Code:
      Value[0] = direction;
      statement
      If I comment that statement out everything works as expected, so my question is again why does the assignment Value[0] =direction fail with a Index out of Bounds exception.

      Comment


        #4
        Hello SuneSorgenfrei,

        Thanks for your note.

        This could occur if a plot has not been added to the script using the AddPlot() method.

        AddPlot(): https://ninjatrader.com/support/help...t8/addplot.htm

        Please confirm that you have added a plot to the script by calling the AddPlot() method in OnStateChange() when the State == State.SetDefaults.

        If you have added a plot to the script using the AddPlot() method and still get the same error, please send me a copy of your script so that I may investigate this matter further.

        Let me know if I may further assist.
        <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
          Hi Brandon
          Thanks for your help, that did fix the issue and also a issue I had in using the Indicator in a Strategy.

          Best Regards,
          Sune

          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