bbgo_origin/pkg/indicator/obv_test.go

48 lines
1.0 KiB
Go
Raw Normal View History

2021-05-10 09:46:46 +00:00
package indicator
import (
"reflect"
"testing"
"github.com/c9s/bbgo/pkg/types"
)
2021-05-10 10:16:12 +00:00
func Test_calculateOBV(t *testing.T) {
2021-05-10 09:46:46 +00:00
buildKLines := func(prices, volumes []float64) (kLines []types.KLine) {
for i, p := range prices {
kLines = append(kLines, types.KLine{High: p, Low: p, Close: p, Volume: volumes[i]})
}
return kLines
}
tests := []struct {
name string
kLines []types.KLine
window int
2021-05-22 12:20:48 +00:00
want types.Float64Slice
2021-05-10 09:46:46 +00:00
}{
{
name: "trivial_case",
kLines: buildKLines([]float64{0}, []float64{1}),
window: 0,
2021-05-22 12:20:48 +00:00
want: types.Float64Slice{1.0},
2021-05-10 09:46:46 +00:00
},
{
name: "easy_case",
kLines: buildKLines([]float64{3, 2, 1, 4}, []float64{3, 2, 2, 6}),
window: 0,
2021-05-22 12:20:48 +00:00
want: types.Float64Slice{3, 1, -1, 5},
2021-05-10 09:46:46 +00:00
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
obv := OBV{IntervalWindow: types.IntervalWindow{Window: tt.window}}
obv.calculateAndUpdate(tt.kLines)
if !reflect.DeepEqual(obv.Values, tt.want) {
t.Errorf("calculateAndUpdate() = %v, want %v", obv.Values, tt.want)
}
})
}
}