2021-05-08 16:56:44 +00:00
|
|
|
package indicator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
macd implements moving average convergence divergence indicator
|
|
|
|
|
|
|
|
Moving Average Convergence Divergence (MACD)
|
|
|
|
- https://www.investopedia.com/terms/m/macd.asp
|
2022-07-14 02:45:22 +00:00
|
|
|
- https://school.stockcharts.com/doku.php?id=technical_indicators:macd-histogram
|
2021-05-08 16:56:44 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
//go:generate callbackgen -type MACD
|
|
|
|
type MACD struct {
|
|
|
|
types.IntervalWindow // 9
|
|
|
|
ShortPeriod int // 12
|
|
|
|
LongPeriod int // 26
|
2021-05-22 12:20:48 +00:00
|
|
|
Values types.Float64Slice
|
2022-07-14 02:28:53 +00:00
|
|
|
FastEWMA *EWMA
|
|
|
|
SlowEWMA *EWMA
|
|
|
|
SignalLine *EWMA
|
2021-05-22 12:20:48 +00:00
|
|
|
Histogram types.Float64Slice
|
2021-05-08 16:56:44 +00:00
|
|
|
|
|
|
|
EndTime time.Time
|
|
|
|
|
2022-07-13 17:16:39 +00:00
|
|
|
updateCallbacks []func(value float64)
|
2021-05-08 16:56:44 +00:00
|
|
|
}
|
|
|
|
|
2022-04-18 04:08:21 +00:00
|
|
|
func (inc *MACD) Update(x float64) {
|
2021-05-08 16:56:44 +00:00
|
|
|
if len(inc.Values) == 0 {
|
2022-07-14 02:28:53 +00:00
|
|
|
inc.FastEWMA = &EWMA{IntervalWindow: types.IntervalWindow{Window: inc.ShortPeriod}}
|
|
|
|
inc.SlowEWMA = &EWMA{IntervalWindow: types.IntervalWindow{Window: inc.LongPeriod}}
|
|
|
|
inc.SignalLine = &EWMA{IntervalWindow: types.IntervalWindow{Window: inc.Window}}
|
2021-05-08 16:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// update fast and slow ema
|
2022-04-18 04:08:21 +00:00
|
|
|
inc.FastEWMA.Update(x)
|
|
|
|
inc.SlowEWMA.Update(x)
|
2021-05-08 16:56:44 +00:00
|
|
|
|
|
|
|
// update macd
|
|
|
|
macd := inc.FastEWMA.Last() - inc.SlowEWMA.Last()
|
|
|
|
inc.Values.Push(macd)
|
|
|
|
|
|
|
|
// update signal line
|
|
|
|
inc.SignalLine.Update(macd)
|
|
|
|
|
|
|
|
// update histogram
|
|
|
|
inc.Histogram.Push(macd - inc.SignalLine.Last())
|
|
|
|
}
|
|
|
|
|
2022-07-14 02:28:53 +00:00
|
|
|
func (inc *MACD) Last() float64 {
|
|
|
|
if len(inc.Values) == 0 {
|
|
|
|
return 0.0
|
|
|
|
}
|
|
|
|
|
|
|
|
return inc.Values[len(inc.Values)-1]
|
|
|
|
}
|
|
|
|
|
2022-07-26 10:39:49 +00:00
|
|
|
func (inc *MACD) Length() int {
|
|
|
|
return len(inc.Values)
|
2022-07-13 17:12:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-26 10:39:49 +00:00
|
|
|
func (inc *MACD) PushK(k types.KLine) {
|
|
|
|
inc.Update(k.Close.Float64())
|
2021-05-08 16:56:44 +00:00
|
|
|
}
|
|
|
|
|
2022-07-26 10:39:49 +00:00
|
|
|
func (inc *MACD) MACD() types.SeriesExtend {
|
|
|
|
out := &MACDValues{MACD: inc}
|
|
|
|
out.SeriesBase.Series = out
|
|
|
|
return out
|
2021-05-08 16:56:44 +00:00
|
|
|
}
|
|
|
|
|
2022-07-26 10:39:49 +00:00
|
|
|
func (inc *MACD) Singals() types.SeriesExtend {
|
|
|
|
return inc.SignalLine
|
2021-05-08 16:56:44 +00:00
|
|
|
}
|
2022-04-04 04:14:17 +00:00
|
|
|
|
|
|
|
type MACDValues struct {
|
2022-06-29 12:49:02 +00:00
|
|
|
types.SeriesBase
|
2022-04-04 04:14:17 +00:00
|
|
|
*MACD
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *MACDValues) Last() float64 {
|
|
|
|
if len(inc.Values) == 0 {
|
|
|
|
return 0.0
|
|
|
|
}
|
2022-07-14 02:28:53 +00:00
|
|
|
|
2022-04-04 04:14:17 +00:00
|
|
|
return inc.Values[len(inc.Values)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *MACDValues) Index(i int) float64 {
|
|
|
|
length := len(inc.Values)
|
|
|
|
if length == 0 || length-1-i < 0 {
|
|
|
|
return 0.0
|
|
|
|
}
|
2022-07-14 02:28:53 +00:00
|
|
|
|
2022-04-04 04:14:17 +00:00
|
|
|
return inc.Values[length-1+i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *MACDValues) Length() int {
|
|
|
|
return len(inc.Values)
|
|
|
|
}
|