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

indicator not showing in range chart

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

    indicator not showing in range chart

    My custom indicator works yesterday, but not today. Then I found out it works on minute charts but not range charts. Got error:

    Error on calling 'OnBarUpdate' method for indicator 'SwingIndex' on bar 0: 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.

    Can anyone help?

    Code as follow:

    #region Variables

    private bool _initialized = false;
    private int dailylimit = 10000;

    #endregion


    protected override void Initialize()
    {
    Add(new Plot(Color.Orange, "SwingIndex"));
    Add(new Line(Color.DarkViolet, 0,""));

    Overlay = false;
    }


    protected override void OnBarUpdate()
    {
    if (!_initialized)
    _initialized = true;

    double R1 = Math.Abs(High[0] - Close[1]);
    double R2 = Math.Abs(Low[0] - Close[1]);
    double R3 = Math.Abs(High[0] - Low[0]);
    double R4 = Math.Abs(Close[1] - Open[1]);
    double K = Math.Max(R1, R2);
    double R ;
    double X ;
    double T = dailylimit;

    if (R1 >= Math.Max(R2, R3))
    {
    R = R1 - R2/2 + R4/4;
    }
    else if (R2 >= Math.Max(R1, R3))
    {
    R = R2 - R1/2 + R4/4;
    }
    else
    {
    R = R3 + R4/4;
    }

    if (CurrentBar == 0)
    Value.Set(Input[0]);

    else
    {
    X = (Close[0] - Close[1]) + (Close[0] - Open[0])/2 + (Close[1] - Open[1])/4;
    Value.Set(50 * (X/R) * (K/T));
    }

    }

    #2
    Welcome to our forums - you likely see this as you're missing a check for having enough CurrentBars at the OnBarUpdate() start :

    BertrandNinjaTrader Customer Service

    Comment


      #3
      Thanks, works fine now!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Rapine Heihei, Today, 08:19 PM
      1 response
      6 views
      0 likes
      Last Post NinjaTrader_Manfred  
      Started by Rapine Heihei, Today, 08:25 PM
      0 responses
      5 views
      0 likes
      Last Post Rapine Heihei  
      Started by f.saeidi, Today, 08:01 PM
      1 response
      4 views
      0 likes
      Last Post NinjaTrader_Manfred  
      Started by Rapine Heihei, Today, 07:51 PM
      0 responses
      6 views
      0 likes
      Last Post Rapine Heihei  
      Started by frslvr, 04-11-2024, 07:26 AM
      5 responses
      98 views
      1 like
      Last Post caryc123  
      Working...
      X