mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-25 16:25:16 +00:00
Merge pull request #1190 from c9s/feature/v2indicator-pivotlow
This commit is contained in:
commit
8792000be0
|
@ -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)
|
||||
}
|
||||
|
|
27
pkg/indicator/v2_pivothigh.go
Normal file
27
pkg/indicator/v2_pivothigh.go
Normal 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
|
||||
}
|
27
pkg/indicator/v2_pivotlow.go
Normal file
27
pkg/indicator/v2_pivotlow.go
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user