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

ATR and StdDev upper and lower

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

    ATR and StdDev upper and lower

    Hello. Please look at script for indicator. Errors said that "+" and "-" can't be used in bold red area.

    Code:
    ......
    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
    	/// <summary>
    	/// LWMPind is plotted at standard deviation levels and average true range levels above and below a open (close) price.
    	/// </summary>
    	[Description("LWMPind is plotted at standard deviation levels and average true range levels above and below a open (close) price.")]
    	public class LWMPind : Indicator
    	{
    		#region Variables
    		private	double		numStdDev	= 1;
    		private int			period		= 360;
    		#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.Green, "Upper ATR"));
    			Add(new Plot(Color.Orange, "Upper band"));
    			Add(new Plot(Color.Orange, "Lower band"));
    			Add(new Plot(Color.Red, "Lower ATR"));
    
    			Overlay				= true;
    		}
    
    		/// <summary>
    		/// Called on each bar update event (incoming tick)
    		/// </summary>
    		protected override void OnBarUpdate()
    		{
    		    double stdDevValue = StdDev(Period)[0];
                
    			UpperStdDev.Set(Open[0] + NumStdDev * stdDevValue);
                LowerStdDev.Set(Open[0] - NumStdDev * stdDevValue);
    			
    			if (CurrentBar == 0)
    				Value.Set(High[0] - Low[0]);
    			else
    			{
    				double trueRange = High[0] - Low[0];
    				trueRange = Math.Max(Math.Abs(Low[0] - Close[1]), Math.Max(trueRange, Math.Abs(High[0] - Close[1])));
    				Value.Set(((Math.Min(CurrentBar + 1, Period) - 1 ) * Value[1] + trueRange) / Math.Min(CurrentBar + 1, Period));
    			}
    			
    		[COLOR="Red"][B]	UpperATR.Set(Open[0]+Value.Set);
    			LowerATR.Set(Open[0]-Value.Set);[/B][/COLOR]
    		}
    
    		#region Properties
    		/// <summary>
    		/// Gets the upperStdDev value.
    		/// </summary>
    		[Browsable(false)]
    		[XmlIgnore()]
    		public DataSeries UpperStdDev
    		{
    			get { return Values[0]; }
    		}
    		
    		/// <summary>
    		/// Get the lowerStdDev value.
    		/// </summary>
    		[Browsable(false)]
    		[XmlIgnore()]
    		public DataSeries LowerStdDev
    		{
    			get { return Values[1]; }
    		}
    
    		/// <summary>
    		/// </summary>
    		[Description("Number of standard deviations")]
    		[GridCategory("Parameters")]
    		[Gui.Design.DisplayNameAttribute("# of std. dev.")]
    		public double NumStdDev
    		{
    			get { return numStdDev; }
    			set { numStdDev = Math.Max(0, value); }
    		}
    
    		/// <summary>
    		/// </summary>
    		[Description("Numbers of bars used for calculations")]
    		[GridCategory("Parameters")]
    		public int Period
    		{
    			get { return period; }
    			set { period = Math.Max(1, value); }
    		}
    
    		/// <summary>
    		/// Get the upperATR value.
    		/// </summary>
    		[Browsable(false)]
    		[XmlIgnore()]
    		public DataSeries UpperATR
    		{
    			get { return Values[3]; }
    		}
    		
    		/// <summary>
    		/// Get the lowerATR value.
    		/// </summary>
    		[Browsable(false)]
    		[XmlIgnore()]
    		public DataSeries LowerATR
    		{
    			get { return Values[4]; }
    		}
    		#endregion
    	}
    }

    #2
    Hello alexstox,
    To assist you further may I know are you trying to add the data for the dataseries. If so then please try using the below code to do it.


    Code:
    UpperATR.Set(Open[0]+Value[0]);
    LowerATR.Set(Open[0]-Value[0]);
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      I just try to add ATR value to Open, and extract ATR value from Open.
      I don't know what [0] mean in this case: first int or value from ATR indicator calculation?

      P.S. I'm sorry, I'm not programmer. But at least I feel like after using NT =)))))

      Comment


        #4
        Hello alexstox,
        Value[0] will return you the current value of the first plot ("Upper ATR").
        JoydeepNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Joydeep View Post
          Hello alexstox,
          Value[0] will return you the current value of the first plot ("Upper ATR").
          Wait a sec! [0] should be "UpperStdDev", because it is first in "protected override void OnBarUpdate()"
          OR it first because of "protected override void Initialize()" first place?
          Oh my God! If second is true, there are many mistakes in "#region Properties":
          for example
          /// <summary>
          /// Gets the upperStdDev value.
          /// </summary>
          [Browsable(false)]
          [XmlIgnore()]
          public DataSeries UpperStdDev
          {
          get { return Values[0]; }
          }
          should be "return Values[1]".

          Comment


            #6
            Hello alexstox,
            The Values are referred as per the plot values
            Code:
            Add(new Plot(Color.Green, "Upper ATR"));           //Values[0]
            Add(new Plot(Color.Orange, "Upper band"));         //values[1]
            Add(new Plot(Color.Orange, "Lower band"));          //Values[2]
            Add(new Plot(Color.Red, "Lower ATR"));               //Values[3]
            JoydeepNinjaTrader Customer Service

            Comment


              #7
              So, right version will be
              Code:
              ......
              // This namespace holds all indicators and is required. Do not change it.
              namespace NinjaTrader.Indicator
              {
              	/// <summary>
              	/// LWMPind is plotted at standard deviation levels and average true range levels above and below a open (close) price.
              	/// </summary>
              	[Description("LWMPind is plotted at standard deviation levels and average true range levels above and below a open (close) price.")]
              	public class LWMPind : Indicator
              	{
              		#region Variables
              		private	double		numStdDev	= 1;
              		private int			period		= 360;
              		#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.Green, "Upper ATR"));
              			Add(new Plot(Color.Orange, "Upper band"));
              			Add(new Plot(Color.Orange, "Lower band"));
              			Add(new Plot(Color.Red, "Lower ATR"));
              
              			Overlay				= true;
              		}
              
              		/// <summary>
              		/// Called on each bar update event (incoming tick)
              		/// </summary>
              		protected override void OnBarUpdate()
              		{
              		    double stdDevValue = StdDev(Period)[0];
                          
              			UpperStdDev.Set(Open[0] + NumStdDev * stdDevValue);
                          LowerStdDev.Set(Open[0] - NumStdDev * stdDevValue);
              			
              			if (CurrentBar == 0)
              				Value.Set(High[0] - Low[0]);
              			else
              			{
              				double trueRange = High[0] - Low[0];
              				trueRange = Math.Max(Math.Abs(Low[0] - Close[1]), Math.Max(trueRange, Math.Abs(High[0] - Close[1])));
              				Value.Set(((Math.Min(CurrentBar + 1, Period) - 1 ) * Value[1] + trueRange) / Math.Min(CurrentBar + 1, Period));
              			}
              			
              			UpperATR.Set(Open[0]+Value[B][COLOR="SeaGreen"][0][/COLOR][/B]);
              			LowerATR.Set(Open[0]-Value[B][COLOR="seagreen"][3][/COLOR][/B]);
              		}
              
              		#region Properties
              		/// <summary>
              		/// Gets the upperStdDev value.
              		/// </summary>
              		[Browsable(false)]
              		[XmlIgnore()]
              		public DataSeries UpperStdDev
              		{
              			get { return Values[B][COLOR="seagreen"][1][/COLOR][/B]; }
              		}
              		
              		/// <summary>
              		/// Get the lowerStdDev value.
              		/// </summary>
              		[Browsable(false)]
              		[XmlIgnore()]
              		public DataSeries LowerStdDev
              		{
              			get { return Values[B][COLOR="seagreen"][2][/COLOR][/B]; }
              		}
              
              		/// <summary>
              		/// </summary>
              		[Description("Number of standard deviations")]
              		[GridCategory("Parameters")]
              		[Gui.Design.DisplayNameAttribute("# of std. dev.")]
              		public double NumStdDev
              		{
              			get { return numStdDev; }
              			set { numStdDev = Math.Max(0, value); }
              		}
              
              		/// <summary>
              		/// </summary>
              		[Description("Numbers of bars used for calculations")]
              		[GridCategory("Parameters")]
              		public int Period
              		{
              			get { return period; }
              			set { period = Math.Max(1, value); }
              		}
              
              		/// <summary>
              		/// Get the upperATR value.
              		/// </summary>
              		[Browsable(false)]
              		[XmlIgnore()]
              		public DataSeries UpperATR
              		{
              			get { return Values[B][COLOR="seagreen"][0][/COLOR][/B]; }
              		}
              		
              		/// <summary>
              		/// Get the lowerATR value.
              		/// </summary>
              		[Browsable(false)]
              		[XmlIgnore()]
              		public DataSeries LowerATR
              		{
              			get { return Values[B][COLOR="seagreen"][3][/COLOR][/B]; }
              		}
              		#endregion
              	}
              }

              Comment


                #8
                Originally posted by alexstox View Post
                So, right version will be
                NO! No errors after compile. But after putting indicator in chart - nothing! just bars and no indicator.

                Comment


                  #9
                  There was a run time error which was preventing this from loading. You can check for these on the Log tab of the Control Center:

                  1/29/2013 2:06:09 PM|3|4|Error on calling 'OnBarUpdate' method for indicator 'LWMPind' on bar 0: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
                  In order to current this, please add the following to the beginning of OnBarUpdate

                  Code:
                  			protected override void OnBarUpdate()
                  			{
                  				
                  [B]			if(CurrentBar < 3)
                  				return;[/B]
                  This will ensure you can calculate your highest index value (Value[3]).
                  MatthewNinjaTrader Product Management

                  Comment


                    #10
                    Thanks! Now OK with plotting.
                    But it's not what I need.
                    It looks like add everything to first bar open =)))
                    BUt I need this indicator as Bollinger Bands - from Open of the day I should see lines: 1)"Open+ ATR", 2)"Open + StdDev", 3)"Open - ATR", 4)"Open - StdDev"
                    Click image for larger version

Name:	1.PNG
Views:	1
Size:	36.3 KB
ID:	866842

                    Comment


                      #11
                      Hello alexstox,
                      Value[3] refers to the 3 bars back value of the first plot. Are you trying to do that.
                      Code:
                      LowerATR.Set(Open[0]-Value[3]
                      If you are trying to are trying to get the value of the 3rd plot then the code will be Values[3][0].
                      JoydeepNinjaTrader Customer Service

                      Comment


                        #12
                        Let me explain: as result I need 2 lines above chart bars (price) and 2 lines under price: 2 upper lines should be ATR and Standard Deviation, 2 lower lines the same. So, it should look like Bollinger ind, but this lines should be built not from moving average but from bar Open price. For example, today's bar Open=80, StdDev=5, ATR=7, so upper ATR line = Open+ATR, upper StdDev line = Open + StdDev, lower ATR line = Open - ATR, lower StdDev line = Open - StdDev.

                        Comment


                          #13
                          Hello alexstox,
                          Please refer to this indicator which draws a line based on the ATR above the price bar.
                          JoydeepNinjaTrader Customer Service

                          Comment


                            #14
                            Originally posted by NinjaTrader_Joydeep View Post
                            Hello alexstox,
                            Please refer to this indicator which draws a line based on the ATR above the price bar.
                            http://ninjatrader.com/support/forum...tid=-1&lpage=1
                            Can you comment everything here? I mean what row for what etc.

                            Of course you can't, because support is really busy here.

                            But do you have some example of big indicator (big is with many rows,values,variables), where a lot of comments "for dummies"?

                            Comment


                              #15
                              Hello alexstox,
                              Unfortunately we do not have any specific sample code in this regard.

                              I will leave the thread open for any of our forum members who can share his/her valuable input.
                              JoydeepNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by lorem, Yesterday, 09:18 AM
                              4 responses
                              13 views
                              0 likes
                              Last Post lorem
                              by lorem
                               
                              Started by Spiderbird, Today, 12:15 PM
                              0 responses
                              5 views
                              0 likes
                              Last Post Spiderbird  
                              Started by cmtjoancolmenero, Yesterday, 03:58 PM
                              12 responses
                              42 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by FrazMann, Today, 11:21 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post FrazMann  
                              Started by geddyisodin, Yesterday, 05:20 AM
                              8 responses
                              52 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Working...
                              X