mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 01:01:56 +00:00
indicator: improve macd indicator update callback
This commit is contained in:
parent
24fd81986c
commit
e2dd7c7360
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user