forked from iamjinlei/go-tart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstochslow.go
77 lines (65 loc) · 2.33 KB
/
stochslow.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
package tart
// The Slow Stochastic Oscillator is a momentum indicator that shows the location
// of the close relative to the high-low range over a set number of periods. The
// indicator can range from 0 to 100. The difference between the Slow and Fast
// Stochastic Oscillator is the Slow %K incorporates a %K slowing period of 3 that
// controls the internal smoothing of %K. Setting the smoothing period to 1 is
// equivalent to plotting the Fast Stochastic Oscillator.
// https://www.fidelity.com/learning-center/trading-investing/technical-analysis/technical-indicator-guide/slow-stochastic
type StochSlow struct {
fastKN int64
slowKN int64
slowDN int64
util int64
stochK *StochasticK
slowK *Ma
slowD *Ma
sz int64
}
func NewStochSlow(fastKN int64, kt MaType, slowKN int64, dt MaType, slowDN int64) *StochSlow {
return &StochSlow{
fastKN: fastKN,
slowKN: slowKN,
slowDN: slowDN,
util: fastKN + slowKN + slowDN - 2,
stochK: NewStochasticK(fastKN),
slowK: NewMa(kt, slowKN),
slowD: NewMa(dt, slowDN),
sz: 0,
}
}
func (s *StochSlow) Update(h, l, c float64) (float64, float64) {
s.sz++
fastK := s.stochK.Update(h, l, c)
if s.sz < s.fastKN {
return 0, 0
}
slowK := s.slowK.Update(fastK)
slowD := s.slowD.Update(slowK)
if s.sz < s.util {
return 0, 0
}
return slowK, slowD
}
func (s *StochSlow) InitPeriod() int64 {
return s.util - 1
}
func (s *StochSlow) Valid() bool {
return s.sz > s.InitPeriod()
}
// The Slow Stochastic Oscillator is a momentum indicator that shows the location
// of the close relative to the high-low range over a set number of periods. The
// indicator can range from 0 to 100. The difference between the Slow and Fast
// Stochastic Oscillator is the Slow %K incorporates a %K slowing period of 3 that
// controls the internal smoothing of %K. Setting the smoothing period to 1 is
// equivalent to plotting the Fast Stochastic Oscillator.
// https://www.fidelity.com/learning-center/trading-investing/technical-analysis/technical-indicator-guide/slow-stochastic
func StochSlowArr(h, l, c []float64, fastKN int64, kt MaType, slowKN int64, dt MaType, slowDN int64) ([]float64, []float64) {
k := make([]float64, len(c))
d := make([]float64, len(c))
s := NewStochSlow(fastKN, kt, slowKN, dt, slowDN)
for i := 0; i < len(c); i++ {
k[i], d[i] = s.Update(h[i], l[i], c[i])
}
return k, d
}