package strategy import ( "sig-pub/pkg/types" ) // SuperTrend2Macd 结合super trend和rsi指标策略 type SuperTrend2Macd struct { } func (s *SuperTrend2Macd) New() ISigStrategy { return &SuperTrend2Macd{} } func (s *SuperTrend2Macd) Meta() StrategyMeta { return StrategyMeta{ Name: "SuperTrend2Macd", Desc: "双 SuperTrend + MACD + 成交量", Input: []types.InputArg{ // {Name: "trend1Window", Type: types.InputTypeUInt, Desc: "SuperTrend ATR周期"}, // {Name: "trend1Mul", Type: types.InputTypeUInt, Desc: "SuperTrend multipiler"}, // {Name: "trend2Window", Type: types.InputTypeUInt, Desc: "SuperTrend ATR周期"}, // {Name: "trend2Mul", Type: types.InputTypeUInt, Desc: "SuperTrend multipiler"}, // {Name: "rsiWindow", Type: types.InputTypeUInt, Desc: "rsi周期"}, }, } } // Init 校验参数, 并根据参数初始化策略 func (s *SuperTrend2Macd) Init(input types.Input) (err error) { return } func (s *SuperTrend2Macd) CandlePeriods(ctx ISingleSigStrategyContext) int16 { return max( ctx.Indicator("SuperTrend", types.Input{"window": 10, "mul": 3}).CandlePeriods(), ctx.Indicator("SuperTrend", types.Input{"window": 14, "mul": 2}).CandlePeriods(), ctx.Indicator("MacdDEA", types.Input{"fast": 12, "slow": 26, "singal": 9}).CandlePeriods(), ctx.Indicator("MacdDIF", types.Input{"fast": 12, "slow": 26, "singal": 9}).CandlePeriods(), 21, ) } func (s *SuperTrend2Macd) Update(ctx ISingleSigStrategyContext) (side types.Side) { superTrend1 := ctx.Indicator("SuperTrend", types.Input{"window": 10, "mul": 3}) superTrend2 := ctx.Indicator("SuperTrend", types.Input{"window": 14, "mul": 2}) // 買入:SuperTrend 轉綠(綠線在價格下方)且 RSI > 50(確認動能向上) // 賣出:SuperTrend 轉紅,或 RSI < 30(超賣退出) // 風險控制:止損 3%(SuperTrend 線為參考),無固定止盈(讓利潤奔跑) trend1Directions := superTrend1.StateSeries("direction", 0, 2) trend2Directions := superTrend2.StateSeries("direction", 0, 2) macdDea := ctx.Indicator("MacdDEA", types.Input{"fast": 12, "slow": 26, "singal": 9}).Series(0, 2) // macd_dea信号线 macdDif := ctx.Indicator("MacdDIF", types.Input{"fast": 12, "slow": 26, "singal": 9}).Series(0, 2) // macd_dif线 crossover := macdDif[0] > macdDea[0] && macdDif[1] < macdDea[1] // 金叉 crossunder := macdDif[0] < macdDea[0] && macdDif[1] > macdDea[1] // 死叉 volAvg := ctx.Series(1, 20).Vol().Avg() vol := ctx.Get(0).VolF64() if trend1Directions[0] == 1 && trend1Directions[1] == -1 && trend2Directions[0] == 1 && trend2Directions[1] == -1 { if crossover { if vol > volAvg*1.5 { return types.SideLong } } } if trend1Directions[0] == -1 && trend1Directions[1] == 1 && trend2Directions[0] == -1 && trend2Directions[1] == 1 { if crossunder { if vol > volAvg*1.5 { return types.SideShort } } } return }