-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmacd_float64.go
64 lines (52 loc) · 1.83 KB
/
macd_float64.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
63
64
package ma
const (
// DefaultLongMACDPeriod is a common MACD period for the long input EMA algorithm.
DefaultLongMACDPeriod = 26
// DefaultShortMACDPeriod is a common MACD period for the short input EMA algorithm.
DefaultShortMACDPeriod = 12
// DefaultSignalEMAPeriod is a common MACD period for the signal EMA that will crossover the MACD line.
DefaultSignalEMAPeriod = 9
)
// MACD represents the state of a Moving Average Convergence Divergence (MACD) algorithm.
type MACD struct {
Long *EMA
Short *EMA
}
// MACDResults holds the results fo an MACD calculation.
type MACDResults struct {
Long float64
Result float64
Short float64
}
// NewMACD creates a new MACD data structure and returns the initial result.
func NewMACD(long, short *EMA) MACD {
return MACD{
Long: long,
Short: short,
}
}
// Calculate produces the next MACD result given the next input.
func (macd MACD) Calculate(next float64) MACDResults {
short := macd.Short.Calculate(next)
long := macd.Long.Calculate(next)
return MACDResults{
Long: long,
Result: 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 MACD) SignalEMA(firstMACDResult float64, next []float64, smoothing float64) (signalEMA *EMA, signalResult float64, macdResults []MACDResults) {
macdFloats := make([]float64, len(next))
macdResults = make([]MACDResults, len(next))
for i, p := range next {
result := macd.Calculate(p)
macdFloats[i] = result.Result
macdResults[i] = result
}
_, sma := NewSMA(append([]float64{firstMACDResult}, macdFloats...))
return NewEMA(uint(len(next)+1), sma, smoothing), sma, macdResults
}