package strategy import ( "fmt" "sig-pub/api/pb" ) // GoldX 金叉策略 type GoldX struct { ISigStrategy 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 SigStrategyParam) (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) Update(ctx ISigStrategyContext) (side pb.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 pb.Side_BUY } if crossunder { return pb.Side_SELL } return }