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

Stochastics indicator not showing the correct stochastic

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

    Stochastics indicator not showing the correct stochastic


    Hi I am trying to create an indicator to show when a stochastic is below 20

    I would like to show when a 60-10 stochastic is below 20

    But it only shows when a 9-3 stochastic is below 20 for some reason?

    Thanks





    Code:
    namespace NinjaTrader.NinjaScript.Indicators
    {
    public class StochasticHighlighter : Indicator
    {
    private Stochastics stoch60_10;
    
    private double stochValue60_10;
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Highlights a vertical bar when 60-10 stochastics are below 20.";
    Name = "MyCustomIndicator";
    Calculate = Calculate.OnBarClose;
    IsOverlay = true;
    AddPlot(Brushes.Transparent, "InvisiblePlot"); // Adding an invisible plot to comply with indicator structure
    }
    else if (State == State.DataLoaded)
    {
    stoch60_10 = Stochastics(Close, 60, 10, 3);
    }
    }
    
    protected override void OnBarUpdate()
    {
    if (CurrentBar < Math.Max(60, 10) || CurrentBar == 0)
    return;
    
    stochValue60_10 = stoch60_10.K[0];
    
    if (stochValue60_10 < 20)
    {
    
    Draw.VerticalLine(this, "Highlight_" + CurrentBar, 0, Brushes.Red, DashStyleHelper.Solid, 2);
    
    string label = string.Format("Stoch (60-10): {0:F2}", stochValue60_10);
    Draw.Text(this, "StochLabel_" + CurrentBar, label, 0, High[0] + TickSize, Brushes.Blue);
    }
    
    }
    
    
    }
    
    
    }​





    #2
    Hello danieldunn2024,

    In the code you provided the indicators parameters are what you wanted so that should work for the purpose you described. How are you currently checking to know it is the 9-3 below 20 instead of 60-10?

    This is the indicator that is being used in the code and its parameters are 60 and 10: Stochastics(Close, 60, 10, 3);
    Your condition then uses that value: if (stochValue60_10 < 20)

    If your chart has a manually applied stochastics indicator that is set to 9-3 I would suggest to remove that and instead add the 60-10 stochastics on the chart which would represent the same values you are using in code here.

    JesseNinjaTrader Customer Service

    Comment


      #3
      Thanks for helping it is the same though even without the 9-3 indicator

      It would have worked great if it was the 9-3 I was trying to highlight, but its the 60-10 I wanted to, not sure where I went wrong!


      Click image for larger version

Name:	1.png
Views:	22
Size:	93.6 KB
ID:	1304837


      Click image for larger version

Name:	2.png
Views:	21
Size:	69.3 KB
ID:	1304838

      Comment


        #4
        Hello danieldunn2024,

        I would suggest to replot the value that the script is seeing or use a Print so you can see what the script is seeing. Your code is using the parameters Stochastics(Close, 60, 10, 3) so that is what is being used for the drawings on the chart. The indicators on the chart are not being directly used so that may be why you are seeing differences.

        The easiest way would be to remove all the indicators from the chart except for your custom indicator and then use AddPlot to have your custom indicator add its own plot.



        After adding the plot set the value of the plot to the indicator you are calling:


        Code:
        stochValue60_10 = stoch60_10.K[0];
        ​Value[0] = stochValue60_10;
        JesseNinjaTrader Customer Service

        Comment


          #5
          I think it may be an issue here

          stoch60_10 = Stochastics(Close, 60, 10, 3);

          I have tried

          stoch60_10 = Stochastics(10,60,3);

          and it is better but still not quite there

          Click image for larger version

Name:	3.png
Views:	19
Size:	48.9 KB
ID:	1304843

          Comment


            #6
            Hello danieldunn2024,

            Please try the suggestion of replotting the indicator data instead of manually applying an indicator. Does that show the correct placement of drawings against the indicator value?
            JesseNinjaTrader Customer Service

            Comment


              #7
              It doesn’t sorry

              Comment


                #8
                Hello danieldunn2024,

                Can you provide an image of the chart after adding the custom plot and removing the manually applied indicators? Also please provide a copy of the code you tried.
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  The highlighted lines were lines I was not expecting to see as the 60-10 stochastic was not under 20

                  And there probably should have been some lines in the middle and one more line on the end of all of them as the stochastic was still under 20


                  Click image for larger version  Name:	4.png Views:	0 Size:	67.4 KB ID:	1304863



                  Even if I remove the 60-10 stochastic it does not make a difference

                  Click image for larger version  Name:	5.png Views:	0 Size:	50.9 KB ID:	1304864
                  Last edited by danieldunn2024; 05-24-2024, 01:13 PM.

                  Comment


                    #10
                    Below is my code, thanks for taking a look

                    Code:
                    #region Using declarations
                    using System;
                    using System.Collections.Generic;
                    using System.ComponentModel;
                    using System.ComponentModel.DataAnnotations;
                    using System.Linq;
                    using System.Text;
                    using System.Threading.Tasks;
                    using System.Windows;
                    using System.Windows.Input;
                    using System.Windows.Media;
                    using System.Xml.Serialization;
                    using NinjaTrader.Cbi;
                    using NinjaTrader.Gui;
                    using NinjaTrader.Gui.Chart;
                    using NinjaTrader.Gui.SuperDom;
                    using NinjaTrader.Gui.Tools;
                    using NinjaTrader.Data;
                    using NinjaTrader.NinjaScript;
                    using NinjaTrader.Core.FloatingPoint;
                    using NinjaTrader.NinjaScript.DrawingTools;
                    using System.Windows.Media;
                    #endregion
                    
                    
                    
                    
                    // This namespace holds Indicators in this folder and is required. Do not change it.
                    namespace NinjaTrader.NinjaScript.Indicators
                    {
                        public class StochasticHighlighter : Indicator
                        {
                            private Stochastics stoch60_10;
                    
                            private double stochValue60_10;
                    
                            protected override void OnStateChange()
                            {
                                if (State == State.SetDefaults)
                                {
                                    Description = @"Highlights a vertical bar when 60-10 stochastics are below 20.";
                                    Name = "MyCustomIndicator";
                                    Calculate = Calculate.OnBarClose;
                                    IsOverlay = true;
                                    AddPlot(Brushes.Transparent, "InvisiblePlot"); // Adding an invisible plot to comply with indicator structure
                                }
                                else if (State == State.DataLoaded)
                                {
                                    stoch60_10 =  Stochastics(10,60,3);
                                }
                            }
                    
                            protected override void OnBarUpdate()
                            {
                                if (CurrentBar < Math.Max(60,10) || CurrentBar == 0)
                                    return;
                            
                                stochValue60_10 = stoch60_10.K[0];
                                
                                if (stochValue60_10 < 20)
                                {
                                    
                                    Draw.VerticalLine(this, "Highlight_" + CurrentBar, 0, Brushes.Red, DashStyleHelper.Solid, 2);
                            
                                     string label = string.Format("Stoch (60-10): {0:F2}", stochValue60_10);
                                     Draw.Text(this, "StochLabel_" + CurrentBar, label, 0, High[0] + TickSize, Brushes.Red);
                                }
                                
                             }    
                        
                        
                        }        
                            
                            
                    }
                    
                    #region NinjaScript generated code. Neither change nor remove.
                    
                    namespace NinjaTrader.NinjaScript.Indicators
                    {
                        public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
                        {
                            private StochasticHighlighter[] cacheStochasticHighlighter;
                            public StochasticHighlighter StochasticHighlighter()
                            {
                                return StochasticHighlighter(Input);
                            }
                    
                            public StochasticHighlighter StochasticHighlighter(ISeries<double> input)
                            {
                                if (cacheStochasticHighlighter != null)
                                    for (int idx = 0; idx < cacheStochasticHighlighter.Length; idx++)
                                        if (cacheStochasticHighlighter[idx] != null &&  cacheStochasticHighlighter[idx].EqualsInput(input))
                                            return cacheStochasticHighlighter[idx];
                                return CacheIndicator<StochasticHighlighter>(new StochasticHighlighter(), input, ref cacheStochasticHighlighter);
                            }
                        }
                    }
                    
                    namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
                    {
                        public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
                        {
                            public Indicators.StochasticHighlighter StochasticHighlighter()
                            {
                                return indicator.StochasticHighlighter(Input);
                            }
                    
                            public Indicators.StochasticHighlighter StochasticHighlighter(ISeries<double> input )
                            {
                                return indicator.StochasticHighlighter(input);
                            }
                        }
                    }
                    
                    namespace NinjaTrader.NinjaScript.Strategies
                    {
                        public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
                        {
                            public Indicators.StochasticHighlighter StochasticHighlighter()
                            {
                                return indicator.StochasticHighlighter(Input);
                            }
                    
                            public Indicators.StochasticHighlighter StochasticHighlighter(ISeries<double> input )
                            {
                                return indicator.StochasticHighlighter(input);
                            }
                        }
                    }
                    
                    #endregion
                    ​

                    Comment


                      #11
                      I have changed this line

                      stoch60_10 = Stochastics(10,60,3);

                      to this

                      stoch60_10 = Stochastics(3,60,10);

                      It seems to work a bit better, is that the same for you?


                      Also improving on this indicator, how can I make it so that I can have this indicator attached to the 5 minute time frame while looking at the three minute time frame on the chart?

                      I want to turn this into a strategy eventually, I just want to create an indicator to begin with and then refine it.

                      I am assuming there will be a way to make a strategy for entries based on the indicators? I am assuming I will be able to take the coding and adjust it slightly.

                      Thank you

                      Comment


                        #12
                        Hello danieldunn2024,

                        You would have to confirm if that is correct for what you wanted to do, the parameters for the stochastic are Stochastics(int periodD, int periodK, int smooth). If changing the parameters around is what you wanted then that should work.

                        When you use multiple timeframes you would have to replot the indicator which is what I had described doing in post 4. To supply a specific dataseries to the indicator you can pass the secondary series, for example: Stochastics(Closes[1], periodD, periodK, smooth)
                        JesseNinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by mmike, Today, 10:33 PM
                        0 responses
                        2 views
                        0 likes
                        Last Post mmike
                        by mmike
                         
                        Started by Pole123, Today, 06:02 PM
                        0 responses
                        8 views
                        0 likes
                        Last Post Pole123
                        by Pole123
                         
                        Started by samish18, Today, 05:27 PM
                        0 responses
                        5 views
                        0 likes
                        Last Post samish18  
                        Started by Tonkingrf1551, Today, 03:59 PM
                        0 responses
                        10 views
                        0 likes
                        Last Post Tonkingrf1551  
                        Started by rocker84, Today, 03:10 PM
                        0 responses
                        14 views
                        0 likes
                        Last Post rocker84  
                        Working...
                        X