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

Error... index with a value that is invalid since it is out of range.

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

    Error... index with a value that is invalid since it is out of range.

    Hello.

    I am confused as to why my code is giving me this error. I have a working indicator that marks doji with a white dot on bar close. I am now trying to draw an extended line between two marked doji. I am using a list to collect the x-axis value via datetime and a list to collect the y-axis value as a double via the close price of the current bar (if, and only if, it's a doji). Everything works perfectly without error EXCEPT for the Draw.ExtendedLine portion.
    • When this is active and I try to run it I get an error saying "Error on calling "OnBarUpdate" method on bar 27: You are accessing an index with a value that is invalid since it is out of range."
    • I have a line at the beginning of OnBarUpdate that says " if (CurrentBars[0] < 20) { return; }" and if I increase this number it increases the number of the bar that throws the error.
    How can I solve this issue? It seems the forum is full of this exact question but I couldn't find a clear answer.

    Here is the code for this portion:

    if (dBull == dBear)
    {
    DOJINUETRAL[0] = Convert.ToDouble(Close[0]);
    List<DateTime> xValue = new List<DateTime>();
    xValue.Add(Time[0]);
    List<double> yValue = new List<double>();
    yValue.Add(Close[0]);
    //Draw Doji Lines
    if (dojiLineIsActive == true)
    {
    Draw.ExtendedLine(this, "Doji Line", true, xValue[2], yValue[2], xValue[1], yValue[1], Brushes.LimeGreen, DashStyleHelper.Dot, 2);
    }
    }

    #2
    Originally posted by benjamin$ View Post

    I am confused as to why my code is giving me this error.

    Here is the code for this portion:

    if (dBull == dBear)
    {
    DOJINUETRAL[0] = Convert.ToDouble(Close[0]);
    List<DateTime> xValue = new List<DateTime>();
    xValue.Add(Time[0]);
    List<double> yValue = new List<double>();
    yValue.Add(Close[0]);
    //Draw Doji Lines
    if (dojiLineIsActive == true)
    {
    Draw.ExtendedLine(this, "Doji Line", true, xValue[2], yValue[2], xValue[1], yValue[1], Brushes.LimeGreen, DashStyleHelper.Dot, 2);
    }
    }
    Your lists xValue and yValue don't have elements at those index positions.

    How can they?
    They're re-created via new each time dBull == dBear is true.

    Comment


      #3
      Originally posted by bltdavid View Post

      Your lists xValue and yValue don't have elements at those index positions.

      How can they?
      They're re-created via new each time dBull == dBear is true.
      I only moved them there for this post so I didn't have to post my whole code. But it throws the same error even when the lists are generated prior to OnStateChange().

      Is there a specific location for generating lists within Ninjascript?

      Comment


        #4
        Probably same reason for the error -- no elements at those index positions.

        If you want any further help, I suggest you post/attach the exact code, with
        no cosmetic re-arrangements -- such changes are not helpful to debugging
        your problem -- all they do is exasperate the detectives who piece together
        the crime scene.

        [The novice police detective knows there was a stabbing, so they helpfully
        pull out all the kitchen knives from all the drawers, sorted longest to shortest,
        so they can be easily examined on the counter by more senior detectives
        upon their arrival. Do you think that novice detective made the crime
        scene any easier to understand? Just like a detective, you need to
        freeze everything in time, so that others can recreate the exact
        picture to discover what happened -- when you change code like
        what you did you're not really solving the original problem -- so
        don't do that, you misrepresented your actual issue -- that is not
        a good thing to do.]

        You can allocate lists anywhere you need them, it just depends on
        your logic and what you're trying to accomplish.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Belfortbucks, Today, 09:29 PM
        0 responses
        6 views
        0 likes
        Last Post Belfortbucks  
        Started by zstheorist, Today, 07:52 PM
        0 responses
        7 views
        0 likes
        Last Post zstheorist  
        Started by pmachiraju, 11-01-2023, 04:46 AM
        8 responses
        151 views
        0 likes
        Last Post rehmans
        by rehmans
         
        Started by mattbsea, Today, 05:44 PM
        0 responses
        6 views
        0 likes
        Last Post mattbsea  
        Started by RideMe, 04-07-2024, 04:54 PM
        6 responses
        33 views
        0 likes
        Last Post RideMe
        by RideMe
         
        Working...
        X