Announcement

Collapse
No announcement yet.

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 Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        647 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        368 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        108 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        572 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        573 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X