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]))
Comment