indicator: improve rma preload

This commit is contained in:
c9s 2022-07-14 10:54:46 +08:00
parent da4dbf4800
commit 7696c9f21e
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 23 additions and 11 deletions

View File

@ -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())
}
}

View File

@ -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)
}
}

View File

@ -72,6 +72,9 @@ func (inc *RMA) PushK(k types.KLine) {
}
func (inc *RMA) CalculateAndUpdate(kLines []types.KLine) {
last := kLines[len(kLines)-1]
if len(inc.Values) == 0 {
for _, k := range kLines {
if inc.EndTime != zeroTime && !k.EndTime.After(inc.EndTime) {
continue
@ -79,10 +82,14 @@ func (inc *RMA) CalculateAndUpdate(kLines []types.KLine) {
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