indicator: refactor pivot function to floats

This commit is contained in:
c9s 2022-09-15 11:49:19 +08:00
parent 539513ada0
commit 432f9df137
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 36 additions and 31 deletions

View File

@ -0,0 +1,34 @@
package floats
func (s Slice) Pivot(left, right int, f func(a, pivot float64) bool) (float64, bool) {
return CalculatePivot(s, left, right, f)
}
func CalculatePivot(values Slice, left, right int, f func(a, pivot float64) bool) (float64, bool) {
length := len(values)
if right == 0 {
right = left
}
if length == 0 || length < left+right+1 {
return 0.0, false
}
end := length - 1
index := end - right
val := values[index]
for i := index - left; i <= index+right; i++ {
if i == index {
continue
}
// return if we found lower value
if !f(values[i], val) {
return 0.0, false
}
}
return val, true
}

View File

@ -63,43 +63,14 @@ func (inc *PivotLow) PushK(k types.KLine) {
inc.EmitUpdate(inc.Last())
}
func calculatePivotF(values floats.Slice, left, right int, f func(a, pivot float64) bool) (float64, bool) {
length := len(values)
if right == 0 {
right = left
}
if length == 0 || length < left+right+1 {
return 0.0, false
}
end := length - 1
index := end - right
val := values[index]
for i := index - left; i <= index+right; i++ {
if i == index {
continue
}
// return if we found lower value
if !f(values[i], val) {
return 0.0, false
}
}
return val, true
}
func calculatePivotHigh(highs floats.Slice, left, right int) (float64, bool) {
return calculatePivotF(highs, left, right, func(a, pivot float64) bool {
return floats.CalculatePivot(highs, left, right, func(a, pivot float64) bool {
return a < pivot
})
}
func calculatePivotLow(lows floats.Slice, left, right int) (float64, bool) {
return calculatePivotF(lows, left, right, func(a, pivot float64) bool {
return floats.CalculatePivot(lows, left, right, func(a, pivot float64) bool {
return a > pivot
})
}