|
|
|
|
@ -1,14 +1,12 @@
|
|
|
|
|
package strategy |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"fmt" |
|
|
|
|
"sig-pub/pkg/types" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
// GoldX 金叉策略
|
|
|
|
|
type GoldX struct { |
|
|
|
|
ISigStrategy |
|
|
|
|
short, long int16 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (s *GoldX) New() ISigStrategy { |
|
|
|
|
@ -20,39 +18,47 @@ func (s *GoldX) Meta() StrategyMeta {
|
|
|
|
|
Name: "GoldX", |
|
|
|
|
Desc: "金叉策略", |
|
|
|
|
Input: []types.InputArg{ |
|
|
|
|
{Name: "short", Type: types.InputTypeUInt, Desc: "短周期"}, |
|
|
|
|
{Name: "long", Type: types.InputTypeUInt, Desc: "长周期"}, |
|
|
|
|
{Name: "fast", Type: types.InputTypeUInt, Desc: "macd快线周期"}, |
|
|
|
|
{Name: "slow", Type: types.InputTypeUInt, Desc: "macd慢线周期"}, |
|
|
|
|
{Name: "singal", Type: types.InputTypeUInt, Desc: "macd信号线周期"}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (s *GoldX) Init(input types.Input) (err error) { // 校验参数, 并根据参数初始化策略
|
|
|
|
|
s.short = input.Int16("short") |
|
|
|
|
s.long = input.Int16("long") |
|
|
|
|
if s.long <= s.short { |
|
|
|
|
err = fmt.Errorf("param short should bigger then short") |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
// Init 校验参数, 并根据参数初始化策略
|
|
|
|
|
func (s *GoldX) Init(input types.Input) (err error) { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (s *GoldX) RequiredSeries(input types.Input) int16 { |
|
|
|
|
return max(s.long, s.short) + 1 |
|
|
|
|
func (s *GoldX) CandlePeriods(ctx ISingleSigStrategyContext) int16 { |
|
|
|
|
return max( |
|
|
|
|
ctx.Indicator("macd", ctx.Input()).CandlePeriods(), |
|
|
|
|
ctx.Indicator("macd_dea", ctx.Input()).CandlePeriods(), |
|
|
|
|
ctx.Indicator("macd_hist", ctx.Input()).CandlePeriods(), |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (s *GoldX) Update(ctx ISingleSigStrategyContext) (side types.Side) { |
|
|
|
|
sma14 := ctx.Indicator("sma", s.short) |
|
|
|
|
sma28 := ctx.Indicator("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] // 下穿
|
|
|
|
|
macd := ctx.Indicator("macd", ctx.Input()).Series(0, 2) // macd线
|
|
|
|
|
macdDea := ctx.Indicator("macd_dea", ctx.Input()).Series(0, 2) // macd信号线
|
|
|
|
|
macdHist := ctx.Indicator("macd_hist", ctx.Input()).Get(0) // macd柱状图
|
|
|
|
|
|
|
|
|
|
// todo 包装方法 crossover/crossunder
|
|
|
|
|
// 1.MACD 线接近或上穿零轴(表示整体多头市场)
|
|
|
|
|
crossover := macd[0] > macdDea[0] && macd[1] < macdDea[1] // 上穿
|
|
|
|
|
crossunder := macd[0] < macdDea[0] && macd[1] > macdDea[1] // 下穿
|
|
|
|
|
|
|
|
|
|
if crossover { |
|
|
|
|
return types.SideLong |
|
|
|
|
// 2.附加确认条件: 柱状图从负值转为正值
|
|
|
|
|
if macdHist > 0 { |
|
|
|
|
// todo 3.成交量放大(结合 OBV 等指标验证资金流入)。
|
|
|
|
|
return types.SideLong |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if crossunder { |
|
|
|
|
return types.SideShort |
|
|
|
|
if macdHist < 0 { |
|
|
|
|
return types.SideShort |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|