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

Why does my code keep breaking?

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

    Why does my code keep breaking?

    I am working on developing an indicator, starting with a base indicator that works perfectly fine, and the second I use an additional declaration or try to use an array it completely breaks and when debugging in Visual Studio it won't get to OnBarUpdate().

    Any thoughts?

    #2
    Hello cfp462,

    When you are running the indicator in NinjaTrader what error do you get when you try to run it? Note that the error may be in the Log tab of the Control Center?
    JCNinjaTrader Customer Service

    Comment


      #3
      Error on calling 'OnBarUpdate' method for indicator 'nameofindicator' on bar 1: Index was outside the bounds of the array.

      Comment


        #4
        Originally posted by cfp462 View Post
        Error on calling 'OnBarUpdate' method for indicator 'nameofindicator' on bar 1: Index was outside the bounds of the array.
        Sounds like you are accessing a barSeries that you have not added. Unfortunately, the only real way to tell what is happening is by seeing some code.

        Comment


          #5
          //
          // Copyright (C) 2006, NinjaTrader LLC <www.ninjatrader.com>.
          // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
          //

          #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;
          using System.Linq;
          #endregion

          // This namespace holds all indicators and is required. Do not change it.
          namespace NinjaTrader.Indicator
          {
          /// <summary>
          /// The Test4 (Simple Moving Average) is an indicator that shows the average value of a security's price over a period of time.
          /// </summary>
          [Description("")]
          public class Test4 : Indicator
          {
          #region Variables
          private int period = 14;
          private bool draw = true;
          private double x = 0;
          private int i = 10;
          private int[] signals = new int[10];
          private int sum = 0;
          #endregion

          /// <summary>
          /// This method is used to configure the indicator and is called once before any bar data is loaded.
          /// </summary>
          protected override void Initialize()
          {
          Add(new Plot(Color.Orange, "Test4"));
          Overlay = false;
          }

          /// <summary>
          /// Called on each bar update event (incoming tick).
          /// </summary>
          protected override void OnBarUpdate()
          {
          if (CurrentBar == 0)
          Value.Set(0);
          else
          {
          if (FirstTickOfBar)
          draw = false;

          if ('xyz happens; not calling anything crazy here just Close, Open, High etc.')
          {
          draw = true;
          DrawDot("tag1"+CurrentBar, true, 0, Close[0], Color.Lime);

          for(int n=0; n< 10; n++)
          {
          signals[n] = signals[n + 1];
          }
          signals[0] = 1;
          }
          if ('xyz happens; not calling anything crazy here just Close, Open, High etc.')
          {
          draw = true;
          DrawDot("tag1" + CurrentBar, true, 0, Close[0], Color.Red);

          for(int n=0; n< 10; n++)
          signals[n] = signals[n + 1];
          signals[0] = 0;
          }

          sum = signals.Sum();
          Value.Set(sum);

          }
          }

          Comment


            #6
            Originally posted by cfp462 View Post
            //
            // Copyright (C) 2006, NinjaTrader LLC <www.ninjatrader.com>.
            // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
            //

            #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;
            using System.Linq;
            #endregion

            // This namespace holds all indicators and is required. Do not change it.
            namespace NinjaTrader.Indicator
            {
            /// <summary>
            /// The Test4 (Simple Moving Average) is an indicator that shows the average value of a security's price over a period of time.
            /// </summary>
            [Description("")]
            public class Test4 : Indicator
            {
            #region Variables
            private int period = 14;
            private bool draw = true;
            private double x = 0;
            private int i = 10;
            private int[] signals = new int[10];
            private int sum = 0;
            #endregion

            /// <summary>
            /// This method is used to configure the indicator and is called once before any bar data is loaded.
            /// </summary>
            protected override void Initialize()
            {
            Add(new Plot(Color.Orange, "Test4"));
            Overlay = false;
            }

            /// <summary>
            /// Called on each bar update event (incoming tick).
            /// </summary>
            protected override void OnBarUpdate()
            {
            if (CurrentBar == 0)
            Value.Set(0);
            else
            {
            if (FirstTickOfBar)
            draw = false;

            if ('xyz happens; not calling anything crazy here just Close, Open, High etc.')
            {
            draw = true;
            DrawDot("tag1"+CurrentBar, true, 0, Close[0], Color.Lime);

            for(int n=0; n< 10; n++)
            {
            signals[n] = signals[n + 1];
            }
            signals[0] = 1;
            }
            if ('xyz happens; not calling anything crazy here just Close, Open, High etc.')
            {
            draw = true;
            DrawDot("tag1" + CurrentBar, true, 0, Close[0], Color.Red);

            for(int n=0; n< 10; n++)
            signals[n] = signals[n + 1];
            signals[0] = 0;
            }

            sum = signals.Sum();
            Value.Set(sum);

            }
            }
            See what I have emphasized in red. When n==9, which is less than 10, signals[n+1] will translate to signals[10], which is outside the array bounds, which will go from index 0 to 9, not 10, as the array was declared to have 10 members.

            Comment


              #7
              So you're awesome, that definitely fixed the problem of it not running. But for some reason it is only counting between 1 and 0.

              What I was trying to do was have it count the last 10 numbers. Any thoughts?

              Comment


                #8
                Hello cfp462,

                Thanks for your note.

                What does your for loop look like now?

                It appears you were attempting to shift the values for signal back 1.

                This should happen with

                for (int i=1; i<9; i++)
                signal[n] = signal[n+1];

                Let me know if this does not resolve your inquiry.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  I managed to get everything to work, had to run the loop backwards to get the array to work properly but got it all sorted.

                  If anyone else needs help with a similar issue, please feel free to ask and I can lend a hand.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by mjairg, 07-20-2023, 11:57 PM
                  3 responses
                  213 views
                  1 like
                  Last Post PaulMohn  
                  Started by TheWhiteDragon, 01-21-2019, 12:44 PM
                  4 responses
                  544 views
                  0 likes
                  Last Post PaulMohn  
                  Started by GLFX005, Today, 03:23 AM
                  0 responses
                  3 views
                  0 likes
                  Last Post GLFX005
                  by GLFX005
                   
                  Started by XXtrader, Yesterday, 11:30 PM
                  2 responses
                  12 views
                  0 likes
                  Last Post XXtrader  
                  Started by Waxavi, Today, 02:10 AM
                  0 responses
                  7 views
                  0 likes
                  Last Post Waxavi
                  by Waxavi
                   
                  Working...
                  X