mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 00:35:15 +00:00
indicator: refactor pivot function to floats
This commit is contained in:
parent
539513ada0
commit
432f9df137
34
pkg/datatype/floats/pivot.go
Normal file
34
pkg/datatype/floats/pivot.go
Normal 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
|
||||
}
|
|
@ -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
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user