Merge pull request #1190 from c9s/feature/v2indicator-pivotlow

This commit is contained in:
Yo-An Lin 2023-06-01 21:20:22 +08:00 committed by GitHub
commit 8792000be0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 5 deletions

View File

@ -40,6 +40,14 @@ func (f *Float64Series) PushAndEmit(x float64) {
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)
// A Float64Calculator should be able to calculate the float64 result from a single float64 argument input
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 {
sub.AddSubscriber(c)
} else {
source.OnUpdate(c)
}
f.Subscribe(source, c)
}

View File

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

View File

@ -0,0 +1,27 @@
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
}