forked from c9s/bbgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smma.go
47 lines (40 loc) · 1.08 KB
/
smma.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
package indicatorv2
import (
"github.com/c9s/bbgo/pkg/types"
)
type SMMAStream struct {
*types.Float64Series
window int
rawValues *types.Queue
source types.Float64Source
}
func SMMA2(source types.Float64Source, window int) *SMMAStream {
s := &SMMAStream{
Float64Series: types.NewFloat64Series(),
window: window,
rawValues: types.NewQueue(window),
source: source,
}
s.Bind(source, s)
return s
}
func (s *SMMAStream) Calculate(v float64) float64 {
var out float64
sourceLen := s.source.Length()
if sourceLen < s.window {
// Until we reach the end of the period, sum the prices.
// First, calculate the sum, and it will be automatically saved too.
s.rawValues.Sum(s.window)
// Then save the input value to use it later on.
s.rawValues.Update(v)
} else if sourceLen == s.window {
// We need the SMA for the first time.
s.rawValues.Update(v)
out = s.rawValues.Mean(s.window)
} else {
// For all the rest values, just use the formula.
last := s.Slice.Last(0)
out = (last*float64((s.window-1.0)) + v) / float64(s.window)
}
return out
}