diff --git a/pkg/indicator/dmi.go b/pkg/indicator/dmi.go index 9432a880d..d3352bfc5 100644 --- a/pkg/indicator/dmi.go +++ b/pkg/indicator/dmi.go @@ -15,6 +15,7 @@ import ( //go:generate callbackgen -type DMI type DMI struct { types.IntervalWindow + ADXSmoothing int atr *ATR DMP types.UpdatableSeriesExtend @@ -23,7 +24,8 @@ type DMI struct { DIMinus *types.Queue ADX types.UpdatableSeriesExtend PrevHigh, PrevLow float64 - UpdateCallbacks []func(diplus, diminus, adx float64) + + updateCallbacks []func(diplus, diminus, adx float64) } func (inc *DMI) Update(high, low, cloze float64) { @@ -32,6 +34,7 @@ func (inc *DMI) Update(high, low, cloze float64) { inc.DMN = &RMA{IntervalWindow: inc.IntervalWindow, Adjust: true} inc.ADX = &RMA{IntervalWindow: types.IntervalWindow{Window: inc.ADXSmoothing}, Adjust: true} } + if inc.atr == nil { inc.atr = &ATR{IntervalWindow: inc.IntervalWindow} inc.atr.Update(high, low, cloze) @@ -41,6 +44,7 @@ func (inc *DMI) Update(high, low, cloze float64) { inc.DIMinus = types.NewQueue(500) return } + inc.atr.Update(high, low, cloze) up := high - inc.PrevHigh dn := inc.PrevLow - low @@ -92,14 +96,15 @@ func (inc *DMI) PushK(k types.KLine) { } func (inc *DMI) CalculateAndUpdate(allKLines []types.KLine) { + last := allKLines[len(allKLines)-1] + if inc.ADX == nil { for _, k := range allKLines { inc.PushK(k) inc.EmitUpdate(inc.DIPlus.Last(), inc.DIMinus.Last(), inc.ADX.Last()) } } else { - k := allKLines[len(allKLines)-1] - inc.PushK(k) + inc.PushK(last) inc.EmitUpdate(inc.DIPlus.Last(), inc.DIMinus.Last(), inc.ADX.Last()) } } diff --git a/pkg/indicator/dmi_callbacks.go b/pkg/indicator/dmi_callbacks.go index 93e8dd14d..ed8453907 100644 --- a/pkg/indicator/dmi_callbacks.go +++ b/pkg/indicator/dmi_callbacks.go @@ -5,11 +5,11 @@ package indicator import () func (inc *DMI) OnUpdate(cb func(diplus float64, diminus float64, adx float64)) { - inc.UpdateCallbacks = append(inc.UpdateCallbacks, cb) + inc.updateCallbacks = append(inc.updateCallbacks, cb) } func (inc *DMI) EmitUpdate(diplus float64, diminus float64, adx float64) { - for _, cb := range inc.UpdateCallbacks { + for _, cb := range inc.updateCallbacks { cb(diplus, diminus, adx) } } diff --git a/pkg/indicator/rma.go b/pkg/indicator/rma.go index 5ad79de10..caa39b507 100644 --- a/pkg/indicator/rma.go +++ b/pkg/indicator/rma.go @@ -72,17 +72,24 @@ func (inc *RMA) PushK(k types.KLine) { } func (inc *RMA) CalculateAndUpdate(kLines []types.KLine) { - for _, k := range kLines { - if inc.EndTime != zeroTime && !k.EndTime.After(inc.EndTime) { - continue - } + last := kLines[len(kLines)-1] - inc.PushK(k) + if len(inc.Values) == 0 { + for _, k := range kLines { + if inc.EndTime != zeroTime && !k.EndTime.After(inc.EndTime) { + continue + } + + inc.PushK(k) + } + } else { + inc.PushK(last) } inc.EmitUpdate(inc.Last()) - inc.EndTime = kLines[len(kLines)-1].EndTime.Time() + inc.EndTime = last.EndTime.Time() } + func (inc *RMA) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) { if inc.Interval != interval { return