• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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 Atilla, Today, 04:05 PM
                  0 responses
                  14 views
                  0 likes
                  Last Post Atilla
                  by Atilla
                   
                  Started by Displacer, Today, 02:57 PM
                  2 responses
                  13 views
                  0 likes
                  Last Post Displacer  
                  Started by SAILIKITY, 05-22-2019, 05:41 PM
                  1 response
                  9 views
                  0 likes
                  Last Post NinjaTrader_PaulH  
                  Started by rmillar, 01-09-2019, 06:02 PM
                  6 responses
                  74 views
                  1 like
                  Last Post eDanny
                  by eDanny
                   
                  Started by mlarocco, Today, 11:39 AM
                  4 responses
                  23 views
                  0 likes
                  Last Post mlarocco  
                  Working...
                  X