-
Notifications
You must be signed in to change notification settings - Fork 143
/
indicator_modified_moving_average.go
48 lines (38 loc) · 1.35 KB
/
indicator_modified_moving_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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package techan
import "github.com/sdcoffey/big"
type modifiedMovingAverageIndicator struct {
indicator Indicator
window int
resultCache resultCache
}
// NewMMAIndicator returns a derivative indciator which returns the modified moving average of the underlying
// indictator. An in-depth explanation can be found here:
// https://en.wikipedia.org/wiki/Moving_average#Modified_moving_average
func NewMMAIndicator(indicator Indicator, window int) Indicator {
return &modifiedMovingAverageIndicator{
indicator: indicator,
window: window,
resultCache: make([]*big.Decimal, 10000),
}
}
func (mma *modifiedMovingAverageIndicator) Calculate(index int) big.Decimal {
if cachedValue := returnIfCached(mma, index, func(i int) big.Decimal {
return NewSimpleMovingAverage(mma.indicator, mma.window).Calculate(i)
}); cachedValue != nil {
return *cachedValue
}
todayVal := mma.indicator.Calculate(index)
lastVal := mma.Calculate(index - 1)
result := lastVal.Add(big.NewDecimal(1.0 / float64(mma.window)).Mul(todayVal.Sub(lastVal)))
cacheResult(mma, index, result)
return result
}
func (mma modifiedMovingAverageIndicator) cache() resultCache {
return mma.resultCache
}
func (mma *modifiedMovingAverageIndicator) setCache(cache resultCache) {
mma.resultCache = cache
}
func (mma modifiedMovingAverageIndicator) windowSize() int {
return mma.window
}