Skip to content

Commit

Permalink
Rename Std to StdFromSma and have a Std function without the Sma prov…
Browse files Browse the repository at this point in the history
…ided (#107)

Fixes #106
  • Loading branch information
cinar authored Jun 26, 2022
1 parent b2f3489 commit f8c8f86
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
11 changes: 8 additions & 3 deletions volatility_indicators.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func BollingerBandWidth(middleBand, upperBand, lowerBand []float64) ([]float64,
func BollingerBands(closing []float64) ([]float64, []float64, []float64) {
middleBand := Sma(20, closing)

std := Std(20, closing, middleBand)
std := StdFromSma(20, closing, middleBand)
std2 := multiplyBy(std, 2)

upperBand := add(middleBand, std2)
Expand Down Expand Up @@ -115,7 +115,12 @@ func ChandelierExit(high, low, closing []float64) ([]float64, []float64) {
}

// Standard deviation.
func Std(period int, values, ma []float64) []float64 {
func Std(period int, values []float64) []float64 {
return StdFromSma(period, values, Sma(period, values))
}

// Standard deviation from the given SMA.
func StdFromSma(period int, values, sma []float64) []float64 {
result := make([]float64, len(values))

sum2 := 0.0
Expand All @@ -124,7 +129,7 @@ func Std(period int, values, ma []float64) []float64 {
if i < period-1 {
result[i] = 0.0
} else {
result[i] = math.Sqrt(sum2/float64(period) - ma[i]*ma[i])
result[i] = math.Sqrt(sum2/float64(period) - sma[i]*sma[i])
w := values[i-(period-1)]
sum2 -= w * w
}
Expand Down
11 changes: 10 additions & 1 deletion volatility_indicators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,16 @@ func TestStd(t *testing.T) {
expected := []float64{0, 1, 1, 1, 2, 1, 1, 1, 1}
period := 2

actual := Std(period, values, Sma(period, values))
actual := Std(period, values)
testEquals(t, roundDigitsAll(actual, 3), expected)
}

func TestStdFromSma(t *testing.T) {
values := []float64{2, 4, 6, 8, 12, 14, 16, 18, 20}
expected := []float64{0, 1, 1, 1, 2, 1, 1, 1, 1}
period := 2

actual := StdFromSma(period, values, Sma(period, values))
testEquals(t, roundDigitsAll(actual, 3), expected)
}

Expand Down

0 comments on commit f8c8f86

Please sign in to comment.