-
-
Notifications
You must be signed in to change notification settings - Fork 296
/
driftma.go
57 lines (48 loc) · 1 KB
/
driftma.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package drift
import (
"github.com/c9s/bbgo/pkg/indicator"
"github.com/c9s/bbgo/pkg/types"
)
type DriftMA struct {
types.SeriesBase
drift *indicator.WeightedDrift
ma1 types.UpdatableSeriesExtend
ma2 types.UpdatableSeriesExtend
}
func (s *DriftMA) Update(value, weight float64) {
s.ma1.Update(value)
if s.ma1.Length() == 0 {
return
}
s.drift.Update(s.ma1.Last(0), weight)
if s.drift.Length() == 0 {
return
}
s.ma2.Update(s.drift.Last(0))
}
func (s *DriftMA) Last(i int) float64 {
return s.ma2.Last(i)
}
func (s *DriftMA) Index(i int) float64 {
return s.ma2.Last(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(),
ma1: types.Clone(s.ma1),
ma2: types.Clone(s.ma2),
}
out.SeriesBase.Series = &out
return &out
}
func (s *DriftMA) TestUpdate(v, weight float64) *DriftMA {
out := s.Clone()
out.Update(v, weight)
return out
}