2022-07-26 08:50:45 +00:00
|
|
|
package indicator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2022-08-25 09:31:42 +00:00
|
|
|
"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
|
|
|
|
|
2022-08-25 09:31:42 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-10-16 04:36:52 +00:00
|
|
|
if inc.RightWindow == nil {
|
|
|
|
inc.RightWindow = &inc.Window
|
|
|
|
}
|
|
|
|
|
|
|
|
low, ok := calculatePivotLow(inc.Lows, inc.Window, *inc.RightWindow)
|
2022-08-24 09:34:01 +00:00
|
|
|
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()
|
2023-05-31 11:35:44 +00:00
|
|
|
inc.EmitUpdate(inc.Last(0))
|
2022-07-26 09:00:17 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 09:31:42 +00:00
|
|
|
func calculatePivotHigh(highs floats.Slice, left, right int) (float64, bool) {
|
2023-05-30 05:14:36 +00:00
|
|
|
return floats.FindPivot(highs, left, right, func(a, pivot float64) bool {
|
2022-08-24 09:53:22 +00:00
|
|
|
return a < pivot
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-25 09:31:42 +00:00
|
|
|
func calculatePivotLow(lows floats.Slice, left, right int) (float64, bool) {
|
2023-05-30 05:14:36 +00:00
|
|
|
return floats.FindPivot(lows, left, right, func(a, pivot float64) bool {
|
2022-08-24 09:37:44 +00:00
|
|
|
return a > pivot
|
|
|
|
})
|
|
|
|
}
|