mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-21 22:43:52 +00:00
indicator: update pivot low and pivot high indicator
This commit is contained in:
parent
9d97eedc0e
commit
8fcc3ee368
|
@ -81,6 +81,11 @@ func (s *StandardIndicatorSet) VWMA(iw types.IntervalWindow) *indicator.VWMA {
|
|||
}
|
||||
|
||||
|
||||
func (s *StandardIndicatorSet) PivotHigh(iw types.IntervalWindow) *indicator.PivotHigh {
|
||||
inc := s.allocateSimpleIndicator(&indicator.PivotHigh{IntervalWindow: iw}, iw)
|
||||
return inc.(*indicator.PivotHigh)
|
||||
}
|
||||
|
||||
func (s *StandardIndicatorSet) PivotLow(iw types.IntervalWindow) *indicator.PivotLow {
|
||||
inc := s.allocateSimpleIndicator(&indicator.PivotLow{IntervalWindow: iw}, iw)
|
||||
return inc.(*indicator.PivotLow)
|
||||
|
|
65
pkg/indicator/pivothigh.go
Normal file
65
pkg/indicator/pivothigh.go
Normal file
|
@ -0,0 +1,65 @@
|
|||
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
|
||||
|
||||
Lows floats.Slice
|
||||
Values floats.Slice
|
||||
EndTime time.Time
|
||||
|
||||
updateCallbacks []func(value float64)
|
||||
}
|
||||
|
||||
func (inc *PivotHigh) Length() int {
|
||||
return inc.Values.Length()
|
||||
}
|
||||
|
||||
func (inc *PivotHigh) Last() float64 {
|
||||
if len(inc.Values) == 0 {
|
||||
return 0.0
|
||||
}
|
||||
|
||||
return inc.Values.Last()
|
||||
}
|
||||
|
||||
func (inc *PivotHigh) Update(value float64) {
|
||||
if len(inc.Lows) == 0 {
|
||||
inc.SeriesBase.Series = inc
|
||||
}
|
||||
|
||||
inc.Lows.Push(value)
|
||||
|
||||
if len(inc.Lows) < inc.Window {
|
||||
return
|
||||
}
|
||||
|
||||
low, ok := calculatePivotHigh(inc.Lows, inc.Window, inc.RightWindow)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
if low > 0.0 {
|
||||
inc.Values.Push(low)
|
||||
}
|
||||
}
|
||||
|
||||
func (inc *PivotHigh) 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())
|
||||
}
|
||||
|
15
pkg/indicator/pivothigh_callbacks.go
Normal file
15
pkg/indicator/pivothigh_callbacks.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
// Code generated by "callbackgen -type PivotHigh"; DO NOT EDIT.
|
||||
|
||||
package indicator
|
||||
|
||||
import ()
|
||||
|
||||
func (inc *PivotHigh) OnUpdate(cb func(value float64)) {
|
||||
inc.updateCallbacks = append(inc.updateCallbacks, cb)
|
||||
}
|
||||
|
||||
func (inc *PivotHigh) EmitUpdate(value float64) {
|
||||
for _, cb := range inc.updateCallbacks {
|
||||
cb(value)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user