forked from sdcoffey/techan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
indicator_stochastic_oscillator.go
54 lines (44 loc) · 1.48 KB
/
indicator_stochastic_oscillator.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
package techan
import (
"math"
"github.com/sdcoffey/big"
)
type kIndicator struct {
closePrice Indicator
minValue Indicator
maxValue Indicator
window int
}
// NewFastStochasticIndicator returns a derivative Indicator which returns the fast stochastic indicator (%K) for the
// given window.
// https://www.investopedia.com/terms/s/stochasticoscillator.asp
func NewFastStochasticIndicator(series *TimeSeries, timeframe int) Indicator {
return kIndicator{
closePrice: NewClosePriceIndicator(series),
minValue: NewMinimumValueIndicator(NewLowPriceIndicator(series), timeframe),
maxValue: NewMaximumValueIndicator(NewHighPriceIndicator(series), timeframe),
window: timeframe,
}
}
func (k kIndicator) Calculate(index int) big.Decimal {
closeVal := k.closePrice.Calculate(index)
minVal := k.minValue.Calculate(index)
maxVal := k.maxValue.Calculate(index)
if minVal.EQ(maxVal) {
return big.NewDecimal(math.Inf(1))
}
return closeVal.Sub(minVal).Div(maxVal.Sub(minVal)).Mul(big.NewDecimal(100))
}
type dIndicator struct {
k Indicator
window int
}
// NewSlowStochasticIndicator returns a derivative Indicator which returns the slow stochastic indicator (%D) for the
// given window.
// https://www.investopedia.com/terms/s/stochasticoscillator.asp
func NewSlowStochasticIndicator(k Indicator, window int) Indicator {
return dIndicator{k, window}
}
func (d dIndicator) Calculate(index int) big.Decimal {
return NewSimpleMovingAverage(d.k, d.window).Calculate(index)
}