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 outside of bounds error

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

    Index outside of bounds error

    Hello, I have another related question. I'm trying to make a KAMA indicator that uses Range(4) bars as the secondary data series, so that it may be called by a strategy that uses any other type of bars.

    I'm getting the "... index outside of bounds error". I've tried to see where the error originates by using prints, but I can't fix the issue.

    Code:
        public class KAMASecondarySeries : Indicator
        {
            private Series<double>    diffSeries;
            private double            fastCF;
            private double            slowCF;
            private SUM                sum;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                    = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDescriptionKAMA;
                    Name                        = NinjaTrader.Custom.Resource.NinjaScriptIndicatorNameKAMA;
                    Fast                        = 2;
                    IsSuspendedWhileInactive    = true;
                    IsOverlay                    = true;
                    Period                        = 10;
                    Slow                        = 30;
                    RangePeriod                 = 4;
    
                    AddPlot(Brushes.DodgerBlue, NinjaTrader.Custom.Resource.NinjaScriptIndicatorNameKAMA);
    
                }
                else if (State == State.Configure)
                {
                    AddDataSeries(BarsPeriodType.Range, RangePeriod);
    
                    //fastCF        = 2.0 / (Fast + 1);
                    //slowCF        = 2.0 / (Slow + 1);
                }
                else if (State == State.DataLoaded)
                {
    
    
                    fastCF        = 2.0 / (Fast + 1);
                    slowCF        = 2.0 / (Slow + 1);
    
    
                    diffSeries = new Series<double>(this);
                    sum = SUM(diffSeries, Period);
                }
            }
    
            protected override void OnBarUpdate()
            {
    
                        Print(Time[0]+" 1 ");
    
                if (CurrentBars[0] < RangePeriod && BarsInProgress== 0 )
                    {
                        Value[0] = Input[0];
    
                        //Print(Time[0]+" Value[0] "+Value[0]);
                        Print(Time[0]+" 2 ");
                        return;
                    }
    
                    if (CurrentBars[1] < Period && BarsInProgress== 1)
                    {
                        Value[0] = Input[0];
                        Print(Time[0]+" 3 ");
    
                        return;
                    }
                        Print(Time[0]+" Value[0] "+Value[0]);
                        Print(Time[0]+" 4 ");
    
    
                if (BarsInProgress == 1 && CurrentBars[1] > Period)
                {
                    double input0 = Input[0];
                    diffSeries[0] = CurrentBar > 0 ? Math.Abs(input0 - Input[1]) : input0;
    
    
                    double signal = Math.Abs(input0 - Input[Period]);
                    double noise  = sum[0];
    
                    Print(Time[0]+" signal "+signal);
                    // Prevent div by zero
                    if (noise == 0)
                    {
                        Value[0] = Value[1];
                        return;
                    }
    
                    Print(Time[0]+" xValue[0] "+Value[0]);
    
                 double value1   = Value[1];
                *** This Print #5 line does not print, suggesting the Value[1] is incorrect ***
                 Print(Time[0]+" 5 ");
    
                    Value[0]        = value1 + Math.Pow((signal / noise) * (fastCF - slowCF) + slowCF, 2) * (input0 - value1);
                }
                if (BarsInProgress == 0 && CurrentBars[0] > Period )
                    Value[0] = Values[1][0];
    
            }
    
            #region Properties
            [Range(1, 125), NinjaScriptProperty]
            [Display(ResourceType = typeof(Custom.Resource), Name = "Fast", GroupName = "NinjaScriptParameters", Order = 0)]
            public int Fast
            { get; set; }
    
            [Range(5, int.MaxValue), NinjaScriptProperty]
            [Display(ResourceType = typeof(Custom.Resource), Name = "Period", GroupName = "NinjaScriptParameters", Order = 1)]
            public int Period
            { get; set; }
    
            [Range(1, 125), NinjaScriptProperty]
            [Display(ResourceType = typeof(Custom.Resource), Name = "Slow", GroupName = "NinjaScriptParameters", Order = 2)]
            public int Slow
            { get; set; }
    
            [Range(1, int.MaxValue), NinjaScriptProperty]
            [Display(ResourceType = typeof(Custom.Resource), Name = "RANGE Period", GroupName = "NinjaScriptParameters", Order = 3)]
            public int RangePeriod
            { get; set; }
    
            #end​
    What am I doing wrong?
    Last edited by bobperez; 12-20-2022, 03:20 PM.

    #2
    Hi Bob, thanks for posting. I moved your post to a new topic since its not related.

    This is a good application of the powerful Visual Studio debugging technique. You can attach Visual Studio to the NinjaTrader process, run the code that is causing this exception then Visual Studio will break at the line of code where the exception is occurring. It's a very useful debugging technique that is used frequently to find run-time errors. See here for a guide on doing this:

    https://ninjatrader.com/support/help..._studio_debugg ing.htm

    This guide uses an older version of Visual Studio, but you can use the current version.

    99% of the time, the "index outside of bounds error" in a script is from accessing a Series<T> index that does not yet exist.

    Kind regards,

    -ChrisL
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hi Chris,

      Thanks. I'll try that.

      Bob

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by rocketman7, Today, 09:41 AM
      3 responses
      7 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by traderqz, Today, 09:44 AM
      2 responses
      4 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by stafe, 04-15-2024, 08:34 PM
      8 responses
      40 views
      0 likes
      Last Post stafe
      by stafe
       
      Started by rocketman7, Today, 02:12 AM
      7 responses
      31 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by guillembm, Yesterday, 11:25 AM
      3 responses
      16 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Working...
      X