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 on calling 'OnBarUpdate' method for indicator on bar 0: object reference....

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

    error on calling 'OnBarUpdate' method for indicator on bar 0: object reference....

    Hi support!

    First off, a big thanks to your efforts on this forum, without your responses to the various questions my automated strategy development wouldn't have got off the ground, so thanks! Compared to the likes of metatrader, you are seriously good!

    So to my issue: I'm trying to write a custom indicator, and whenever I attach it to a chart I get the error: "Error on calling 'OnBarUpdate' method for indicator 'CustomRSI' on bar 0: Object reference not set to an instance of an object."

    The code is simply declaring a two-dimensional array, and then filling it with OHLC data. If this is causing the error, I'd love to know a better way to do this. I'm a relatively experienced C++ programmer, and have done a course in C#, but it's fair to say I'm new to ninjatrader.

    I've tried a 'try catch' setup but don't seem to get anything out of it when it runs.

    Code posted below:

    namespace NinjaTrader.Indicator
    {
    /// <summary>
    /// CustomRSI indicator to feed master indicator
    /// </summary>
    [Description("CustomRSI indicator to feed master indicator")]
    public class CustomRSI : Indicator
    {
    #region Variables
    // Wizard generated variables
    private int rSI_Buy_Variable = 1; // Default setting for RSI_Buy_Variable

    double[,] afMultiVarCalculationArray = new double[1000,221];

    int CalculationStartingPoint= 1000;
    int NumberOfMultiCalcArrayCols = 221;


    // User defined variables (add any user defined variables below)
    #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.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
    Overlay = false;

    for(int i=0;i<CalculationStartingPoint;i++)
    {
    for(int j=0;j<NumberOfMultiCalcArrayCols;j++)
    {
    afMultiVarCalculationArray[i,j] = 0;
    }
    }

    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    if(CurrentBar < CalculationStartingPoint)
    {
    Print("Not enough bars to calculate");
    return;
    }
    int first;
    first=CurrentBar - CalculationStartingPoint -1; // start index for all the bar, the "-1" converts to array position

    for(int i=0;i<CalculationStartingPoint;i++)
    {
    afMultiVarCalculationArray[i,1]=(float)Close[first+i];
    afMultiVarCalculationArray[i,2]=(float)High[first+i];
    afMultiVarCalculationArray[i,3]=(float)Low[first+i];
    afMultiVarCalculationArray[i,4]=(float)Open[first+i];
    afMultiVarCalculationArray[i,5]=(float)Volume[first+i];
    }
    for(int i=0;i<CalculationStartingPoint;i++)
    for(int j=7;j<NumberOfMultiCalcArrayCols;j++)
    afMultiVarCalculationArray[i,j]=0;

    // Use this method for calculating your indicator values. Assign a value to each
    // plot below by replacing 'Close[0]' with your own formula.
    Plot0.Set(Close[0]);
    }

    #region Properties
    [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
    [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
    public DataSeries Plot0
    {
    get { return Values[0]; }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public int RSI_Buy_Variable
    {
    get { return rSI_Buy_Variable; }
    set { rSI_Buy_Variable = Math.Max(1, value); }
    }
    #endregion
    }
    }

    #2
    Welcome to our forums and thanks for the kind words - I have given your script a quick run here on my ES charts, but weren't able to see the issue with accessing the nonvalid object reference. Which instruments / charts were you seeing this on please?

    I can see this error only if your needed for check for enough bars is removed...could this have just been an older log error potentially?
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Hi there,

      Thanks for the prompt response support! Good work!

      You're right, I was running an older version of the code without it - thanks v.much for confirming, I'm getting on fine now.

      Keep up the great work!!

      Cheers!

      Comment


        #4
        Hi Support,

        I got similar error message in NT7:

        Error on calling 'OnBarUpdate' method for indicator 'xx' on bar 2: Index is out or range.

        Any idea what might be an issue?

        Thank you

        Miroslav

        Comment


          #5
          Originally posted by miroslav View Post
          Hi Support,

          I got similar error message in NT7:

          Error on calling 'OnBarUpdate' method for indicator 'xx' on bar 2: Index is out or range.

          Any idea what might be an issue?

          Thank you

          Miroslav
          Not without seeing what you are trying to access with an index.

          Comment


            #6
            Originally posted by koganam View Post
            Not without seeing what you are trying to access with an index.
            Hello,

            here is it.

            thank you for your help

            Miroslav
            Attached Files

            Comment


              #7
              The trouble is probably not enough bars available for the indicators to do their calculations.

              Try adding this code to the beginning of your OnBarUpdate(),

              Code:
              if (CurrentBar < this.bandPeriod)
                  return;
              Why check against "bandPeriod"?
              Because this looks to be the highest 'period' argument being passed into the various SMA calls.

              Comment


                #8
                sHello bltdavid,

                I have tried what you proposed and it does not help too much. Error message remains the same but instead of bar 2 now it is bar 45.

                Thank you

                Miroslav

                Comment


                  #9
                  Hello miroslav,


                  Can you please send me your log and trace files from within NinjaTrader. Go to Control Center-->Help-->Mail to Support in NinjaTrader. Put 'Att Shawn', in the subject field and enable 'Log and trace files'. Also, give a reference of this thread in the body of the email.
                  Shawn B.NinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by mmenigma, Today, 02:22 PM
                  0 responses
                  1 view
                  0 likes
                  Last Post mmenigma  
                  Started by frankthearm, Today, 09:08 AM
                  9 responses
                  34 views
                  0 likes
                  Last Post NinjaTrader_Clayton  
                  Started by NRITV, Today, 01:15 PM
                  2 responses
                  9 views
                  0 likes
                  Last Post NRITV
                  by NRITV
                   
                  Started by maybeimnotrader, Yesterday, 05:46 PM
                  5 responses
                  26 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Started by quantismo, Yesterday, 05:13 PM
                  2 responses
                  20 views
                  0 likes
                  Last Post quantismo  
                  Working...
                  X