From 957e57e50585d1f80b779f9627a797d89908ecc1 Mon Sep 17 00:00:00 2001 From: Onur Cinar Date: Thu, 22 Jun 2023 13:02:24 -0700 Subject: [PATCH] Fixed the NaN numbers in Stochastic Oscillator. (#123) # Describe Request Fixed the NaN numbers in Stochastic Oscillator. Fixed #122 # Change Type Bug fix. --- helper.go | 15 +++++++++++++++ momentum_indicators.go | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/helper.go b/helper.go index e6f80cc..e74250f 100644 --- a/helper.go +++ b/helper.go @@ -290,3 +290,18 @@ func abs(values []float64) []float64 { return result } + +// fillNaNWith fills the NaN values with the given fill value. +func fillNaNWith(values []float64, fill float64) []float64 { + result := make([]float64, len(values)) + + for i, value := range values { + if math.IsNaN(value) { + value = fill + } + + result[i] = value + } + + return result +} diff --git a/momentum_indicators.go b/momentum_indicators.go index e8c235f..b021ed8 100644 --- a/momentum_indicators.go +++ b/momentum_indicators.go @@ -184,7 +184,7 @@ func StochasticOscillator(high, low, closing []float64) ([]float64, []float64) { lowestLow14 := Min(14, low) k := multiplyBy(divide(subtract(closing, lowestLow14), subtract(highestHigh14, lowestLow14)), float64(100)) - d := Sma(3, k) + d := Sma(3, fillNaNWith(k, 0)) return k, d }