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

Having trouble w/ simple DrawRegion...

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

    Having trouble w/ simple DrawRegion...

    Hi,
    I keep getting this error message when I try to add this indicator to a chart (it won't plot) but it complies fine and the weird thing is I have these same lines of code (at the bottom just below the comment "//shading the region between the keltnerchannels")
    in another one of my indicators and it works fine. Here's the message...

    Error on calling 'OnBarUpdate' method for indicator 'KeltnerChannelRiseFallShade' on bar 0: KeltnerChannelRiseFallShade.DrawRegion: startBarsAgo out of valid range 0 through 0, was 1.

    I just cut and pasted NinjaTraders KeltnerChannel indicator and am modifying it.

    Any help will, I assure you, will be greatly appreciated.

    HTML Code:
    public class KeltnerChannelRiseFallShade : Indicator
    {
    #region Variables
    private int period = 21;
    private double offsetMultiplier = 2.5;
    private DataSeries diff;
    private int opacitylevel =2;
    private int wmaperiod = 200;
    private bool shading = true;
    #endregion
    /// <summary>
    /// This method is used to configure the indicator and is called once before any bar data is loaded.
    /// </summary>
    protected override void Initialize()
    {
    Add(new Plot(Color.DarkGray, "Midline"));
    Add(new Plot(Color.Blue, "Upper"));
    Add(new Plot(Color.Blue, "Lower"));
    diff = new DataSeries(this);
    Overlay = true;
    CalculateOnBarClose =false;
    }
    /// <summary>
    /// Called on each bar update event (incoming tick).
    /// </summary>
    protected override void OnBarUpdate()
    {
    diff.Set(High[0] - Low[0]);
    double wmashading = WMA(Typical,wmaperiod)[0];
    double middle = SMA(Typical, Period)[0];
    double offset = SMA(diff, Period)[0] * offsetMultiplier;
    double upper = middle + offset;
    double lower = middle - offset;
    Midline.Set(middle);
    Upper.Set(upper);
    Lower.Set(lower);
     
    if (Rising(Midline))
    {
    PlotColors[0][0] = Color.Green;
    }
    else if (Falling(Midline))
    {
    PlotColors[0][0] = Color.Red;
    }
    else
    {
    PlotColors[0][0] = Color.Yellow;
    }
    //shading the region between the keltnerchannels
    if (shading && Close[0] > wmashading)
    {
    DrawRegion("shade" + CurrentBar, 1, 0, Upper, Lower, Color.Empty, Color.PaleGreen, opacitylevel);
    }
    else if (shading && Close[0] < wmashading)
    {
    DrawRegion("shade" + CurrentBar, 1, 0, Upper, Lower, Color.Empty, Color.LightPink, opacitylevel);
    }
    Last edited by Blash; 02-16-2011, 12:01 AM.

    #2
    Hi Blash, I would try working with CurrentBar instead of the 1 for your startBarsAgo.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      You are trying to start your shading one bar ago on bar0. I would filter out bar0 by using

      Code:
      if (CurrentBar > 0 && shading && Close[0] > wmashading){//process code}
      instead of
      Code:
      if (shading && Close[0] > wmashading){//process code}
      Of course an alternative is to just return on bar0, by saying

      Code:
      if (CurrentBar == 0) return;

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Rapine Heihei, 04-23-2024, 07:51 PM
      2 responses
      30 views
      0 likes
      Last Post Max238
      by Max238
       
      Started by Shansen, 08-30-2019, 10:18 PM
      24 responses
      943 views
      0 likes
      Last Post spwizard  
      Started by Max238, Today, 01:28 AM
      0 responses
      9 views
      0 likes
      Last Post Max238
      by Max238
       
      Started by rocketman7, Today, 01:00 AM
      0 responses
      5 views
      0 likes
      Last Post rocketman7  
      Started by wzgy0920, 04-20-2024, 06:09 PM
      2 responses
      28 views
      0 likes
      Last Post wzgy0920  
      Working...
      X