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

Index out of bounds error...

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

    Index out of bounds error...

    I understand *why* this happens, normally, but cannot figure out *why* in this instance:
    My indicator is simply supposed to draw a green line when up, and red when down. Here's the logic:

    #region Variables
    private DataSeries vol;
    #endregion

    protected override void Initialize()
    {
    Add(new Plot(new Pen(Color.Orange, 2), PlotStyle.Line, "Plot0"));
    Add(new Plot(new Pen(Color.Indigo, 2), PlotStyle.Line, "Plot1"));
    Add(new Plot(new Pen(Color.LimeGreen, 2), PlotStyle.Line, "Up"));
    Add(new Plot(new Pen(Color.DarkRed, 2), PlotStyle.Line, "Down"));
    Add(new Plot(new Pen(Color.Yellow, 2), PlotStyle.Line, "Neutral"));
    vol = new DataSeries(this);
    }

    protected override void OnBarUpdate()
    {
    double newSlope = 0;
    double study = 0;
    double primary = 0;
    if (CurrentBar == 0)
    {
    return;
    }
    else
    {
    newSlope = (Volume[0] - Volume[1]) / (ToTime(Time[0]) - ToTime(Time[1]));

    vol.Set(newSlope);
    study = SMA(vol, 6)[0];
    primary = vol[1]+vol[0];
    //
    //Plot0.Set(primary);
    Plot1.Set(study);

    if (vol[0] > vol[1])
    {

    Up.Set(primary);
    }
    else if (vol[0] < vol[1])
    {

    Down.Set(primary);
    }
    else
    {
    Neutral.Set(primary);
    }
    //DrawHorizontalLine("L", Low[1], Color.Yellow);
    }

    }

    If I comment out the if/elseif/else condition, the study works(without colors of course). AND if I print out "hey" in the second condition (vol[0] < vol[1]) "hey" prints to the output window. There's something funky in the vol[n] ...why is this giving me the "Index out of bounds of array" error?
    Last edited by funk101; 05-23-2007, 01:27 AM. Reason: added some logic

    #2
    Likely vol[1] does not exist when CurrentBar == 1 since on this bar, this is the first time you call vol.Set(). When CurrentBar == 0 add vol.Set(0) and see if this resolves the issue.
    RayNinjaTrader Customer Service

    Comment


      #3
      Ah!

      Of course. That was it. Thanks.

      Comment


        #4
        Ok, check this if you will...

        Trying to get the different colored line. I'm getting empty patches. Check image, and code please. Everything works fine, the thumb.gif shows up, but the line is missing the neutral color:

        protected override void OnBarUpdate()
        {
        if (CurrentBar == 0)
        {
        Plot0.Set(0);
        Plot1.Set(0);
        vol.Set(0);
        Up.Set(0);
        Down.Set(0);
        Neutral.Set(0);
        }
        else
        {
        newSlope = (Volume[0] - Volume[1]) / (ToTime(Time[0]) - ToTime(Time[1]));

        vol.Set(newSlope);
        study = SMA(vol, 6)[0];
        primary = vol[1]+vol[0];
        //
        //Plot0.Set(primary);
        Plot1.Set(study);
        Print("new "+newSlope+" old "+primary);
        if (newSlope >= primary)
        {
        Up.Set(primary);
        //Plot0.Set(primary);
        image = new Bitmap(imagePath+"thumbsUp.gif");
        }
        else if (newSlope < primary)
        {
        //Print("down");
        Down.Set(primary);
        //Plot0.Set(primary);
        image = new Bitmap(imagePath+"thumbsDown.gif");
        }
        else
        {
        //Plot0.Set(primary);
        Neutral.Set(primary);
        }


        DrawHorizontalLine("L", Low[1], Color.Yellow);
        }

        }
        Attached Files

        Comment


          #5
          Dealing with the "gap" is discussed in the following thread.

          RayNinjaTrader Customer Service

          Comment


            #6
            Additional thread dealing with gaps.

            RayNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by terofs, Today, 04:18 PM
            0 responses
            5 views
            0 likes
            Last Post terofs
            by terofs
             
            Started by nandhumca, Today, 03:41 PM
            0 responses
            4 views
            0 likes
            Last Post nandhumca  
            Started by The_Sec, Today, 03:37 PM
            0 responses
            3 views
            0 likes
            Last Post The_Sec
            by The_Sec
             
            Started by GwFutures1988, Today, 02:48 PM
            1 response
            5 views
            0 likes
            Last Post NinjaTrader_Clayton  
            Started by ScottWalsh, 04-16-2024, 04:29 PM
            6 responses
            33 views
            0 likes
            Last Post ScottWalsh  
            Working...
            X