mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-21 22:43:52 +00:00
indicator: add v2 CMA indicator
This commit is contained in:
parent
3e9458499c
commit
8ebf5723a7
|
@ -64,15 +64,3 @@ func (inc *AD) CalculateAndUpdate(kLines []types.KLine) {
|
|||
inc.EmitUpdate(inc.Last(0))
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -90,14 +90,3 @@ func (inc *ALMA) CalculateAndUpdate(allKLines []types.KLine) {
|
|||
inc.Update(allKLines[len(allKLines)-1].Close.Float64())
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -5,11 +5,11 @@ package indicator
|
|||
import ()
|
||||
|
||||
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) {
|
||||
for _, cb := range inc.UpdateCallbacks {
|
||||
for _, cb := range inc.updateCallbacks {
|
||||
cb(value)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -98,15 +98,3 @@ func (inc *CCI) CalculateAndUpdate(allKLines []types.KLine) {
|
|||
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)
|
||||
}
|
||||
|
|
|
@ -14,13 +14,14 @@ type CA struct {
|
|||
Interval types.Interval
|
||||
Values floats.Slice
|
||||
length float64
|
||||
UpdateCallbacks []func(value float64)
|
||||
updateCallbacks []func(value float64)
|
||||
}
|
||||
|
||||
func (inc *CA) Update(x float64) {
|
||||
if len(inc.Values) == 0 {
|
||||
inc.SeriesBase.Series = inc
|
||||
}
|
||||
|
||||
newVal := (inc.Values.Last(0)*inc.length + x) / (inc.length + 1.)
|
||||
inc.length += 1
|
||||
inc.Values.Push(newVal)
|
||||
|
@ -54,15 +55,3 @@ func (inc *CA) CalculateAndUpdate(allKLines []types.KLine) {
|
|||
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)
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
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
23
pkg/indicator/v2_cma.go
Normal 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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user