Announcement

Collapse
No announcement yet.

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 Bobbybattles, Today, 01:31 PM
      0 responses
      3 views
      0 likes
      Last Post Bobbybattles  
      Started by papaoutai, Today, 12:43 PM
      2 responses
      8 views
      0 likes
      Last Post papaoutai  
      Started by sdauteuil, Today, 12:16 PM
      0 responses
      5 views
      0 likes
      Last Post sdauteuil  
      Started by arturo8317, Today, 11:40 AM
      1 response
      9 views
      0 likes
      Last Post NinjaTrader_Jim  
      Started by richbois, Today, 11:53 AM
      1 response
      5 views
      0 likes
      Last Post NinjaTrader_PaulH  
      Working...
      X