-
-
Notifications
You must be signed in to change notification settings - Fork 296
/
tr.go
49 lines (38 loc) · 842 Bytes
/
tr.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
package indicatorv2
import (
"math"
"github.com/c9s/bbgo/pkg/types"
)
// This TRStream calculates the ATR first
type TRStream struct {
// embedded struct
*types.Float64Series
// private states
previousClose float64
}
func TR2(source KLineSubscription) *TRStream {
s := &TRStream{
Float64Series: types.NewFloat64Series(),
}
source.AddSubscriber(func(k types.KLine) {
s.calculateAndPush(k.High.Float64(), k.Low.Float64(), k.Close.Float64())
})
return s
}
func (s *TRStream) calculateAndPush(high, low, cls float64) {
if s.previousClose == .0 {
s.previousClose = cls
return
}
trueRange := high - low
hc := math.Abs(high - s.previousClose)
lc := math.Abs(low - s.previousClose)
if trueRange < hc {
trueRange = hc
}
if trueRange < lc {
trueRange = lc
}
s.previousClose = cls
s.PushAndEmit(trueRange)
}