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

well I'm lost...

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

    well I'm lost...

    I created an indicator that is not working.

    When I run my script I get the following error message :

    Error on calling 'OnBarUpdate' method for indicator 'Pivots' on bar 3118: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

    After checking the code I finally pinpoint the issue but this is confusing and I don't understand it.

    I conditionally stored some price values on a list named xx and at some point I try to get the index of the closest ones below/above the last Close. Here is the code :

    var24=999999.00;
    var25=999999.00;
    for (int i=0 ; i<xx.Count ; i++)
    {
    var58 = Close[0]-xx[i].Cprice;
    if (var58<0 && Math.Abs(var58)< Math.Abs(var24))
    {
    var24 = var58;
    var28 = i;
    }
    if (var58>=0 && var58 < var25)
    {
    var25 = var58;
    var29 = i;
    }
    }
    Print(var28+" "+var29+" "+xx.Count);

    As you can see my index should NEVER reach xx.Count as I specified it in the loop but when I print my var28 & var29 (that contain index values)+xx.Count.........SURPRISE!!!...I found out that at some point index equals xx.Count.

    How the hell is it possible ?
    Attached Files
    Last edited by baba123; 01-12-2011, 10:40 PM.

    #2
    Your Print statement is after the loop, where I would expect i to be xx.Count. Are you saying that i is reaching xx.Count and executing the loop code that way?

    --EV
    Last edited by ETFVoyageur; 01-13-2011, 12:51 AM.

    Comment


      #3
      the highest value "i" can take is xx.Count-1 but as you can see in the output file this is not the case and I do not understand why.

      Anyway is there a more proper way to achieve what I want, I mean get the index of the closest values stored in my list element above/below the last Close ?

      Comment


        #4
        Do you have the needed CurrentBars check at the OnBarUpdate() start at all?

        BertrandNinjaTrader Customer Service

        Comment


          #5
          Yes, I already checked this.

          The thing I do not understand is the fact that when writing "for(int i=0; i<xx.Count; i++)"
          at some point i =xx.Count as you can see in the output file.

          This is what is causing error in my script....

          Comment


            #6
            Originally posted by baba123 View Post
            Yes, I already checked this.

            The thing I do not understand is the fact that when writing "for(int i=0; i<xx.Count; i++)"
            at some point i =xx.Count as you can see in the output file.

            This is what is causing error in my script....
            Once again, your print statement is AFTER your loop, and so i will be xx.Count when you get there. The loop will never be executed with i==xx.Count, but that does not mean i cannot assume that value. Try the following code:

            Code:
                        int xx;
                        for (xx=0; xx<10; xx++)
                            Print ("In loop x= " + xx);
                        Print ("After loop x=" + xx);
            It will print:
            Code:
            In loop x= 0
            In loop x= 1
            In loop x= 2
            In loop x= 3
            In loop x= 4
            In loop x= 5
            In loop x= 6
            In loop x= 7
            In loop x= 8
            In loop x= 9
            After loop x=10
            -- EV

            Comment


              #7
              That is because of the way that you are incrementing your index. Try this instead:

              Code:
              for(int i=0; i<xx.Count; ++i
              In which case, you may also have to start your count at i = -1. It just depends on what you want your processing to do.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by GLFX005, Today, 03:23 AM
              0 responses
              1 view
              0 likes
              Last Post GLFX005
              by GLFX005
               
              Started by XXtrader, Yesterday, 11:30 PM
              2 responses
              11 views
              0 likes
              Last Post XXtrader  
              Started by Waxavi, Today, 02:10 AM
              0 responses
              6 views
              0 likes
              Last Post Waxavi
              by Waxavi
               
              Started by TradeForge, Today, 02:09 AM
              0 responses
              14 views
              0 likes
              Last Post TradeForge  
              Started by Waxavi, Today, 02:00 AM
              0 responses
              3 views
              0 likes
              Last Post Waxavi
              by Waxavi
               
              Working...
              X