Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

query on stochastic and fast stochastic

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

    query on stochastic and fast stochastic

    Hi NT,

    Looking at the code for these indicators, it seems the calculation is non-standard.
    Taking the Fast Stochastic for example, the lines of interest are:
    nom.Set(Close[0] - MIN(Low, PeriodK)[0]);
    den.Set(MAX(High, PeriodK)[0] - MIN(Low, PeriodK)[0]);
    ...
    double nomSum = SUM(nom, PeriodD)[0];
    double denSum = SUM(den, PeriodD)[0];
    ...
    D.Set(Math.Min(100, 100 * nomSum / denSum));
    So the %D number is average(C-L) / average(H-L). But this is not the same as the average( (C-L) / (H-L) ). Which is what I expected, because every reference I can find on the stochastic, claims that %D is a moving average of %K and %K is (C-L)/(H-L).

    There is a similar situation with the regular Stochastic indicator, where %K should be smoothed by a moving average and then %D should be the result smoothed once more.

    Am I missing something? I am no expert on these indicators, nor on NinjaScript programming.

    thanks

    #2
    Hello,

    Can you give me an example as what your using for reference.

    The stochastic indicator has been with us for many years unchanged and this would be my first query on it. Therefor I highly doubt a programming issue but would look into if you felt that it was warranted. To my knowledge it programmed correctly.

    I look forward to assisting you further.

    Comment


      #3
      Originally posted by trapezoid View Post
      Hi NT,

      Looking at the code for these indicators, it seems the calculation is non-standard.
      Taking the Fast Stochastic for example, the lines of interest are:
      nom.Set(Close[0] - MIN(Low, PeriodK)[0]);
      den.Set(MAX(High, PeriodK)[0] - MIN(Low, PeriodK)[0]);
      ...
      double nomSum = SUM(nom, PeriodD)[0];
      double denSum = SUM(den, PeriodD)[0];
      ...
      D.Set(Math.Min(100, 100 * nomSum / denSum));
      So the %D number is average(C-L) / average(H-L). But this is not the same as the average( (C-L) / (H-L) ). Which is what I expected, because every reference I can find on the stochastic, claims that %D is a moving average of %K and %K is (C-L)/(H-L).

      There is a similar situation with the regular Stochastic indicator, where %K should be smoothed by a moving average and then %D should be the result smoothed once more.

      Am I missing something? I am no expert on these indicators, nor on NinjaScript programming.

      thanks
      Arithmetically a ratio of averages over the same period will be identical to a ratio of sums over the same period. The formula is correct: it just does away with an extraneous division, so to speak, by the same number, of the numerator and denominator of a calculation.

      Also in this particular case Average(H - L) is identically equal to (H - L), as (H - L), per definition, is constant over any fixed period, so it is even clearer that they are the same.
      Last edited by koganam; 07-11-2011, 10:15 AM.

      Comment


        #4
        Hello Koganam,

        Thanks for helping with this query. I am sure I have made a simple mistake somewhere but I still do not see it. I agree that ratios of sums and ratios of averages are equivalent, because the normalisation cancels out. But what I am actually referring to is the difference between the average of ratios and the ratio of averages, which are not the same.

        Take for example, 1/2,1/100. The ratio of the averages is (1+1)/(2+100) = 1/51 (omitting the divisor which cancels), The average of the ratios is 0.5*(1/2) + 0.5*(1/100) = 51/200

        The results are vastly different in this case because the scale of the two values was deliberately chosen to be very diffferent.

        Also, regarding your comment:
        "Also in this particular case Average(H - L) is identically equal to (H - L), as (H - L), per definition, is constant over any fixed period, so it is even clearer that they are the same. "
        I think because we are using a sliding window for %K calculation, the effective period is not fixed when the result is averaged for the %D calculation.

        Brett,

        Thanks for your response. Here is an example of a reference


        If you read down to the description of the fast and slow stochastic, it says:
        Fast Stochastic Oscillator:
        • Fast %K = %K basic calculation
        • Fast %D = 3-period SMA of Fast %K

        Slow Stochastic Oscillator:
          • Slow %K = Fast %K smoothed with 3-period SMA
          • Slow %D = 3-period SMA of Slow %K


        I also checked the C source code of ta-lib http://ta-lib.org/

        The widely-used ta-lib is opensource, but written in C, and I am not sure if you follow that, but the implementation seems to also compute %D as an SMA of %K. Here is a relevant snippet (full source code is attached). The first bit calculates the %K value, and the second bit calls a "MA" function on the result, agreeing with the reference above.
        /* Calculate stochastic. */
        tempBuffer[outIdx++] = (inClose[today]-lowest)/diff;

        ........( code skipped) ..............

        /* Un-smoothed K calculation completed. This K calculation is not returned
        * to the caller. It is always smoothed and then return.
        * Some documentation will refer to the smoothed version as being
        * "K-Slow", but often this end up to be shorten to "K".
        */
        retCode = FUNCTION_CALL_DOUBLE(MA)( 0, outIdx-1,
        tempBuffer, optInSlowK_Period,
        optInSlowK_MAType,
        outBegIdx, outNBElement, tempBuffer );
        I understand that this indicator has been widely used. As I said, I am sure I am missing something obvious here, but I would very much like to know what is going on.

        thanks
        Attached Files
        Last edited by trapezoid; 07-12-2011, 01:51 AM.

        Comment


          #5
          Hello,

          Thanks for that.

          Just to be clear so that I can submit this to development as I need the simplified version to send to them so they can quickly review.

          Can you please let me know what you would change our formula too or what change you would make? I could then discuss with them and let you know if this is incorrect or correct and why.

          I look forward to assisting you further.

          Comment


            #6
            Ah! Lightbulb goes off. You are saying the %D should be the moving average of %K, so cannot be calculated from the (changing each bar) static sums of values used to create %K.

            I think you are right about that one. In fact, that also means that the calculation of %K in the Stochastic (would be called the Full Stochastic), is defective, as in there too, %K should be the SMA smoothed raw %K value.

            I apologize. I should have read the question a whole lot more carefully before I responded.

            Comment


              #7
              Koganam,

              Thanks for clarification.

              Since you also see issue here I went ahead and asked my product manager to review this thread to get his input. I will re-post when I have clarification.

              Comment


                #8
                Originally posted by NinjaTrader_Brett View Post
                Hello,

                Thanks for that.

                Just to be clear so that I can submit this to development as I need the simplified version to send to them so they can quickly review.

                Can you please let me know what you would change our formula too or what change you would make? I could then discuss with them and let you know if this is incorrect or correct and why.

                I look forward to assisting you further.
                For the Fast Stochastic:


                Code:
                		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)
                                K.Set(CurrentBar == 0 ? 50 : K[1]);
                            else
                                K.Set(Math.Min(100, Math.Max(0, 100 * nom[0] / den[0])));
                
                                D.Set(SMA(K, PeriodD)[0]);
                        }
                For what you call the Stochastic, called the Full Stochastic in most other places, attached.
                Attached Files

                Comment


                  #9
                  kogonam,

                  hold still looking into.
                  Last edited by NinjaTrader_Brett; 07-12-2011, 09:48 AM.

                  Comment


                    #10
                    Koganam and trapezoid,

                    We are changing this to use SMA(%K,PeriodD); in future releases of the software.

                    There are two schools of thought and multiple methods to doing this. The Stochastics smoothing method we use now yields similar results to the SMA() method. However can be different in some cases. We looked around and found that there are now more sources using SMA() method for stochastics now and as such we will change to be in line with what is starting to become industry standard.

                    Please look forward to this change in the next release of NinjaTrader 7. Thanks for your patience and bringing this to my attention.
                    Last edited by NinjaTrader_Brett; 07-12-2011, 01:12 PM.

                    Comment


                      #11
                      Thanks.

                      It is positive response like this that makes a great product. Now you know why I do not let anybody mess with my NinjaTrader.

                      Comment


                        #12
                        Hi Brett and Konogram,

                        Thank you both for your help.

                        Brett, it is great that you agree and that NT are planning to change their implementation of the two indicators. I look forward to the updated version.

                        Konogram, thank you for chiming in - always helps to have another independent pair of eyes looking at a problem.

                        best regards,

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by SantoshXX, Today, 03:09 AM
                        0 responses
                        6 views
                        0 likes
                        Last Post SantoshXX  
                        Started by DanielTynera, Today, 01:14 AM
                        0 responses
                        2 views
                        0 likes
                        Last Post DanielTynera  
                        Started by yertle, 04-18-2024, 08:38 AM
                        9 responses
                        41 views
                        0 likes
                        Last Post yertle
                        by yertle
                         
                        Started by techgetgame, Yesterday, 11:42 PM
                        0 responses
                        12 views
                        0 likes
                        Last Post techgetgame  
                        Started by sephichapdson, Yesterday, 11:36 PM
                        0 responses
                        2 views
                        0 likes
                        Last Post sephichapdson  
                        Working...
                        X