From e2dd7c736037bd75e99838b2afa1efce35544b48 Mon Sep 17 00:00:00 2001 From: c9s Date: Thu, 15 Sep 2022 17:53:12 +0800 Subject: [PATCH] indicator: improve macd indicator update callback --- pkg/indicator/macd.go | 11 +++++++---- pkg/indicator/macd_callbacks.go | 6 +++--- pkg/strategy/pivotshort/failedbreakhigh.go | 4 ++-- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/pkg/indicator/macd.go b/pkg/indicator/macd.go index 5bba09419..e8146bd17 100644 --- a/pkg/indicator/macd.go +++ b/pkg/indicator/macd.go @@ -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 { diff --git a/pkg/indicator/macd_callbacks.go b/pkg/indicator/macd_callbacks.go index ba10d8bcf..93a1bc8c9 100644 --- a/pkg/indicator/macd_callbacks.go +++ b/pkg/indicator/macd_callbacks.go @@ -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) } } diff --git a/pkg/strategy/pivotshort/failedbreakhigh.go b/pkg/strategy/pivotshort/failedbreakhigh.go index 92c1416ae..d8e5e4a9b 100644 --- a/pkg/strategy/pivotshort/failedbreakhigh.go +++ b/pkg/strategy/pivotshort/failedbreakhigh.go @@ -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) }) }