-
-
Notifications
You must be signed in to change notification settings - Fork 301
/
Copy pathstddev.go
56 lines (43 loc) · 1.05 KB
/
stddev.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
package indicator
import (
"time"
"github.com/c9s/bbgo/pkg/datatype/floats"
"github.com/c9s/bbgo/pkg/types"
)
const MaxNumOfStdev = 600
const MaxNumOfStdevTruncateSize = 300
//go:generate callbackgen -type StdDev
type StdDev struct {
types.SeriesBase
types.IntervalWindow
Values floats.Slice
rawValues *types.Queue
EndTime time.Time
updateCallbacks []func(value float64)
}
func (inc *StdDev) Last(i int) float64 {
return inc.Values.Last(i)
}
func (inc *StdDev) Index(i int) float64 {
return inc.Last(i)
}
func (inc *StdDev) Length() int {
return inc.Values.Length()
}
var _ types.SeriesExtend = &StdDev{}
func (inc *StdDev) Update(value float64) {
if inc.rawValues == nil {
inc.rawValues = types.NewQueue(inc.Window)
inc.SeriesBase.Series = inc
}
inc.rawValues.Update(value)
var std = inc.rawValues.Stdev()
inc.Values.Push(std)
if len(inc.Values) > MaxNumOfStdev {
inc.Values = inc.Values[MaxNumOfStdevTruncateSize-1:]
}
}
func (inc *StdDev) PushK(k types.KLine) {
inc.Update(k.Close.Float64())
inc.EndTime = k.EndTime.Time()
}