-
Notifications
You must be signed in to change notification settings - Fork 17
/
zero_lag.go
34 lines (29 loc) · 907 Bytes
/
zero_lag.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
package go_ehlers_indicators
import "math"
// ZeroLag filter from this paper: http://mesasoftware.com/papers/ZeroLag.pdf
func ZeroLag(vals []float64, length, gainLimit int) []float64 {
ema := make([]float64, len(vals))
ecs := make([]float64, len(vals))
ema[0] = vals[0]
for i := 1; i < len(ecs); i++ {
alpha := 2.0 / (float64(length) + 1.0)
ema[i] = alpha*vals[i] + (1.0-alpha)*ema[i-1]
leastErr := 1e7
var bestGain float64
for v := -gainLimit; v < gainLimit; v++ {
gain := float64(v) / 10.0
ecs[i] = alpha*(ema[i]+gain*(vals[i]-ecs[i-1])) + (1.0-alpha)*ecs[i-1]
err := vals[i] - ecs[i]
absErr := math.Abs(err)
if absErr < leastErr {
leastErr = absErr
bestGain = gain
}
}
ecs[i] = alpha*(ema[i]+bestGain*(vals[i]-ecs[i-1])) + (1-alpha)*ecs[i-1]
}
return ecs
}
func ZeroLagDefault(vals []float64, length int) []float64 {
return ZeroLag(vals, length, 50)
}