Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Need indicator to change color with slope reversal

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

    Need indicator to change color with slope reversal

    I'm new to Ninjatrader charting. I have been using Sierra Charts and am used to seeing stochastics and linreg lines turn color when their slope reverses. How can I accomplish this in Ninjatrader?

    #2
    Originally posted by ProfitPilgrim View Post
    I'm new to Ninjatrader charting. I have been using Sierra Charts and am used to seeing stochastics and linreg lines turn color when their slope reverses. How can I accomplish this in Ninjatrader?

    Hello ProfitPilgrim,

    Thank you for your post.

    This needs to be created as a custom strategy using NinjaScript.

    Building Custom Indicators: http://www.ninjatrader-support.com/H...verview18.html

    Also try the indicator file sharing section of this Support Forum for custom indicators that other users share for free.
    Ray S.NinjaTrader Customer Service

    Comment


      #3
      Thanks RJ. I think I'm close. I'm working at my first custom indicator, a modified stochastic, updating by tick, which is intended to change to one color when it rises above the previous bars indicator value,and change to another color when it drops below the previous bars indicator value.
      See the red text below, which is where I need to reference the indicator's previous bars value.

      I've added this:
      Code:
      protected override void Initialize()
      		{
      			Add(new Plot(Color.Green, "D"));
      			Add(new Plot(Color.Orange, "K"));
      
                              Add(new Plot(Color.FromKnownColor(KnownColor.ActiveCaption), PlotStyle.Dot, "PSup"));
                              Add(new Plot(Color.FromKnownColor(KnownColor.Red), PlotStyle.Dot, "PSdown"));
      
      			Plots[2].Min = [COLOR="Red"]WHAT GOES HERE TO MEAN THE PREV BAR'S VALUE?[/COLOR];
      
      			Plots[3].Max = [COLOR="Red"]WHAT GOES HERE TO MEAN THE PREV BAR'S VALUE?[/COLOR];
      then in the section protected override void OnBarUpdate(), I've added
      Code:
      			K.Set(100 * SUM(nom, Smooth)[0] / (SUM(den, Smooth)[0] == 0 ? 1.0 : SUM(den, Smooth)[0]));	// Smooth  = SlowKperiod
      			PSup.Set(100 * SUM(nom, Smooth)[0] / (SUM(den, Smooth)[0] == 0 ? 1.0 : SUM(den, Smooth)[0]));	// Smooth  = SlowKperiod
      			PSdown.Set(100 * SUM(nom, Smooth)[0] / (SUM(den, Smooth)[0] == 0 ? 1.0 : SUM(den, Smooth)[0]));	// Smooth  = SlowKperiod
      Last edited by ProfitPilgrim; 01-27-2009, 07:57 PM.

      Comment


        #4
        .Min and .Max have nothing to do with previous value setting. It is the setting for the visible range of that plot when seen on a chart.

        You cannot access values of your own plots from Initialize(). You should never place any logic in Initialize(). All logic should be placed in OnBarUpdate(). To access previous values just use the [] indexing and reference back. 0 means current bar, 1 means previous bar, 2 means bar before that.

        Close[0] = current close price
        Close[1] = previous close price
        K[0] = current K
        K[1] = previous K
        etc.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Dawn of victory...?

          Thanks for the very responsive support Josh and RJ. It's refreshing. When I compile and reload with the below revision nothing plots (unless I comment out the last two lines. I would have thought that plots 3 and 4 would appear or not appear based on whether the new values of PSup and PSdown were respectively as high as and as low as their previous bar's value. Can you push me over the hill? I'd like to modify other NT indicators to change color with slope reversal so once I succeed with one I think I'll be able to replicate others and move away from other charting programs that already have this function. It would be great if Ninjatrader added that function to all their indicators, but in the meantime I'm diving in to custom indicators with a determination to solve it.

          Code:
          		protected override void OnBarUpdate()
          		{
          			nom.Set(Close[0] - MIN(Low, PeriodK)[0]);
          			den.Set(MAX(High, PeriodK)[0] - MIN(Low, PeriodK)[0]);										// PeriodK = Kperiod
          
          			K.Set(100 * SUM(nom, Smooth)[0] / (SUM(den, Smooth)[0] == 0 ? 1.0 : SUM(den, Smooth)[0]));	// Smooth  = SlowKperiod
          			PSup.Set(100 * SUM(nom, Smooth)[0] / (SUM(den, Smooth)[0] == 0 ? 1.0 : SUM(den, Smooth)[0]));	// Smooth  = SlowKperiod
          			PSdown.Set(100 * SUM(nom, Smooth)[0] / (SUM(den, Smooth)[0] == 0 ? 1.0 : SUM(den, Smooth)[0]));	// Smooth  = SlowKperiod
          			
          			Plots[3].Min = PSup[1];
          			Plots[4].Max = PSdown[1];

          Comment


            #6
            ProfitPilgrim,

            Check the Control Center logs for errors. You are running into this: http://www.ninjatrader-support2.com/...ead.php?t=3170
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Josh I fixed that per your reference (See below) so the D and K lines reappeared, but if I make D and K transparent I observe that my custom plots still aren't appearing. What clues ensue?

              Code:
              			if (CurrentBar > 1) Plots[2].Min = PSup[1];
              			if (CurrentBar > 1) Plots[3].Max = PSdown[1];

              Comment


                #8
                ProfitPilgrim,

                If you make the lines transparent of course they won't show. Not sure what you are getting at. Also, please understand the use of .Min and .Max. What is the reason you are using them? They are only there to limit what range of values the plot will display. Your current code for those lines doesn't do anything substantial.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  My feeble attempt at changing color with slope was born of the tutorial on changing colors which I understood deployed the technique of having multiple plots of the same value and turning them on and off depending, in the tutorial's case, whether they were above below or neutral to another line.

                  In my case D and K are the original plots but don't need to be visible because one or another of the identical color plots should be visible at all times. The PSup plot should be visible provided it is at or above the defined minimum, which is the previous bar's plot value, and vica versa with the PSdown.

                  Comment


                    #10
                    I do not follow you ProfitPilgrim. On every single bar you are constantly messing with the Min and Max. These settings are not applied just for that current value. It is applied to the plot overall across all data points. For sure if you keep changing Min and Max it will not show anything.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      I see Josh. Thanks. I had assumed min max settings would not retroactively change previous plots but was equivalent to setting validity of the value bar by bar based on whether it was above/below the most recent min/max setting, supposing it would continue to display the plot of previous values that had been established by min or max at the time of each bar. So much for supposition...

                      I've search the user's guide since your last post in effort to determine how changing an indicator color bar by bar could be done. Is it possible to do something like this pseudocode
                      Code:
                      if PSup[0] >= PSup[1] PSup[0].Color = color.yellow
                      I'm not familiar with NT object classes yet but have done OOP in another language. Can you direct me to class objects in the content or give me an example of the appropriate nomenclature?

                      Comment


                        #12
                        You cannot change plot colors like that. Please see this for changing plot colors: http://www.ninjatrader-support2.com/...ead.php?t=3227
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #13
                          Thank you Josh for that valuable thread. I have examined and attempted to reproduce it in stochastics. I am trying not too lean heavily on you for coding support and accept the custom development responsibility as mine, having spent thus far 8 1/2 logged hours working at changing color with slope reversal, but haven't yet succeeded. Having create 4 extra data series PS PSdown SS and SSdown I have tried this code without success, though it compiles OK. In two trading days, on Feb 1 I am scheduled to sim trade for an entire month so have a very strong sense of urgency (up at 4:20 this morning) to modify only 2 indicators to switch color with slope reversal. They are linreg and stochastic. I have been using another platform but in the past have used NinjaTrader in realtime trading and would be able to make the switch if only I can accomplish this simple revision to code. I remain grateful for the direction you've provided to support documentation and have followed each trail. What's our next step? Do you see what is wrong with this code?
                          Code:
                          		protected override void Initialize()
                          		{
                          			Add(new Plot(Color.Green, "D"));
                          			Add(new Plot(Color.Orange, "K"));
                          
                                      Add(new Plot(Color.FromKnownColor(KnownColor.ActiveCaption), PlotStyle.Dot, "PS"));
                                      Add(new Plot(Color.FromKnownColor(KnownColor.Red), PlotStyle.Dot, "PSdown"));
                                      Add(new Plot(Color.FromKnownColor(KnownColor.Yellow), PlotStyle.Dot, "SS"));
                                      Add(new Plot(Color.FromKnownColor(KnownColor.HotPink), PlotStyle.Dot, "SSdown"));
                                      Add(new Line(Color.FromKnownColor(KnownColor.DarkOrange), 65, "SixtyFive"));
                                      Add(new Line(Color.FromKnownColor(KnownColor.DarkOrange), 35, "ThirtyFive"));
                          			
                          			Overlay				= true;
                          			
                          			// Set the pen used to draw the plot as a dashed line 
                          			Plots[3].Pen.DashStyle = DashStyle.Dot;
                          			
                          			den			= new DataSeries(this);
                          			nom			= new DataSeries(this);
                          		}
                          
                          		/// <summary>
                          		/// Calculates the indicator value(s) at the current index.
                          		/// </summary>
                          		protected override void OnBarUpdate()
                          		{
                          			nom.Set(Close[0] - MIN(Low, PeriodK)[0]);
                          			den.Set(MAX(High, PeriodK)[0] - MIN(Low, PeriodK)[0]);										// PeriodK = Kperiod
                          
                          			if (CurrentBar < 1)
                          				return;
                          
                          			K.Set(100 * SUM(nom, Smooth)[0] / (SUM(den, Smooth)[0] == 0 ? 1.0 : SUM(den, Smooth)[0]));	// Smooth  = SlowKperiod							
                          			PS.Set(100 * SUM(nom, Smooth)[0] / (SUM(den, Smooth)[0] == 0 ? 1.0 : SUM(den, Smooth)[0]));	// Smooth  = SlowKperiod			
                          			PSdown.Set(100 * SUM(nom, Smooth)[0] / (SUM(den, Smooth)[0] == 0 ? 1.0 : SUM(den, Smooth)[0]));	// Smooth  = SlowKperiod
                          			
                          			D.Set(SMA(K, PeriodD)[0]);																	// PeriodD = SlowDperiod
                          			SS.Set(SMA(K, PeriodD)[0]);																	// PeriodD = SlowDperiod
                          			SSdown.Set(SMA(K, PeriodD)[0]);																	// PeriodD = SlowDperiod
                          						
                          			// Plot ActiveCaption if the PS is rising
                          			// Rising() returns true when the current value is greater than the value of the previous bar.
                          			if (Rising(PS))
                          			{
                          				// Connects the rising plot segment with the other plots
                          				PS.Set(SMA(K, PeriodD)[1]);
                          				
                          				// Adds the new rising plot line segment to the line
                          				PS.Set(SMA(K, PeriodD)[0]);
                          			}
                          			
                          			// Plot red if the PS is falling
                          			// Falling() returns true when the current value is less than the value of the previous bar.
                          			else if (Falling(PSdown))
                          			{
                          				// Connects the new falling plot segment with the rest of the line
                          				PSdown.Set(SMA(K, PeriodD)[1]);
                          				
                          				// Adds the new falling plot line segment to the line
                          				PSdown.Set(SMA(K, PeriodD)[0]);
                          			}
                          			
                          			// Plot yellow if the SS is up
                          			if (Rising(SS))
                          			{
                          				// Connects the neutral plot segment with the rest of the line
                          				SS.Set(SUM(nom, Smooth)[1] / (SUM(den, Smooth)[0] == 0 ? 1.0 : SUM(den, Smooth)[0]));
                          				
                          				// Adds the new neutral plot line segment to the line
                          				SS.Set(SUM(nom, Smooth)[0] / (SUM(den, Smooth)[0] == 0 ? 1.0 : SUM(den, Smooth)[0]));
                          			}			
                          			
                          			// Plot hot pink if the SS is down
                          			else if (Falling(SSdown))
                          
                          			{
                          				// Connects the neutral plot segment with the rest of the line
                          				SSdown.Set(SUM(nom, Smooth)[1] / (SUM(den, Smooth)[0] == 0 ? 1.0 : SUM(den, Smooth)[0]));
                          				
                          				// Adds the new neutral plot line segment to the line
                          				SSdown.Set(SUM(nom, Smooth)[0] / (SUM(den, Smooth)[0] == 0 ? 1.0 : SUM(den, Smooth)[0]));
                          			}
                          Last edited by ProfitPilgrim; 01-29-2009, 08:07 AM.

                          Comment


                            #14
                            ProfitPilgrim,

                            You didn't copy the sample properly.

                            Code:
                            if (Rising(PS))
                            {
                                 // Connects the rising plot segment with the other plots
                                PS.Set([COLOR=Red][B]1, [/B][/COLOR]SMA(K, PeriodD)[1]);
                                            
                                // Adds the new rising plot line segment to the line
                                PS.Set(SMA(K, PeriodD)[0]);
                            }
                            Your else-if condition is wrong too.

                            You can't just switch the series in the middle. You start with if (Rising(PS)). The else-if is Falling(PS) not Falling(PSDown).

                            Same applies for all other areas of your code.
                            Josh P.NinjaTrader Customer Service

                            Comment


                              #15
                              Josh what does the one stand for? If its the dataseries it shouldn't be one. I tried 3 and 4 since those are the related series but it didn't work.
                              Thanks


                              Code:
                              			if (Rising(PS))
                              			{
                              				// Connects the rising plot segment with the other plots
                              				PS.Set(1,SMA(K, PeriodD)[1]);
                              				
                              				// Adds the new rising plot line segment to the line
                              				PS.Set(SMA(K, PeriodD)[0]);
                              			}
                              			
                              			// Plot red if the PS is falling
                              			// Falling() returns true when the current value is less than the value of the previous bar.
                              			else if (Falling(PS))
                              			{
                              				// Connects the new falling plot segment with the rest of the line
                              				PSdown.Set(1,SMA(K, PeriodD)[1]);
                              				
                              				// Adds the new falling plot line segment to the line
                              				PSdown.Set(SMA(K, PeriodD)[0]);
                              			}
                              			
                              			// Plot yellow if the SS is up
                              //			else
                              			if (Rising(SS))
                              			{
                              				// Connects the neutral plot segment with the rest of the line
                              				SS.Set(1,SUM(nom, Smooth)[1] / (SUM(den, Smooth)[0] == 0 ? 1.0 : SUM(den, Smooth)[0]));
                              				
                              				// Adds the new neutral plot line segment to the line
                              				SS.Set(SUM(nom, Smooth)[0] / (SUM(den, Smooth)[0] == 0 ? 1.0 : SUM(den, Smooth)[0]));
                              			}			
                              			
                              			// Plot hot pink if the SS is down
                              			else if (Falling(SS))
                              
                              			{
                              				// Connects the neutral plot segment with the rest of the line
                              				SSdown.Set(1,SUM(nom, Smooth)[1] / (SUM(den, Smooth)[0] == 0 ? 1.0 : SUM(den, Smooth)[0]));
                              				
                              				// Adds the new neutral plot line segment to the line
                              				SSdown.Set(SUM(nom, Smooth)[0] / (SUM(den, Smooth)[0] == 0 ? 1.0 : SUM(den, Smooth)[0]));
                              			}

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Yesterday, 05:17 AM
                              0 responses
                              54 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              130 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              71 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              44 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              49 views
                              0 likes
                              Last Post TheRealMorford  
                              Working...
                              X