-
Notifications
You must be signed in to change notification settings - Fork 17
/
rsi.go
108 lines (94 loc) · 3.83 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package tart
// Developed by J. Welles Wilder, the Relative Strength Index (RSI) is a momentum
// oscillator that measures the speed and change of price movements. RSI oscillates
// between zero and 100. According to Wilder, RSI is considered overbought when
// above 70 and oversold when below 30. Signals can also be generated by looking
// for divergences, failure swings and centerline crossovers. RSI can also be used
// to identify the general trend.
// RSI is an extremely popular momentum indicator that has been featured in a number
// of articles, interviews and books over the years. In particular, Constance Brown's
// book, Technical Analysis for the Trading Professional, features the concept of
// bull market and bear market ranges for RSI. Andrew Cardwell, Brown's RSI mentor,
// introduced positive and negative reversals for RSI and, additionally, turned the
// notion of divergence, literally and figuratively, on its head.
// Wilder features RSI in his 1978 book, New Concepts in Technical Trading Systems.
// This book also includes the Parabolic SAR, Average True Range and the Directional
// Movement Concept (ADX). Despite being developed before the computer age,
// Wilder's indicators have stood the test of time and remain extremely popular.
// https://school.stockcharts.com/doku.php?id=technical_indicators:relative_strength_index_rsi
// https://www.investopedia.com/terms/r/rsi.asp
// https://www.fidelity.com/learning-center/trading-investing/technical-analysis/technical-indicator-guide/RSI
type Rsi struct {
n int64
up *Ema
dn *Ema
prevC float64
sz int64
}
func NewRsi(n int64) *Rsi {
k := 1.0 / float64(n)
return &Rsi{
n: n,
up: NewEma(n, k),
dn: NewEma(n, k),
prevC: 0,
sz: 0,
}
}
func (r *Rsi) Update(v float64) float64 {
r.sz++
chg := v - r.prevC
r.prevC = v
if r.sz == 1 {
return 0
}
var up, dn float64
if chg > 0 {
up = r.up.Update(chg)
dn = r.dn.Update(0)
} else {
up = r.up.Update(0)
dn = r.dn.Update(-chg)
}
if r.sz <= r.n {
return 0
}
sum := up + dn
if almostZero(sum) {
return 0
}
return up / sum * 100.0
}
func (r *Rsi) InitPeriod() int64 {
return r.n
}
func (r *Rsi) Valid() bool {
return r.sz > r.InitPeriod()
}
// Developed by J. Welles Wilder, the Relative Strength Index (RSI) is a momentum
// oscillator that measures the speed and change of price movements. RSI oscillates
// between zero and 100. According to Wilder, RSI is considered overbought when
// above 70 and oversold when below 30. Signals can also be generated by looking
// for divergences, failure swings and centerline crossovers. RSI can also be used
// to identify the general trend.
// RSI is an extremely popular momentum indicator that has been featured in a number
// of articles, interviews and books over the years. In particular, Constance Brown's
// book, Technical Analysis for the Trading Professional, features the concept of
// bull market and bear market ranges for RSI. Andrew Cardwell, Brown's RSI mentor,
// introduced positive and negative reversals for RSI and, additionally, turned the
// notion of divergence, literally and figuratively, on its head.
// Wilder features RSI in his 1978 book, New Concepts in Technical Trading Systems.
// This book also includes the Parabolic SAR, Average True Range and the Directional
// Movement Concept (ADX). Despite being developed before the computer age,
// Wilder's indicators have stood the test of time and remain extremely popular.
// https://school.stockcharts.com/doku.php?id=technical_indicators:relative_strength_index_rsi
// https://www.investopedia.com/terms/r/rsi.asp
// https://www.fidelity.com/learning-center/trading-investing/technical-analysis/technical-indicator-guide/RSI
func RsiArr(in []float64, n int64) []float64 {
out := make([]float64, len(in))
r := NewRsi(n)
for i, v := range in {
out[i] = r.Update(v)
}
return out
}