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

Add an Indicator to another Indicator

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

    Add an Indicator to another Indicator

    Forgive me if this topic has been covered elsewhere on this or another Forum area, but I am trying to figure out how to "hard code" a 1 period SMA to plot on top of the %K line on a Stochastics indicator.

    I can build a chart to do this by simply setting the "Input series" of the SMA to the %K line of the stochastics indicator, what I need help with is how to add [ hard code ] the SMA into the Stochastics indicator to give me that same result.

    I customized the existing Stochastics indicator in NT-8 to color the %K line based on an "IsRising" or "IsFalling" condition and as you can see on "Pane 3)" of the attached screenshot, the %K color change is tied to the Rising or Falling of the %D

    What I am trying to accomplish is correctly shown in "Pane 2)" of the attached screenshot.

    Any assistance would be greatly appreciated.


    #2
    Hello TopGunNote,

    Thanks for your post.

    You wrote, "... the %K color change is tied to the Rising or Falling of the %D" That could not be true unless there was an error in your customization. That error could happen if you used PlotBrushes[0][0] which points to the %D plot instead of using PlotBrushes[1][0] which would point to the %K plot.

    The first index points to the plot to color and the second index point to the specific bar to color which is typically the current bar [0].
    The sequence of PlotBrushes[n] relates directly to the sequence of plots added to the indicator in State.SetDefaults. The first plot is always [0], the second would be [1] the third [2], etc.

    Reference: https://ninjatrader.com/support/help...lotbrushes.htm

    Try this in the copy of the Stochastic indicator you customized, below where K is assign a value

    if (IsRising(K))
    PlotBrushes[1][0] = Brushes.White;
    else if (IsFalling(K))
    PlotBrushes[1][0] = Brushes.Red;
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thanks for your speedy response NinjaTrader_PaulH
      My code is slightly different than yours, but I was under the impression that I had pointed to the correct "plot [1][0] " as you suggest.
      Maybe I missed something when I wrote my version?

      Here is a copy of what I coded:

      public class irStochastics : Indicator
      {
      private Series<double> den;
      private Series<double> fastK;
      private MIN min;
      private MAX max;
      private Series<double> nom;
      private SMA smaFastK;
      private SMA smaK;
      private bool slopeColor = true;

      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDe scriptionStochastics;
      Name = "irStochastics";
      IsSuspendedWhileInactive = true;
      PeriodD = 14;
      PeriodK = 42;
      Smooth = 3;

      UpTrend = Brushes.White;
      DownTrend = Brushes.Red;

      AddPlot(Brushes.Yellow, NinjaTrader.Custom.Resource.StochasticsD);
      AddPlot(Brushes.Goldenrod, NinjaTrader.Custom.Resource.StochasticsK);

      AddLine(Brushes.DarkCyan, 20, NinjaTrader.Custom.Resource.NinjaScriptIndicatorLo wer);
      AddLine(Brushes.White, 50, NinjaTrader.Custom.Resource.NinjaScriptIndicatorMi ddle);
      AddLine(Brushes.DarkCyan, 80, NinjaTrader.Custom.Resource.NinjaScriptIndicatorUp per);
      }
      else if (State == State.DataLoaded)
      {
      den = new Series<double>(this);
      nom = new Series<double>(this);
      fastK = new Series<double>(this);
      min = MIN(Low, PeriodK);
      max = MAX(High, PeriodK);
      smaFastK = SMA(fastK, Smooth);
      smaK = SMA(K, PeriodD);
      }
      }

      protected override void OnBarUpdate()
      {
      double min0 = min[0];
      nom[0] = Close[0] - min0;
      den[0] = max[0] - min0;

      if (den[0].ApproxCompare(0) == 0)
      fastK[0] = CurrentBar == 0 ? 50 : fastK[1];
      else
      fastK[0] = Math.Min(100, Math.Max(0, 100 * nom[0] / den[0]));

      // Slow %K == Fast %D
      K[0] = smaFastK[0];
      D[0] = smaK[0];


      if(IsRising(Value))
      {
      if(slopeColor)
      // PlotBrushes[0][0] = UpTrend;
      PlotBrushes[1][0] = UpTrend;
      }
      if(IsFalling(Value))
      {
      if(slopeColor)
      // PlotBrushes[0][0] = DownTrend;
      PlotBrushes[1][0] = DownTrend;
      }
      }
      Last edited by TopGunNote; 08-15-2019, 01:20 PM.

      Comment


        #4
        Hello TopGunNote,

        Thanks for your reply.

        The use of "Value" in the IsRising and IsFalling would point to the first plot, %D

        If you replace "Value" with K, do you get the expected results?
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Yes, thank you for that NinjaTrader_PaulH that has corrected my coloration issue on the Stochastics %K

          I guess this means you're not going to help me figure out how to attach an indicator to another indicator ?

          Comment


            #6
            Hello TopGunNote,

            Thanks for your reply.

            Regarding, "I guess this means you're not going to help me figure out how to attach an indicator to another indicator ?". Perhaps I have misunderstood, can you clarify/restate what you want to accomplish now that the coloring of the stochastic K is resolved.
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Thank you again NinjaTrader_PaulH for your assistance in resolving my coding mistake in regards to the "IsRising" - "IsFalling" color change of the %K of the Stochastics indicator.

              My original question was asking for assistance/guidance on how to properly code the attachment of an indicator to another indicator. (a 1 period SMA on top of the %K of the Stochastic)

              As an example, please see the attached screenshot where I have attached a 34 period EMA to the 14 period CCI and a 1 period SMA to the same CCI.
              (Note the CCI histogram is displayed "behind" the other two indicators)

              It's easy enough to do this on a chart through the GUI, but I'd like to know how to code the indicators together into one indicator to simplify the use of a combination of indicators like this example

              Comment


                #8
                Hello TopGunNote,

                Thanks for your reply and clarification.

                If I understand correctly you want to create an indicator based on the CCI and then add a 1 period SMA so you can lay it on top the of the CCI to color the SMA? That would seem redundant as it would be far simpler and less resources to add the code to color the copy of the CCI indicator plot itself using the same code as previously discussed (PlotBrushes[][]).


                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_PaulH View Post
                  Hello TopGunNote,

                  Thanks for your reply and clarification.

                  If I understand correctly you want to create an indicator based on the CCI and then add a 1 period SMA so you can lay it on top the of the CCI to color the SMA? That would seem redundant as it would be far simpler and less resources to add the code to color the copy of the CCI indicator plot itself using the same code as previously discussed (PlotBrushes[][]).

                  No, that isn't what I was asking.

                  Let's forget about the 1 SMA for now and just focus on adding a 34 period EMA (of the CCI Histogram) to the CCI as one completed indicator.

                  (the color of the EMA is less important in this case, what I am more concerned with is getting the EMA to plot in front of (on top of) the CCI histogram).

                  As I said this is easy enough for a user to do through the GUI by adding a 34 EMA to a chart that looks to the CCI as it's "Input Series", but I would like to know how to do this in the code for the CCI.

                  Hopefully this adds more clarity to my question.

                  Comment


                    #10
                    Hello TopGunNote,

                    Thanks for your reply.

                    Create a copy of the CCI code with a new name.

                    As you want to plot an EMA, from this indicator you would need to add another plot for the EMA using AddPlot() in State.SetDefaults. Make sure you add the plot after the CCI plot to keep the sequence correct. Set the plot color and name as you wish, for example I'll call it "MyEMA". AddPlot(Brushes.CornflowerBlue, "MyEMA");
                    Reference: https://ninjatrader.com/support/help...s/?addplot.htm

                    In the OnBarUpdate() you can add MyEMA[0] = EMA(Value, 34)[0]; // Value is the input series to the EMA, in this case would be the CCI.

                    A point of clarity to help, if you only have one plot then Value is all you need, when you have a multiplot then you can still refer to the first plot as Value but the second plot would be Values[1]. You can also refer then to Values[0] as it would be the same as Value, so you could also write this as MyEMA[0] = EMA(Values[0], 34)[0];
                    References:



                    In the #Region Properties you would add:

                    [Browsable(false)]
                    [XmlIgnore]
                    public Series<double> MyEMA
                    {
                    get { return Values[1]; } // Values[1] relates to the 2nd added plot
                    }
                    Paul H.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_PaulH View Post
                      Hello TopGunNote,

                      Thanks for your reply.

                      Create a copy of the CCI code with a new name.

                      As you want to plot an EMA, from this indicator you would need to add another plot for the EMA using AddPlot() in State.SetDefaults. Make sure you add the plot after the CCI plot to keep the sequence correct. Set the plot color and name as you wish, for example I'll call it "MyEMA". AddPlot(Brushes.CornflowerBlue, "MyEMA");
                      Reference: https://ninjatrader.com/support/help...s/?addplot.htm

                      In the OnBarUpdate() you can add MyEMA[0] = EMA(Value, 34)[0]; // Value is the input series to the EMA, in this case would be the CCI.

                      A point of clarity to help, if you only have one plot then Value is all you need, when you have a multiplot then you can still refer to the first plot as Value but the second plot would be Values[1]. You can also refer then to Values[0] as it would be the same as Value, so you could also write this as MyEMA[0] = EMA(Values[0], 34)[0];
                      References:



                      In the #Region Properties you would add:

                      [Browsable(false)]
                      [XmlIgnore]
                      public Series<double> MyEMA
                      {
                      get { return Values[1]; } // Values[1] relates to the 2nd added plot
                      }
                      Thank you for explaining this process to me, much appreciated.
                      Have a great weekend.

                      Comment


                        #12
                        Originally posted by NinjaTrader_PaulH View Post
                        Hello TopGunNote,

                        Thanks for your reply.

                        The use of "Value" in the IsRising and IsFalling would point to the first plot, %D

                        If you replace "Value" with K, do you get the expected results?

                        I wanted to reach out and thank you for the help you gave me sorting out that Stochastics coloration issue last week NinjaTrader_PaulH

                        It ended up being a lot closer to what I was looking for in the end.

                        Thanks again,

                        Comment


                          #13
                          Hello TopGunNote,

                          Thanks for your reply, appreciate the feedback.

                          Really glad to hear of your success which the best feedback of all.
                          Paul H.NinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Perr0Grande, Yesterday, 08:16 PM
                          1 response
                          7 views
                          0 likes
                          Last Post NinjaTrader_Jesse  
                          Started by f.saeidi, Yesterday, 08:12 AM
                          3 responses
                          24 views
                          0 likes
                          Last Post NinjaTrader_Jesse  
                          Started by algospoke, Yesterday, 06:40 PM
                          1 response
                          14 views
                          0 likes
                          Last Post NinjaTrader_Jesse  
                          Started by quantismo, Yesterday, 05:13 PM
                          1 response
                          14 views
                          0 likes
                          Last Post NinjaTrader_Gaby  
                          Started by The_Sec, 04-16-2024, 02:29 PM
                          3 responses
                          16 views
                          0 likes
                          Last Post NinjaTrader_ChristopherS  
                          Working...
                          X