2022-08-10 11:36:30 +00:00
|
|
|
package drift
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/c9s/bbgo/pkg/indicator"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DriftMA struct {
|
|
|
|
types.SeriesBase
|
2022-08-23 08:22:45 +00:00
|
|
|
drift *indicator.WeightedDrift
|
2022-08-10 11:36:30 +00:00
|
|
|
ma1 types.UpdatableSeriesExtend
|
|
|
|
ma2 types.UpdatableSeriesExtend
|
|
|
|
}
|
|
|
|
|
2022-08-23 08:22:45 +00:00
|
|
|
func (s *DriftMA) Update(value, weight float64) {
|
2022-08-10 11:36:30 +00:00
|
|
|
s.ma1.Update(value)
|
2022-08-23 08:22:45 +00:00
|
|
|
s.drift.Update(s.ma1.Last(), weight)
|
2022-08-10 11:36:30 +00:00
|
|
|
s.ma2.Update(s.drift.Last())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DriftMA) Last() float64 {
|
|
|
|
return s.ma2.Last()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DriftMA) Index(i int) float64 {
|
|
|
|
return s.ma2.Index(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DriftMA) Length() int {
|
|
|
|
return s.ma2.Length()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DriftMA) ZeroPoint() float64 {
|
|
|
|
return s.drift.ZeroPoint()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DriftMA) Clone() *DriftMA {
|
|
|
|
out := DriftMA{
|
|
|
|
drift: s.drift.Clone(),
|
2022-08-23 08:22:45 +00:00
|
|
|
ma1: types.Clone(s.ma1),
|
2022-08-10 11:36:30 +00:00
|
|
|
ma2: types.Clone(s.ma2),
|
|
|
|
}
|
|
|
|
out.SeriesBase.Series = &out
|
|
|
|
return &out
|
|
|
|
}
|
|
|
|
|
2022-08-23 08:22:45 +00:00
|
|
|
func (s *DriftMA) TestUpdate(v, weight float64) *DriftMA {
|
2022-08-10 11:36:30 +00:00
|
|
|
out := s.Clone()
|
2022-08-23 08:22:45 +00:00
|
|
|
out.Update(v, weight)
|
2022-08-10 11:36:30 +00:00
|
|
|
return out
|
|
|
|
}
|