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

Getting 'outside bounds' error when attempting to access Value[i]

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

    Getting 'outside bounds' error when attempting to access Value[i]

    Code:
    protected override void OnBarUpdate()
            {
                switch(CurrentBar)
                {
                    case 0:
                        //b1 is incomplete.
                        return;
                        break;
                    case 1:
                        Value[1] = Low[1];
                        Value[0] = High[0];
                        Print(Value[1]);
                        Print(Value[0]);
                        break;
                    default:
                        break;
                }
    
            }​
    On running the above, I get the following error in the Output window;

    Indicator 'CompositeTrendBar': Error on calling 'OnBarUpdate' method on bar 1: Index was outside the bounds of the array.

    I know it's something basic. Please point out what I'm missing.

    Thanks

    #2
    Hello trader_rick,

    Thank you for writing in.

    We have a tip page in the help guide about this type of error message. It goes over how to make sure you have enough bars in the data series you are accessing:


    I suspect this may be related to your use of Value[1] and Low[1] because when the indicator processes on the first bar of the chart, 1 bar ago did not exist yet. You may need to add a CurrentBar check to the beginning of OnBarUpdate() such as the following:
    Code:
    protected override void OnBarUpdate()
    {
    if (CurrentBar < 2)
    return;
    }​​
    Please let me know if I may be of further assistance.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      Emily,

      Thank you for your speedy response.

      From the documentation, my understanding is that the first bar of the chart has index 0.
      Therefore, in the code above, we are currently processing the 2nd bar of the chart. Hence, the value of the previous bar (bar 0, i.e. the first bar of the chart) should exist.

      But that is moot. Even if I omit the reference to
      Code:
      Value[1]
      ,
      Code:
      Value[0]
      still fails with the same error.

      Comment


        #4
        Hello trader_rick,

        Thank you for your reply.

        In order to narrow down the line of code causing the error, you could add print statements every line (or every couple of lines) to see which print is the last to appear in the output before the error is thrown. This process is described in the forum post about debugging your code here:


        Another option is to use the Visual Studio debugger in order to identify which line is causing the error. There is a guide to setting up Visual Studio debugging here:


        Once you have attached Visual Studio to the NinjaTrader process, you should run your indicator and reproduce the error. Visual Studio should break at the line that is causing the error as long as it is set to break when an exception is thrown as detailed in the following publicly available link:
        Learn how to specify which exceptions the debugger breaks on, at which point you want the debugger to break, and how breaks are handled.


        Once you are able to identify which line of code is throwing the error, that can help to determine what needs to be adjusted in your logic in order to prevent the error.

        Please let me know if I may be of further assistance.
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          Emily,

          I don't need the debugger. I know where the error is coming from. It's from one line involving NinjaScript. I offer this simpler version;

          Code:
              protected override void OnBarUpdate()
                  {
                      switch(CurrentBar)
                      {
                          case 0:
                              Print(High[0]);
                              return;
                              break;
                          case 1:
                              Value[0] = High[0];
                              Print(Value[0]);
                              break;
                          default:
                              break;
                      }
          
                  }​
          If I replace Value[0] in the Print statement with High[0], the code runs just fine.

          Where am I going wrong?
          Last edited by trader_rick; 07-31-2023, 02:05 PM.

          Comment


            #6
            Hello trader_rick,

            Thank you for your reply.

            Value[0] is generated by adding a plot to your script. Do you call AddPlot() in OnStateChange when State is State.SetDefaults() or State.Configure()? I have created a sample that adds a plot, then sets Value[0] to Close[0] and prints Value[0] without issues. Please see the attached script for your reference.

            For more details on AddPlot() and Value, please see the help guide pages below:Please let me know if I may be of further assistance.
            Attached Files
            Emily C.NinjaTrader Customer Service

            Comment


              #7
              Ah, that was the problem. I hadn't added a plot. I just want to store the data. So for that I presume I use ISeries<T>?

              Thanks Emily

              Comment


                #8
                Originally posted by trader_rick View Post
                Ah, that was the problem. I hadn't added a plot. I just want to store the data. So for that I presume I use ISeries<T>?

                Thanks Emily
                You're welcome! That is correct, you could use ISeries<T> to store data. We also have a reference sample that demonstrates using a series or dataseries object to store calculations. Please see the following links for more information:Please feel free to reach out with any additional questions or concerns.
                Emily C.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by rhyminkevin, Today, 04:58 PM
                3 responses
                48 views
                0 likes
                Last Post Anfedport  
                Started by iceman2018, Today, 05:07 PM
                0 responses
                5 views
                0 likes
                Last Post iceman2018  
                Started by lightsun47, Today, 03:51 PM
                0 responses
                7 views
                0 likes
                Last Post lightsun47  
                Started by 00nevest, Today, 02:27 PM
                1 response
                14 views
                0 likes
                Last Post 00nevest  
                Started by futtrader, 04-21-2024, 01:50 AM
                4 responses
                50 views
                0 likes
                Last Post futtrader  
                Working...
                X