indicator: fix pivot low window calculation

This commit is contained in:
c9s 2022-08-24 17:34:01 +08:00
parent 8a020c34e3
commit 1a9c9a6d30
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 76 additions and 13 deletions

View File

@ -1,11 +1,8 @@
package indicator package indicator
import ( import (
"fmt"
"time" "time"
log "github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
) )
@ -15,6 +12,8 @@ type PivotLow struct {
types.IntervalWindow types.IntervalWindow
RightWindow int `json:"rightWindow"`
Lows types.Float64Slice Lows types.Float64Slice
Values types.Float64Slice Values types.Float64Slice
EndTime time.Time EndTime time.Time
@ -45,9 +44,8 @@ func (inc *PivotLow) Update(value float64) {
return return
} }
low, err := calculatePivotLow(inc.Lows, inc.Window) low, ok := calculatePivotLow(inc.Lows, inc.Window, inc.RightWindow)
if err != nil { if !ok {
log.WithError(err).Errorf("can not calculate pivot low")
return return
} }
@ -66,17 +64,31 @@ func (inc *PivotLow) PushK(k types.KLine) {
inc.EmitUpdate(inc.Last()) inc.EmitUpdate(inc.Last())
} }
func calculatePivotLow(lows types.Float64Slice, window int) (float64, error) { func calculatePivotLow(lows types.Float64Slice, left, right int) (float64, bool) {
length := len(lows) length := len(lows)
if length == 0 || length < window {
return 0., fmt.Errorf("insufficient elements for calculating with window = %d", window) if right == 0 {
right = left
}
if length == 0 || length < left+right+1 {
return 0.0, false
} }
end := length - 1 end := length - 1
min := lows[end-(window-1):].Min() index := end - right
if min == lows.Index(int(window/2.)-1) { val := lows[index]
return min, nil
for i := index - left; i <= index+right; i++ {
if i == index {
continue
}
// return if we found lower value
if lows[i] < val {
return 0.0, false
}
} }
return 0., nil return val, true
} }

View File

@ -0,0 +1,51 @@
package indicator
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_calculatePivotLow(t *testing.T) {
t.Run("normal", func(t *testing.T) {
low, ok := calculatePivotLow([]float64{15.0, 13.0, 12.0, 10.0, 14.0, 15.0}, 2, 2)
// ^left ----- ^pivot ---- ^right
assert.True(t, ok)
assert.Equal(t, 10.0, low)
low, ok = calculatePivotLow([]float64{15.0, 13.0, 12.0, 10.0, 14.0, 9.0}, 2, 2)
// ^left ----- ^pivot ---- ^right
assert.False(t, ok)
low, ok = calculatePivotLow([]float64{15.0, 9.0, 12.0, 10.0, 14.0, 15.0}, 2, 2)
// ^left ----- ^pivot ---- ^right
assert.False(t, ok)
})
t.Run("different left and right", func(t *testing.T) {
low, ok := calculatePivotLow([]float64{11.0, 12.0, 16.0, 15.0, 13.0, 12.0, 10.0, 14.0, 15.0}, 5, 2)
// ^left ---------------------- ^pivot ---- ^right
assert.True(t, ok)
assert.Equal(t, 10.0, low)
low, ok = calculatePivotLow([]float64{9.0, 8.0, 16.0, 15.0, 13.0, 12.0, 10.0, 14.0, 15.0}, 5, 2)
// ^left ---------------------- ^pivot ---- ^right
// 8.0 < 10.0
assert.False(t, ok)
assert.Equal(t, 0.0, low)
})
t.Run("right window 0", func(t *testing.T) {
low, ok := calculatePivotLow([]float64{15.0, 13.0, 12.0, 10.0, 14.0, 15.0}, 2, 0)
assert.True(t, ok)
assert.Equal(t, 10.0, low)
})
t.Run("insufficient length", func(t *testing.T) {
low, ok := calculatePivotLow([]float64{15.0, 13.0, 12.0, 10.0, 14.0, 15.0}, 3, 3)
assert.False(t, ok)
assert.Equal(t, 0.0, low)
})
}