package strategy import ( "fmt" "sig-pub/pkg/types" ) // GoldX 金叉策略 type GoldX struct { ISigStrategy IIntervalSigStrategy short, long int16 } func (s *GoldX) New() ISigStrategy { return &GoldX{} } func (s *GoldX) Meta() StrategyMeta { return StrategyMeta{ Name: "GoldX", Desc: "金叉策略", Args: []Param{ {Name: "short", Type: ParamTypeUInt, Desc: "短周期"}, {Name: "long", Type: ParamTypeUInt, Desc: "长周期"}, }, } } func (s *GoldX) Init(param StrategyParam) (err error) { // 校验参数, 并根据参数初始化策略 if s.short, err = param.GetInt16E("short"); err != nil { return } if s.long, err = param.GetInt16E("long"); err != nil { return } if s.long <= s.short { err = fmt.Errorf("param short should bigger then short") return } return } func (s *GoldX) RequiredSeries() int16 { return max(s.long, s.short) + 1 } func (s *GoldX) Update(ctx ISigStrategyContext) (side types.Side) { sma14 := ctx.IndicatorW("sma", s.short) sma28 := ctx.IndicatorW("sma", s.long) // 包装方法 crossover/crossunder s14 := sma14.Series(0, 2) s28 := sma28.Series(0, 2) crossover := s14[0] > s28[0] && s14[1] < s28[1] // 上穿 crossunder := s14[0] < s28[0] && s14[1] > s28[1] // 下穿 if crossover { return types.SideLong } if crossunder { return types.SideShort } return } func (s *GoldX) UpdateByIntervals(ctx IIntervalStrategyContext) (side types.Side) { series5m := ctx.Series(types.Interval5m, 0, 2) series5m.Close().Diff() return }