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

Help with this error on indicator

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

    Help with this error on indicator

    I am trying to convert this script from TOS to NT8 and I keep getting this error -
    "" Error on calling 'OnBarUpdate' method on bar 50: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart. "

    Things I have tried -
    1. OnBarUpdate - check currentbar < 502 return
    2. Print statements

    I check the currentbar == 50 and I try to initialize the dataseries gk_sum in the NadaryaSmoothed function and I get this error. I am not sure what I am doing wrong. It keeps complaining that there are 4 bars even when I am checking for currentbar as 50. I am attaching my code here.

    Please help! BB.zip

    #2
    Hello Graci117,

    Thanks for your post.

    "Error on calling 'OnBarUpdate' method on bar 50: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart. "

    This error message indicates that you are accessing an invalid index.

    Note that "accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart. "​ is just an example of what could be happening. It does not mean this is the actual issue with the code in the script.

    If the "LongPeriod" of the indicator you are trying to access is 100, this means you would need to load at least 100 bars on the chart and add a CurrentBar check to ensure that at least 100 bars have processed before the script can calculate logic.

    Further, you are trying to assign the indicator values to custom Series<double> objects in the script. You must use a BarsAgo on the custom Series<double> when assigning a value to it.

    For example: smoothed_bolu_1[0] = X, where X is the value you are assigning to the Series<double> object.

    Series<double>: https://ninjatrader.com/support/help...t8/seriest.htm

    You may also need to use a BarsAgo when accessing the indicator value also, such as NadarayaSmoothed(Bollinger (Close, ShortStdev, ShortPeriod).Upper, SmoothingFactor)[0]

    You will ultimately need to debug your script further by adding more prints that print out the all the values you are acessing and the index to understand exaclty what is causing the error to occur in your script. If the index is a BarsAgo, the value you are accessing must be less than or equal to CurrentBar.

    Below is a link to a forum post that demonstrates how to use prints to understand behavior.
    https://ninjatrader.com/support/foru...121#post791121
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Thanks for your response. Upon debugging it futher and changing the code I found that the smoothed[i] after i >256 is throwing the error.


      private Series<double> NadarayaSmoothed(Series<double> src, double h)
      {
      int n = 500;
      double curBar = Math.Abs(CurrentBar);
      double den = 2 * Math.Pow(h, 2);
      double[] gk_sum = new double[n];
      double p = 0.0; // Initialize p here
      // List<double> smoothed = new List<double>();

      Series<double> smoothed = new Series<double>(this);
      double q = 0.0; // Initialize q here




      for (int i = 0; i < n; i++)
      {

      if (i == 0)
      {

      p += Math.Exp(-Math.Pow(i, 2) / den);


      gk_sum[i] = p;
      //Print( gk_sum[i] + "--cusssr");


      }
      else
      {
      //Print(curBar + "--cusssr");
      gk_sum[i] = gk_sum[i - 1];
      //Print(i + "--cur1");
      //Print( gk_sum[i] + "--ssss");
      }
      if (CurrentBar == 502)
      {
      Print( i + "--iiiiii"); //prints i as 256
      Print( gk_sum[i] + "--ssss");
      }


      q += src[i] * Math.Exp(-Math.Pow(i, 2) / den) / gk_sum[i];
      Print("test1"); //prints until i is 256
      smoothed[i] = q;
      }


      return smoothed;
      }​

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by LiamTwine, Today, 08:10 AM
      0 responses
      2 views
      0 likes
      Last Post LiamTwine  
      Started by Balage0922, Today, 07:38 AM
      0 responses
      5 views
      0 likes
      Last Post Balage0922  
      Started by JoMoon2024, Today, 06:56 AM
      0 responses
      6 views
      0 likes
      Last Post JoMoon2024  
      Started by Haiasi, 04-25-2024, 06:53 PM
      2 responses
      19 views
      0 likes
      Last Post Massinisa  
      Started by Creamers, Today, 05:32 AM
      0 responses
      6 views
      0 likes
      Last Post Creamers  
      Working...
      X