-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsi.go
46 lines (41 loc) · 801 Bytes
/
rsi.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
package indicators
import "math"
// Relative Strength Index
// https://www.investopedia.com/terms/r/rsi.asp
type RSI struct {
n float64
i float64
cp float64 // previous close
ai float64 // average increase
ad float64 // average decrease
rsi float64
}
func NewRSI(len int) *RSI {
rsi := &RSI{float64(len), 0, math.NaN(), 0, 0, 50.0}
return rsi
}
func (x *RSI) Update(o, h, l, c float64, v int64) {
if math.IsNaN(x.cp) {
x.rsi = 0.5
} else {
n := math.Max(x.n, x.i)
i := 0.0 // increase
d := 0.0 // decrease
if x.cp < c {
i = c - x.cp
} else {
d = x.cp - c
}
x.ai = (x.ai*(n-1) + i) / n
x.ad = (x.ad*(n-1) + d) / n
if x.ad == 0 {
x.rsi = 100
} else {
x.rsi = 100 - 100/(1+x.ai/x.ad)
}
}
x.i++
}
func (x *RSI) Value() float64 {
return x.rsi
}