19 changed files with 237 additions and 70 deletions
@ -0,0 +1,72 @@
|
||||
package indicator |
||||
|
||||
import "sig-pub/pkg/types" |
||||
|
||||
// SuperTrend 超级趋势
|
||||
type SuperTrend struct { |
||||
} |
||||
|
||||
func (c SuperTrend) Meta() IndicatorMeta { |
||||
return IndicatorMeta{ |
||||
Name: "SuperTrend", |
||||
Input: []types.InputArg{ |
||||
{Name: "window", Type: types.InputTypeUInt, Desc: "ATR周期(7/14)"}, |
||||
{Name: "mul", Type: types.InputTypeUInt, Desc: "乘数(建议2-4)"}, |
||||
}, |
||||
State: []string{"direction"}, |
||||
} |
||||
} |
||||
|
||||
func (c SuperTrend) CandlePeriods(ctx IIndicatorContext) int16 { |
||||
return ctx.Indicator("ATR", ctx.Input().Int16("window")).CandlePeriods() |
||||
} |
||||
|
||||
func (c SuperTrend) Calculate(ctx IIndicatorContext) (vector float64) { |
||||
window := ctx.Input().Int16("window") |
||||
mul := ctx.Input().Float("mul") |
||||
|
||||
atr := ctx.Indicator("ATR", window).Get(0) |
||||
hl2 := ctx.Get(0).HL2() |
||||
closeP := ctx.Get(0).CloseF64() |
||||
|
||||
upper := hl2 + mul*atr // 潛在上漲時的阻力位
|
||||
lower := hl2 - mul*atr // 潛在下跌時的支撐位
|
||||
|
||||
prevTrend, ok := ctx.State().Get("_trend", 1) |
||||
prevDirection, _ := ctx.State().Get("direction", 1) // 方向: 1.up, -1.down
|
||||
|
||||
// 1.初始化
|
||||
if !ok { |
||||
if closeP > upper { |
||||
prevTrend = lower |
||||
prevDirection = 1 |
||||
} else { |
||||
prevTrend = upper |
||||
prevDirection = -1 |
||||
} |
||||
ctx.State().Set("_trend", prevTrend) |
||||
ctx.State().Set("direction", prevDirection) |
||||
return prevTrend |
||||
} |
||||
|
||||
// 2.迭代计算
|
||||
trend, direction := prevTrend, prevDirection |
||||
if prevDirection == 1 { |
||||
// uptrend
|
||||
trend = max(lower, prevTrend) |
||||
if closeP < prevTrend { |
||||
trend = upper // 取上轨作为新红线
|
||||
direction = -1 // 转下跌趋势
|
||||
} |
||||
} else { |
||||
// downtrend
|
||||
trend = min(upper, prevTrend) |
||||
if closeP > prevTrend { |
||||
trend = lower // 取下轨作为新绿线
|
||||
direction = 1 // 转上升趋势
|
||||
} |
||||
} |
||||
ctx.State().Set("_trend", trend) |
||||
ctx.State().Set("direction", direction) |
||||
return trend |
||||
} |
||||
@ -0,0 +1,50 @@
|
||||
package strategy |
||||
|
||||
import "sig-pub/pkg/types" |
||||
|
||||
// SuperTrendRSI 结合super trend和rsi指标策略
|
||||
type SuperTrendRSI struct { |
||||
} |
||||
|
||||
func (s *SuperTrendRSI) New() ISigStrategy { |
||||
return &SuperTrendRSI{} |
||||
} |
||||
|
||||
func (s *SuperTrendRSI) Meta() StrategyMeta { |
||||
return StrategyMeta{ |
||||
Name: "SuperTrendRSI", |
||||
Desc: "金叉策略", |
||||
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 *SuperTrendRSI) Init(input types.Input) (err error) { |
||||
return |
||||
} |
||||
|
||||
func (s *SuperTrendRSI) CandlePeriods(ctx ISingleSigStrategyContext) int16 { |
||||
return max( |
||||
ctx.Indicator("SuperTrend", types.Input{ |
||||
"window": ctx.Input().Int16("trendPeriod"), |
||||
"mul": ctx.Input().Int16("trendMultipiler"), |
||||
}).CandlePeriods(), |
||||
ctx.Indicator("RSI", types.Input{"window": ctx.Input().Int16("rsiWindow")}).CandlePeriods(), |
||||
) |
||||
} |
||||
|
||||
func (s *SuperTrendRSI) Update(ctx ISingleSigStrategyContext) (side types.Side) { |
||||
superTrend := ctx.Indicator("SuperTrend", types.Input{ |
||||
"window": ctx.Input().Int16("trendPeriod"), |
||||
"mul": ctx.Input().Int16("trendMultipiler"), |
||||
}) |
||||
rsi := ctx.Indicator("RSI", types.Input{"window": ctx.Input().Int16("rsiWindow")}) |
||||
|
||||
_, _ = superTrend, rsi |
||||
|
||||
return |
||||
} |
||||
Loading…
Reference in new issue