-
Notifications
You must be signed in to change notification settings - Fork 17
/
diff.go
60 lines (49 loc) · 1.29 KB
/
diff.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
58
59
60
package tart
// This is also known as Momentum (MOM).
// The Momentum (MOM) indicator compares the current price with
// the previous price from a selected number of periods ago.
// This indicator is similar to the “Rate of Change” indicator,
// but the MOM does not normalize the price, so different
// instruments can have different indicator values based on
// their point values.
type Diff struct {
n int64
hist *CBuf
sz int64
}
func NewDiff(n int64) *Diff {
return &Diff{
n: n,
hist: NewCBuf(n),
sz: 0,
}
}
func (d *Diff) Update(v float64) float64 {
d.sz++
old := d.hist.Append(v)
if d.sz <= d.n {
return 0
}
return v - old
}
func (d *Diff) InitPeriod() int64 {
return d.n
}
func (d *Diff) Valid() bool {
return d.sz > d.InitPeriod()
}
// This is also known as Momentum (MOM).
// The Momentum (MOM) indicator compares the current price with
// the previous price from a selected number of periods ago.
// This indicator is similar to the “Rate of Change” indicator,
// but the MOM does not normalize the price, so different
// instruments can have different indicator values based on
// their point values.
func DiffArr(in []float64, n int64) []float64 {
out := make([]float64, len(in))
d := NewDiff(n)
for i, v := range in {
out[i] = d.Update(v)
}
return out
}