Skip to content

Commit

Permalink
Fixing the StochasticOscillator calculation. Fixes #69. (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
cinar authored May 3, 2022
1 parent 170ffb7 commit fb3ff0d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions momentum_indicators.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ func StochasticOscillator(high, low, closing []float64) ([]float64, []float64) {
checkSameSize(high, low, closing)

highestHigh14 := Max(14, high)
lowestLow14 := Min(15, low)
lowestLow14 := Min(14, low)

k := divide(substract(closing, lowestLow14), multiplyBy(substract(highestHigh14, lowestLow14), float64(100)))
k := multiplyBy(divide(substract(closing, lowestLow14), substract(highestHigh14, lowestLow14)), float64(100))
d := Sma(3, k)

return k, d
Expand Down
30 changes: 30 additions & 0 deletions momentum_indicators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,33 @@ func TestChaikinOscillator(t *testing.T) {
actual, _ := ChaikinOscillator(2, 5, low, high, closing, volume)
testEquals(t, roundDigitsAll(actual, 2), expected)
}

func TestStochasticOscillator(t *testing.T) {
high := []float64{
127.01, 127.62, 126.59, 127.35, 128.17,
128.43, 127.37, 126.42, 126.90, 126.85,
125.65, 125.72, 127.16, 127.72, 127.69,
128.22, 128.27, 128.09, 128.27, 127.74,
}
low := []float64{
125.36, 126.16, 124.93, 126.09, 126.82,
126.48, 126.03, 124.83, 126.39, 125.72,
124.56, 124.57, 125.07, 126.86, 126.63,
126.80, 126.71, 126.80, 126.13, 125.92,
}
closing := []float64{
126.00, 126.60, 127.10, 127.20, 128.10,
128.20, 126.30, 126.00, 126.60, 127.00,
127.50, 128.00, 128.10, 127.29, 127.18,
128.01, 127.11, 127.73, 127.06, 127.33,
}
expectedK := []float64{
38.79, 54.87, 80.67, 84.39, 97.84,
93.43, 39.14, 32.5, 49.17, 60.28,
75.97, 88.89, 91.47, 70.54, 67.7,
89.15, 65.89, 81.91, 64.60, 74.66,
}

actualK, _ := StochasticOscillator(high, low, closing)
testEquals(t, roundDigitsAll(actualK, 2), expectedK)
}

0 comments on commit fb3ff0d

Please sign in to comment.