-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathatr.go
90 lines (79 loc) · 3.14 KB
/
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
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
package tart
// Developed by J. Welles Wilder, the Average True Range (ATR)
// is an indicator that measures volatility. As with most of
// his indicators, Wilder designed ATR with commodities and
// daily prices in mind. Commodities are frequently more volatile
// than stocks. They were are often subject to gaps and limit moves,
// which occur when a commodity opens up or down its maximum
// allowed move for the session. A volatility formula based only
// on the high-low range would fail to capture volatility from
// gap or limit moves. Wilder created Average True Range to
// capture this “missing” volatility. It is important to remember
// that ATR does not provide an indication of price direction,
// just volatility.
//
// Wilder features ATR in his 1978 book, New Concepts in Technical
// Trading Systems. This book also includes the Parabolic SAR,
// RSI 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:average_true_range_atr
// https://www.investopedia.com/terms/a/atr.asp
// https://www.fidelity.com/learning-center/trading-investing/technical-analysis/technical-indicator-guide/atr
type Atr struct {
n int64
tr *TRange
ema *Ema
sz int64
}
func NewAtr(n int64) *Atr {
return &Atr{
n: n,
tr: NewTRange(),
ema: NewEma(n, 1.0/float64(n)),
sz: 0,
}
}
func (a *Atr) Update(h, l, c float64) float64 {
a.sz++
tr := a.tr.Update(h, l, c)
if a.sz == 1 {
return 0
}
return a.ema.Update(tr)
}
func (a *Atr) InitPeriod() int64 {
return 1
}
func (a *Atr) Valid() bool {
return a.sz > 1
}
// Developed by J. Welles Wilder, the Average True Range (ATR)
// is an indicator that measures volatility. As with most of
// his indicators, Wilder designed ATR with commodities and
// daily prices in mind. Commodities are frequently more volatile
// than stocks. They were are often subject to gaps and limit moves,
// which occur when a commodity opens up or down its maximum
// allowed move for the session. A volatility formula based only
// on the high-low range would fail to capture volatility from
// gap or limit moves. Wilder created Average True Range to
// capture this “missing” volatility. It is important to remember
// that ATR does not provide an indication of price direction,
// just volatility.
//
// Wilder features ATR in his 1978 book, New Concepts in Technical
// Trading Systems. This book also includes the Parabolic SAR,
// RSI 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:average_true_range_atr
// https://www.investopedia.com/terms/a/atr.asp
// https://www.fidelity.com/learning-center/trading-investing/technical-analysis/technical-indicator-guide/atr
func AtrArr(h, l, c []float64, n int64) []float64 {
out := make([]float64, len(c))
a := NewAtr(n)
for i := 0; i < len(c); i++ {
out[i] = a.Update(h[i], l[i], c[i])
}
return out
}