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 a simple indicator

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

    Error on a simple indicator

    I have this simple indicator where I am trying to modify HMA. I keep getting error "Error on calling 'OnBarUpdate' method on bar 1: Index was outside the bounds of the array." I tried increasing the first line onbarupdate to a very high number and I keep getting the error. What am I doing wrong?

    I understand I can do a simple multiplication on length in the input, but this is just the start of my code and I can't get past that.


    public class HMASuite : Indicator
    {
    HMA hullMAMult;
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Indicator here.";
    Name = "HMASuite";
    Calculate = Calculate.OnEachTick;
    IsOverlay = false;
    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;
    Length = 60;
    Multiplier = 3;
    AddLine(Brushes.LightSalmon, 4, "HullMultiplier");
    }
    else if (State == State.Configure)
    {

    }
    else if (State == State.DataLoaded)
    {
    hullMAMult = HMA(Close, Length * Multiplier);
    }
    }

    protected override void OnBarUpdate()
    {
    //Add your custom indicator logic here.

    if (CurrentBar < 1)
    return;

    Value[0] = hullMAMult[0];

    Print("Test");
    //= hullMAMult[0];
    if(IsRising(Value)) {PlotBrushes[0][0] = Brushes.Green;}
    else if(IsFalling(Value)) {PlotBrushes[0][0] = Brushes.Red;}
    else {PlotBrushes[0][0] = Brushes.Yellow;}
    }

    Last edited by Graci117; 12-31-2023, 05:18 PM.

    #2
    HelloGraci117,

    Thanks for your post.

    This error message means an invalid index was used.

    With the error:

    "Error on calling 'OnBarUpdate' method on bar 0: 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 message indicates an invalid index was used as well.

    As one example, calling Close[1] when CurrentBar is 0 (the very first bar) will cause this error.

    The reason for this is because there will not be a bar ago; essentially there is not enough data at bar 0 look back one bar.

    The script will start from the far left of the chart at the very beginning where the CurrentBar will equal 0 and then start working to the right, calling OnBarUpdate() for each bar.
    In this specific example, it would be needed to wait for the second bar to be built (from all series if there are multiple series added), to ensure that you have enough data to call an index 1 bar ago.

    Below is a link to the help guide on this specific example of an indexing error.

    Make sure you have enough bars: https://ninjatrader.com/support/help...+enough​

    Ultimately, you would need to make sure that there are enough bars in the data series for the indexes being used in your script. For example, if you are using an HMA period of Length*Multiplier, you should make sure you have enough bars on the chart to access this period.

    Also, below are links to the help guide on CurrentBar and CurrentBars.

    CurrentBar: https://ninjatrader.com/support/help...currentbar.htm
    CurrentBars: https://ninjatrader.com/support/help...urrentbars.htm

    Last edited by NinjaTrader_BrandonH; 12-31-2023, 06:23 PM.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_BrandonH View Post
      HelloGraci117,

      Thanks for your post.

      This error message means an invalid index was used.

      With the error:

      "Error on calling 'OnBarUpdate' method on bar 0: 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 message indicates an invalid index was used as well.

      As one example, calling Close[1] when CurrentBar is 0 (the very first bar) will cause this error.

      The reason for this is because there will not be a bar ago; essentially there is not enough data at bar 0 look back one bar.

      The script will start from the far left of the chart at the very beginning where the CurrentBar will equal 0 and then start working to the right, calling OnBarUpdate() for each bar.
      In this specific example, it would be needed to wait for the second bar to be built (from all series if there are multiple series added), to ensure that you have enough data to call an index 1 bar ago.

      Below is a link to the help guide on this specific example of an indexing error.
      https://ninjatrader.com/support/help...nough_bars.htm

      Ultimately, you would need to make sure that there are enough bars in the data series for the indexes being used in your script. For example, if you are using an HMA period of Length*Multiplier, you should make sure you have enough bars on the chart to access this period.

      Also, below are links to the help guide on CurrentBar and CurrentBars.
      http://ninjatrader.com/support/helpG...currentbar.htm
      https://ninjatrader.com/support/help...urrentbars.htm
      Thank you Brandon. I have tried setting current bar to 300 and still get an error with the length of 60.

      if (CurrentBar < 300)
      return;

      BTW none of the links you provided work.​

      Comment


        #4
        Hello Graci117,

        Thanks for your notes.

        I see you are calling PlotBrushes in your script but you are not adding a plot to the script using the AddPlot() method.

        Since you are adding a line to the script using AddLine(), you would have to use Lines[0].Brush to change the color of a line.

        Further, when calling AddLine(), you cannot use Value[0] to assign a value to a line. Value[0] would only be used for plots.

        Lines[0] would need to be used since you are adding a line to the script, not a plot.

        See the help guide pages below for more information.

        AddLine(): https://ninjatrader.com/support/help...t8/addline.htm
        Lines: https://ninjatrader.com/support/help.../nt8/lines.htm

        That said, I will re-add those help guide links to post # 2.
        Last edited by NinjaTrader_BrandonH; 12-31-2023, 06:26 PM.
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_BrandonH View Post
          Hello Graci117,

          Thanks for your notes.

          I see you are calling PlotBrushes in your script but you are not adding a plot to the script using the AddPlot() method.

          Since you are adding a line to the script using AddLine(), you would have to use Lines[0].Brush to change the color of a line.

          Further, when calling AddLine(), you cannot use Value[0] to assign a value to a line. Value[0] would only be used for plots.

          Lines[0] would need to be used since you are adding a line to the script, not a plot.

          See the help guide pages below for more information.

          AddLine(): https://ninjatrader.com/support/help...t8/addline.htm
          Lines: https://ninjatrader.com/support/help.../nt8/lines.htm

          That said, I will re-add those help guide links to post # 2.
          Thanks so much!! That worked!!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by fx.practic, 10-15-2013, 12:53 AM
          5 responses
          5,403 views
          0 likes
          Last Post Bidder
          by Bidder
           
          Started by Shai Samuel, 07-02-2022, 02:46 PM
          4 responses
          94 views
          0 likes
          Last Post Bidder
          by Bidder
           
          Started by DJ888, Yesterday, 10:57 PM
          0 responses
          6 views
          0 likes
          Last Post DJ888
          by DJ888
           
          Started by MacDad, 02-25-2024, 11:48 PM
          7 responses
          158 views
          0 likes
          Last Post loganjarosz123  
          Started by Belfortbucks, Yesterday, 09:29 PM
          0 responses
          8 views
          0 likes
          Last Post Belfortbucks  
          Working...
          X