Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

RMA indicator "Index outside the bounds of the array" error

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

    RMA indicator "Index outside the bounds of the array" error

    DISCLAIMER: The indicator file itself compiles just fine. This error happens when I try to use it in a strategy, such as a simple crossover.
    Within the strategy I have the condition of
    if (CurrentBars[0] < 1)
    return;​

    Here is my code - I used the existing SMA code and simply modified it to create a new indicator.

    namespace NinjaTrader.NinjaScript.Indicators
    {
    public class RMA : Indicator
    {
    private double prevRMA;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Calculates the Relative Moving Average (Wilder's Moving Average).";
    Name = "RMA";
    IsOverlay = true;
    IsSuspendedWhileInactive = true;
    Period = 14;
    }
    else if (State == State.Configure)
    {
    }
    else if (State == State.DataLoaded)
    {
    prevRMA = 0;
    }
    }

    protected override void OnBarUpdate()
    {
    if (CurrentBar == 0)
    {
    // Initialize RMA with the first input value
    prevRMA = Input[0];
    Value[0] = Input[0];
    }
    else
    {
    // RMA Calculation: RMA = (PrevRMA * (Period - 1) + Input) / Period
    Value[0] = (prevRMA * (Period - 1) + Input[0]) / Period;

    // Update the previous RMA for the next calculation
    prevRMA = Value[0];
    }
    }

    region Properties
    [Range(1, int.MaxValue), NinjaScriptProperty]
    [Display(ResourceType = typeof(Custom.Resource), Name = "Period", GroupName = "NinjaScriptParameters", Order = 0)]
    public int Period
    { get; set; }
    #endregion
    }
    }


    #2
    Hello unpronounceable1700,

    Are you using the indicator in some condition or just adding it visually? The indicator needs to be called by OnBarUpdate in the strategy to process, if you are not doing that it may have problems.

    Are you seeing any errors in the control center log tab when running it?

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello unpronounceable1700,

      Are you using the indicator in some condition or just adding it visually? The indicator needs to be called by OnBarUpdate in the strategy to process, if you are not doing that it may have problems.

      Are you seeing any errors in the control center log tab when running it?
      Yup, it's going to be used in a condition.
      The error comes in the output menu where I get some variation of
      Strategy 'test': Error on calling 'OnBarUpdate' method on bar 19: Index was outside the bounds of the array.​

      Comment


        #4
        Here is a slightly modified version, but this too also failed

        namespace NinjaTrader.NinjaScript.Indicators
        {
        public class RMA : Indicator
        {
        private double prevRMA;

        protected override void OnStateChange()
        {
        if (State == State.SetDefaults)
        {
        Description = @"Enter the description for your new custom Indicator here.";
        Name = "RMA";
        IsOverlay = true;
        IsSuspendedWhileInactive = true;
        Period = 14;
        }
        else if (State == State.Configure)
        {
        }

        else if (State == State.DataLoaded)
        {
        prevRMA = 0;
        }
        }

        protected override void OnBarUpdate()
        {

        // Ensure enough bars are available
        if (CurrentBar < Period - 1)
        {
        if (CurrentBar >= 0)
        {
        Value[0] = Input[0]; // Use the first input value
        prevRMA = Input[0]; // Initialize prevRMA
        }
        return;
        }

        // RMA Calculation: RMA = (PrevRMA * (Period - 1) + Input) / Period
        Value[0] = (prevRMA * (Period - 1) + Input[0]) / Period;

        // Update the previous RMA for the next calculation
        prevRMA = Value[0];
        }

        region Properties
        [Range(1, int.MaxValue), NinjaScriptProperty]
        [Display(ResourceType = typeof(Custom.Resource), Name = "Period", GroupName = "NinjaScriptParameters", Order = 0)]
        public int Period
        { get; set; }
        #endregion
        }
        }​

        Comment


          #5
          Hello unpronounceable1700,

          Have you debugged and found that specific line is the problem? The error you provided just says the strategy name so it may be other logic being used or how the indicator is being used in the strategy.

          Comment


            #6
            Originally posted by NinjaTrader_Jesse View Post
            Hello unpronounceable1700,

            Have you debugged and found that specific line is the problem? The error you provided just says the strategy name so it may be other logic being used or how the indicator is being used in the strategy.
            Just a simple

            protected override void OnBarUpdate()
            {
            if (CurrentBars[0] < 30)
            return;

            if (CrossAbove(RMA(9), RMA(21), 1))
            {
            EnterLong();
            }

            if (CrossBelow(RMA(9), RMA(21), 1))
            {
            EnterShort();
            }
            }​

            With this I get the error - Strategy 'test': Error on calling 'OnBarUpdate' method on bar 30: Index was outside the bounds of the array.

            So I assume it has something do with the indicator itself, rather than anything in the strategy.

            Comment


              #7
              Hello unpronounceable1700,

              From the given example I don't see which specific line it may be, have you debigged the strategy or indicator using prints to see which line is having an error?

              Comment


                #8
                SOLVED: This seemed to be the solution

                namespace NinjaTrader.NinjaScript.Indicators
                {
                public class RMA : Indicator
                {
                private double sum; // Running sum for initialization

                protected override void OnStateChange()
                {
                if (State == State.SetDefaults)
                {
                Description = @"Enter the description for your new custom Indicator here.";
                Name = "RMA";
                IsOverlay = true;
                IsSuspendedWhileInactive = true;
                Period = 14;
                AddPlot(Brushes.Goldenrod, "RMA");
                }
                else if (State == State.Configure)
                {
                }
                }

                protected override void OnBarUpdate()
                {
                if (CurrentBar < Period - 1)
                {
                // Accumulate sum for the first full period
                sum += Close[0];
                return;
                }
                else if (CurrentBar == Period - 1)
                {
                // Calculate the initial RMA value
                sum += Close[0];
                Values[0][0] = sum / Period;
                }
                else
                {
                // Rolling RMA calculation
                Values[0][0] = ((Values[0][1] * (Period - 1)) + Close[0]) / Period;
                }
                }

                region Properties
                [Range(1, int.MaxValue), NinjaScriptProperty]
                [Display(ResourceType = typeof(Custom.Resource), Name = "Period", GroupName = "NinjaScriptParameters", Order = 0)]
                public int Period
                { get; set; }
                #endregion
                }
                }​

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                566 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                330 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                101 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                547 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                548 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X