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.
66 lines
1.4 KiB
66 lines
1.4 KiB
package strategy |
|
|
|
import ( |
|
"fmt" |
|
"sig-pub/api/pb" |
|
"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 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 |
|
} |
|
|
|
func (s *GoldX) UpdateByIntervals(ctx IIntervalStrategyContext) (side pb.Side) { |
|
series5m := ctx.Series(types.Interval5m, 0, 2) |
|
series5m.Close().Diff() |
|
return |
|
}
|
|
|