-
Notifications
You must be signed in to change notification settings - Fork 30
/
boll.go
52 lines (46 loc) · 1.02 KB
/
boll.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
package indicator
import (
"math"
)
type Boll struct {
n int
k float64
}
//NewBoll(20, 2)
func NewBoll(n int, k int32) *Boll {
return &Boll{n: n, k: float64(k)}
}
func (this *Boll) sma(lines []Kline) float64 {
s := len(lines)
var sum float64 = 0
for i := 0; i < s; i++ {
sum += float64(lines[i].Close)
}
return sum / float64(s)
}
func (this *Boll) dma(lines []Kline, ma float64) float64 {
s := len(lines)
//log.Println(s)
var sum float64 = 0
for i := 0; i < s; i++ {
sum += (lines[i].Close - ma) * (lines[i].Close - ma)
}
return math.Sqrt(sum / float64(this.n-1))
}
func (this *Boll) Boll(lines []Kline) (mid []float64, up []float64, low []float64) {
l := len(lines)
mid = make([]float64, l)
up = make([]float64, l)
low = make([]float64, l)
if l < this.n {
return
}
for i := l - 1; i > this.n-1; i-- {
ps := lines[(i - this.n + 1): i+1 ]
mid[i] = this.sma(ps)
dm := this.k * this.dma(ps, mid[i])
up[i] = mid[i] + dm
low[i] = mid[i] - dm
}
return
}