Skip to content

Commit

Permalink
Fixed the NaN numbers in Stochastic Oscillator. (#123)
Browse files Browse the repository at this point in the history
# Describe Request

Fixed the NaN numbers in Stochastic Oscillator.

Fixed #122 

# Change Type

Bug fix.
  • Loading branch information
cinar authored Jun 22, 2023
1 parent cfa1965 commit 957e57e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion momentum_indicators.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 957e57e

Please sign in to comment.