indicator: add v2 CMA indicator

This commit is contained in:
c9s 2023-06-01 14:27:03 +08:00
parent 3e9458499c
commit 8ebf5723a7
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
7 changed files with 27 additions and 62 deletions

View File

@ -64,15 +64,3 @@ func (inc *AD) CalculateAndUpdate(kLines []types.KLine) {
inc.EmitUpdate(inc.Last(0)) inc.EmitUpdate(inc.Last(0))
inc.EndTime = kLines[len(kLines)-1].EndTime.Time() inc.EndTime = kLines[len(kLines)-1].EndTime.Time()
} }
func (inc *AD) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
if inc.Interval != interval {
return
}
inc.CalculateAndUpdate(window)
}
func (inc *AD) Bind(updater KLineWindowUpdater) {
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
}

View File

@ -90,14 +90,3 @@ func (inc *ALMA) CalculateAndUpdate(allKLines []types.KLine) {
inc.Update(allKLines[len(allKLines)-1].Close.Float64()) inc.Update(allKLines[len(allKLines)-1].Close.Float64())
inc.EmitUpdate(inc.Last(0)) inc.EmitUpdate(inc.Last(0))
} }
func (inc *ALMA) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
if inc.Interval != interval {
return
}
inc.CalculateAndUpdate(window)
}
func (inc *ALMA) Bind(updater KLineWindowUpdater) {
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
}

View File

@ -5,11 +5,11 @@ package indicator
import () import ()
func (inc *CA) OnUpdate(cb func(value float64)) { func (inc *CA) OnUpdate(cb func(value float64)) {
inc.UpdateCallbacks = append(inc.UpdateCallbacks, cb) inc.updateCallbacks = append(inc.updateCallbacks, cb)
} }
func (inc *CA) EmitUpdate(value float64) { func (inc *CA) EmitUpdate(value float64) {
for _, cb := range inc.UpdateCallbacks { for _, cb := range inc.updateCallbacks {
cb(value) cb(value)
} }
} }

View File

@ -98,15 +98,3 @@ func (inc *CCI) CalculateAndUpdate(allKLines []types.KLine) {
inc.EmitUpdate(inc.Last(0)) inc.EmitUpdate(inc.Last(0))
} }
} }
func (inc *CCI) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
if inc.Interval != interval {
return
}
inc.CalculateAndUpdate(window)
}
func (inc *CCI) Bind(updater KLineWindowUpdater) {
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
}

View File

@ -14,13 +14,14 @@ type CA struct {
Interval types.Interval Interval types.Interval
Values floats.Slice Values floats.Slice
length float64 length float64
UpdateCallbacks []func(value float64) updateCallbacks []func(value float64)
} }
func (inc *CA) Update(x float64) { func (inc *CA) Update(x float64) {
if len(inc.Values) == 0 { if len(inc.Values) == 0 {
inc.SeriesBase.Series = inc inc.SeriesBase.Series = inc
} }
newVal := (inc.Values.Last(0)*inc.length + x) / (inc.length + 1.) newVal := (inc.Values.Last(0)*inc.length + x) / (inc.length + 1.)
inc.length += 1 inc.length += 1
inc.Values.Push(newVal) inc.Values.Push(newVal)
@ -54,15 +55,3 @@ func (inc *CA) CalculateAndUpdate(allKLines []types.KLine) {
inc.EmitUpdate(inc.Last(0)) inc.EmitUpdate(inc.Last(0))
} }
} }
func (inc *CA) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
if inc.Interval != interval {
return
}
inc.CalculateAndUpdate(window)
}
func (inc *CA) Bind(updater KLineWindowUpdater) {
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
}

View File

@ -115,15 +115,3 @@ func (inc *DMI) CalculateAndUpdate(allKLines []types.KLine) {
inc.EmitUpdate(inc.DIPlus.Last(0), inc.DIMinus.Last(0), inc.ADX.Last(0)) inc.EmitUpdate(inc.DIPlus.Last(0), inc.DIMinus.Last(0), inc.ADX.Last(0))
} }
} }
func (inc *DMI) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
if inc.Interval != interval {
return
}
inc.CalculateAndUpdate(window)
}
func (inc *DMI) Bind(updater KLineWindowUpdater) {
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
}

23
pkg/indicator/v2_cma.go Normal file
View File

@ -0,0 +1,23 @@
package indicator
type CMAStream struct {
Float64Series
}
func CMA2(source Float64Source) *CMAStream {
s := &CMAStream{
Float64Series: NewFloat64Series(),
}
s.Bind(source, s)
return s
}
func (s *CMAStream) Calculate(x float64) float64 {
l := float64(s.slice.Length())
cma := (s.slice.Last(0)*l + x) / (l + 1.)
return cma
}
func (s *CMAStream) Truncate() {
s.slice.Truncate(MaxNumOfEWMA)
}