5 Star 4 Fork 3

Gitee 极速下载/bbgo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/c9s/bbgo
克隆/下载
cma.go 1.17 KB
一键复制 编辑 原始数据 按行查看 历史
c9s 提交于 2年前 . indicator: add v2 CMA indicator
package indicator
import (
"github.com/c9s/bbgo/pkg/datatype/floats"
"github.com/c9s/bbgo/pkg/types"
)
// Refer: Cumulative Moving Average, Cumulative Average
// Refer: https://en.wikipedia.org/wiki/Moving_average
//
//go:generate callbackgen -type CA
type CA struct {
types.SeriesBase
Interval types.Interval
Values floats.Slice
length 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)
if len(inc.Values) > MaxNumOfEWMA {
inc.Values = inc.Values[MaxNumOfEWMATruncateSize-1:]
inc.length = float64(len(inc.Values))
}
}
func (inc *CA) Last(i int) float64 {
return inc.Values.Last(i)
}
func (inc *CA) Index(i int) float64 {
return inc.Last(i)
}
func (inc *CA) Length() int {
return len(inc.Values)
}
var _ types.SeriesExtend = &CA{}
func (inc *CA) PushK(k types.KLine) {
inc.Update(k.Close.Float64())
}
func (inc *CA) CalculateAndUpdate(allKLines []types.KLine) {
for _, k := range allKLines {
inc.PushK(k)
inc.EmitUpdate(inc.Last(0))
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/mirrors/bbgo.git
git@gitee.com:mirrors/bbgo.git
mirrors
bbgo
bbgo
main

搜索帮助