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

Projected Line for Bollinger Bands for the current and prior sessions

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

    Projected Line for Bollinger Bands for the current and prior sessions

    Hello,All,

    Would anyone take on this to assist in adding the projected lines for the system BollingerBands indicator?What i mean depicted in the attached image.the circled areas is where the Bands stops at the session`s end.From there i want to project parallel lines for the upper and lower bands.

    Thank you in advance
    Attached Files

    #2
    Hello outsource,

    This is an update that I am currently preparing a response for you and will update this post as soon as I finish.

    Thank you for your patience.
    Michael M.NinjaTrader Quality Assurance

    Comment


      #3
      Hello outsource,

      You can use something like this in a new indicator to get the variables for the lines:

      Code:
      #region Variables
      private List<double[]> barSession = new List<double[]>();
      #endregion
      protected override void OnBarUpdate(){
      	if(Bars.FirstBarOfSession){
      		double[] temp = new double[3];
      		temp[0] = CurrentBar;
      		temp[1] = Bollinger(2, 14).Values[0][0]; //top bollinger band line
      		temp[2] = Bollinger(2, 14).Values[2][0]; //bottom bollinger band line
      		barSession.Add(temp);
      		if(barSession.Count > 1){
      			//Draw the lines with the variables below
                              //barSession[x][0] is the first bar of a session
                              //barSession[x][1] is the top bollinger bar line
                              //barSession[x][2] is the bottom bollinger band line
                              //x is whatever session you want to access
      		}
      	}
      }
      Then you just have to draw the lines on the chart however you see fit. If you have any questions or if we may be of further assistance, please let us know.
      Michael M.NinjaTrader Quality Assurance

      Comment


        #4
        Originally posted by NinjaTrader_MichaelM View Post
        Hello outsource,

        You can use something like this in a new indicator to get the variables for the lines:

        Code:
        #region Variables
        private List<double[]> barSession = new List<double[]>();
        #endregion
        protected override void OnBarUpdate(){
        	if(Bars.FirstBarOfSession){
        		double[] temp = new double[3];
        		temp[0] = CurrentBar;
        		temp[1] = Bollinger(2, 14).Values[0][0]; //top bollinger band line
        		temp[2] = Bollinger(2, 14).Values[2][0]; //bottom bollinger band line
        		barSession.Add(temp);
        		if(barSession.Count > 1){
        			//Draw the lines with the variables below
                                //barSession[x][0] is the first bar of a session
                                //barSession[x][1] is the top bollinger bar line
                                //barSession[x][2] is the bottom bollinger band line
                                //x is whatever session you want to access
        		}
        	}
        }
        Then you just have to draw the lines on the chart however you see fit. If you have any questions or if we may be of further assistance, please let us know.
        Thanks a lot,Michael!

        I`ve never worked with the new indicators(never created them from scratch),so i believe i`ll have a lot of errors.I do not see the properties for your snippet,as well.Could you please expand you work a bit?

        Best!

        Comment


          #5
          Hello outsource,

          I have provided the full source code below for reference. I have also attached an exported version of the indicator for your convenience. To import this indicator into NinjaTrader 7 please open the NinjaTrader control center and navigate to File -> Utilities -> Import NinjaScript...

          Please note that we normally do not create custom code for users. This is an example to help you understand basic indicator development.

          Code:
          #region Using declarations
          using System;
          using System.ComponentModel;
          using System.Collections.Generic;
          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>
              /// Enter the description of your new custom indicator here
              /// </summary>
              [Description("Creates projected lines for the upper and lower bollinger bands based on their values at the first bar of a session.")]
              public class BollingerBandLines : Indicator
              {
                  #region Variables
          			private List<double[]> bollingerSessionList  = new List<double[]>();
          			private int period 				   = 14;
          			private double numStdDev 		   = 2;
                  #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()
                  {
                      Overlay			 = true;
          			DrawOnPricePanel = true;
                  }
          
                  /// <summary>
                  /// Called on each bar update event (incoming tick)
                  /// </summary>
                  protected override void OnBarUpdate()
                  {
          			if(Bars.FirstBarOfSession)
          			{
          				//Get the variables (the bar the session started at, the upper bollinger band value at that bar, the lower bollinger band value at that bar)
          				double[] bollingerSession = new double[3];
          				bollingerSession[0] = CurrentBar;
          				bollingerSession[1] = Bollinger(numStdDev, period).Values[0][0];
          				bollingerSession[2] = Bollinger(numStdDev, period).Values[2][0];
          				bollingerSessionList.Add(bollingerSession);
          			}
          			if(bollingerSessionList.Count > 1)
          			{
          				//Draw the upper bollinger session line
          				DrawLine("Session" + bollingerSessionList.Count.ToString() + "BollingerUpper", CurrentBar - (int)bollingerSessionList[bollingerSessionList.Count - 1][0], bollingerSessionList[bollingerSessionList.Count - 1][1], 0, bollingerSessionList[bollingerSessionList.Count - 1][1], Color.Black); 
          				//Draw the lower bollinger session line
          				DrawLine("Session" + bollingerSessionList.Count.ToString() + "BollingerLower", CurrentBar - (int)bollingerSessionList[bollingerSessionList.Count - 1][0], bollingerSessionList[bollingerSessionList.Count - 1][2], 0, bollingerSessionList[bollingerSessionList.Count - 1][2], Color.Black);
          				//Draw/update the ray that shows the most recent upper bollinger session value
          				DrawRay("LastSessionBollingerUpper", 1, bollingerSessionList[bollingerSessionList.Count - 1][1], 0, bollingerSessionList[bollingerSessionList.Count - 1][1], Color.Black);
          				//Draw/update the ray that shows the most recent lower bollinger session value
          				DrawRay("LastSessionBollingerLower", 1, bollingerSessionList[bollingerSessionList.Count - 1][2], 0, bollingerSessionList[bollingerSessionList.Count - 1][2], Color.Black);
          			}
                  }
          		
          		#region Properties
          		/// <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); }
          		}
          		#endregion
              }
          }
          Please feel free to check out our ninjascript tutorials to better understand how to develop custom indicators and strategies here: http://www.ninjatrader.com/support/h..._resources.htm

          If you have any questions or if we may be of further assistance, please let us know.
          Attached Files
          Michael M.NinjaTrader Quality Assurance

          Comment


            #6
            Originally posted by NinjaTrader_MichaelM View Post
            Hello outsource,

            I have provided the full source code below for reference. I have also attached an exported version of the indicator for your convenience. To import this indicator into NinjaTrader 7 please open the NinjaTrader control center and navigate to File -> Utilities -> Import NinjaScript...

            Please note that we normally do not create custom code for users. This is an example to help you understand basic indicator development.

            Code:
            #region Using declarations
            using System;
            using System.ComponentModel;
            using System.Collections.Generic;
            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>
                /// Enter the description of your new custom indicator here
                /// </summary>
                [Description("Creates projected lines for the upper and lower bollinger bands based on their values at the first bar of a session.")]
                public class BollingerBandLines : Indicator
                {
                    #region Variables
            			private List<double[]> bollingerSessionList  = new List<double[]>();
            			private int period 				   = 14;
            			private double numStdDev 		   = 2;
                    #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()
                    {
                        Overlay			 = true;
            			DrawOnPricePanel = true;
                    }
            
                    /// <summary>
                    /// Called on each bar update event (incoming tick)
                    /// </summary>
                    protected override void OnBarUpdate()
                    {
            			if(Bars.FirstBarOfSession)
            			{
            				//Get the variables (the bar the session started at, the upper bollinger band value at that bar, the lower bollinger band value at that bar)
            				double[] bollingerSession = new double[3];
            				bollingerSession[0] = CurrentBar;
            				bollingerSession[1] = Bollinger(numStdDev, period).Values[0][0];
            				bollingerSession[2] = Bollinger(numStdDev, period).Values[2][0];
            				bollingerSessionList.Add(bollingerSession);
            			}
            			if(bollingerSessionList.Count > 1)
            			{
            				//Draw the upper bollinger session line
            				DrawLine("Session" + bollingerSessionList.Count.ToString() + "BollingerUpper", CurrentBar - (int)bollingerSessionList[bollingerSessionList.Count - 1][0], bollingerSessionList[bollingerSessionList.Count - 1][1], 0, bollingerSessionList[bollingerSessionList.Count - 1][1], Color.Black); 
            				//Draw the lower bollinger session line
            				DrawLine("Session" + bollingerSessionList.Count.ToString() + "BollingerLower", CurrentBar - (int)bollingerSessionList[bollingerSessionList.Count - 1][0], bollingerSessionList[bollingerSessionList.Count - 1][2], 0, bollingerSessionList[bollingerSessionList.Count - 1][2], Color.Black);
            				//Draw/update the ray that shows the most recent upper bollinger session value
            				DrawRay("LastSessionBollingerUpper", 1, bollingerSessionList[bollingerSessionList.Count - 1][1], 0, bollingerSessionList[bollingerSessionList.Count - 1][1], Color.Black);
            				//Draw/update the ray that shows the most recent lower bollinger session value
            				DrawRay("LastSessionBollingerLower", 1, bollingerSessionList[bollingerSessionList.Count - 1][2], 0, bollingerSessionList[bollingerSessionList.Count - 1][2], Color.Black);
            			}
                    }
            		
            		#region Properties
            		/// <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); }
            		}
            		#endregion
                }
            }
            Please feel free to check out our ninjascript tutorials to better understand how to develop custom indicators and strategies here: http://www.ninjatrader.com/support/h..._resources.htm

            If you have any questions or if we may be of further assistance, please let us know.
            Thanks a lot,Michael!Lucky me

            Last thing if you can assist with would be great.I`d like to expose the lines for strategy and add e-mail and audible alerts.

            Maybe someone,besides Michael,would take on these additions?

            Thanks to All in advance!

            Comment


              #7
              And thank,Michael once again,for taking your time, and going beyond the scope of your service!

              Comment


                #8
                Hello outsource,

                I have already made adjustments to allow accessing the values in another version of the same indicator. I have attached the version that allows you to access the line values to this post.

                Import the attached file and overwrite the previous version. Then access the values in your strategy in a similar fashion to below:

                Code:
                protected override void OnBarUpdate(){
                    double upperBollingerLineValue = BollingerBandLines(2, 14).Values[0][0];
                    double lowerBollingerLineValue = BollingerBandLines(2, 14).Values[1][0];
                }
                You can create audible and email alerts using the usual NinjaScript methods found in our help guide: http://www.ninjatrader.com/support/h..._reference.htm

                Please let us know if we may be of further assistance.
                Attached Files
                Michael M.NinjaTrader Quality Assurance

                Comment


                  #9
                  Originally posted by NinjaTrader_MichaelM View Post
                  Hello outsource,

                  I have already made adjustments to allow accessing the values in another version of the same indicator. I have attached the version that allows you to access the line values to this post.

                  Import the attached file and overwrite the previous version. Then access the values in your strategy in a similar fashion to below:

                  Code:
                  protected override void OnBarUpdate(){
                      double upperBollingerLineValue = BollingerBandLines(2, 14).Values[0][0];
                      double lowerBollingerLineValue = BollingerBandLines(2, 14).Values[1][0];
                  }
                  You can create audible and email alerts using the usual NinjaScript methods found in our help guide: http://www.ninjatrader.com/support/h..._reference.htm

                  Please let us know if we may be of further assistance.
                  Thanks Michael,

                  if iwant to acces the line`s color,how would i do that?I managed to do so via editor scriptor so far.

                  Comment


                    #10
                    Hi Michael,

                    i think you have a bug there.

                    the line # 50:

                    Code:
                    bollingerSession[2] = Bollinger(numStdDev, period).Values[U][2][/U][0];
                    Should it be the Values[1][0] instead?

                    Comment


                      #11
                      Hey Guys,

                      anyone would please assist in accessing the lines colors,width,etc..?

                      Comment


                        #12
                        Hello outsource,

                        Thank you for bringing this to my attention however this is not a bug. The Bollinger indicator uses three data series: the upper value (Values[0]), the middle value (Values[1]), and the lower value (Values[2]).

                        Please let me know if you have any further questions.
                        Michael M.NinjaTrader Quality Assurance

                        Comment


                          #13
                          Originally posted by NinjaTrader_MichaelM View Post
                          Hello outsource,

                          Thank you for bringing this to my attention however this is not a bug. The Bollinger indicator uses three data series: the upper value (Values[0]), the middle value (Values[1]), and the lower value (Values[2]).

                          Please let me know if you have any further questions.

                          Hi Michael,

                          in the first snippet you`ve provided,you introduced ''X" as the x sessions back:

                          "//x is whatever session you want to access".

                          In the modified version i believe it in the following line:

                          bollingerSessionList[bollingerSessionList.Count - 1][2]

                          So,something like Count - 1 is meant to count the session back,right?The question is,why cant i set the value more then 3?When i set,for e.g., Count - 3 - nothing appears.What if i want to set the Count to 100,how would i do that?

                          Thank you.

                          Comment


                            #14
                            Hello outsource,

                            bollingerSessionList.Count equals the number of elements in the list. When accessing a list like an array, the indices begin at 0. We have to subtract 1 from the number of elements in the list to get the index of the last element in the list If you subtract 2 you get the index of the second to last element in the list (one element back). If you subtract 100 you get the index of the one hundredth to the last element in the list (ninety nine elements back).

                            Please note: You have to make sure that a value exists in the list at the index you specify before you can access it or you may receive an error. Because the BollingerBandLines indicator only required access to the last index, we only had to check that there was at least 1 value in the list. For example:

                            Code:
                            if(bollingerSessionList.Count > 1){
                            //This code only guarantees that the variables from the one session exist (not more than one session)
                            //This is because we are only drawing the line of the current session in the OnBarUpdate() method
                            }
                            If we want to access the values from 100 sessions ago, we have to do two things:

                            1. Make sure the chart we are working with has at least 100 sessions of data. How many sessions are loaded into a chart *usually* depends on what the "Days to load" option is set to. You can check the "Days to load" option by pressing CTRL + F from your chart and looking in the right column.

                            2. Make sure we aren't trying to access the value within the indicator before bollingerSessionList has 100 sessions in it.

                            Code:
                            if(bollingerSessionList.Count > 100){
                            //This code makes sure the bollingerSessionList contains data for at least 100 sessions. 
                            //Therefore we can safely call bollingerSessionList[bollingerSessionList.Count - 101][0]
                            //bollingerSessionList[bollingerSessionList.Count - 101][0] returns the first bar number of the session that happened 100 sessions ago.
                            }
                            If you have any further questions, please let us know.
                            Michael M.NinjaTrader Quality Assurance

                            Comment


                              #15
                              Originally posted by NinjaTrader_MichaelM View Post
                              Hello outsource,

                              bollingerSessionList.Count equals the number of elements in the list. When accessing a list like an array, the indices begin at 0. We have to subtract 1 from the number of elements in the list to get the index of the last element in the list If you subtract 2 you get the index of the second to last element in the list (one element back). If you subtract 100 you get the index of the one hundredth to the last element in the list (ninety nine elements back).

                              Please note: You have to make sure that a value exists in the list at the index you specify before you can access it or you may receive an error. Because the BollingerBandLines indicator only required access to the last index, we only had to check that there was at least 1 value in the list. For example:

                              Code:
                              if(bollingerSessionList.Count > 1){
                              //This code only guarantees that the variables from the one session exist (not more than one session)
                              //This is because we are only drawing the line of the current session in the OnBarUpdate() method
                              }
                              If we want to access the values from 100 sessions ago, we have to do two things:

                              1. Make sure the chart we are working with has at least 100 sessions of data. How many sessions are loaded into a chart *usually* depends on what the "Days to load" option is set to. You can check the "Days to load" option by pressing CTRL + F from your chart and looking in the right column.

                              2. Make sure we aren't trying to access the value within the indicator before bollingerSessionList has 100 sessions in it.

                              Code:
                              if(bollingerSessionList.Count > 100){
                              //This code makes sure the bollingerSessionList contains data for at least 100 sessions. 
                              //Therefore we can safely call bollingerSessionList[bollingerSessionList.Count - 101][0]
                              //bollingerSessionList[bollingerSessionList.Count - 101][0] returns the first bar number of the session that happened 100 sessions ago.
                              }
                              If you have any further questions, please let us know.
                              Hi Michael,

                              thanks for the explicit explanation.How hard would it be to output this option to the code menu,to be able to access the sessions number via code menu,rather then the script editor?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Aviram Y, Today, 05:29 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post Aviram Y  
                              Started by quantismo, 04-17-2024, 05:13 PM
                              3 responses
                              25 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by ScottWalsh, 04-16-2024, 04:29 PM
                              7 responses
                              34 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by cls71, Today, 04:45 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post cls71
                              by cls71
                               
                              Started by mjairg, 07-20-2023, 11:57 PM
                              3 responses
                              216 views
                              1 like
                              Last Post PaulMohn  
                              Working...
                              X