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.
 
 

35 lines
729 B

package indicator
import (
"sig-pub/pkg/types"
)
// EMA stateful indicator
type EMA struct {
}
func (c *EMA) Name() string {
return "ema"
}
func (c *EMA) RequiredSeries(window int16, in types.Input) int16 {
return window + 1
}
// Calculate 计算单根k线sma指标
func (c *EMA) Calculate(ctx IIndicatorContext, window int16) (vector float64) {
alpha := 2.0 / float64(window+1)
close := ctx.Get(0).CloseF64()
prevSMA := ctx.Series(1, window).Close().Avg()
vector = ((close - prevSMA) * alpha) + prevSMA
// ctx.GetSelf(0) // 自己计算的上一个值
// 计算eam
// closeSeries := ctx.Series(0, window).Close().Reverse()
// ema := talib.Ema(closeSeries, int(window))
// vector = ema[len(ema)-1]
return
}