-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ema_big.go
38 lines (30 loc) · 1007 Bytes
/
ema_big.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
package ma
import (
"math/big"
)
var (
bigOne = big.NewFloat(1)
)
// BigEMA represents the state of an Exponential Moving Average (EMA) algorithm.
type BigEMA struct {
constant *big.Float
prev *big.Float
oneMinusConstant *big.Float
}
// NewBigEMA creates a new EMA data structure.
func NewBigEMA(periods uint, sma, smoothing *big.Float) *BigEMA {
if smoothing == nil || smoothing.Cmp(big.NewFloat(0)) == 0 {
smoothing = big.NewFloat(DefaultEMASmoothing)
}
ema := &BigEMA{
constant: new(big.Float).Quo(smoothing, new(big.Float).Add(bigOne, big.NewFloat(float64(periods)))),
prev: sma,
}
ema.oneMinusConstant = new(big.Float).Sub(bigOne, ema.constant)
return ema
}
// Calculate produces the next EMA result given the next input.
func (ema *BigEMA) Calculate(next *big.Float) (result *big.Float) {
ema.prev = new(big.Float).Add(new(big.Float).Mul(next, ema.constant), new(big.Float).Mul(ema.prev, ema.oneMinusConstant))
return new(big.Float).Copy(ema.prev)
}