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

Indexing problems

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

    Indexing problems

    So I have been searching the forum for a couple days on this topic and the problems I am having but cant seem to find a solution. I am try to find the average of the Rate of Change indicator over a certain period and on Two different data series at the same time. It complies but every time I run it I get the" Error on calling 'OnBarUpdate' method for indicator 'test' on bar 2: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart."

    this is my code can you see any problems:

    #region Variables
    // Wizard generated variables
    private int roclength = 50; // Default setting for MyInput0
    private int actroclength = 50;
    private double sum = 0;
    private double sum2 = 0;
    private double avg = 0;
    private double avg2 = 0;
    private double num = 0;
    private double dom = 0;
    private double dom2 = 0;
    private double dom3 = 0;
    private double dom4 = 0;


    // 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("NQ 12-17", PeriodType.Minute, 1);
    Add("ES 12-17", PeriodType.Minute, 1);
    Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
    Overlay = false;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Use this method for calculating your indicator values. Assign a value to each
    // plot below by replacing 'Close[0]' with your own formula.
    if (CurrentBars[0] <= BarsRequired || CurrentBars[1] <= BarsRequired || CurrentBars[2] <= BarsRequired || CurrentBars[3] <= BarsRequired)
    return;

    /*if (CurrentBar < 2)
    {
    return;
    }*/

    double[] roc = new double[roclength];
    double[] roc2 = new double[roclength];
    double[] corr = new double[roclength];
    double[] corr2 = new double[roclength];

    for(int i = roclength; i > 0; i--)
    {
    roc[i] = ROC(BarsArray[1],actroclength)[i];
    roc2[i] = ROC(BarsArray[2], actroclength)[i];
    }

    for(int j = roclength; j > 0; j--)
    {
    sum += roc[j];
    sum2 += roc2[j];
    }

    avg = sum/roclength;
    avg2 = sum2/roclength;

    Plot0.Set(avg);
    }

    #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 Roclength
    {
    get { return roclength; }
    set { roclength = Math.Max(1, value); }
    }

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

    #2
    Hello bradc343,

    Thank you for your note.

    You should include a current bar check for the additional data series.

    For example if you add the following to the top of your code, does your script run as expected?

    Code:
    if(CurrentBars[0]<10 || CurrentBars[1] < 10 || CurrentBars[2] <10) return;
    You would want to see the following link for more information,


    I look forward to your reply.
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Thanks for advice

      Thnak you so much for your help but I am still having problems. I have replaced my line of code with: if(CurrentBars[0]<10 || CurrentBars[1] < 10 || CurrentBars[2] <10)
      that didnt work for me I still got the error: Index was outside the bounds of the array.
      No matter what number I put in there I got the same error. I tried 50, 200 and 400. I even tried replacing if(CurrentBars[0]<10 || CurrentBars[1] < 10 || CurrentBars[2] <10) with if (CurrentBar < 50){return;} that didnt work. I got the same error. I am not giving up on this code I need to learn how to solve this problem. Is there something else I am doing wrong? Am I initializing something wrong or calling the rsi data wrong or do I need to use if (BarsInProgress == 1) line f code..

      Thank you so much in advance

      Comment


        #4
        Hello bradc343,

        Thank you for your note.

        Without the full code we're unable to test on our end.

        If you'd like to upload the full code I can take a look and see if anything jumps out. Or if you'd prefer to email a copy, send to platformsupport[at]ninjatrader[dot]com with Attn: Alan P in the Subject line. Also within the email please include a link to this thread, and the files.

        I look forward to your reply.
        Alan P.NinjaTrader Customer Service

        Comment


          #5
          Accessing 50 bars back (roclength) in a loop is not a very good idea until you have at least 50 bars to access..
          eDanny
          NinjaTrader Ecosystem Vendor - Integrity Traders

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by jclose, Today, 09:37 PM
          0 responses
          4 views
          0 likes
          Last Post jclose
          by jclose
           
          Started by WeyldFalcon, 08-07-2020, 06:13 AM
          10 responses
          1,413 views
          0 likes
          Last Post Traderontheroad  
          Started by firefoxforum12, Today, 08:53 PM
          0 responses
          9 views
          0 likes
          Last Post firefoxforum12  
          Started by stafe, Today, 08:34 PM
          0 responses
          10 views
          0 likes
          Last Post stafe
          by stafe
           
          Started by sastrades, 01-31-2024, 10:19 PM
          11 responses
          169 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Working...
          X