You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

40 lines
895 B

package indicator
import (
"sig-pub/pkg/types"
)
// EMA stateful indicator
type EMA struct {
}
func (c *EMA) Meta() IndicatorMeta {
return IndicatorMeta{
Name: "ema",
Input: []types.InputArg{
{Name: "window", Type: types.InputTypeUInt, Desc: "窗口大小"},
},
}
}
func (c *EMA) CandlePeriods(ctx IIndicatorContext) int16 {
return ctx.Input().Int16("window") + 1
}
// Calculate 计算单根k线sma指标
func (c *EMA) Calculate(ctx IIndicatorContext) (vector float64) {
window := ctx.Input().Int16("window")
prevEma, ok := ctx.State().Get("vector", 1)
if !ok {
// 初始值用 sma 替代
prevEma = ctx.Series(1, window).Close().Avg()
}
multiplier := 2.0 / float64(window+1)
close := ctx.Get(0).CloseF64()
vector = multiplier*close + (1-multiplier)*prevEma
// same: vector = ((close - prevEma) * multiplier) + prevEma
ctx.State().Set("vector", vector)
return
}