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

Bug in Stochastics Oscillator

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

    Bug in Stochastics Oscillator

    Would like to explain here an interesting little bug in the stochastics indicator. I found that the value of the stochastics indicator sometimes exceeded +500, and here comes the explanation.

    Line 57 checks a double value for 0. This is should never be done, if the double value is calculated, because the accuracy of the float is limited to about 15 digits.

    Even worse, the value that is checked for 0, is the result of a recursive data series SUM. If you look, how SUM works, it adds and subtracts a value for every bar. So in case that SUM() should become 0, it carries an error of all the past additions and subtractions for the DataSeries. For a 1min chart with a lookback period of 65 days there are about 140,000 operations performed with all the errors from the float adding up. So if 1 operation yields an error of about 1 E-15, the recursive mechanism of SUM() increases this error hundred- or thousandfold to 1 E-12. I have actually tested this.

    In case SUM(nom,Smooth) and SUM(den, Smooth) both should take the value 0, they may actually have a value of Close[0]*1 E-12. Checking for 0 then returns false, and the Stochastics may jump to +500 or below -300, as observed. So to be on the safe side, the formula for the Stochastics indicator should be modified, either by using an appropriate Almost Equal method which can be used to replace the offensive code, or by simply replacing line 57 with

    K.Set(100 * SUM(nom, Smooth)[0] / (Math.Abs(SUM(den, Smooth)[0]) < Close[0]*Math.Pow(10.0,-11.0) ? 1.0 : SUM(den, Smooth)[0]))

    #2
    Thank you very much Harry - on which instrument, timeframe and session template did you run into those Stoch values?
    BertrandNinjaTrader Customer Service

    Comment


      #3
      To reproduce you will need an input series to the stochastics that has a constant value over k+s periods, where k is the period of the StochasticsK and s the period of the Smoothing . This typically occurs, when the market is quiet. In this case SUM(den, Smooth)[0] and SUM(nom, Smooth)[0] should both return the value 0, but they return the cumulated error instead.

      Most of the input series will only exhibit two decimal places, and the error does not necessarily occur. However, a calculated or adjusted input series is likely to generate the error.

      Easiest to produce this is to apply the Stochastics to a RSI (indicator on indicator) as the RSI as input series does not behave in the same orderly manner as most of the input series from historical data do.

      Originally posted by NinjaTrader_Bertrand View Post
      Thank you very much Harry - on which instrument, timeframe and session template did you run into those Stoch values?

      Comment


        #4
        Thanks again Harry, will check into.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Any news relating to this problem?

          Comment


            #6
            Harry, unfortunately I was not able to reproduce those issues on my end so far, but will keep an eye out.
            BertrandNinjaTrader Customer Service

            Comment


              #7
              Do you agree that checking a double for zero is a problem?

              Originally posted by NinjaTrader_Bertrand View Post
              Harry, unfortunately I was not able to reproduce those issues on my end so far, but will keep an eye out.

              Comment


                #8
                Yes, theoretically I'm inclined to agree, but we still need a case scenario to reproduce this on our end first. I tried your StochRSI setup but never ran into abnormaly values in my testing here, so if you spot this, please let know the exact setup used and I'm happy to investigate further. Thanks
                BertrandNinjaTrader Customer Service

                Comment


                  #9
                  I have run into issues when using this code for an indicator on indicator approach. Currently do not have the time to reproduce and run tests.

                  I think this is a potentially dangerous point, because usually you do not run into problems, as long as you feed indicators with rational numbers. So no problem for NT 6.5. But the indicator on indicator creates residues that will bypass the checking for zero. This is a new feature of NT7 and will on data series with longer lookback periods create this type of error that I already observed.

                  Originally posted by NinjaTrader_Bertrand View Post
                  Yes, theoretically I'm inclined to agree, but we still need a case scenario to reproduce this on our end first. I tried your StochRSI setup but never ran into abnormaly values in my testing here, so if you spot this, please let know the exact setup used and I'm happy to investigate further. Thanks

                  Comment


                    #10
                    Thanks for the insight Harry - I see your point, will keep it on my list to check into and keep an eye on through testings.
                    BertrandNinjaTrader Customer Service

                    Comment


                      #11
                      Harry,

                      Would you mind sending me a PM to dierk AT ninjatrader DOT com on that subject?

                      Thanks

                      Comment


                        #12
                        That was back in March, so I have to try to rebuild the problem. I had used the stochastics in an indicator on indicator approach. This led to rounding errors, which I had tracked and the rounding errors produced a near zero division sending the Stochastics up to values of 500 and more on my charts. It does not happen, when applying the Stochastics on a price chart, as the tickSize does prevent the rounding errors. That is what I have retained, but will dig further to prepare an example and send it to you per mail. I had modified my stochastics formula in provisory way, and that did the trick.

                        Originally posted by NinjaTrader_Dierk View Post
                        Harry,

                        Would you mind sending me a PM to dierk AT ninjatrader DOT com on that subject?

                        Thanks

                        Comment


                          #13
                          Thanks, we'll try to reproduce too.

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by trilliantrader, 04-18-2024, 08:16 AM
                          5 responses
                          22 views
                          0 likes
                          Last Post trilliantrader  
                          Started by Davidtowleii, Today, 12:15 AM
                          0 responses
                          3 views
                          0 likes
                          Last Post Davidtowleii  
                          Started by guillembm, Yesterday, 11:25 AM
                          2 responses
                          9 views
                          0 likes
                          Last Post guillembm  
                          Started by junkone, 04-21-2024, 07:17 AM
                          9 responses
                          70 views
                          0 likes
                          Last Post jeronymite  
                          Started by mgco4you, Yesterday, 09:46 PM
                          1 response
                          14 views
                          0 likes
                          Last Post NinjaTrader_Manfred  
                          Working...
                          X