99 lines
2.2 KiB
Go
99 lines
2.2 KiB
Go
package trend
|
||
|
||
import (
|
||
"github.com/banbox/banbot/config"
|
||
"github.com/banbox/banbot/core"
|
||
"github.com/banbox/banbot/strat"
|
||
ta "github.com/banbox/banta"
|
||
)
|
||
|
||
/*
|
||
初版:https://jesse.trade/strategies/trendswingtrader-v1
|
||
去掉e1<c或e1>c后,更不稳定
|
||
adx对部分条件应用会更好,直接全部过滤略差
|
||
加入海龟资金管理&ER开仓系数后表现略差
|
||
*/
|
||
|
||
func swing(p *config.RunPolicyConfig) *strat.TradeStrat {
|
||
slRate := p.Def("slRate", 4.5, core.PNorm(0.5, 5))
|
||
tpRate := p.Def("tpRate", 8, core.PNorm(3, 13))
|
||
return &strat.TradeStrat{
|
||
WarmupNum: 200,
|
||
OnBar: func(s *strat.StratJob) {
|
||
e := s.Env
|
||
c := e.Close.Get(0)
|
||
adx := ta.ADX(e.High, e.Low, e.Close, 14).Get(0)
|
||
atr := ta.ATR(e.High, e.Low, e.Close, 14).Get(0)
|
||
e1 := ta.EMA(e.Close, 30).Get(0)
|
||
e2 := ta.EMA(e.Close, 50).Get(0)
|
||
e3 := ta.EMA(e.Close, 120).Get(0)
|
||
openRate := float64(1)
|
||
trend := float64(0)
|
||
tag := ""
|
||
if e2 < e1 && e1 < c {
|
||
if e3 < e2 {
|
||
tag = "long1"
|
||
} else {
|
||
tag = "long2"
|
||
if adx <= 25 {
|
||
// 表现较差
|
||
openRate *= 0.5
|
||
}
|
||
}
|
||
trend = 1
|
||
} else if e2 > e1 && e1 > c {
|
||
if e3 > e2 {
|
||
tag = "short1"
|
||
} else {
|
||
tag = "short2"
|
||
if adx <= 25 {
|
||
// 表现较差
|
||
openRate *= 0.5
|
||
}
|
||
}
|
||
trend = -1
|
||
}
|
||
if tag != "" {
|
||
if adx <= 25 {
|
||
tag += "_a"
|
||
}
|
||
}
|
||
if len(s.LongOrders) > 0 {
|
||
for _, od := range s.LongOrders {
|
||
sl := od.GetStopLoss()
|
||
curSL := c - atr*slRate
|
||
if curSL > sl.Price {
|
||
sl.Price = curSL
|
||
}
|
||
}
|
||
} else if trend == 1 && tag != "" && openRate > 0 {
|
||
s.OpenOrder(&strat.EnterReq{
|
||
Tag: tag,
|
||
CostRate: openRate,
|
||
StopLossVal: atr * slRate,
|
||
TakeProfitVal: atr * tpRate,
|
||
TakeProfitRate: 0.5,
|
||
})
|
||
}
|
||
if len(s.ShortOrders) > 0 {
|
||
for _, od := range s.ShortOrders {
|
||
sl := od.GetStopLoss()
|
||
curSL := c + atr*slRate
|
||
if curSL < sl.Price {
|
||
sl.Price = curSL
|
||
}
|
||
}
|
||
} else if trend == -1 && tag != "" && openRate > 0 {
|
||
s.OpenOrder(&strat.EnterReq{
|
||
Tag: tag,
|
||
Short: true,
|
||
CostRate: openRate,
|
||
StopLossVal: atr * slRate,
|
||
TakeProfitVal: atr * tpRate,
|
||
TakeProfitRate: 0.5,
|
||
})
|
||
}
|
||
},
|
||
}
|
||
}
|