7 changed files with 186 additions and 11 deletions
@ -0,0 +1,79 @@ |
|||||||
|
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 |
||||||
|
} |
||||||
@ -0,0 +1,72 @@ |
|||||||
|
package strategy |
||||||
|
|
||||||
|
import "sig-pub/pkg/types" |
||||||
|
|
||||||
|
// SuperTrendMacdRSI 结合super trend和rsi指标策略
|
||||||
|
type SuperTrendMacdRSI struct { |
||||||
|
} |
||||||
|
|
||||||
|
func (s *SuperTrendMacdRSI) New() ISigStrategy { |
||||||
|
return &SuperTrendMacdRSI{} |
||||||
|
} |
||||||
|
|
||||||
|
func (s *SuperTrendMacdRSI) Meta() StrategyMeta { |
||||||
|
return StrategyMeta{ |
||||||
|
Name: "SuperTrendMacdRSI", |
||||||
|
Desc: "SuperTrend + MACD + RSI 量化策略", |
||||||
|
Input: []types.InputArg{ |
||||||
|
// {Name: "trendWindow", Type: types.InputTypeUInt, Desc: "SuperTrend ATR周期"},
|
||||||
|
// {Name: "trendMultipiler", Type: types.InputTypeUInt, Desc: "SuperTrend multipiler"},
|
||||||
|
// {Name: "rsiWindow", Type: types.InputTypeUInt, Desc: "rsi周期"},
|
||||||
|
}, |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Init 校验参数, 并根据参数初始化策略
|
||||||
|
func (s *SuperTrendMacdRSI) Init(input types.Input) (err error) { |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
func (s *SuperTrendMacdRSI) CandlePeriods(ctx ISingleSigStrategyContext) int16 { |
||||||
|
return max( |
||||||
|
ctx.Indicator("SuperTrend", types.Input{"window": 10, "mul": 3}).CandlePeriods(), |
||||||
|
ctx.Indicator("RSI", 14).CandlePeriods(), |
||||||
|
ctx.Indicator("MacdDEA", types.Input{"fast": 12, "slow": 26, "singal": 9}).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 *SuperTrendMacdRSI) Update(ctx ISingleSigStrategyContext) (side types.Side) { |
||||||
|
superTrend := ctx.Indicator("SuperTrend", types.Input{"window": 10, "mul": 3}) |
||||||
|
rsi := ctx.Indicator("RSI", 14).Get(0) |
||||||
|
macdHist := ctx.Indicator("Macd", types.Input{"fast": 12, "slow": 26, "singal": 9}).Get(0) // macd柱
|
||||||
|
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] // 死叉
|
||||||
|
|
||||||
|
closeP := ctx.Get(0).CloseF64() |
||||||
|
volAvg := ctx.Series(1, 20).Vol().Avg() |
||||||
|
vol := ctx.Get(0).VolF64() |
||||||
|
|
||||||
|
trend := superTrend.Get(0) |
||||||
|
trendDirection := superTrend.State("direction", 0) |
||||||
|
|
||||||
|
// 金叉状态且正向扩张
|
||||||
|
if crossover && macdHist > 0 { |
||||||
|
// RSI 强度过滤
|
||||||
|
if rsi > 50 { |
||||||
|
// SuperTrend 趋势确认
|
||||||
|
if closeP > trend && trendDirection == 1 { |
||||||
|
// 成交量过滤
|
||||||
|
if vol > volAvg*1.5 { |
||||||
|
return types.SideLong |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
return |
||||||
|
} |
||||||
Loading…
Reference in new issue