bbgo_origin/pkg/indicator/obv_test.go

62 lines
1.4 KiB
Go
Raw Permalink Normal View History

2021-05-10 09:46:46 +00:00
package indicator
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
2021-05-10 09:46:46 +00:00
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
2021-05-10 09:46:46 +00:00
)
const Delta = 1e-9
2021-05-10 10:16:12 +00:00
func Test_calculateOBV(t *testing.T) {
buildKLines := func(prices, volumes []fixedpoint.Value) (kLines []types.KLine) {
2021-05-10 09:46:46 +00:00
for i, p := range prices {
kLines = append(kLines, types.KLine{High: p, Low: p, Close: p, Volume: volumes[i]})
}
return kLines
}
var easy1 = []byte(`[3, 2, 1, 4]`)
var easy2 = []byte(`[3, 2, 2, 6]`)
var input1 []fixedpoint.Value
var input2 []fixedpoint.Value
_ = json.Unmarshal(easy1, &input1)
_ = json.Unmarshal(easy2, &input2)
2021-05-10 09:46:46 +00:00
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(
[]fixedpoint.Value{fixedpoint.Zero}, []fixedpoint.Value{fixedpoint.One},
),
2021-05-10 09:46:46 +00:00
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(input1, input2),
2021-05-10 09:46:46 +00:00
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)
assert.Equal(t, len(obv.Values), len(tt.want))
for i, v := range obv.Values {
assert.InDelta(t, v, tt.want[i], Delta)
2021-05-10 09:46:46 +00:00
}
})
}
}