-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavg_true_range.go
204 lines (161 loc) · 4.5 KB
/
avg_true_range.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package technical
import (
"math"
)
/*
Average True Range (ATR) developed by J. Welles Wilder Jr
is a technical analysis indicator that measures the price change volatility.
*/
// TrueRange64 (TR) computes the ATR Wilder True Range for the given period
// True range is defined as the following:
// max(High, last) − min(Low, last)
// where last is the previous period close, or last value
func TrueRange64(period []float64, last float64) float64 {
if len(period) == 0 {
return 0.0
}
magic := 4000000000.0 // magic number
high := 0.0
low := magic // first val is magic number
for _, v := range period {
if v > high {
high = v
}
if v < low && v > 0.0 {
low = v
}
}
if last == 0.0 {
v := high - low
if v < 0.0 {
return 0.0
}
return v
}
v := math.Max(high, last) - math.Min(low, last)
// if period is all 0 or negative return 0
if v < 0.0 || v == magic {
return 0.0
}
return v
}
// TrueRange32 is 32 bit version of TrueRange64
func TrueRange32(period []float32, last float32) float32 {
if len(period) == 0 {
return 0.0
}
magic := float32(4000000000.0) // magic number
high := float32(0.0)
low := magic // first val is magic number
for _, v := range period {
if v > high {
high = v
}
if v < low && v > 0.0 {
low = v
}
}
if last == 0.0 {
v := high - low
if v < 0.0 {
return 0.0
}
return v
}
v := float32(math.Max(float64(high), float64(last)) - math.Min(float64(low), float64(last)))
// if period is all 0 or negative return 0
if v < 0.0 || v == magic {
return 0.0
}
return v
}
// RollingATR64 computes the next ATR value based on the prior periods ATR (lastATR),
// the current periods TrueRange64 (curTR), and the number of periods (n)
func RollingATR64(lastATR float64, curTR float64, n int) float64 {
if n == 0 {
return 0.0
}
return (lastATR*float64(n-1) + curTR) / float64(n)
}
// RollingATR32 is 32 bit version of RollingATR64
func RollingATR32(lastATR float32, curTR float32, n int) float32 {
if n == 0 {
return 0.0
}
return (lastATR*float32(n-1) + curTR) / float32(n)
}
// StaticATR64 computes an ATR value from a full length time series based on a
// number of periods (n) and the period size (s)
// It is assumed that the period size (s) is of the same unit of time as
// the indices of the series for the values they represent.
// ex. s = 7 days and a series with 7 elements assumes each index represents
// a value for a daily unit of measurement
// If there are not enough data points in the series to compute a
// complete ATR value the simple average of the partially complete
// true ranges of the derived periods is used as an approximate ATR
// To compute a true range a period must be totally complete
func StaticATR64(series []float64, n int, s int) float64 {
if n == 0 || s == 0 {
return 0.0
}
var trngs []float64
// iterate over the series in period size parts and compute true ranges
for i := 0; i < len(series); i += s {
if i+s > len(series) {
break
}
period := series[i : i+s]
// compute and add true range
last := 0.0
if i != 0 {
last = series[i-1]
}
trngs = append(trngs, TrueRange64(period, last))
}
// if dont have enough period true range values just return avg
if n >= len(trngs) {
return SimpleAvg64(trngs)
}
// first ATR is simple avg of warm up true ranges
// i.e. true ranges of the first n complete periods
// after that, additional ATR values are computed
// using RollingATR64 providing exponential decay of prior atr
atr := SimpleAvg64(trngs[0:n])
for i := n; i < len(trngs); i++ {
atr = RollingATR64(atr, trngs[i], n)
}
return atr
}
// StaticATR32 is 32 bit version of StaticATR64
func StaticATR32(series []float32, n int, s int) float32 {
if n == 0 || s == 0 {
return 0.0
}
var trngs []float32
// iterate over the series in period size parts and compute true ranges
for i := 0; i < len(series); i += s {
if i+s > len(series) {
break
}
period := series[i : i+s]
// compute and add true range
last := float32(0.0)
if i != 0 {
last = series[i-1]
}
trngs = append(trngs, TrueRange32(period, last))
}
// if dont have enough period true range values just return avg
if n >= len(trngs) {
return SimpleAvg32(trngs)
}
// first ATR is simple avg of warm up true ranges
// i.e. true ranges of the first n complete periods
// after that, additional ATR values are computed
// using RollingATR32 providing exponential decay of prior atr
atr := SimpleAvg32(trngs[0:n])
for i := n; i < len(trngs); i++ {
atr = RollingATR32(atr, trngs[i], n)
}
return atr
}