Skip to content

Commit

Permalink
indicator: improve rma preload
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Jul 14, 2022
1 parent da4dbf4 commit 7696c9f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
11 changes: 8 additions & 3 deletions pkg/indicator/dmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
//go:generate callbackgen -type DMI
type DMI struct {
types.IntervalWindow

ADXSmoothing int
atr *ATR
DMP types.UpdatableSeriesExtend
Expand All @@ -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) {
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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())
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/indicator/dmi_callbacks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 13 additions & 6 deletions pkg/indicator/rma.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]

if len(inc.Values) == 0 {
for _, k := range kLines {
if inc.EndTime != zeroTime && !k.EndTime.After(inc.EndTime) {
continue
}

inc.PushK(k)
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
Expand Down

0 comments on commit 7696c9f

Please sign in to comment.