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 Johnny90, Today, 04:19 AM
      0 responses
      9 views
      0 likes
      Last Post Johnny90  
      Started by jeanne, Today, 03:43 AM
      0 responses
      9 views
      0 likes
      Last Post jeanne
      by jeanne
       
      Started by brucejay27, Today, 02:58 AM
      0 responses
      7 views
      0 likes
      Last Post brucejay27  
      Started by maxeintstine, Today, 02:46 AM
      0 responses
      8 views
      0 likes
      Last Post maxeintstine  
      Started by constituyente, Today, 02:37 AM
      0 responses
      5 views
      0 likes
      Last Post constituyente  
      Working...
      X