indicator: improve macd indicator update callback

This commit is contained in:
c9s 2022-09-15 17:53:12 +08:00
parent 24fd81986c
commit e2dd7c7360
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 12 additions and 9 deletions

View File

@ -33,7 +33,7 @@ type MACD struct {
EndTime time.Time
updateCallbacks []func(fast, slow, signal, histogram float64)
updateCallbacks []func(macd, signal, histogram float64)
}
func (inc *MACD) Update(x float64) {
@ -56,17 +56,20 @@ func (inc *MACD) Update(x float64) {
inc.slowEWMA.Update(x)
// update MACD value, it's also the signal line
macd := inc.fastEWMA.Last() - inc.slowEWMA.Last()
fast := inc.fastEWMA.Last()
slow := inc.slowEWMA.Last()
macd := fast - slow
inc.Values.Push(macd)
// update signal line
inc.signalLine.Update(macd)
signal := inc.signalLine.Last()
// update histogram
histogram := macd - inc.signalLine.Last()
histogram := macd - signal
inc.Histogram.Push(histogram)
inc.EmitUpdate(inc.fastEWMA.Last(), inc.slowEWMA.Last(), macd, histogram)
inc.EmitUpdate(macd, signal, histogram)
}
func (inc *MACD) Last() float64 {

View File

@ -4,12 +4,12 @@ package indicator
import ()
func (inc *MACD) OnUpdate(cb func(fast float64, slow float64, signal float64, histogram float64)) {
func (inc *MACD) OnUpdate(cb func(macd float64, signal float64, histogram float64)) {
inc.updateCallbacks = append(inc.updateCallbacks, cb)
}
func (inc *MACD) EmitUpdate(fast float64, slow float64, signal float64, histogram float64) {
func (inc *MACD) EmitUpdate(macd float64, signal float64, histogram float64) {
for _, cb := range inc.updateCallbacks {
cb(fast, slow, signal, histogram)
cb(macd, signal, histogram)
}
}

View File

@ -106,8 +106,8 @@ func (s *FailedBreakHigh) Bind(session *bbgo.ExchangeSession, orderExecutor *bbg
if s.MACDConfig != nil {
s.macd = standardIndicator.MACD(s.MACDConfig.IntervalWindow, s.MACDConfig.ShortPeriod, s.MACDConfig.LongPeriod)
s.macd.OnUpdate(func(fast float64, slow float64, signal float64, histogram float64) {
log.Infof("MACD %+v: fast: %f slow: %f, signal: %f histogram: %f", s.macd.IntervalWindow, fast, slow, signal, histogram)
s.macd.OnUpdate(func(macd, signal, histogram float64) {
log.Infof("MACD %+v: macd: %f, signal: %f histogram: %f", s.macd.IntervalWindow, macd, signal, histogram)
})
}