indicator: add v2 pivot low indicator

This commit is contained in:
c9s 2023-06-01 17:17:14 +08:00
parent 8a8edc7bb6
commit 9a486388fa
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 38 additions and 5 deletions

View File

@ -40,6 +40,14 @@ func (f *Float64Series) PushAndEmit(x float64) {
f.EmitUpdate(x) f.EmitUpdate(x)
} }
func (f *Float64Series) Subscribe(source Float64Source, c func(x float64)) {
if sub, ok := source.(Float64Subscription); ok {
sub.AddSubscriber(c)
} else {
source.OnUpdate(c)
}
}
// Bind binds the source event to the target (Float64Calculator) // Bind binds the source event to the target (Float64Calculator)
// A Float64Calculator should be able to calculate the float64 result from a single float64 argument input // A Float64Calculator should be able to calculate the float64 result from a single float64 argument input
func (f *Float64Series) Bind(source Float64Source, target Float64Calculator) { func (f *Float64Series) Bind(source Float64Source, target Float64Calculator) {
@ -60,9 +68,5 @@ func (f *Float64Series) Bind(source Float64Source, target Float64Calculator) {
} }
} }
if sub, ok := source.(Float64Subscription); ok { f.Subscribe(source, c)
sub.AddSubscriber(c)
} else {
source.OnUpdate(c)
}
} }

View File

@ -0,0 +1,29 @@
package indicator
import (
"github.com/c9s/bbgo/pkg/datatype/floats"
)
type PivotLowStream struct {
Float64Series
rawValues floats.Slice
window, rightWindow int
}
func PivotLow2(source Float64Source, window, rightWindow int) *PivotLowStream {
s := &PivotLowStream{
Float64Series: NewFloat64Series(),
window: window,
rightWindow: rightWindow,
}
s.Subscribe(source, func(x float64) {
s.rawValues.Push(x)
if low, ok := calculatePivotLow(s.rawValues, s.window, s.rightWindow); ok {
s.PushAndEmit(low)
}
})
return s
}