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

Problem creating Indicator

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

    Problem creating Indicator

    I have spent a long time trying to make the following code work:

    protected override void Initialize()
    {
    Add(new Plot(Color.Orange, PlotStyle.Line, "ER"));
    ATRperiod = 21;
    ClosePeriod = 21;
    CalculateOnBarClose = true;
    Overlay = false;
    PriceTypeSupported = true;
    }

    /// <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.

    Print(Time[0].ToString());
    double ATRvalue = ATR(ATRperiod)[0];
    Print("current ATR value is" + ATRvalue.ToString());
    Print (Close[0]);
    Plot0.Set (Close[0]);
    // Print (Close[ClosePeriod]);
    // Closediff = Math.Abs (Close[0]- Close[ClosePeriod]);
    // Print("current Closediff is" + Closediff.ToString());
    // double ER = Math.Abs (Closediff / ATRvalue);
    // Plot0.Set (ER);
    // Print("ER" + ER.ToString());

    The ATR function works fine; but If i use any value for Close[barsago] other than "0" i.e. current bar, the code won't work and I get this meassage in the log:

    Error on calling the 'OnBarUpdate' method for indicator 'ER' on bar 0: Index was out of range. Must be non-negative and less than the size of the collection.

    I would be embarassed to admit how long I have spent on this without figuring out the problem

    please help?

    #2
    imported post

    Nick09,

    The problem is that you passing in an index value representing the number of bars ago where the index value is trying to access a bar that does not yet exist.

    I would add the following codeat the beginning of OnBarUpdate()

    if (CurrentBar < ClosePeriod || CurrentBar < ATRperiod)
    return;
    The above will check to make sure that there are enough bars to calculate against.

    Ray
    RayNinjaTrader Customer Service

    Comment


      #3
      imported post

      Thanks Ray, that did the trick!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by TradeForge, Today, 02:09 AM
      0 responses
      0 views
      0 likes
      Last Post TradeForge  
      Started by Waxavi, Today, 02:00 AM
      0 responses
      2 views
      0 likes
      Last Post Waxavi
      by Waxavi
       
      Started by elirion, Today, 01:36 AM
      0 responses
      4 views
      0 likes
      Last Post elirion
      by elirion
       
      Started by gentlebenthebear, Today, 01:30 AM
      0 responses
      4 views
      0 likes
      Last Post gentlebenthebear  
      Started by samish18, Yesterday, 08:31 AM
      2 responses
      9 views
      0 likes
      Last Post elirion
      by elirion
       
      Working...
      X