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

Delete Old Lines

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

    Delete Old Lines

    Hello

    I am trying to create a Line indicator that will plot the Weekly ATR on any time frame chart. When doing that, I would like it to place based on the opening price of the current week and then delete all previous lines that were created. I would also like the line to extend across the entire chart.

    Here is what I have created so far:

    Code:
            protected override void Initialize()
            {
    
                Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "ATRHigh"));
                Add(new Plot(Color.FromKnownColor(KnownColor.Green), PlotStyle.Line, "ATRLow"));
    			Add(PeriodType.Week, 1);
                Overlay				= 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.
    			if (CurrentBars[0] <= BarsRequired || CurrentBars[1] <= BarsRequired)
    				return;
                ATRHigh.Set(ATR(BarsArray[1],length)[0]+High[0]);
                ATRLow.Set(Low[0]-ATR(BarsArray[1],length)[0]);
            }
    At this point the above code calculates the Weekly ATR above and below the current price of whatever time frame I am using (I'd like this to change to just calculating to the opening price of the current week)

    This posted code also leaves all previous ATR calulations on the chart (I would like the previous ones deleted)

    The next change should be that the line should span the entire chart.

    Thank you very much for your help!!

    #2
    Hello,

    Thank you for the question.

    Currently you are using A Plot which would have a "history" or a value for historical bars that it was set.

    I would like to ask, you said that you want to remove the prior calculations, should there only be 1 instance of each plot for the Current value only?

    If so a Drawing Object may work better for what you are doing, you could leave the existing plot code or convert it to a DataSeries to remove the visual all together, and then just use a Drawing Object to draw the current values. when the value changes the object would be moved to the new value instead of duplicated leaving only the lines for the current instance.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thank you, Jesse, very much for the information.

      yes, it sounds like a Drawing Object is much more what I am looking for.

      So would I take the Plot information out of the initialize section?

      And would I then add the Add(new Line(....)) to the OnBarUpdate() below my current .Set values?

      i am a bit confused about this part.

      Thank you for your help

      Comment


        #4
        Hello,

        Well it depends on how much work you want to do and what will work for you.

        Your script works as it, so you have the options to just leave it as is and only Add the drawing objects.

        This would leave the two plots on the chart in addition to the drawing object, the plots could then be set to Transparent to "hide" them.

        The other method would be to change the Plots to DataSeries. This does exactly the same as what you are doing now, the only difference is that you do not Plot the data but Store the data.

        I would recommend converting the Plots to DataSeries as you wouldn't need to worry about the plots at all in this case.

        So instead of this:

        Code:
          
        protected override void Initialize()
        {
             Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "ATRHigh"));
             Add(new Plot(Color.FromKnownColor(KnownColor.Green), PlotStyle.Line, "ATRLow"));
             Add(PeriodType.Week, 1);
             Overlay = true;
                }

        You would have the following:

        Code:
        private DataSeries ATRHigh;
        private DataSeries ATRLow;
        		
        protected override void Initialize()
        {
             ATRHigh = new DataSeries(this);
             ATRLow = new DataSeries(this);
        }
        Also the Properties near the bottom could be removed, you would see something similar to the following:

        Code:
        [Browsable(false)]	
        [XmlIgnore()]	
        public DataSeries ATRHigh
        {
              get { return Values[0]; }
        }
        [Browsable(false)]	
        [XmlIgnore()]	
        public DataSeries ATRLow
        {
              get { return Values[1]; }
        }
        Finally, the DrawLine or other drawing object you want to use, would be set to the value of ATRHigh[0] for the current value, or set how you need it when you need it.

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Thanx!

          I am trying it the way that you recommended with the DataSeries. It seems like the better way.

          Without a doubt, I have done something wrong because now there is nothing showing up on the chart.

          I feel that I am missing something because how will it know that I want to use the Weekly ATR?

          I am posting the entire code that I have. I have marked each of the main methods in blue and bold to help with reading it. Thank you again for your help

          Code:
          #region Using declarations
          using System;
          using System.ComponentModel;
          using System.Diagnostics;
          using System.Drawing;
          using System.Drawing.Drawing2D;
          using System.Xml.Serialization;
          using NinjaTrader.Cbi;
          using NinjaTrader.Data;
          using NinjaTrader.Gui.Chart;
          #endregion
          
          // This namespace holds all indicators and is required. Do not change it.
          namespace NinjaTrader.Indicator
          {
              /// <summary>
              /// Posts on the chart the projected weekly ATR from the current price. Primarily to be used after the market closes on Friday in order to see where price might go during the following week.
              /// </summary>
              [Description("Posts on the chart the projected weekly ATR from the current price. Primarily to be used after the market closes on Friday in order to see where price might go during the following week.")]
              public class WeeklyRangeATR : Indicator
              {
          [COLOR="Blue"][B]        #region Variables[/B][/COLOR]
                  // Wizard generated variables
                     // private int length = 14; // Default setting for ATRLength
          			private DataSeries ATRHigh;
          			private DataSeries ATRLow;
                  // User defined variables (add any user defined variables below)
                  #endregion
          
                  /// <summary>
                  /// This method is used to configure the indicator and is called once before any bar data is loaded.
                  /// </summary>
          [COLOR="blue"][B]        protected override void Initialize()[/B][/COLOR]
                  {
          			ATRHigh = new DataSeries(this);
          			ATRLow = new DataSeries(this);
                  }
          
                  /// <summary>
                  /// Called on each bar update event (incoming tick)
                  /// </summary>
          [COLOR="blue"][B]        protected override void OnBarUpdate()[/B][/COLOR]
                  {
                      // Use this method for calculating your indicator values. Assign a value to each
                      // plot below by replacing 'Close[0]' with your own formula.
          			if (CurrentBars[0] <= BarsRequired || CurrentBars[1] <= BarsRequired)
          				return;
                      ATRHigh.Set(ATR(BarsArray[1],length)[0]+High[0]);
                      ATRLow.Set(Low[0]-ATR(BarsArray[1],length)[0]);
                  }
          
          [COLOR="blue"][B]        #region Properties[/B][/COLOR]
          //        [Browsable(false)]	// this line prevents the data series from being displayed in the indicator properties dialog, do not remove
          //        [XmlIgnore()]		// this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
          //        public DataSeries ATRHigh
          //        {
          //            get { return Values[0]; }
          //        }
          //
          //        [Browsable(false)]	// this line prevents the data series from being displayed in the indicator properties dialog, do not remove
          //        [XmlIgnore()]		// this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
          //        public DataSeries ATRLow
          //        {
          //            get { return Values[1]; }
          //        }
          //
          //        [Description("")]
          //        [GridCategory("Parameters")]
          //        public int ATRLength
          //        {
          //            get { return length; }
          //            set { length = Math.Max(1, value); }
          //        }
                  #endregion
              }
          }
          
          #region NinjaScript generated code. Neither change nor remove.
          // This namespace holds all indicators and is required. Do not change it.
          namespace NinjaTrader.Indicator
          {
              public partial class Indicator : IndicatorBase
              {
                  private WeeklyRangeATR[] cacheWeeklyRangeATR = null;
          
                  private static WeeklyRangeATR checkWeeklyRangeATR = new WeeklyRangeATR();
          
                  /// <summary>
                  /// Posts on the chart the projected weekly ATR from the current price. Primarily to be used after the market closes on Friday in order to see where price might go during the following week.
                  /// </summary>
                  /// <returns></returns>
                  public WeeklyRangeATR WeeklyRangeATR()
                  {
                      return WeeklyRangeATR(Input);
                  }
          
                  /// <summary>
                  /// Posts on the chart the projected weekly ATR from the current price. Primarily to be used after the market closes on Friday in order to see where price might go during the following week.
                  /// </summary>
                  /// <returns></returns>
                  public WeeklyRangeATR WeeklyRangeATR(Data.IDataSeries input)
                  {
                      if (cacheWeeklyRangeATR != null)
                          for (int idx = 0; idx < cacheWeeklyRangeATR.Length; idx++)
                              if (cacheWeeklyRangeATR[idx].EqualsInput(input))
                                  return cacheWeeklyRangeATR[idx];
          
                      lock (checkWeeklyRangeATR)
                      {
                          if (cacheWeeklyRangeATR != null)
                              for (int idx = 0; idx < cacheWeeklyRangeATR.Length; idx++)
                                  if (cacheWeeklyRangeATR[idx].EqualsInput(input))
                                      return cacheWeeklyRangeATR[idx];
          
                          WeeklyRangeATR indicator = new WeeklyRangeATR();
                          indicator.BarsRequired = BarsRequired;
                          indicator.CalculateOnBarClose = CalculateOnBarClose;
          #if NT7
                          indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
                          indicator.MaximumBarsLookBack = MaximumBarsLookBack;
          #endif
                          indicator.Input = input;
                          Indicators.Add(indicator);
                          indicator.SetUp();
          
                          WeeklyRangeATR[] tmp = new WeeklyRangeATR[cacheWeeklyRangeATR == null ? 1 : cacheWeeklyRangeATR.Length + 1];
                          if (cacheWeeklyRangeATR != null)
                              cacheWeeklyRangeATR.CopyTo(tmp, 0);
                          tmp[tmp.Length - 1] = indicator;
                          cacheWeeklyRangeATR = tmp;
                          return indicator;
                      }
                  }
              }
          }
          
          // This namespace holds all market analyzer column definitions and is required. Do not change it.
          namespace NinjaTrader.MarketAnalyzer
          {
              public partial class Column : ColumnBase
              {
                  /// <summary>
                  /// Posts on the chart the projected weekly ATR from the current price. Primarily to be used after the market closes on Friday in order to see where price might go during the following week.
                  /// </summary>
                  /// <returns></returns>
                  [Gui.Design.WizardCondition("Indicator")]
                  public Indicator.WeeklyRangeATR WeeklyRangeATR()
                  {
                      return _indicator.WeeklyRangeATR(Input);
                  }
          
                  /// <summary>
                  /// Posts on the chart the projected weekly ATR from the current price. Primarily to be used after the market closes on Friday in order to see where price might go during the following week.
                  /// </summary>
                  /// <returns></returns>
                  public Indicator.WeeklyRangeATR WeeklyRangeATR(Data.IDataSeries input)
                  {
                      return _indicator.WeeklyRangeATR(input);
                  }
              }
          }
          
          // This namespace holds all strategies and is required. Do not change it.
          namespace NinjaTrader.Strategy
          {
              public partial class Strategy : StrategyBase
              {
                  /// <summary>
                  /// Posts on the chart the projected weekly ATR from the current price. Primarily to be used after the market closes on Friday in order to see where price might go during the following week.
                  /// </summary>
                  /// <returns></returns>
                  [Gui.Design.WizardCondition("Indicator")]
                  public Indicator.WeeklyRangeATR WeeklyRangeATR()
                  {
                      return _indicator.WeeklyRangeATR(Input);
                  }
          
                  /// <summary>
                  /// Posts on the chart the projected weekly ATR from the current price. Primarily to be used after the market closes on Friday in order to see where price might go during the following week.
                  /// </summary>
                  /// <returns></returns>
                  public Indicator.WeeklyRangeATR WeeklyRangeATR(Data.IDataSeries input)
                  {
                      if (InInitialize && input == null)
                          throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");
          
                      return _indicator.WeeklyRangeATR(input);
                  }
              }
          }
          #endregion

          Comment


            #6
            Hello,

            That all looks mostly correct and you would not see anything because the Plot was replaced with a DataSeries. Also it looks like you had copied and pasted the Initialize example I provided which did not include your Add() statement, you will need the Add statement if you are using extra data:

            Code:
            Add(PeriodType.Week, 1);
            It looks like you are missing the DrawingObject now.

            If ATRHigh and Low are the values the drawing object needs to be at, you would just need to add the lines after you set these values.

            Code:
            ATRHigh.Set(ATR(BarsArray[1],length)[0]+High[0]);
            DrawHorizontalLine("ATRHigh", ATRHigh[0], Color.Blue);
            In fact, if you only need to display these values but do not need the history of it, you really wouldn't really need a DataSeries at all and just the actual equation or:

            Code:
            DrawHorizontalLine("ATRHigh", ATR(BarsArray[1],length)[0]+High[0], Color.Blue);
            Finally, if the script does not have enough data loaded for the weekly, you would get an index error in the Tools -> Output window. To prevent errors, you can check that BarsArray[1] has a bar using the following:

            Code:
            if(CurrentBars[1] < 1) return;
            I look forward to being of further assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              ah, this makes a lot more sense. Yes, I did just blindly copy and paste what you had, which explains why I couldn't understand how the logic would work.

              Attached is a pic of this now.

              Now, if it is possible to do, I would like to somehow capture the opening price of the week and attach the ATR to that price. Currently the lines will move as the price moves, but I would like it to not do that.

              For example, the opening price for this week on the AUDUSD chart attached is .7296. How can I say, "Add the weekly ATR to .7296 and subtract it from .7296" and it keeps doing that until next week opens up?
              Attached Files

              Comment


                #8
                Here is what I have tried to do on my own. I created another Array for the Daily time frame and created a variable called Open. The goal is to be able to store that variable in order to add the ATR price to it

                But when I go to OnBarUpdate, I have written this and get an error:

                Code:
                			if (Time[0].DayOfWeek == DayOfWeek.Monday)
                			{
                				open = Open(BarsArray[2])[0];
                			}
                I get the error that "Open" does not exist in this context. But the Open with the capital "O" is supposed to be the price point and not a variable.

                Could you let me know what is wrong with that? Also, could you let me know if I am on the right track as well as some pointers in how to move forward from here?

                Thank you very much for your help

                Comment


                  #9
                  Hello,

                  In this case, if you just need the open from the Secondary series, you could do something like the following:

                  Code:
                  protected override void OnBarUpdate()
                  {
                       if(BarsInProgress == 1)
                       {
                                  if(CurrentBars[1] > 1) 
                                  {
                  		     DrawDot("Open", true, 0, Opens[1][0], Color.Red);	
                                  }
                        }
                  }
                  This code would only execute when the Weekly bar Closes which is the: if(BarsInProgress == 1){

                  if(CurrentBars[1] > 1) checks that there is at least 1 weekly bar loaded.

                  If the script is running on CalculateOnBarClose == true, Opens[1][0] would be the currently closed bar or the Open from the last weekly bar.

                  Instead if CalculateOnBarClose == false, you would need to use Opens[1][1] instead as this would reference the prior bar and not the building bar.


                  In your example you are using Open(BarsArray[2]) but instead would just need to reference Opens[SeriesIndex][BarsAgo]

                  the Plural for Open or Opens would be referring to the collection of all series on the chart, there is a plural for most items like Opens, Closes, Highs, Times, Lows, Volumes, etc.

                  I look forward to being of further assistance.
                  JesseNinjaTrader Customer Service

                  Comment


                    #10
                    This got me exactly what I wanted. Thank you very much for all of your help!!!!

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by MacDad, 02-25-2024, 11:48 PM
                    7 responses
                    157 views
                    0 likes
                    Last Post loganjarosz123  
                    Started by Belfortbucks, Today, 09:29 PM
                    0 responses
                    6 views
                    0 likes
                    Last Post Belfortbucks  
                    Started by zstheorist, Today, 07:52 PM
                    0 responses
                    7 views
                    0 likes
                    Last Post zstheorist  
                    Started by pmachiraju, 11-01-2023, 04:46 AM
                    8 responses
                    151 views
                    0 likes
                    Last Post rehmans
                    by rehmans
                     
                    Started by mattbsea, Today, 05:44 PM
                    0 responses
                    6 views
                    0 likes
                    Last Post mattbsea  
                    Working...
                    X