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

Stochastic EMA Formula and color

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

    Stochastic EMA Formula and color

    Hello,
    I have Stochastic that paint the slope of the %D line in 2 colors, can someone help me with adding also 2 colors slope for %K?
    And Im try to change the formula SMA to EMA but I cant find the place in the script where to do that...
    I attach the Indi:


    Thanks
    Attached Files

    #2
    Hello nikodine,

    Thanks for your post.

    The indicator cannot be changed from SMA to EMA because it does not contain the stochastics code that builds the lines, it only calls the existing stochastics indicator. You would need to make a copy of the existing Stochastics indicator, rename the copy and then change lines 65 & 66 from SMA to EMA.


    To change the Stochastics_Color indicator to color the %K line you would need to make a number of changes as follows (may want to make a copy of this indicator to make these changes on as I am referring to specific line numbers in the existing code):
    1) Delete line 39
    2) Copy lines 40 - 42 and paste below line 42, then change plot names by adding "K" on the end, like RisingK. Reason is all plots must have unique names.
    3) Delete line 53
    4) Copy lines 60 - 82, paste them below line 82
    5) In the newly pasted lines, change the Plot line names to what you changes the plot names to in lines 43-45 and Change the .D to .K in each line.
    6) Delete lines 96- 101
    7) Line 108, change Values[1] to Values[0]
    8) Change line 115 from Value[2] to Value[1]
    9) Change line 122 from Value[3] to Value[2]
    10 Copy lines 104 - 123 and paste them below 123, in the lines pasted change the Values[] in sequence from 0,1,2 to 3, 4, 5
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Paul View Post
      Hello nikodine,

      Thanks for your post.

      The indicator cannot be changed from SMA to EMA because it does not contain the stochastics code that builds the lines, it only calls the existing stochastics indicator. You would need to make a copy of the existing Stochastics indicator, rename the copy and then change lines 65 & 66 from SMA to EMA.


      To change the Stochastics_Color indicator to color the %K line you would need to make a number of changes as follows (may want to make a copy of this indicator to make these changes on as I am referring to specific line numbers in the existing code):
      1) Delete line 39
      2) Copy lines 40 - 42 and paste below line 42, then change plot names by adding "K" on the end, like RisingK. Reason is all plots must have unique names.
      3) Delete line 53
      4) Copy lines 60 - 82, paste them below line 82
      5) In the newly pasted lines, change the Plot line names to what you changes the plot names to in lines 43-45 and Change the .D to .K in each line.
      6) Delete lines 96- 101
      7) Line 108, change Values[1] to Values[0]
      8) Change line 115 from Value[2] to Value[1]
      9) Change line 122 from Value[3] to Value[2]
      10 Copy lines 104 - 123 and paste them below 123, in the lines pasted change the Values[] in sequence from 0,1,2 to 3, 4, 5

      Paul, thank you for your answer!
      I think will be better to combine the idea of EMA and Colors in one indi.
      I done the job for changing the SMA to EMA in the copy of original Stoch., now I want to make the colors as in the "Stochastics_Color, so what would be the syntax to make the %K and %D colored the slope, and when to put it?

      Thank you again for your help.
      Cheers!

      Comment


        #4
        Hello nikodine,

        Thanks for your reply.

        To add the colors to a copy of the Stochastics indicator will be much easier. You will be using Rising, Falling and PlotColors.
        References:
        http://ninjatrader.com/support/helpG...t7/?rising.htm
        http://ninjatrader.com/support/helpG...7/?falling.htm
        http://ninjatrader.com/support/helpG...plotcolors.htm

        The PlotColors helpguide link provides an example that is pretty much what you need.

        In your copy of the stochastics, you will be working with the "D" and the "K" plots. Note the order they are listed in the initialize section, D is first and K is second. Because of that order, PlotColors[0][0] will be the D and PlotColors[1][0] will be K
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Paul View Post
          Hello nikodine,

          Thanks for your reply.

          To add the colors to a copy of the Stochastics indicator will be much easier. You will be using Rising, Falling and PlotColors.
          References:
          http://ninjatrader.com/support/helpG...t7/?rising.htm
          http://ninjatrader.com/support/helpG...7/?falling.htm
          http://ninjatrader.com/support/helpG...plotcolors.htm

          The PlotColors helpguide link provides an example that is pretty much what you need.

          In your copy of the stochastics, you will be working with the "D" and the "K" plots. Note the order they are listed in the initialize section, D is first and K is second. Because of that order, PlotColors[0][0] will be the D and PlotColors[1][0] will be K

          Hey Paul,
          Thank you for your reply.

          Im getting some errors here and cannot compile the file, can you check where Im wrong with the syntax?

          #region Variables
          private int periodD = 7; // SlowDperiod
          private int periodK = 14; // Kperiod
          private int smooth = 3; // SlowKperiod
          private DataSeries den;
          private DataSeries nom;
          private DataSeries fastK;
          #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.Green, "D"));
          Add(new Plot(Color.Orange, "K"));

          Add(new Line(Color.DarkViolet, 20, "Lower"));
          Add(new Line(Color.YellowGreen, 80, "Upper"));

          den = new DataSeries(this);
          nom = new DataSeries(this);
          fastK = 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]);

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

          // Slow %K == Fast %D
          K.Set(EMA(fastK, Smooth)[0]);
          D.Set(EMA(K, PeriodD)[0]);
          }
          protected override void Initialize()
          {
          // Add two plots
          Add(new Plot(Color.Blue, PlotStyle.Line, "Rising"));
          Add(new Plot(Color.Orange, PlotStyle.Line, "Falling"));
          }
          protected override void OnBarUpdate()
          {

          // Sets values to our two plots
          Rising.Set(EMA(High,"D")[0]);
          Falling.Set(EMA(Low,"K")[0]);

          // Color the Upper plot based on plot value conditions
          if (Rising)
          PlotColors[0][0] = Color.Blue;
          else if (Falling)
          PlotColors[1][0] = Color.Red;
          // Color the Lower plot based on plot value conditions
          if (Rising(Falling))
          PlotColors[0][0] = Color.Blue;
          else if (Falling(Lower))
          PlotColors[1][0] = Color.Red;
          }
          #region Properties
          /// <summary>
          /// Gets the slow D value.
          /// </summary>
          [Browsable(false)]
          [XmlIgnore()]
          public DataSeries D
          {
          get { return Values[0]; }
          }

          /// <summary>
          /// Gets the slow K value.
          /// </summary>
          [Browsable(false)]
          [XmlIgnore()]
          public DataSeries K
          {
          get { return Values[1]; }
          }

          /// <summary>
          /// </summary>
          [Description("Numbers of bars used for moving average over K values")]
          [GridCategory("Parameters")]
          public int PeriodD
          {
          get { return periodD; }
          set { periodD = Math.Max(1, value); }
          }

          /// <summary>
          /// </summary>
          [Description("Numbers of bars used for calculating the K values")]
          [GridCategory("Parameters")]
          public int PeriodK
          {
          get { return periodK; }
          set { periodK = Math.Max(1, value); }
          }

          /// <summary>
          /// </summary>
          [Description("Number of bars for smoothing the slow K values")]
          [GridCategory("Parameters")]
          public int Smooth
          {
          get { return smooth; }
          set { smooth = Math.Max(1, value); }
          }
          #endregion
          }
          }

          Comment


            #6
            Hello nikodine,

            Thanks for your post.

            Remove what you have added:
            protected override void Initialize()
            {
            // Add two plots
            Add(new Plot(Color.Blue, PlotStyle.Line, "Rising"));
            Add(new Plot(Color.Orange, PlotStyle.Line, "Falling"));
            }
            protected override void OnBarUpdate()
            {

            // Sets values to our two plots
            Rising.Set(EMA(High,"D")[0]);
            Falling.Set(EMA(Low,"K")[0]);

            // Color the Upper plot based on plot value conditions
            if (Rising)
            PlotColors[0][0] = Color.Blue;
            else if (Falling)
            PlotColors[1][0] = Color.Red;
            // Color the Lower plot based on plot value conditions
            if (Rising(Falling))
            PlotColors[0][0] = Color.Blue;
            else if (Falling(Lower))
            PlotColors[1][0] = Color.Red;
            }


            Immediately after this line: D.Set(EMA(K, PeriodD)[0]); and before the }
            Add:

            if (Rising(K))
            PlotColors[0][0] = Color.Blue;
            else if (Falling(K))
            PlotColors[0][0] = Color.Red;
            else
            PlotColors[0][0] = Color.Yellow;

            // Color the Lower plot based on plot value conditions
            if (Rising(D))
            PlotColors[1][0] = Color.Blue;
            else if (Falling(D))
            PlotColors[1][0] = Color.Red;
            else
            PlotColors[1][0] = Color.Yellow;


            (or whatever colors you wish).
            Paul H.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by usazencort, Today, 01:16 AM
            0 responses
            1 view
            0 likes
            Last Post usazencort  
            Started by kaywai, 09-01-2023, 08:44 PM
            5 responses
            603 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
            21 views
            0 likes
            Last Post Pattontje  
            Started by flybuzz, 04-21-2024, 04:07 PM
            17 responses
            230 views
            0 likes
            Last Post TradingLoss  
            Working...
            X