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 kuxosude, Today, 02:17 AM
      0 responses
      2 views
      0 likes
      Last Post kuxosude  
      Started by PanicTrades, 05-10-2020, 08:45 AM
      6 responses
      70 views
      0 likes
      Last Post damon_achey  
      Started by soon123, Yesterday, 11:44 PM
      0 responses
      9 views
      0 likes
      Last Post soon123
      by soon123
       
      Started by Hyper, Yesterday, 11:10 PM
      0 responses
      5 views
      0 likes
      Last Post Hyper
      by Hyper
       
      Started by soon123, Yesterday, 10:20 PM
      0 responses
      5 views
      0 likes
      Last Post soon123
      by soon123
       
      Working...
      X