Merge pull request #1230 from c9s/c9s/indicator-sma-example

This commit is contained in:
c9s 2023-07-11 11:17:04 +08:00 committed by GitHub
commit 32cd19b852
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 2 deletions

View File

@ -26,7 +26,7 @@ type BOLLStream struct {
// -> calculate stdDev -> calculate bandWidth -> get latest SMA -> upBand, downBand
func BOLL(source types.Float64Source, window int, k float64) *BOLLStream {
// bind these indicators before our main calculator
sma := SMA2(source, window)
sma := SMA(source, window)
stdDev := StdDev(source, window)
s := &BOLLStream{

View File

@ -12,7 +12,7 @@ type SMAStream struct {
rawValues *types.Queue
}
func SMA2(source types.Float64Source, window int) *SMAStream {
func SMA(source types.Float64Source, window int) *SMAStream {
s := &SMAStream{
Float64Series: types.NewFloat64Series(),
window: window,

View File

@ -0,0 +1,22 @@
package indicatorv2
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/c9s/bbgo/pkg/types"
)
func TestSMA(t *testing.T) {
source := types.NewFloat64Series()
sma := SMA(source, 9)
data := []float64{1, 2, 3, 4, 5, 6, 7, 8, 9}
for _, d := range data {
source.PushAndEmit(d)
}
assert.InDelta(t, 5, sma.Last(0), 0.001)
assert.InDelta(t, 4.5, sma.Last(1), 0.001)
}