Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Essentially the same code but one is giving me errors

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

    Essentially the same code but one is giving me errors

    Hi,

    I have 2 indicators where the difference is one has a List<T> and the other doesn't. The list has tags added to it and nothing else but it is giving out an error message:-

    Error on calling 'OnBarUpdate' method on bar 14: 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.

    The one without the list doesn't have any error messages. Can you please tell me what is wrong?

    The code below uses the List<T>:-

    Code:
    if (CurrentBar < (level*2 + 10)) return;
    
    // if( DemandLineCount == DemandLineMaxCount )
    // {
    // DemandLines.RemoveAt(DemandLines.Count-1);
    // }
    
    // Identify the first pivot low
    if((Math.Min(Low[1],Close[2]) < Math.Min(Low[0],Close[1])) && (Math.Min(Low[1],Close[2]) < Math.Min(Low[2], Close[3])))
    {
    DemandPoint0 = Math.Min(Low[1],Close[2]);
    DemandPoint0Y = CurrentBar-level;
    // Draw.Dot(this, "DPDot"+CurrentBar, true, 1, DemandPoint0, Brushes.Red, true);
    // Print(Time[0].ToString());
    // Print("CurrentBara = " + CurrentBar.ToString());
    }
    // Hunting for a lower pivot low that has already occurred which meets the same pivot low condition and to draw a ray connecting the 2 points
    for( int x = 1; x <= Math.Min(CurrentBar-4, 50); x++)
    {
    if ((Math.Min(Low[x], Close[x+1]) < Math.Min(Low[x-1], Close[x])) && (Math.Min(Low[x], Close[x+1]) < Math.Min(Low[x+1], Close[x+2])))
    {
    if (Math.Min(Low[x], Close[x+1]) < DemandPoint0)
    {
    DemandPoint1 = Math.Min(Low[x], Close[x+1]);
    DemandPoint1Y = x;
    string drawTag = "DPL" + CurrentBar;
    Draw.Ray(this, drawTag, true, DemandPoint1Y, DemandPoint1, CurrentBar - DemandPoint0Y , DemandPoint0, Brushes.Pink, DashStyleHelper.Solid, 1);
    DemandLineCount++;
    DemandLines.Add(drawTag);
    Print(Time[0].ToString());
    Print("DemandLines Last Item = "+ DemandLines[DemandLines.Count-1]);
    Print("DemandLines Last Item = "+ DemandLines[DemandLines.Count-2]);
    // Print("x =" + x.ToString());
    // Print("low[x] = " + Low[x].ToString());
    // Print("low[1] = " + Low[1].ToString());
    // Print("CurrentBar = " + CurrentBar.ToString());
    // Print("DemandPoint0 = " + DemandPoint0.ToString());
    // Print("DemandPoint0Y = " + DemandPoint0Y.ToString());
    break;
    }
    }
    }
    The one below does not have List<T>:-

    Code:
    if (CurrentBar < (level*2 + 10)) return;
    
    // Identify the first pivot low
    if((Math.Min(Low[1],Close[2]) < Math.Min(Low[0],Close[1])) && (Math.Min(Low[1],Close[2]) < Math.Min(Low[2], Close[3])))
    {
    DemandPoint0 = Math.Min(Low[1],Close[2]);
    DemandPoint0Y = CurrentBar-level;
    // Draw.Dot(this, "DPDot"+CurrentBar, true, 1, DemandPoint0, Brushes.Red, true);
    // Print(Time[0].ToString());
    // Print("CurrentBara = " + CurrentBar.ToString());
    }
    // Hunting for a lower pivot low that has already occurred which meets the same pivot low condition and to draw a ray connecting the 2 points
    for( int x = 1; x <= Math.Min(CurrentBar-4, 50); x++)
    {
    if ((Math.Min(Low[x], Close[x+1]) < Math.Min(Low[x-1], Close[x])) && (Math.Min(Low[x], Close[x+1]) < Math.Min(Low[x+1], Close[x+2])))
    {
    if (Math.Min(Low[x], Close[x+1]) < DemandPoint0)
    {
    DemandPoint1 = Math.Min(Low[x], Close[x+1]);
    DemandPoint1Y = x;
    Draw.Ray(this, "DPL1"+CurrentBar, true, DemandPoint1Y, DemandPoint1, CurrentBar - DemandPoint0Y , DemandPoint0, Brushes.Pink, DashStyleHelper.Solid, 1);
    
    // Print(Time[0].ToString());
    // Print("x =" + x.ToString());
    // Print("low[x] = " + Low[x].ToString());
    // Print("low[1] = " + Low[1].ToString());
    // Print("CurrentBar = " + CurrentBar.ToString());
    // Print("DemandPoint0 = " + DemandPoint0.ToString());
    // Print("DemandPoint0Y = " + DemandPoint0Y.ToString());
    break;
    }
    }
    }

    #2
    Hi,

    Resolved the issue. It was this line that caused the error:-

    Print("DemandLines Last Item = "+ DemandLines[DemandLines.Count-2]);

    Why would a print statement cause an error?

    Regards
    Kay Wai

    Comment


      #3
      Another question I have is when I print out the last item on the DemandLines List (referring to this print statement:

      Print("DemandLines Last Item = "+ DemandLines[DemandLines.Count-1])

      the first print refers to is when the ray + tag was first drawn and created, which is correct. But I'm getting tags on the following bar (actually for 6 following bars) when there is no new ray being drawn.

      Is there something that I'm doing wrong?
      Last edited by kaywai; 05-28-2022, 02:28 AM.

      Comment


        #4
        I tried doing away with the "list" after I found some code NinjaTrader PaulH shared regarding a similar matter. Please see attached.

        I'm getting the same issue as I described with the list way of doing things. Once it starts printing, the count doesn't stop. I couldn't remove any tag. Below is how I've changed it:-

        Code:
        if (CurrentBar < (level*2 + 10)) return;
        
        // if( DemandLineCount == DemandLineMaxCount )
        // {
        // DemandLines.RemoveAt(DemandLines.Count-1);
        // }
        
        // Identify the first pivot low
        if((Math.Min(Low[1],Close[2]) < Math.Min(Low[0],Close[1])) && (Math.Min(Low[1],Close[2]) < Math.Min(Low[2], Close[3])))
        {
        DemandPoint0 = Math.Min(Low[1],Close[2]);
        DemandPoint0Y = CurrentBar-level;
        // Draw.Dot(this, "DPDot"+CurrentBar, true, 1, DemandPoint0, Brushes.Red, true);
        // Print(Time[0].ToString());
        // Print("CurrentBara = " + CurrentBar.ToString());
        }
        // Hunting for a lower pivot low that has already occurred which meets the same pivot low condition and to draw a ray connecting the 2 points
        for( int x = 1; x <= Math.Min(CurrentBar-14, 50); x++)
        {
        if ((Math.Min(Low[x], Close[x+1]) < Math.Min(Low[x-1], Close[x])) && (Math.Min(Low[x], Close[x+1]) < Math.Min(Low[x+1], Close[x+2])))
        {
        if (Math.Min(Low[x], Close[x+1]) < DemandPoint0)
        {
        DemandPoint1 = Math.Min(Low[x], Close[x+1]);
        DemandPoint1Y = x;
        DemandLineCount++;
        // string drawTag = "DPL" + DemandLineCount;
        Draw.Ray(this, "DPL" + DemandLineCount, true, DemandPoint1Y, DemandPoint1, CurrentBar - DemandPoint0Y , DemandPoint0, Brushes.Pink, DashStyleHelper.Solid, 1);
        // DemandLines.Add(drawTag);
        Print(Time[0].ToString());
        Print("DemandLine Count is: " + DemandLineCount.ToString());
        // Print("DemandLines Last Item = "+ DemandLines[DemandLines.Count-1]);
        // Print("DemandLines Last Item = "+ DemandLines[DemandLines.Count-2]);
        // Print("x =" + x.ToString());
        // Print("low[x] = " + Low[x].ToString());
        // Print("low[1] = " + Low[1].ToString());
        // Print("CurrentBar = " + CurrentBar.ToString());
        // Print("DemandPoint0 = " + DemandPoint0.ToString());
        // Print("DemandPoint0Y = " + DemandPoint0Y.ToString());
        break;
        // To Remove Draw Objects
        if( DemandLineCount > DemandLineMaxCount)
        {
        for( int i = 1; i <= 5; i++)
        {
        RemoveDrawObject("DPL" + i);
        }
        DemandLineCount = DemandLineCount - 5;
        }
        }
        }
        }
        Can somebody please help?

        Regards
        Kay Wai
        Attached Files

        Comment


          #5
          Originally posted by kaywai View Post
          Resolved the issue. It was this line that caused the error:-

          Print("DemandLines Last Item = "+ DemandLines[DemandLines.Count-2]);

          Why would a print statement cause an error?
          Your thinking is misguided, the syntax of your print statement is fine.

          What you have is a logic error, which is more subtle.

          The problem is accessing an element in DemandLines that doesn't exist.

          Consider this:
          On the first time through, when an element is added to DemandLines
          for the first time using Add(), the value of DemandLines.Count is 1.

          So, that means DemandLines.Count-2 is actually -1, and accessing
          the -1 element is what produces the error you see. Negative indexes
          are considered illegal in C#.

          The way to fix this is to add guard code, to protect against negative
          indexes, like this,

          if (DemandLines.Count-2 >= 0)
          Print("DemandLines Last Item = "+ DemandLines[DemandLines.Count-2]);


          Or, since this is a just a print statement, and doesn't really contribute
          to the functionality of the code, just delete it.

          Last edited by bltdavid; 05-28-2022, 10:49 AM.

          Comment


            #6
            bltdavid i did the latter. thx. and your explanation made sense. appreciate it.

            i'm having trouble removing draw objects. the second methods is not doing what i thought it would do, So i've gone back to list. But I don't know how to removedrawobject using list.

            I'm trying to cap the number of rays drawn to 10 (DemandLineMaxCount) and if it gets to 10, to remove the oldest ray.

            the error i'm getting so far in the code below is "cannot convert type 'char' to 'string'. This is the part which is causing the issue.

            Code:
            foreach (string drawTag in DemandLines[DemandLines.Count-1])
            {
            Print(Time[0].ToString());
            Print( drawTag);
            if(DemandLines.Count == DemandLineMaxCount )
            {
            DemandLines.RemoveAt(DemandLines.Count-1);
            RemoveDrawObject(drawTag);
            }
            }
            would you mind taking a look?

            regards
            kaywai
            Last edited by kaywai; 05-28-2022, 01:11 PM.

            Comment


              #7
              Hello kaywai,

              bltdavid is right on the money. Can't ask for something that doesn't exit. And negative indexes do not exist. Any index used must be equal to or greater than 0 and less than the size of the collection (count).
              Hello, I want to create an indicator that show data in a chart but calculate in other charttime different to the time of the chart where is showed. For example:


              However, where is the code where the list DemandLines is being declared? Is this a List<string>? Or is this a List<char>? Or is this a List<DrawObject> or List<Line>?

              What object type is being supplied here as drawTag?

              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Hi ChelseaB,

                It's a List<string>. I've included the whole code here:-

                Need help with this - NinjaTrader Support Forum

                I think I've close to being done now save for 1 piece of logic that I have yet to comprehend. Hopefully you can point me in the right direction.

                Regards
                Kay Wai

                Comment


                  #9
                  Hello Kay Wai,

                  A list of what? A list requries an object type. What is the object type?

                  Are you creating duplicate forum threads on the same subject? Please let me know so that I merge these threads together if they are duplicates.
                  Please refrain from making duplicate posts on the forums.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Hi ChelseaB,

                    Same indicator but different set of issues. Didn't want to confuse readers. If you think they are the same, feel free to merge them.

                    Regards
                    Kay Wai

                    Comment


                      #11
                      Hello Kay Wai,

                      If its not the same inquiry make a new thread. If its the same inquiry, (doesn't matter what script), please do not duplicate inquiry.

                      Is this the same inquiry?
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        No it isn't

                        Comment


                          #13
                          Hello kaywai,

                          If the object is a string, I would not expect this error that the object is a char type.

                          May I have you create a new test script so I may test on my end?
                          Copy just the definition for the List<string>, the instantiation and assignment, and the loop.
                          Please do not include any other code.

                          To export a NinjaTrader 8 NinjaScript so this can be shared and imported by the recipient do the following:
                          1. Click Tools -> Export -> NinjaScript...
                          2. Click the 'add' link -> check the box(es) for the script(s) and reference(s) you want to include
                          3. Click the 'Export' button
                          4. Enter a unique name for the file in the value for 'File name:'
                          5. Choose a save location -> click Save
                          6. Click OK to clear the export location message
                          By default your exported file will be in the following location:
                          • (My) Documents/NinjaTrader 8/bin/Custom/ExportNinjaScript/<export_file_name.zip>
                          Below is a link to the help guide on Exporting NinjaScripts.
                          http://ninjatrader.com/support/helpG...-us/export.htm

                          Once exported, please attach the file as an attachment to your reply.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Hi Chelsea, what do you mean by instantiation and assignment?

                            I modified the foreach loop from "foreach (string drawTag in DemandLines[DemandLines.Count-1])" to "foreach (string drawTag in DemandLines.ToList())"

                            Regards
                            Kay Wai

                            Comment


                              #15
                              Hello Kay Wai,

                              private List<string> myStringList; <-- definition of the variable

                              new List<string>(); <-- instantiation of the actual object

                              myStringList = new List<string>(); <-- assigning the instantiated object to a variable
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              572 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              331 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
                              549 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              550 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X