Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Index outside bounds error....

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

    Index outside bounds error....

    I am receiving this error in my log when I am enabling my strategy that reverts to this indicator I have created. Not sure how to fix it. Can you help please? Thank you. See attached error and code.

    Code:
    		protected override void OnBarUpdate()
    		{
    			Upper0.Set(MAX(High, LongPeriod)[0]);
    			Lower0.Set(MIN(Low, LongPeriod)[0]);
    			ShortStopATR.Set(Close[0] + (nAtr * ATR(8)[0]));
    			LongStopATR.Set(Close[0] - (nAtr * ATR(8)[0]));
    
    		}
    Attached Files

    #2
    I also added this to both my strategy and indicator but it did not solve my problem.

    Code:
    if (CurrentBar < 1) return;

    Comment


      #3
      Hello cfree5119,

      Thanks for the post. Does the indicator work by itself when you apply it to a chart? In your strategy: How do you use the indicator causing the error message?
      Ryan M.NinjaTrader Customer Service

      Comment


        #4
        Originally posted by cfree5119 View Post
        I also added this to both my strategy and indicator but it did not solve my problem.

        Code:
        if (CurrentBar < 1) return;
        It looks like you want to use:

        Code:
        if (CurrentBar < LongPeriod) return;

        Comment


          #5
          It does not work when I try to add just the indicator to the chart. When I put it in my strategy, I use the below code.
          Code:
          Add(DonchianChannelATR(IndicatorSensitivity, LongEntryBand, NAtr));
          koganam, your solution did not affect the error I am having.

          Comment


            #6
            Thanks for the reply. First step in troubleshooting this is getting the indicator to plot by itself, since the strategy doesn't actually use its values but only attempts to add for visualization.

            Can you please share the indicator script you're having trouble plotting and we'll take a look?
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              Thanks Ryan.
              Code:
              			
              #region Using declarations
              using System;
              using System.Diagnostics;
              using System.Drawing;
              using System.Drawing.Drawing2D;
              using System.ComponentModel;
              using System.Xml.Serialization;
              using NinjaTrader.Data;
              using NinjaTrader.Gui.Chart;
              #endregion
              
              // This namespace holds all indicators and is required. Do not change it.
              namespace NinjaTrader.Indicator
              {
              
              	[Description("")]
              	public class DonchianChannelATR : Indicator
              	{
              		#region Variables
              		private int longPeriod = 480;
              		private double nAtr = 2.2;
              		private int indicatorSensitivity = 8;
              		#endregion
              
              
              		protected override void Initialize()
              		{
              			Add(new Plot(Color.Blue, "Upper0"));
              			Add(new Plot(Color.Blue, "Lower0"));
              			Add(new Plot(Color.Green, "ShortStopATR"));
              			Add(new Plot(Color.Red, "LongStopATR"));
              			Overlay	= true;
              		}
              		
              		protected override void OnBarUpdate()
              		{
              			if (CurrentBar < LongPeriod) return;
              			
              			Upper0.Set(MAX(High, LongPeriod)[0]);
              			Lower0.Set(MIN(Low, LongPeriod)[0]);
              			ShortStopATR.Set(Close[0] + (nAtr * ATR(8)[0]));
              			LongStopATR.Set(Close[0] - (nAtr * ATR(8)[0]));
              
              		}
              		
              		#region Properties
              		[Description("Numbers of bars used for calculations")]
              		[GridCategory("Parameters")]
              		public int LongPeriod
              		{
              			get { return longPeriod; }
              			set { longPeriod = Math.Max(1, value); }
              		}
              		
              		[Description("Numbers of deviations used for calcuations")]
              		[GridCategory("Parameters")]
              		public double NAtr
              		{
              			get { return nAtr; }
              			set { nAtr = Math.Max(1, value); }
              		}
              		
                      [Description("Indicator smoothing")]
                      [GridCategory("Parameters")]
                      public int IndicatorSensitivity
                      {
                          get { return indicatorSensitivity; }
                          set { indicatorSensitivity = Math.Max(1, value); }
                      }
              
              		[Browsable(false)]
              		[XmlIgnore()]
              		public DataSeries Upper0
              		{
              			get { return Values[0]; }
              		}
              		
              		[Browsable(false)]
              		[XmlIgnore()]
              		public DataSeries Lower0
              		{
              			get { return Values[1]; }
              		}
              
              		[Browsable(false)]
              		[XmlIgnore()]
              		public DataSeries ShortStopATR
              		{
              			get { return Values[4]; }
              		}	
              		
              		[Browsable(false)]
              		[XmlIgnore()]
              		public DataSeries LongStopATR
              		{
              			get { return Values[5]; }
              		}
              		
              		#endregion
              	}
              }

              Comment


                #8
                Thanks for posting the snippet. There are a couple things to check:
                Make sure MaximumBarsLookBack is set to infinite, since your lookback is greater than 256.

                Then in your properties region, you skip some Values. Please see the corrected properties region below for this, with Values going correctly from 0 - 3

                Code:
                [Browsable(false)]
                [XmlIgnore()]
                public DataSeries Upper0
                {
                get { return Values[COLOR="Red"][0][/COLOR]; }
                }
                
                [Browsable(false)]
                [XmlIgnore()]
                public DataSeries Lower0
                {
                get { return Values[COLOR="red"][1][/COLOR]; }
                }
                
                [Browsable(false)]
                [XmlIgnore()]
                public DataSeries ShortStopATR
                {
                get { return Values[COLOR="red"][2];[/COLOR] }
                }	
                
                [Browsable(false)]
                [XmlIgnore()]
                public DataSeries LongStopATR
                {
                get { return Values[COLOR="red"][3][/COLOR]; }
                }
                Ryan M.NinjaTrader Customer Service

                Comment


                  #9
                  Thanks Ryan, that was the issue.

                  Comment


                    #10
                    Let me ask you this... would there be a way to make both the LongStop and ShortStop online increase or decrease respectively? If you look at the plot, the stop line moves in accordance with price. When price moves down I would like to make the LongStop not move down with price. Is this possible? THanks again.

                    Comment


                      #11
                      Sorry, it's not completely clear, but maybe you're trying to fix the stop loss to the indicator value at the point of entry? And then if the indicator value changes, you don't want the stop loss value to match?
                      Ryan M.NinjaTrader Customer Service

                      Comment


                        #12
                        I want plots LongStopATR and ShortStopATR to act like a trailing stop. So once it enters a position upon the price hitting say a 5 day high, the ATR plot that I have set up will follow price upwards but not it's movement downwards. Doest that make sense?

                        Comment


                          #13
                          Yes, that makes sense. It will require some custom coding to implement.

                          Math.Max() would be good for this to take the higher of either 1) the current value of the indicator 2) the highest value of the indicator since entry. Reverse for the short side. -Math.Min() and MIN()

                          Basic example using SMA below:
                          double myStopPrice = Math.Max(SMA(7)[0], MAX(SMA(7), BarsSinceEntry())[0]);

                          Relevant methods linked for this example:
                          Math.Max()
                          MAX()
                          BarsSinceEntry()
                          Ryan M.NinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                          0 responses
                          650 views
                          0 likes
                          Last Post Geovanny Suaza  
                          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                          0 responses
                          370 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by Mindset, 02-09-2026, 11:44 AM
                          0 responses
                          109 views
                          0 likes
                          Last Post Mindset
                          by Mindset
                           
                          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                          0 responses
                          574 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by RFrosty, 01-28-2026, 06:49 PM
                          0 responses
                          577 views
                          1 like
                          Last Post RFrosty
                          by RFrosty
                           
                          Working...
                          X