forked from sdcoffey/techan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
indicator_average.go
30 lines (25 loc) · 906 Bytes
/
indicator_average.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
package techan
import "github.com/sdcoffey/big"
type averageIndicator struct {
Indicator
window int
}
// NewAverageGainsIndicator Returns a new average gains indicator, which returns the average gains
// in the given window based on the given indicator.
func NewAverageGainsIndicator(indicator Indicator, window int) Indicator {
return averageIndicator{
NewCumulativeGainsIndicator(indicator, window),
window,
}
}
// NewAverageLossesIndicator Returns a new average losses indicator, which returns the average losses
// in the given window based on the given indicator.
func NewAverageLossesIndicator(indicator Indicator, window int) Indicator {
return averageIndicator{
NewCumulativeLossesIndicator(indicator, window),
window,
}
}
func (ai averageIndicator) Calculate(index int) big.Decimal {
return ai.Indicator.Calculate(index).Div(big.NewDecimal(float64(Min(index+1, ai.window))))
}