-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathadosc.go
98 lines (89 loc) · 3.05 KB
/
adosc.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
package tart
// Developed by Marc Chaikin, the Chaikin Oscillator measures the
// momentum of the Accumulation Distribution Line using the MACD
// formula. (This makes it an indicator of an indicator.) The
// Chaikin Oscillator is the difference between the 3-day and
// 10-day EMAs of the Accumulation Distribution Line. Like other
// momentum indicators, this indicator is designed to anticipate
// directional changes in the Accumulation Distribution Line by
// measuring the momentum behind the movements. A momentum change
// is the first step to a trend change. Anticipating trend changes
// in the Accumulation Distribution Line can help chartists
// anticipate trend changes in the underlying security. The
// Chaikin Oscillator generates signals with crosses above/below
// the zero line or with bullish/bearish divergences.
// https://school.stockcharts.com/doku.php?id=technical_indicators:chaikin_oscillator
// https://www.investopedia.com/terms/c/chaikinoscillator.asp
type AdOsc struct {
fastN int64
slowN int64
fastK1 float64
fastK2 float64
slowK1 float64
slowK2 float64
ad *Ad
fast float64
slow float64
sz int64
}
func NewAdOsc(fastN, slowN int64) *AdOsc {
if slowN < fastN {
fastN, slowN = slowN, fastN
}
return &AdOsc{
fastN: fastN,
slowN: slowN,
fastK1: 2.0 / float64(fastN+1),
fastK2: 1.0 - 2.0/float64(fastN+1),
slowK1: 2.0 / float64(slowN+1),
slowK2: 1 - 2.0/float64(slowN+1),
ad: NewAd(),
fast: 0,
slow: 0,
sz: 0,
}
}
func (a *AdOsc) Update(h, l, c, v float64) float64 {
a.sz++
ad := a.ad.Update(h, l, c, v)
if a.sz == 1 {
a.fast = ad
a.slow = ad
return 0
}
a.fast = ad*a.fastK1 + a.fast*a.fastK2
a.slow = ad*a.slowK1 + a.slow*a.slowK2
if a.sz < a.slowN {
return 0
}
return a.fast - a.slow
}
func (a *AdOsc) InitPeriod() int64 {
return a.slowN - 1
}
func (a *AdOsc) Valid() bool {
return a.sz > a.InitPeriod()
}
// Developed by Marc Chaikin, the Chaikin Oscillator measures the
// momentum of the Accumulation Distribution Line using the MACD
// formula. (This makes it an indicator of an indicator.) The
// Chaikin Oscillator is the difference between the 3-day and
// 10-day EMAs of the Accumulation Distribution Line. Like other
// momentum indicators, this indicator is designed to anticipate
// directional changes in the Accumulation Distribution Line by
// measuring the momentum behind the movements. A momentum change
// is the first step to a trend change. Anticipating trend changes
// in the Accumulation Distribution Line can help chartists
// anticipate trend changes in the underlying security. The
// Chaikin Oscillator generates signals with crosses above/below
// the zero line or with bullish/bearish divergences.
// https://school.stockcharts.com/doku.php?id=technical_indicators:chaikin_oscillator
// https://www.investopedia.com/terms/c/chaikinoscillator.asp
func AdOscArr(h, l, c, v []float64, fastN, slowN int64) []float64 {
out := make([]float64, len(c))
a := NewAdOsc(fastN, slowN)
for i := 0; i < len(c); i++ {
out[i] = a.Update(h[i], l[i], c[i], v[i])
}
return out
}