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

indicator leaving artifacts

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

    indicator leaving artifacts

    Hi

    I've done a bit of work editing a stoch indicator to colour the line based on slope but have noticed that it leaves artifacts as the line changes slope during candle formation.

    I have attached a screen shot of what the problem looks like. if you refresh the chart it goes away.. is there a way to make it clean up old plots during the candle formation? It's only a problem if it changes colour, plotting different slopes of the same colour is fine.

    here's the code in question.

    any assistance or pointing in the right direction would be much appreciated.

    Code:
    			if (CurrentBar < 1)
    				return;
    			
    			// Plot green if the %K is rising
    			// Rising() returns true when the current value is greater than the value of the previous bar.
    			if (Rising(Stochastics(PeriodD, PeriodK, Smooth).K))
    			{
    				// Connects the rising plot segment with the other plots
    				RisingPlot.Set(1, Stochastics(PeriodD, PeriodK, Smooth).K[1]);				
    				
    				// Adds the new rising plot line segment to the line
    				RisingPlot.Set(Stochastics(PeriodD, PeriodK, Smooth).K[0]);				
    			}
    			
    			// Plot red if the %K is falling
    			// Falling() returns true when the current value is less than the value of the previous bar.
    			else if (Falling(Stochastics(PeriodD, PeriodK, Smooth).K))
    			{
    				// Connects the new falling plot segment with the rest of the line
    				FallingPlot.Set(1, Stochastics(PeriodD, PeriodK, Smooth).K[1]);
    				
    				// Adds the new falling plot line segment to the line
    				FallingPlot.Set(Stochastics(PeriodD, PeriodK, Smooth).K[0]);
    			}
    			
    			// Plot yellow if the %K is neutral
    			else
    			{
    				// Connects the neutral plot segment with the rest of the line
    				NeutralPlot.Set(1, Stochastics(PeriodD, PeriodK, Smooth).K[1]);
    				
    				// Adds the new neutral plot line segment to the line
    				NeutralPlot.Set(Stochastics(PeriodD, PeriodK, Smooth).K[0]);
    			}
    Attached Files

    #2
    Hello NathanO,

    Thank you for your note.

    Use one plot, for example:
    Code:
    			if (CurrentBar < 1)
    				return;
    			
    			// Plot green if the %K is rising
    			// Rising() returns true when the current value is greater than the value of the previous bar.
    			if (Rising(Stochastics(PeriodD, PeriodK, Smooth).K))
    			{
    				StochPlot.Set(Stochastics(PeriodD, PeriodK, Smooth).K[0]);				
    			}
    			
    			// Plot red if the %K is falling
    			// Falling() returns true when the current value is less than the value of the previous bar.
    			else if (Falling(Stochastics(PeriodD, PeriodK, Smooth).K))
    			{
    				StochPlot.Set(Stochastics(PeriodD, PeriodK, Smooth).K[0]);
    			}
    			
    			// Plot yellow if the %K is neutral
    			else
    			{
    				StochPlot.Set(Stochastics(PeriodD, PeriodK, Smooth).K[0]);
    			}
    You can find an example of multi-colored plots at the following link: http://ninjatrader.com/support/forum...ead.php?t=3227

    Please let me know if you have any questions.

    Comment


      #3
      Thanks Patrick

      I'll give that a go and see what happens.

      Comment


        #4
        Patrick

        I understand what you mean by using one plot but I'm not sure how to change the colour. I should have included more code with my first post, here's a larger chunk of the code that includes the part that defines the colors for the slopes.

        so working with the code you provided I would somehow need to define the color to be used with the StochPlot.Set command

        I have optimised the code by only calculating the K value once per bar update.

        I do have some coding background but many years ago and I just don't get how I need to change this code.


        Code:
                protected override void Initialize()
                {
        			// Add one plot for every color you wish to use.
        			Add(new Plot(new Pen(Color.LimeGreen, 2), PlotStyle.Line, "Rising"));
                    Add(new Plot(new Pen(Color.Red, 2), PlotStyle.Line, "Falling"));
        			Add(new Plot(new Pen(Color.Yellow, 2), PlotStyle.Line, "Neutral"));
        			CalculateOnBarClose = false;
        			Add(new Line(Color.DarkViolet, 20, "Lower"));
        			Add(new Line(Color.YellowGreen, 80, "Upper"));
        			// Initialize the DataSeries.
        			Kvalue = new DataSeries(this);
        			
                }
        
                /// <summary>
                /// Called on each bar update event (incoming tick)
                /// </summary>
                protected override void OnBarUpdate()
                {
        			// Checks to make sure we have at least 1 bar before continuing
        			if (CurrentBar < 1)
        			return;
        			
        			Kvalue.Set(Stochastics(PeriodD, PeriodK, Smooth).K[0]);
        			
        			// Plot green if the %K is rising
        			// Rising() returns true when the current value is greater than the value of the previous bar.
        			if (Rising(Kvalue))
        			{
        				// Connects the rising plot segment with the other plots
        				RisingPlot.Set(1, Stochastics(PeriodD, PeriodK, Smooth).K[1]);				
        				
        				// Adds the new rising plot line segment to the line
        				RisingPlot.Set(Stochastics(PeriodD, PeriodK, Smooth).K[0]);		
        				
        			}
        			
        			// Plot red if the %K is falling
        			// Falling() returns true when the current value is less than the value of the previous bar.
        			else if (Falling(Kvalue))
        			{
        				// Connects the new falling plot segment with the rest of the line
        				FallingPlot.Set(1, Stochastics(PeriodD, PeriodK, Smooth).K[1]);
        				
        				// Adds the new falling plot line segment to the line
        				FallingPlot.Set(Stochastics(PeriodD, PeriodK, Smooth).K[0]);
        			}
        			
        			// Plot yellow if the %K is neutral
        			else
        			{
        				// Connects the neutral plot segment with the rest of the line
        				NeutralPlot.Set(1, Stochastics(PeriodD, PeriodK, Smooth).K[1]);
        				
        				// Adds the new neutral plot line segment to the line
        				NeutralPlot.Set(Stochastics(PeriodD, PeriodK, Smooth).K[0]);
        			}
                }

        Comment


          #5
          Use one plot. Test if rising and assign a color to that plot:
          PlotColors[0][0] = Color.LimeGreen;

          Test if falling and set color:
          PlotColors[0][0] = Color.Red;

          Etc.
          eDanny
          NinjaTrader Ecosystem Vendor - Integrity Traders

          Comment


            #6
            Hello NathanO,
            eDanny is correct in that you could use the PlotColors property to create the multicolored plot.
            I would recommend the review the following reference sample that does something very similar with the SMA. http://ninjatrader.com/support/forum...ead.php?t=3227
            Cody B.NinjaTrader Customer Service

            Comment


              #7
              Thanks eDanny and Cody

              I'll have more of a play with this and see how I go. I know it's the only way I wil learn.. I'm just not sure what command tells it to do the plot to the screen.

              PlotColors[0][0] = Color.LimeGreen would set the color but does that also cause it to plot to the screen?

              I would like the colours to be set in the properties by the user but for now happy to hard code them to get it working and sort that out later.

              Appreciate your time. Would you believe the only subject I got top marks in at uni was a programming subject.. but that was a few years ago now.. I'm an infrastructure guy now :-)

              Comment


                #8
                Hello,
                By adding the plot in Initialize this adds the plot to the chart.
                You could create your own variable for colors for users to select. Please see the following link on user defined color inputs: http://ninjatrader.com/support/forum...ead.php?t=4977
                Cody B.NinjaTrader Customer Service

                Comment


                  #9
                  so I've changed the code based on what I saw in the SampleMultiColoredPlot.

                  But now I have nothing drawing at all, not even the upper and lower lines are drawing..

                  What's really annoying is this is out of the box functionality on sierra charts, to change line color based on slope..

                  I must have done something fundamentally wrong if I have nothing drawing at all..

                  Looks like the code I started with came from this thread http://ninjatrader.com/support/forum...ead.php?t=4618

                  Code:
                  // 
                  // Copyright (C) 2007, NinjaTrader LLC <www.ninjatrader.com>.
                  // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
                  //
                  #region Using declarations
                  using System;
                  using System.Diagnostics;
                  using System.Drawing;
                  using System.Drawing.Drawing2D;
                  using System.ComponentModel;
                  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>
                      /// This is a sample indicator plotting a SMA in three colors.
                      /// </summary>
                      [Description("Stochastics K plot only with three colors.")]
                      [Gui.Design.DisplayName("Stoch_K_Slope")]
                      public class Stoch_K_Slope : Indicator
                      {
                  		#region Variables
                  		private int					periodD	= 3;
                  		private int					periodK	= 5;
                  		private int					smooth	= 3;
                  		private DataSeries			Kvalue;
                  		#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.SkyBlue), PlotStyle.Line, "KPlot"));
                  
                  			CalculateOnBarClose = false;
                  			
                  			Add(new Line(Color.DarkViolet, 20, "Lower"));
                  			Add(new Line(Color.YellowGreen, 80, "Upper"));
                  			
                  			// Initialize the DataSeries.
                  			Kvalue = new DataSeries(this);
                  			
                  			
                          }
                  
                          /// <summary>
                          /// Called on each bar update event (incoming tick)
                          /// </summary>
                          protected override void OnBarUpdate()
                          {
                  			// Checks to make sure we have at least 1 bar before continuing
                  			if (CurrentBar < 1)
                  			return;
                  			
                  			//KvalueLine.Set(Stochastics(PeriodD, PeriodK, Smooth).K[0]);
                  			Kvalue.Set(Stochastics(PeriodD, PeriodK, Smooth).K[0]);
                  			
                  			// Plot green if the %K is rising
                  			// Rising() returns true when the current value is greater than the value of the previous bar.
                  			if (Rising(Kvalue))
                  			{
                  				PlotColors[0][0] = Color.LimeGreen;
                  			}
                  			
                  			// Plot red if the %K is falling
                  			// Falling() returns true when the current value is less than the value of the previous bar.
                  			else if (Falling(Kvalue))
                  			{
                  				PlotColors[0][0] = Color.Red;
                  			}
                  			
                  			// Plot Orange if the %K is neutral
                  			else
                  			{
                  				PlotColors[0][0] = Color.Orange;
                  			}
                          }
                  
                          #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 KPlot
                          {
                              get { return Values[0]; }
                          }
                  
                  		// Adds an input for the period of the SMA
                  		/// <summary>
                  		/// </summary>
                  		[Description("Numbers of bars used for calculations")]
                  		[Category("Parameters")]
                  		public int PeriodK
                  		{
                  			get { return periodK; }
                  			set { periodK = Math.Max(1, value); }
                  		}
                  		
                  		// Adds an input for the period of the SMA
                  		/// <summary>
                  		/// </summary>
                  		[Description("Numbers of bars used for calculations")]
                  		[Category("Parameters")]
                  		public int PeriodD
                  		{
                  			get { return periodD; }
                  			set { periodD = Math.Max(1, value); }
                  		}
                  		
                  		[Description("Smoothing period for SMA")]
                  		[Category("Parameters")]
                  		public int Smooth
                  		{
                  			get { return smooth; }
                  			set { smooth = 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 Stoch_K_Slope[] cacheStoch_K_Slope = null;
                  
                          private static Stoch_K_Slope checkStoch_K_Slope = new Stoch_K_Slope();
                  
                          /// <summary>
                          /// Stochastics K plot only with three colors.
                          /// </summary>
                          /// <returns></returns>
                          public Stoch_K_Slope Stoch_K_Slope(int periodD, int periodK, int smooth)
                          {
                              return Stoch_K_Slope(Input, periodD, periodK, smooth);
                          }
                  
                          /// <summary>
                          /// Stochastics K plot only with three colors.
                          /// </summary>
                          /// <returns></returns>
                          public Stoch_K_Slope Stoch_K_Slope(Data.IDataSeries input, int periodD, int periodK, int smooth)
                          {
                              if (cacheStoch_K_Slope != null)
                                  for (int idx = 0; idx < cacheStoch_K_Slope.Length; idx++)
                                      if (cacheStoch_K_Slope[idx].PeriodD == periodD && cacheStoch_K_Slope[idx].PeriodK == periodK && cacheStoch_K_Slope[idx].Smooth == smooth && cacheStoch_K_Slope[idx].EqualsInput(input))
                                          return cacheStoch_K_Slope[idx];
                  
                              lock (checkStoch_K_Slope)
                              {
                                  checkStoch_K_Slope.PeriodD = periodD;
                                  periodD = checkStoch_K_Slope.PeriodD;
                                  checkStoch_K_Slope.PeriodK = periodK;
                                  periodK = checkStoch_K_Slope.PeriodK;
                                  checkStoch_K_Slope.Smooth = smooth;
                                  smooth = checkStoch_K_Slope.Smooth;
                  
                                  if (cacheStoch_K_Slope != null)
                                      for (int idx = 0; idx < cacheStoch_K_Slope.Length; idx++)
                                          if (cacheStoch_K_Slope[idx].PeriodD == periodD && cacheStoch_K_Slope[idx].PeriodK == periodK && cacheStoch_K_Slope[idx].Smooth == smooth && cacheStoch_K_Slope[idx].EqualsInput(input))
                                              return cacheStoch_K_Slope[idx];
                  
                                  Stoch_K_Slope indicator = new Stoch_K_Slope();
                                  indicator.BarsRequired = BarsRequired;
                                  indicator.CalculateOnBarClose = CalculateOnBarClose;
                  #if NT7
                                  indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
                                  indicator.MaximumBarsLookBack = MaximumBarsLookBack;
                  #endif
                                  indicator.Input = input;
                                  indicator.PeriodD = periodD;
                                  indicator.PeriodK = periodK;
                                  indicator.Smooth = smooth;
                                  Indicators.Add(indicator);
                                  indicator.SetUp();
                  
                                  Stoch_K_Slope[] tmp = new Stoch_K_Slope[cacheStoch_K_Slope == null ? 1 : cacheStoch_K_Slope.Length + 1];
                                  if (cacheStoch_K_Slope != null)
                                      cacheStoch_K_Slope.CopyTo(tmp, 0);
                                  tmp[tmp.Length - 1] = indicator;
                                  cacheStoch_K_Slope = 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>
                          /// Stochastics K plot only with three colors.
                          /// </summary>
                          /// <returns></returns>
                          [Gui.Design.WizardCondition("Indicator")]
                          public Indicator.Stoch_K_Slope Stoch_K_Slope(int periodD, int periodK, int smooth)
                          {
                              return _indicator.Stoch_K_Slope(Input, periodD, periodK, smooth);
                          }
                  
                          /// <summary>
                          /// Stochastics K plot only with three colors.
                          /// </summary>
                          /// <returns></returns>
                          public Indicator.Stoch_K_Slope Stoch_K_Slope(Data.IDataSeries input, int periodD, int periodK, int smooth)
                          {
                              return _indicator.Stoch_K_Slope(input, periodD, periodK, smooth);
                          }
                      }
                  }
                  
                  // This namespace holds all strategies and is required. Do not change it.
                  namespace NinjaTrader.Strategy
                  {
                      public partial class Strategy : StrategyBase
                      {
                          /// <summary>
                          /// Stochastics K plot only with three colors.
                          /// </summary>
                          /// <returns></returns>
                          [Gui.Design.WizardCondition("Indicator")]
                          public Indicator.Stoch_K_Slope Stoch_K_Slope(int periodD, int periodK, int smooth)
                          {
                              return _indicator.Stoch_K_Slope(Input, periodD, periodK, smooth);
                          }
                  
                          /// <summary>
                          /// Stochastics K plot only with three colors.
                          /// </summary>
                          /// <returns></returns>
                          public Indicator.Stoch_K_Slope Stoch_K_Slope(Data.IDataSeries input, int periodD, int periodK, int smooth)
                          {
                              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.Stoch_K_Slope(input, periodD, periodK, smooth);
                          }
                      }
                  }
                  #endregion

                  Comment


                    #10
                    Nathan, you are still not plotting anything to change the color of. You are storing the value in a DataSeries (Kvalue) but you are not plotting it.
                    One way to do it would be to add this under this line like this:

                    Kvalue.Set(Stochastics(PeriodD, PeriodK, Smooth).K[0]);
                    KPlot.Set(Kvalue[0]);

                    Lots of this's there. Now you will be plotting the DataSeries. Another way is to completely get rid of the DataSeries and directly plot the Stochastic line like this:

                    KPlot.Set(Stochastics(PeriodD, PeriodK, Smooth).K[0]);

                    There are more efficient ways of doing this but this will fix your problem.
                    Last edited by eDanny; 02-27-2016, 02:35 PM.
                    eDanny
                    NinjaTrader Ecosystem Vendor - Integrity Traders

                    Comment


                      #11
                      Danny

                      Thanks so much! I knew it would be simple :-)

                      the reason I created the dataseries Kvalue was to optimise the code. I figured that doing to stoch calculation once per bar update was better than doing it up to three times per bar update. And I want this indicator to update during the bar formation not just at the close of the bar. Maybe it doesn't make much difference but seemed logical to me.

                      Now I will keep an eye on it with live data tonight and make sure it behaves how I expect and then I can work on setting the colors based on the user choice in the setting screen.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by kaywai, 09-01-2023, 08:44 PM
                      5 responses
                      601 views
                      0 likes
                      Last Post NinjaTrader_Jason  
                      Started by xiinteractive, 04-09-2024, 08:08 AM
                      6 responses
                      22 views
                      0 likes
                      Last Post xiinteractive  
                      Started by Pattontje, Yesterday, 02:10 PM
                      2 responses
                      18 views
                      0 likes
                      Last Post Pattontje  
                      Started by flybuzz, 04-21-2024, 04:07 PM
                      17 responses
                      230 views
                      0 likes
                      Last Post TradingLoss  
                      Started by agclub, 04-21-2024, 08:57 PM
                      3 responses
                      17 views
                      0 likes
                      Last Post TradingLoss  
                      Working...
                      X