2022-08-30 17:43:40 +00:00
|
|
|
package indicator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/datatype/floats"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
//go:generate callbackgen -type PivotHigh
|
|
|
|
type PivotHigh struct {
|
|
|
|
types.SeriesBase
|
|
|
|
|
|
|
|
types.IntervalWindow
|
|
|
|
|
2022-09-06 15:39:13 +00:00
|
|
|
Highs floats.Slice
|
|
|
|
Values floats.Slice
|
2022-08-30 17:43:40 +00:00
|
|
|
EndTime time.Time
|
|
|
|
|
|
|
|
updateCallbacks []func(value float64)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *PivotHigh) Length() int {
|
|
|
|
return inc.Values.Length()
|
|
|
|
}
|
|
|
|
|
2023-05-31 23:46:50 +00:00
|
|
|
func (inc *PivotHigh) Last(i int) float64 {
|
|
|
|
return inc.Values.Last(i)
|
2022-08-30 17:43:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *PivotHigh) Update(value float64) {
|
2022-08-27 05:26:15 +00:00
|
|
|
if len(inc.Highs) == 0 {
|
2022-08-30 17:43:40 +00:00
|
|
|
inc.SeriesBase.Series = inc
|
|
|
|
}
|
|
|
|
|
2022-08-27 05:26:15 +00:00
|
|
|
inc.Highs.Push(value)
|
2022-08-30 17:43:40 +00:00
|
|
|
|
2022-08-27 05:26:15 +00:00
|
|
|
if len(inc.Highs) < inc.Window {
|
2022-08-30 17:43:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-16 04:36:52 +00:00
|
|
|
if inc.RightWindow == nil {
|
|
|
|
inc.RightWindow = &inc.Window
|
|
|
|
}
|
|
|
|
|
|
|
|
high, ok := calculatePivotHigh(inc.Highs, inc.Window, *inc.RightWindow)
|
2022-08-30 17:43:40 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-06 15:39:13 +00:00
|
|
|
if high > 0.0 {
|
|
|
|
inc.Values.Push(high)
|
2022-08-30 17:43:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *PivotHigh) PushK(k types.KLine) {
|
|
|
|
if k.EndTime.Before(inc.EndTime) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-06 15:39:13 +00:00
|
|
|
inc.Update(k.High.Float64())
|
2022-08-30 17:43:40 +00:00
|
|
|
inc.EndTime = k.EndTime.Time()
|
2023-05-31 11:35:44 +00:00
|
|
|
inc.EmitUpdate(inc.Last(0))
|
2022-08-30 17:43:40 +00:00
|
|
|
}
|