2023-05-26 06:31:06 +00:00
|
|
|
package indicator
|
|
|
|
|
2023-05-30 03:35:24 +00:00
|
|
|
import (
|
|
|
|
"github.com/c9s/bbgo/pkg/datatype/floats"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
2023-05-26 06:31:06 +00:00
|
|
|
//go:generate callbackgen -type Float64Updater
|
|
|
|
type Float64Updater struct {
|
|
|
|
updateCallbacks []func(v float64)
|
2023-05-30 03:35:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Float64Series struct {
|
|
|
|
types.SeriesBase
|
|
|
|
Float64Updater
|
2023-05-30 04:13:55 +00:00
|
|
|
slice floats.Slice
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFloat64Series(v ...float64) Float64Series {
|
|
|
|
s := Float64Series{}
|
|
|
|
s.slice = v
|
|
|
|
s.SeriesBase.Series = s.slice
|
|
|
|
return s
|
2023-05-30 03:35:24 +00:00
|
|
|
}
|
|
|
|
|
2023-05-31 11:35:44 +00:00
|
|
|
func (f *Float64Series) Last(i int) float64 {
|
|
|
|
return f.slice.Last(i)
|
2023-05-30 03:35:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Float64Series) Index(i int) float64 {
|
2023-05-31 23:58:58 +00:00
|
|
|
return f.Last(i)
|
2023-05-30 03:35:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Float64Series) Length() int {
|
|
|
|
return len(f.slice)
|
2023-05-26 06:31:06 +00:00
|
|
|
}
|
2023-05-31 23:58:58 +00:00
|
|
|
|
|
|
|
func (f *Float64Series) PushAndEmit(x float64) {
|
|
|
|
f.slice.Push(x)
|
|
|
|
f.EmitUpdate(x)
|
|
|
|
}
|
|
|
|
|
2023-06-01 09:17:14 +00:00
|
|
|
func (f *Float64Series) Subscribe(source Float64Source, c func(x float64)) {
|
|
|
|
if sub, ok := source.(Float64Subscription); ok {
|
|
|
|
sub.AddSubscriber(c)
|
|
|
|
} else {
|
|
|
|
source.OnUpdate(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-31 23:58:58 +00:00
|
|
|
// 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) {
|
|
|
|
var c func(x float64)
|
|
|
|
|
|
|
|
// optimize the truncation check
|
|
|
|
trc, canTruncate := target.(Float64Truncator)
|
|
|
|
if canTruncate {
|
|
|
|
c = func(x float64) {
|
|
|
|
y := target.Calculate(x)
|
|
|
|
target.PushAndEmit(y)
|
|
|
|
trc.Truncate()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c = func(x float64) {
|
|
|
|
y := target.Calculate(x)
|
|
|
|
target.PushAndEmit(y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-01 09:17:14 +00:00
|
|
|
f.Subscribe(source, c)
|
2023-05-31 23:58:58 +00:00
|
|
|
}
|