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

error 'onbarupdate'

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

    error 'onbarupdate'

    Hi!
    i'm receiving the following error message:
    "Error on calling 'OnBarUppdate' method on bar1: you are accessing an index with a value that is invalid since it is out of range..."
    and:
    for the indicator i'm using in my indicator i receive the error: "Error on calling 'OnBarUpdate' method on bar 7: Object referece not set to an instance of an object..."
    this message appears 1 second earlier.

    i'm running this indicator in 1M timeframe and it's working fine.
    using it in volume 1000 chart, the error messages appear.

    what can i do?

    thank you very much!

    #2
    Hi Tradexxx, thanks for your post.

    This usually happens when you are trying to access bars that have not formed yet. For example, if you are looking back 3 bars on the first OnBarUpdate call, add this to the top of OnBarUpdate:

    if(CurrentBar < 3) return;

    Please let me know if I can assist any further.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      hi chris,
      thank you for your post

      i'm using:

      if (CurrentBars[0] < 0
      || CurrentBars[1] < 0)
      return;

      so in case the lookback here is set to 256 (see attached screen) i have to set 256 , instead of "0"?

      like:
      if (CurrentBars[0] < 256
      || CurrentBars[1] < 256)
      return;


      where does the 256 comes from?
      cannot remember having set this anywhere....
      thank you

      Comment


        #4
        Hi Tradexxx, thanks for your reply.

        The Max bars lookback refers to how far back any series in the script will look, this doesn't have to do with the logic in your script (unless your script is indeed looking 256 bars back in an array). Can you post your OnStateChanged and OnBarupdate methods so I can have a look?

        I look forward to hearing from you.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          protected override void OnStateChange()
          {
          if (State == State.SetDefaults)
          {
          Description = @"MyVol_Short.";
          Name = "MyVolShort";
          Calculate = Calculate.OnBarClose;
          IsOverlay = true;
          DisplayInDataBox = true;
          DrawOnPricePanel = true;
          DrawHorizontalGridLines = true;
          DrawVerticalGridLines = true;
          PaintPriceMarkers = true;
          ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
          //Disable this property if your indicator requires custom values that cumulate with each new market data event.
          //See Help Guide for additional information.
          IsSuspendedWhileInactive = true;
          }
          else if (State == State.Configure)
          {
          AddDataSeries(Data.BarsPeriodType.Volume, 2000);
          }

          else if (State == State.DataLoaded)
          {
          PriceActionSwingOscillator1 = PriceActionSwingOscillator(Closes[1], SwingStyle.Standard, 9, 10, false, Show.Trend, true, true, true);

          PriceActionSwingOscillator3 = PriceActionSwingOscillator(Close, SwingStyle.Standard, 2, 10, false, Show.Trend, true, true, true);

          }

          Comment


            #6
            hi chris,

            thank you for your post, above you'll find the code
            (i wasn't able to write comment...)
            Last edited by Tradexxx; 04-17-2020, 11:26 PM.

            Comment


              #7
              Hi Tradexxx, thanks for posting that.

              It looks like the problem is coming from one of these two lines:

              double last_Ten_Bar_High = MIN(Low, 7)[1];
              Draw.Diamond(this, @"PAS Dia_2 " + Convert.ToString(CurrentBars[0]), false, 0, last_Ten_Bar_High, Brushes.Blue);

              Change your CurrentBars checks to check for at least 10 bars before processing this.

              e.g.

              if (CurrentBars[0] < 10
              || CurrentBars[1] < 10)
              return;

              Please let me know if I can assist any further.
              Chris L.NinjaTrader Customer Service

              Comment


                #8
                Hi Chris,
                when i set it to 10, the error message is "Error on calling 'onBarUpdate' method on bar 11: You are accessing an index with a value that is invalid since it is out of range..."

                when i set it to "if (CurrentBar == 0) return;" the error message is "Error on calling 'onBarUpdate' method on bar 11: You are accessing an index with a value that is invalid since it is out of range..."

                any other ideas what could be done?
                thank you

                Comment


                  #9
                  Hi Tradexxx, thanks for your reply.

                  I think the indicator is still reaching an invalid array index on when CurrentBar == 11. What happens if you increase the current bar check number by a large amount like 50 or 100? Also make sure you are checking both of the CurrentBars indexes e.g.

                  if (CurrentBars[0] < 50
                  || CurrentBars[1] < 50)
                  return;
                  Chris L.NinjaTrader Customer Service

                  Comment


                    #10
                    hi chris!
                    i have tried 50 and 100 and also 500...
                    result each time:
                    "...error on calling 'onbarupdate' method on bar 2..."

                    :-(

                    Comment


                      #11
                      Hello Tradexxx,

                      This error could come from a Series reference or an array reference.

                      If you add prints to identify the specific line that throws the error, it should be clearer what the invalid reference is so you can correct it. If it is not clear, you could post that specific line of code that is throwing the error and we could give further insight.

                      Debugging tips - https://ninjatrader.com/support/help...script_cod.htm

                      We look forward to assisting.
                      JimNinjaTrader Customer Service

                      Comment


                        #12
                        Hi Jim,
                        thank you for the information.

                        is it possible to specify the information given in the message field of the control center a bit more?

                        running some windows with the same indicator (or using the market analyzer) makes it difficult to recognize the one window producing the error ...


                        thank you

                        Comment


                          #13
                          Hi Tradexxx, thanks for the reply.

                          Visual Studio debugging can halt a script at the line causing the problem when an exception occurs. We have a guide here on how to enable debug mode and attach Visual Studio to the NinjaTrader process:



                          Please let me know if I can assist any further.
                          Chris L.NinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by ETFVoyageur, 05-07-2024, 07:05 PM
                          16 responses
                          126 views
                          0 likes
                          Last Post bltdavid  
                          Started by ETFVoyageur, Today, 10:13 PM
                          1 response
                          5 views
                          0 likes
                          Last Post ETFVoyageur  
                          Started by somethingcomplex, Today, 10:36 PM
                          0 responses
                          3 views
                          0 likes
                          Last Post somethingcomplex  
                          Started by sofortune, Yesterday, 10:28 AM
                          5 responses
                          22 views
                          0 likes
                          Last Post sofortune  
                          Started by guyonabuffalo, Today, 10:01 PM
                          0 responses
                          2 views
                          0 likes
                          Last Post guyonabuffalo  
                          Working...
                          X