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 //go:generate callbackgen -type DMI
type DMI struct { type DMI struct {
types.IntervalWindow types.IntervalWindow
ADXSmoothing int ADXSmoothing int
atr *ATR atr *ATR
DMP types.UpdatableSeriesExtend DMP types.UpdatableSeriesExtend
@ -23,7 +24,8 @@ type DMI struct {
DIMinus *types.Queue DIMinus *types.Queue
ADX types.UpdatableSeriesExtend ADX types.UpdatableSeriesExtend
PrevHigh, PrevLow float64 PrevHigh, PrevLow float64
UpdateCallbacks []func(diplus, diminus, adx float64)
updateCallbacks []func(diplus, diminus, adx float64)
} }
func (inc *DMI) Update(high, low, cloze 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.DMN = &RMA{IntervalWindow: inc.IntervalWindow, Adjust: true}
inc.ADX = &RMA{IntervalWindow: types.IntervalWindow{Window: inc.ADXSmoothing}, Adjust: true} inc.ADX = &RMA{IntervalWindow: types.IntervalWindow{Window: inc.ADXSmoothing}, Adjust: true}
} }
if inc.atr == nil { if inc.atr == nil {
inc.atr = &ATR{IntervalWindow: inc.IntervalWindow} inc.atr = &ATR{IntervalWindow: inc.IntervalWindow}
inc.atr.Update(high, low, cloze) inc.atr.Update(high, low, cloze)
@ -41,6 +44,7 @@ func (inc *DMI) Update(high, low, cloze float64) {
inc.DIMinus = types.NewQueue(500) inc.DIMinus = types.NewQueue(500)
return return
} }
inc.atr.Update(high, low, cloze) inc.atr.Update(high, low, cloze)
up := high - inc.PrevHigh up := high - inc.PrevHigh
dn := inc.PrevLow - low dn := inc.PrevLow - low
@ -92,14 +96,15 @@ func (inc *DMI) PushK(k types.KLine) {
} }
func (inc *DMI) CalculateAndUpdate(allKLines []types.KLine) { func (inc *DMI) CalculateAndUpdate(allKLines []types.KLine) {
last := allKLines[len(allKLines)-1]
if inc.ADX == nil { if inc.ADX == nil {
for _, k := range allKLines { for _, k := range allKLines {
inc.PushK(k) inc.PushK(k)
inc.EmitUpdate(inc.DIPlus.Last(), inc.DIMinus.Last(), inc.ADX.Last()) inc.EmitUpdate(inc.DIPlus.Last(), inc.DIMinus.Last(), inc.ADX.Last())
} }
} else { } else {
k := allKLines[len(allKLines)-1] inc.PushK(last)
inc.PushK(k)
inc.EmitUpdate(inc.DIPlus.Last(), inc.DIMinus.Last(), inc.ADX.Last()) inc.EmitUpdate(inc.DIPlus.Last(), inc.DIMinus.Last(), inc.ADX.Last())
} }
} }

View File

@ -5,11 +5,11 @@ package indicator
import () import ()
func (inc *DMI) OnUpdate(cb func(diplus float64, diminus float64, adx float64)) { 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) { func (inc *DMI) EmitUpdate(diplus float64, diminus float64, adx float64) {
for _, cb := range inc.UpdateCallbacks { for _, cb := range inc.updateCallbacks {
cb(diplus, diminus, adx) cb(diplus, diminus, adx)
} }
} }

View File

@ -72,17 +72,24 @@ func (inc *RMA) PushK(k types.KLine) {
} }
func (inc *RMA) CalculateAndUpdate(kLines []types.KLine) { func (inc *RMA) CalculateAndUpdate(kLines []types.KLine) {
for _, k := range kLines { last := kLines[len(kLines)-1]
if inc.EndTime != zeroTime && !k.EndTime.After(inc.EndTime) {
continue
}
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.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) { func (inc *RMA) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
if inc.Interval != interval { if inc.Interval != interval {
return return