Announcement

Collapse
No announcement yet.

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