Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help with custom indicator: Index was outside the bounds of the array.

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

    Help with custom indicator: Index was outside the bounds of the array.

    I am trying to develop my first custom indicator but I'm definitely not understanding how the internals are constructed. I'm getting " Error on calling 'OnBarUpdate' method on bar 1: Index was outside the bounds of the array." Let me show some code. Note that I'm trying a basic toy indicator so I can get it working before elaborating on it.

    Code:
     
    public class OpenMinusCloseIndicator : Indicator
        {
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Calculates the Open - Close";
                    Name                                        = "OpenMinusCloseIndicator";
                    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;
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (CurrentBar < 0)
                {
                    return;
                }
    
                OpenMinusClose[0] = Open[0] - Close[0];
            }
    
            #region Properties
            [Browsable(false)]
            [XmlIgnore()]
            public Series<double> OpenMinusClose
            {
                get { 
                    // EXCEPTION IS HERE on the first access in my strategy
                    return Values[0]; 
                }
            }
            #endregion
        }
    In my strategy OnBarUpdate I'm calling it like so:

    Code:
    // First access to this in my strategy produces the error commented above
    Print("OpenMinusClose: " + openMinusCloseIndicator.OpenMinusClose[0]);
    The indicator was instantiated in the State.DataLoaded:

    Code:
        openMinusCloseIndicator = OpenMinusCloseIndicator();

    Here are my questions and confusions:
    * What does Values in the indicator refer to. Yes it refers to a series, but how is the data populated I can't find it in the docs? What if I want multiple indexes with different values to Values, like Values[0] and Values[1]. What exactly determines the order in which those Values indexes are set in the indicator?
    * For example if I wanted to add CloseMinusOpen in addition to OpenMinusClose, how do I know that refers to index 0 or 1 into Values?

    Again this is just a test to help me wrap my mind around having multiple calculations within an indicator. I've studied the RSI.cs indicator as a reference, and the Default[0] and Avg[0] are being set, and I seem to be copying how it is doing that, but still getting an exception, please help!

    #2
    Hello transparenttrader,

    A custom series cannot be mixed with an indicator.

    If this is mean to call an existing indicator, declare this variable as the type of that indicator.

    private OpenMinusCloseIndicator openMinusCloseIndicator;

    And initiate an instance in OnStateChange() when State is State.DataLoaded.
    openMinusCloseIndicator = OpenMinusCloseIndicator();


    If you want a custom series, then use this as a custom series and initiate a custom series when State is State.DataLoaded.
    https://ninjatrader.com/support/help...t8/seriest.htm

    [Browsable(false)]
    [XmlIgnore()]
    public Series<double> OpenMinusClose
    { get; set; }

    OpenMinusClose = new Series<double>(this, MaximumBarsLookBack.Infinite);


    If you want to add a plot to use Value this would be with AddPlot() in State.SetDefaults or State.Configure.
    https://ninjatrader.com/support/help...t8/addplot.htm
    https://ninjatrader.com/support/help...nt8/values.htm

    AddPlot(Brushes.Blue, "OpenMinusClose");

    [Browsable(false)]
    [XmlIgnore()]
    public Series<double> OpenMinusClose
    { get { return Values[0]; } }
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      This is super helpful and it unblocked me, thank you so much!

      I do have a follow up question. My biggest goal is to be able to display the candlestick values for the indicator in the DataBox, so I can do that now, thanks to the AddPlot.

      Here is a follow up question. Can I have my indicator values shown in the DataBox but not actually plotted under the chart? It seems like the AddPlot is what get's it to show up in the DataBlox, however I want to only see it in the DataBox and not in the plot. My use case is I want to debug through candle by candle with the DataBox opwn but not have too many chart indicators plotted in the main view which vertically compresses my view.

      I have the following variables set, however, the value is still being plotted:

      Code:
                      // If IsOverlay is true it will render within the main chart panel
                      IsOverlay                                    = false;
                      // Renders in the databox opened by ctrl+d
                      DisplayInDataBox                            = true;
                      DrawOnPricePanel                            = false;
                      // Not sure what setting this to false does, doesn't appear to change anything
                      DrawHorizontalGridLines                        = false;
                      // Not sure what setting this to false does, doesn't appear to change anything
                      DrawVerticalGridLines                        = false;
                      // Not sure what setting this to false does, doesn't appear to change anything
                      PaintPriceMarkers                            = false;

      Comment


        #4
        I can set AddPlot(Brushes.Transparent, "OpenMinusClose"); and ShowTransparentPlotsInDataBox = true; however the indicator is still plotted (with no visible line)

        Comment


          #5
          Hello transparenttrader,

          Thanks for your replies.

          DrawOnPricePanel - is used to determine if the Draw objects created by the script would be shown on the price panel or the indicator panel. This would not relate to plots.
          Reference: https://ninjatrader.com/support/help...pricepanel.htm

          DrawHorizontalGridLines and DrawVerticalGridLines - determines if the gridlines in the chart should be drawn, by default they typically are. This would not relate to plots.
          Refreences: https://ninjatrader.com/support/help...lgridlines.htm https://ninjatrader.com/support/help...lgridlines.htm

          PaintPriceMarkers - determines if the price marker for each plot should be shown on the price column of the chart, this does relate to plots. Setting it to false will prevent the plots price marker from showing up on the price column and would continue to show the plot itself in the price panel.
          Reference: https://ninjatrader.com/support/help...icemarkers.htm

          Your last post about using a transparent plot is what I would recommend for your goal of "Can I have my indicator values shown in the DataBox but not actually plotted under the chart? " A transparent plot does not change the charts display and does provide a value that can be accessed in the data box. Does the indicator have conditions that are coloring the plot even though you has created it as Transparant? PlotBrushes[][] would be the method that would color the plot different colors.
          Last edited by NinjaTrader_PaulH; 05-08-2020, 07:59 AM. Reason: added further clarification on PaintPriceMarkers.
          Paul H.NinjaTrader Customer Service

          Comment


            #6
            What I really want is for my indicator value(s) to show up in the DataBox but *not* to be plotted. Is that possible?

            Comment


              #7
              Hello,

              Thanks for your reply.

              Here is a short video that demonstrates that by setting the plot transparent and setting ShowTransparentPlotsInDataBox to true in the indicator, that you do see the indicator values in the databox but you do not see the plot itself on the chart and the chart display is not changed by the transparent plot.

              https://paul-ninjatrader.tinytake.co...MV8xMzQzMjY1OA

              Paul H.NinjaTrader Customer Service

              Comment


                #8
                Brilliant, that did the trick, so for future reference, my issue was I had `IsOverlay = false` which forced the Plot to show below the chart. When I flipped that back to true combined with setting `ShowTransparentPlotsInDataBox = true` then the plot below the chart no longer renders, and the values now show in the DataBox with `DisplayInDataBox = true` which is exactly what I want.

                You all in the NinjaTrader Customer Service are seriously amazing, thank you!

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by forrestang, Yesterday, 02:18 PM
                1 response
                23 views
                0 likes
                Last Post NinjaTrader_Jason  
                Started by bdelorm, Yesterday, 11:13 AM
                1 response
                38 views
                0 likes
                Last Post NinjaTrader_Jason  
                Started by markyb0y, Yesterday, 10:04 AM
                1 response
                14 views
                0 likes
                Last Post NinjaTrader_Jason  
                Started by pjptrading, Yesterday, 08:17 AM
                1 response
                15 views
                0 likes
                Last Post NinjaTrader_Jason  
                Started by robertparlsa, Today, 12:03 AM
                0 responses
                5 views
                0 likes
                Last Post robertparlsa  
                Working...
                X