forked from sdcoffey/techan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindicator_gains.go
62 lines (51 loc) · 1.61 KB
/
indicator_gains.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
package techan
import "github.com/sdcoffey/big"
type cumulativeIndicator struct {
Indicator
window int
mult big.Decimal
}
// NewCumulativeGainsIndicator returns a derivative indicator which returns all gains made in a base indicator for a given
// window.
func NewCumulativeGainsIndicator(indicator Indicator, window int) Indicator {
return cumulativeIndicator{
Indicator: indicator,
window: window,
mult: big.ONE,
}
}
// NewCumulativeLossesIndicator returns a derivative indicator which returns all losses in a base indicator for a given
// window.
func NewCumulativeLossesIndicator(indicator Indicator, window int) Indicator {
return cumulativeIndicator{
Indicator: indicator,
window: window,
mult: big.ONE.Neg(),
}
}
func (ci cumulativeIndicator) Calculate(index int) big.Decimal {
total := big.NewDecimal(0.0)
for i := Max(1, index-(ci.window-1)); i <= index; i++ {
diff := ci.Indicator.Calculate(i).Sub(ci.Indicator.Calculate(i - 1))
if diff.Mul(ci.mult).GT(big.ZERO) {
total = total.Add(diff.Abs())
}
}
return total
}
type percentChangeIndicator struct {
Indicator
}
// NewPercentChangeIndicator returns a derivative indicator which returns the percent change (positive or negative)
// made in a base indicator up until the given indicator
func NewPercentChangeIndicator(indicator Indicator) Indicator {
return percentChangeIndicator{indicator}
}
func (pgi percentChangeIndicator) Calculate(index int) big.Decimal {
if index == 0 {
return big.ZERO
}
cp := pgi.Indicator.Calculate(index)
cplast := pgi.Indicator.Calculate(index - 1)
return cp.Div(cplast).Sub(big.ONE)
}