1 Star 4 Fork 10

王布衣 / pandas

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
wma.go 830 Bytes
一键复制 编辑 原始数据 按行查看 历史
王布衣 提交于 2024-02-24 21:43 . 适配num新版本0.1.5
package formula
import (
"gitee.com/quant1x/gox/api"
"gitee.com/quant1x/gox/exception"
"gitee.com/quant1x/num"
"gitee.com/quant1x/pandas"
)
// WMA 通达信S序列的N日加权移动平均 Yn = (1*X1+2*X2+3*X3+...+n*Xn)/(1+2+3+...+Xn)
func WMA(S pandas.Series, N any) pandas.Series {
var X []num.DType
switch v := N.(type) {
case int:
X = num.Repeat[num.DType](num.DType(v), S.Len())
case pandas.Series:
vs := v.DTypes()
X = num.Align(vs, num.NaN(), S.Len())
default:
panic(exception.New(1, "error window"))
}
d := S.Rolling(X).Apply(func(S pandas.Series, N num.DType) num.DType {
if S.Len() == 0 {
return num.NaN()
}
x := S.DTypes()
x = api.Reverse(x)
v := num.CumSum(x)
v1 := num.Sum(v)
v2 := v1 * 2 / N / (N + 1)
if num.DTypeIsNaN(v2) {
v2 = num.NaN()
}
return v2
})
return d
}
Go
1
https://gitee.com/quant1x/pandas.git
git@gitee.com:quant1x/pandas.git
quant1x
pandas
pandas
v1.4.5

搜索帮助