forked from sdcoffey/techan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
indicator_keltner_channel.go
44 lines (37 loc) · 1.3 KB
/
indicator_keltner_channel.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
package techan
import (
"github.com/sdcoffey/big"
)
type keltnerChannelIndicator struct {
ema Indicator
atr Indicator
mul big.Decimal
window int
}
// NewKeltnerChannelUpperIndicator returns Indicator which represents Keltner Channel Upper Band for the given window.
// https://www.investopedia.com/terms/k/keltnerchannel.asp
func NewKeltnerChannelUpperIndicator(series TimeSeries, window int) Indicator {
return keltnerChannelIndicator{
atr: NewAverageTrueRangeIndicator(series, window),
ema: NewEMAIndicator(NewClosePriceIndicator(series), window),
mul: big.ONE,
window: window,
}
}
// NewKeltnerChannelLowerIndicator returns Indicator which represents Keltner Channel Lower Band for the given window.
// https://www.investopedia.com/terms/k/keltnerchannel.asp
func NewKeltnerChannelLowerIndicator(series TimeSeries, window int) Indicator {
return keltnerChannelIndicator{
atr: NewAverageTrueRangeIndicator(series, window),
ema: NewEMAIndicator(NewClosePriceIndicator(series), window),
mul: big.ONE.Neg(),
window: window,
}
}
func (kci keltnerChannelIndicator) Calculate(index int) big.Decimal {
if index <= kci.window-1 {
return big.ZERO
}
coefficient := big.NewFromInt(2).Mul(kci.mul)
return kci.ema.Calculate(index).Add(kci.atr.Calculate(index).Mul(coefficient))
}