bbgo_origin/pkg/indicator/pivotlow.go

77 lines
1.4 KiB
Go
Raw Permalink Normal View History

2022-07-26 08:50:45 +00:00
package indicator
import (
"time"
"github.com/c9s/bbgo/pkg/datatype/floats"
2022-07-26 08:50:45 +00:00
"github.com/c9s/bbgo/pkg/types"
)
//go:generate callbackgen -type PivotLow
type PivotLow struct {
2022-07-26 09:00:17 +00:00
types.SeriesBase
2022-07-26 08:50:45 +00:00
2022-07-26 17:30:43 +00:00
types.IntervalWindow
Lows floats.Slice
Values floats.Slice
2022-07-26 08:50:45 +00:00
EndTime time.Time
updateCallbacks []func(value float64)
}
2022-07-26 16:58:05 +00:00
func (inc *PivotLow) Length() int {
return inc.Values.Length()
}
2023-05-31 23:46:50 +00:00
func (inc *PivotLow) Last(i int) float64 {
return inc.Values.Last(i)
2022-07-26 16:58:05 +00:00
}
2022-07-26 09:00:17 +00:00
func (inc *PivotLow) Update(value float64) {
if len(inc.Lows) == 0 {
inc.SeriesBase.Series = inc
2022-07-26 08:50:45 +00:00
}
2022-07-26 09:00:17 +00:00
inc.Lows.Push(value)
2022-07-26 08:50:45 +00:00
2022-07-26 09:00:17 +00:00
if len(inc.Lows) < inc.Window {
2022-07-26 08:50:45 +00:00
return
}
if inc.RightWindow == nil {
inc.RightWindow = &inc.Window
}
low, ok := calculatePivotLow(inc.Lows, inc.Window, *inc.RightWindow)
if !ok {
2022-07-26 08:50:45 +00:00
return
}
2022-07-26 09:00:17 +00:00
if low > 0.0 {
inc.Values.Push(low)
2022-07-26 08:50:45 +00:00
}
2022-07-26 09:00:17 +00:00
}
2022-07-26 08:50:45 +00:00
2022-07-26 09:00:17 +00:00
func (inc *PivotLow) PushK(k types.KLine) {
if k.EndTime.Before(inc.EndTime) {
return
}
inc.Update(k.Low.Float64())
inc.EndTime = k.EndTime.Time()
inc.EmitUpdate(inc.Last(0))
2022-07-26 09:00:17 +00:00
}
func calculatePivotHigh(highs floats.Slice, left, right int) (float64, bool) {
return floats.FindPivot(highs, left, right, func(a, pivot float64) bool {
return a < pivot
})
}
func calculatePivotLow(lows floats.Slice, left, right int) (float64, bool) {
return floats.FindPivot(lows, left, right, func(a, pivot float64) bool {
return a > pivot
})
}