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
|
|
|
|
*/
|
|
|
|
|
|
|
|
//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
|
2021-05-08 16:56:44 +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
|
|
|
|
|
|
|
|
UpdateCallbacks []func(value float64)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *MACD) calculateMACD(kLines []types.KLine, priceF KLinePriceMapper) float64 {
|
|
|
|
for _, kline := range kLines {
|
2022-03-28 19:19:29 +00:00
|
|
|
inc.Update(kline, priceF)
|
2021-05-08 16:56:44 +00:00
|
|
|
}
|
|
|
|
return inc.Values[len(inc.Values)-1]
|
|
|
|
}
|
|
|
|
|
2022-03-28 19:19:29 +00:00
|
|
|
func (inc *MACD) Update(kLine types.KLine, priceF KLinePriceMapper) {
|
2021-05-08 16:56:44 +00:00
|
|
|
if len(inc.Values) == 0 {
|
|
|
|
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}}
|
|
|
|
}
|
|
|
|
|
|
|
|
price := priceF(kLine)
|
|
|
|
|
|
|
|
// update fast and slow ema
|
|
|
|
inc.FastEWMA.Update(price)
|
|
|
|
inc.SlowEWMA.Update(price)
|
|
|
|
|
|
|
|
// 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())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *MACD) calculateAndUpdate(kLines []types.KLine) {
|
|
|
|
if len(kLines) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var priceF = KLineClosePriceMapper
|
|
|
|
|
2022-03-28 19:19:29 +00:00
|
|
|
for _, k := range kLines {
|
|
|
|
if inc.EndTime != zeroTime && k.EndTime.Before(inc.EndTime) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
inc.Update(k, priceF)
|
2021-05-08 16:56:44 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 19:19:29 +00:00
|
|
|
inc.EmitUpdate(inc.Values[len(inc.Values)-1])
|
|
|
|
inc.EndTime = kLines[len(kLines)-1].EndTime.Time()
|
2021-05-08 16:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *MACD) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
|
|
|
|
if inc.Interval != interval {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
inc.calculateAndUpdate(window)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *MACD) Bind(updater KLineWindowUpdater) {
|
|
|
|
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
|
|
|
|
}
|
2022-04-04 04:14:17 +00:00
|
|
|
|
|
|
|
type MACDValues struct {
|
|
|
|
*MACD
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *MACDValues) Last() float64 {
|
|
|
|
if len(inc.Values) == 0 {
|
|
|
|
return 0.0
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
return inc.Values[length-1+i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *MACDValues) Length() int {
|
|
|
|
return len(inc.Values)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *MACD) MACD() types.Series {
|
|
|
|
return &MACDValues{inc}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *MACD) Singals() types.Series {
|
|
|
|
return &inc.SignalLine
|
|
|
|
}
|