2023-06-01 06:30:16 +00:00
|
|
|
package indicator
|
|
|
|
|
2023-06-04 06:08:20 +00:00
|
|
|
import "github.com/c9s/bbgo/pkg/datatype/floats"
|
|
|
|
|
2023-06-01 06:30:16 +00:00
|
|
|
type MultiplyStream struct {
|
2023-06-09 05:18:08 +00:00
|
|
|
*Float64Series
|
2023-06-04 06:08:20 +00:00
|
|
|
a, b floats.Slice
|
2023-06-01 06:30:16 +00:00
|
|
|
}
|
|
|
|
|
2023-06-04 06:08:20 +00:00
|
|
|
func Multiply(a, b Float64Source) *MultiplyStream {
|
2023-06-01 06:30:16 +00:00
|
|
|
s := &MultiplyStream{
|
|
|
|
Float64Series: NewFloat64Series(),
|
|
|
|
}
|
2023-06-04 06:08:20 +00:00
|
|
|
|
|
|
|
a.OnUpdate(func(v float64) {
|
|
|
|
s.a.Push(v)
|
|
|
|
s.calculate()
|
|
|
|
})
|
|
|
|
b.OnUpdate(func(v float64) {
|
|
|
|
s.b.Push(v)
|
|
|
|
s.calculate()
|
|
|
|
})
|
|
|
|
|
2023-06-01 06:30:16 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2023-06-04 06:08:20 +00:00
|
|
|
func (s *MultiplyStream) calculate() {
|
|
|
|
if s.a.Length() != s.b.Length() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.a.Length() > s.slice.Length() {
|
|
|
|
var numNewElems = s.a.Length() - s.slice.Length()
|
|
|
|
var tailA = s.a.Tail(numNewElems)
|
|
|
|
var tailB = s.b.Tail(numNewElems)
|
|
|
|
var tailC = tailA.Mul(tailB)
|
|
|
|
for _, f := range tailC {
|
|
|
|
s.slice.Push(f)
|
|
|
|
s.EmitUpdate(f)
|
|
|
|
}
|
|
|
|
}
|
2023-06-01 06:30:16 +00:00
|
|
|
}
|