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-30 10:14:54 +00:00
|
|
|
Lows floats.Slice
|
|
|
|
Values floats.Slice
|
|
|
|
KLines []types.KLine
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *PivotLow) Last() float64 {
|
|
|
|
if len(inc.Values) == 0 {
|
|
|
|
return 0.0
|
|
|
|
}
|
|
|
|
|
|
|
|
return inc.Values.Last()
|
|
|
|
}
|
|
|
|
|
2022-08-30 10:14:54 +00:00
|
|
|
func (inc *PivotLow) update(value float64, k types.KLine) {
|
2022-07-26 09:00:17 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-08-24 09:34:01 +00:00
|
|
|
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-08-30 10:14:54 +00:00
|
|
|
inc.KLines = append(inc.KLines, k)
|
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
|
|
|
|
}
|
|
|
|
|
2022-08-30 10:14:54 +00:00
|
|
|
inc.update(k.Low.Float64(), k)
|
2022-07-26 09:00:17 +00:00
|
|
|
inc.EndTime = k.EndTime.Time()
|
|
|
|
inc.EmitUpdate(inc.Last())
|
|
|
|
}
|
|
|
|
|
2022-08-25 09:31:42 +00:00
|
|
|
func calculatePivotHigh(highs floats.Slice, left, right int) (float64, bool) {
|
2022-09-15 03:49:19 +00:00
|
|
|
return floats.CalculatePivot(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) {
|
2022-09-15 03:49:19 +00:00
|
|
|
return floats.CalculatePivot(lows, left, right, func(a, pivot float64) bool {
|
2022-08-24 09:37:44 +00:00
|
|
|
return a > pivot
|
|
|
|
})
|
|
|
|
}
|