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

Draw H/L Lines from multiple time frames

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

    Draw H/L Lines from multiple time frames

    Hello

    I am trying to create an indicator that doesn't appear to be doable from the Wizard.

    I would like to be able to view the indicator from any time frame and it takes into account multiple time frames.

    I would like to see a "Ray" created from 5 candle swings.

    Below is the basic code that I started with and had planned on developing from this point. But, I figured that this would at least produce a ray on the chart (even though it was not the final product - but at least something to get me started), but it does not produce anything on the chart.

    Code:
            protected override void OnBarUpdate()
            {
                if (High[0] < High[2]
    				&& High[1] < High[2]
    				&& High[3] < High[2]
    				&& High[4] < High[4])
    			{
    				DrawRay("",2,High[2],0,High[2],Color.Blue);
    			}
            }
    could you start out by helping me with what is wrong with this code?

    Thank you for your help

    #2
    Originally posted by jg123 View Post
    Hello

    I am trying to create an indicator that doesn't appear to be doable from the Wizard.

    I would like to be able to view the indicator from any time frame and it takes into account multiple time frames.

    I would like to see a "Ray" created from 5 candle swings.

    Below is the basic code that I started with and had planned on developing from this point. But, I figured that this would at least produce a ray on the chart (even though it was not the final product - but at least something to get me started), but it does not produce anything on the chart.

    Code:
            protected override void OnBarUpdate()
            {
               [COLOR="Blue"][B] if (CurrentBar < 4) return;[/B][/COLOR]
                if (High[0] < High[2]
    				&& High[1] < High[2]
    				&& High[3] < High[2]
    				&& High[4] < High[4])
    			{
    				DrawRay("",2,High[2],0,High[2],Color.Blue);
    			}
            }
    could you start out by helping me with what is wrong with this code?

    Thank you for your help
    Wrong index reference. look in your log. Corrected in blue in your code.

    Comment


      #3
      ah yes, It had been a while since I had created anything so I forgot about that.

      With that said, I have now added that line of code and still nothing is showing up on the chart. I have checked the log and there are no new log messages and I have tried creating a print statement, but there is nothing coming up on the output window.

      Here is the entire bit of pertinent code:

      Code:
          public class MyCustomIndicator : Indicator
          {
              #region Variables
              #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.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
                  Overlay				= true;
              }
      
              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {
      			if (CurrentBar < 4)
      			{
      				return;
      			}
                  if (High[0] < High[2]
      				&& High[1] < High[2]
      				&& High[3] < High[2]
      				&& High[4] < High[4])
      			{
      				DrawRay("",2,High[2],0,High[2],Color.Blue);
      			}
              }
      
              #region Properties
              [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 Plot0
              {
                  get { return Values[0]; }
              }
      
      
              #endregion
      I have also attached a chart that was done manually which demonstrates essentially what I am trying to create. But, this is only based on the current time frame and the final product will encompass Monthly, Weekly, Daily, 240, & 60 price action (and the lines will change colors, etc)
      Attached Files

      Comment


        #4
        Originally posted by jg123 View Post
        ah yes, It had been a while since I had created anything so I forgot about that.

        With that said, I have now added that line of code and still nothing is showing up on the chart. I have checked the log and there are no new log messages and I have tried creating a print statement, but there is nothing coming up on the output window.

        Here is the entire bit of pertinent code:

        Code:
            public class MyCustomIndicator : Indicator
            {
                #region Variables
                #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.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
                    Overlay				= true;
                }
        
                /// <summary>
                /// Called on each bar update event (incoming tick)
                /// </summary>
                protected override void OnBarUpdate()
                {
        			if (CurrentBar < 4)
        			{
        				return;
        			}
                    if (High[0] < High[2]
        				&& High[1] < High[2]
        				&& High[3] < High[2]
        				&& [COLOR="red"][B]High[4] < High[4][/B][/COLOR])
        			{
        				DrawRay("",2,High[2],0,High[2],Color.Blue);
        			}
                }
        
                #region Properties
                [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 Plot0
                {
                    get { return Values[0]; }
                }
        
        
                #endregion
        I have also attached a chart that was done manually which demonstrates essentially what I am trying to create. But, this is only based on the current time frame and the final product will encompass Monthly, Weekly, Daily, 240, & 60 price action (and the lines will change colors, etc)
        Well, you do have an impossible condition. I did not really notice it because I saw the missing barEscape and pretty much looked no further.

        I have highlighted your impossible condition, in red, in your original code. It is kind of hard for anything to be less than itself.

        Comment


          #5
          Hello jg123,

          Thank you for your post.

          Koganam is correct, the a value cannot be less than itself. You will need to correct this code. If the drawing object still does not draw, try adding a Print() when it should and checking your Output window to see if the Print() is made. For information on Print() please visit the following link: http://www.ninjatrader.com/support/h.../nt7/print.htm

          Comment


            #6
            Originally posted by jg123 View Post
            ah yes, It had been a while since I had created anything so I forgot about that.

            With that said, I have now added that line of code and still nothing is showing up on the chart. I have checked the log and there are no new log messages and I have tried creating a print statement, but there is nothing coming up on the output window.

            Here is the entire bit of pertinent code:

            Code:
                public class MyCustomIndicator : Indicator
                {
                    #region Variables
                    #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.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
                        Overlay				= true;
                    }
            
                    /// <summary>
                    /// Called on each bar update event (incoming tick)
                    /// </summary>
                    protected override void OnBarUpdate()
                    {
            			if (CurrentBar < 4)
            			[COLOR="Blue"][B]{[/B][/COLOR]
            				return;
            			[COLOR="Blue"][B]}[/B][/COLOR]
                        if (High[0] < High[2]
            				&& High[1] < High[2]
            				&& High[3] < High[2]
            				&& High[4] < High[4])
            			{
            				DrawRay("",2,High[2],0,High[2],Color.Blue);
            			}
                    }
            
                    #region Properties
                    [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 Plot0
                    {
                        get { return Values[0]; }
                    }
            
                    #endregion
            I have also attached a chart that was done manually which demonstrates essentially what I am trying to create. But, this is only based on the current time frame and the final product will encompass Monthly, Weekly, Daily, 240, & 60 price action (and the lines will change colors, etc)
            Firstly, a trivial point. If an 'if' statement has only one action, it doesn't need curly brackets, so if you like, you can remove the brackets in blue.

            In realtime charting, if you want a ray drawn only on the most most recent bar when the conditions are met (maybe you don't), you could use something like:

            if (CurrentBar == Count - 2)

            On edit: thinking about it, you wouldn't be using if (CurrentBar == Count - 2) when working with rays, but you can remove all rays except the last one drawn (in realtime use) using the RemoveDrawObject method.

            But if you'd like to see the rays drawn historically, as you probably do in testing, you'll need a different tag name for each ray (and every draw object in general). There are several ways, but one of the easiest is:

            DrawRay("Ray" + CurrentBar,2,High[2],0,High[2],Color.Blue);
            Last edited by arbuthnot; 05-17-2015, 08:07 AM.

            Comment


              #7
              Thank you everybody! All of these comments have been incredibly helpful.

              now, I have another question. I would like the Ray to be either deleted completely from the chart, or stop it from drawing any further, if the price action has at a later time/date touched that price.

              Here is the current code that I have thanks to all of your help:

              Code:
              protected override void OnBarUpdate()
                      {
              			if (CurrentBar < 4)
              			
              				return;
              			
                          if (High[0] < High[2]
              				&& High[1] < High[2]
              				&& High[3] < High[2]
              				&& High[4] < High[2])
              			{
              				DrawRay("Ray"+CurrentBar,2,High[2],0,High[2],Color.Blue);
              			}
              			
              			if (Low[0] > Low[2]
              				&& Low[1] > Low[2]
              				&& Low[3] > Low[2]
              				&& Low[4] > Low[2])
              			{
              				DrawRay("Ray"+CurrentBar,2,Low[2],0,Low[2],Color.Blue);
              			}
                      }
              And attached is the associated chart on the AUDJPY 240 minute

              Edt: Probably should be using DrawLine() instead of DrawRay() if I want the line to end, right? But the question above still remains
              Attached Files
              Last edited by jg123; 05-18-2015, 04:39 AM.

              Comment


                #8
                Originally posted by jg123 View Post
                Thank you everybody! All of these comments have been incredibly helpful.

                now, I have another question. I would like the Ray to be either deleted completely from the chart, or stop it from drawing any further, if the price action has at a later time/date touched that price.

                Here is the current code that I have thanks to all of your help:

                Code:
                protected override void OnBarUpdate()
                        {
                			if (CurrentBar < 4)
                			
                				return;
                			
                            if (High[0] < High[2]
                				&& High[1] < High[2]
                				&& High[3] < High[2]
                				&& High[4] < High[2])
                			{
                				DrawRay("Ray"+CurrentBar,2,High[2],0,High[2],Color.Blue);
                			}
                			
                			if (Low[0] > Low[2]
                				&& Low[1] > Low[2]
                				&& Low[3] > Low[2]
                				&& Low[4] > Low[2])
                			{
                				DrawRay("Ray"+CurrentBar,2,Low[2],0,Low[2],Color.Blue);
                			}
                        }
                And attached is the associated chart on the AUDJPY 240 minute

                Edt: Probably should be using DrawLine() instead of DrawRay() if I want the line to end, right? But the question above still remains
                Hi jg

                1) I'd say that it might be worth having a look at your conditions as lines/rays are drawn very, very frequently: I've done this myself and it can be very confusing.

                2) you can confine the bars that draw to a specific number back form the latest bar. E.g.:

                if (CurrentBar < Count - 50)

                will exclude all but about 50 of the bars going back from the right of the chart.

                3) This ind is obviously one you'd only be using in realtime trading: historically, the chart is flooded with lines as you've pointed out.

                What you can is do exactly what I've done on a recent ind of mine: remove all but the last object.

                a) Place this in Variables:

                private int BarWithObject;

                but don't repeat BarWithObject in Properties at the bottom. This will carry the value over form one bar till the next.

                b) Place these lines of code in your action statements:

                RemoveDrawObject("Rectangle" + BarWithObject);

                BarWithObject = CurrentBar;


                BarWithObject holds the value of the bar the object is drawn at and when the conditions are next met, removes it using the tag name. Doing this may solve your other problem anyway (with only one line on the chart).

                Comment


                  #9
                  Originally posted by jg123 View Post
                  Thank you everybody! All of these comments have been incredibly helpful.

                  now, I have another question. I would like the Ray to be either deleted completely from the chart, or stop it from drawing any further, if the price action has at a later time/date touched that price.

                  Here is the current code that I have thanks to all of your help:

                  Code:
                  protected override void OnBarUpdate()
                          {
                  			if (CurrentBar < 4)
                  			
                  				return;
                  			
                              if (High[0] < High[2]
                  				&& High[1] < High[2]
                  				&& High[3] < High[2]
                  				&& High[4] < High[2])
                  			{
                  				DrawRay("Ray"+CurrentBar,2,High[2],0,High[2],Color.Blue);
                  			}
                  			
                  			if (Low[0] > Low[2]
                  				&& Low[1] > Low[2]
                  				&& Low[3] > Low[2]
                  				&& Low[4] > Low[2])
                  			{
                  				DrawRay("Ray"+CurrentBar,2,Low[2],0,Low[2],Color.Blue);
                  			}
                          }
                  And attached is the associated chart on the AUDJPY 240 minute

                  Edt: Probably should be using DrawLine() instead of DrawRay() if I want the line to end, right? But the question above still remains
                  Exact question asked and answered.

                  ref: http://www.ninjatrader.com/support/f...78&postcount=6

                  Comment


                    #10
                    once again, these are really great responses and even if not everything is exactly what I am looking for - I am learning a lot. Thanks!

                    I am posting a chart of the ideal indicator. Basically this is on the 240 minute which I have done by hand. There are several colors. Each color represent that swing on a different time frame (Monthly[not currently shown], Weekly, Daily, & 240 Minute)

                    Many of these lines go way way way back so just limiting to the previous 50 or something like that may not actually be the best answer to this.

                    Kogonam, thank you for what you had written I am a bit confused. Is that just how to remove the draw object or is that also creating the lines for the swings?
                    Attached Files

                    Comment


                      #11
                      Originally posted by jg123 View Post
                      once again, these are really great responses and even if not everything is exactly what I am looking for - I am learning a lot. Thanks!

                      I am posting a chart of the ideal indicator. Basically this is on the 240 minute which I have done by hand. There are several colors. Each color represent that swing on a different time frame (Monthly[not currently shown], Weekly, Daily, & 240 Minute)

                      Many of these lines go way way way back so just limiting to the previous 50 or something like that may not actually be the best answer to this.

                      Kogonam, thank you for what you had written I am a bit confused. Is that just how to remove the draw object or is that also creating the lines for the swings?
                      If you read the whole, admittedly, rather long, thread, you should come across the picture in this post: http://www.ninjatrader.com/support/f...90&postcount=1

                      That is what the code draws.

                      Comment


                        #12
                        Thanks for the reply. I will work with that a little bit and see what I am snake to come up with from here. Really appreciated.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by arvidvanstaey, Today, 02:19 PM
                        4 responses
                        11 views
                        0 likes
                        Last Post arvidvanstaey  
                        Started by samish18, 04-17-2024, 08:57 AM
                        16 responses
                        60 views
                        0 likes
                        Last Post samish18  
                        Started by jordanq2, Today, 03:10 PM
                        2 responses
                        9 views
                        0 likes
                        Last Post jordanq2  
                        Started by traderqz, Today, 12:06 AM
                        10 responses
                        18 views
                        0 likes
                        Last Post traderqz  
                        Started by algospoke, 04-17-2024, 06:40 PM
                        5 responses
                        47 views
                        0 likes
                        Last Post NinjaTrader_Jesse  
                        Working...
                        X