/
atr.go
58 lines (45 loc) · 801 Bytes
/
atr.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
package indicators
import (
"github.com/jamestunnell/marketanalysis/models"
)
type ATR struct {
current float64
period int
ma *EMA
warm bool
prev *models.OHLC
}
func NewATR(period int) *ATR {
return &ATR{
current: 0.0,
period: period,
ma: NewEMA(period),
warm: false,
prev: nil,
}
}
func (atr *ATR) Period() int {
return atr.period
}
func (atr *ATR) Warm() bool {
return atr.warm
}
func (atr *ATR) Update(cur *models.OHLC) {
defer atr.updatePrev(cur)
if atr.prev == nil {
return
}
tr := TrueRange(cur, atr.prev)
atr.ma.Update(tr)
if !atr.ma.Warm() {
return
}
atr.warm = true
atr.current = atr.ma.Current()
}
func (atr *ATR) Current() float64 {
return atr.current
}
func (atr *ATR) updatePrev(cur *models.OHLC) {
atr.prev = cur
}