-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmacd_big.go
56 lines (47 loc) · 1.59 KB
/
macd_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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package ma
import (
"math/big"
)
// BigMACD represents the state of a Moving Average Convergence Divergence (MACD) algorithm.
type BigMACD struct {
Long *BigEMA
Short *BigEMA
}
// BigMACDResults holds the results fo an MACD calculation.
type BigMACDResults struct {
Long *big.Float
Result *big.Float
Short *big.Float
}
// NewBigMACD creates a new MACD data structure and returns the initial result.
func NewBigMACD(long, short *BigEMA) BigMACD {
return BigMACD{
Long: long,
Short: short,
}
}
// Calculate produces the next MACD result given the next input.
func (macd BigMACD) Calculate(next *big.Float) BigMACDResults {
short := macd.Short.Calculate(next)
long := macd.Long.Calculate(next)
return BigMACDResults{
Long: long,
Result: new(big.Float).Sub(short, long),
Short: short,
}
}
// SignalEMA creates a signal EMA for the current MACD.
//
// The first MACD result *must* be saved in order to create the signal EMA. Then, the next period samples required for
// the creation of the signal EMA must be given. The period length of the EMA is `1 + len(next)`.
func (macd BigMACD) SignalEMA(firstMACDResult *big.Float, next []*big.Float, smoothing *big.Float) (signalEMA *BigEMA, signalResult *big.Float, macdResults []BigMACDResults) {
macdBigs := make([]*big.Float, len(next))
macdResults = make([]BigMACDResults, len(next))
for i, p := range next {
result := macd.Calculate(p)
macdBigs[i] = result.Result
macdResults[i] = result
}
_, sma := NewBigSMA(append([]*big.Float{firstMACDResult}, macdBigs...))
return NewBigEMA(uint(len(next)+1), sma, smoothing), sma, macdResults
}