This commit is contained in:
2026-08-24 20:53:21 +08:00
commit 6e78bba16a
287 changed files with 32443 additions and 0 deletions

113
trend/20250208/MACDEMAV2.go Normal file
View File

@@ -0,0 +1,113 @@
package trend
import (
"github.com/banbox/banbot/config"
"github.com/banbox/banbot/core"
"github.com/banbox/banbot/strat"
"github.com/banbox/banstagy/com"
ta "github.com/banbox/banta"
"math"
)
/*
备份于2025/02/25
测试加入1d的大周期过滤,对做多或做空都未获得更好效果,反而略差
20210101-20250101
只做多:1776%,回撤28.8%,夏普3.93
多空双开:1913%,回撤22%,夏普4.53
做空部分单独运行波动很大,但和做多一起运行可减少做多的回撤
参考SMACross1加入海龟资金和ErDelta过滤后:
170品种下:夏普从7.97提升到8.91,显示最大回撤从18.71%下降到17.65%;收益从718%变为707%
*/
type entExitMore struct {
enter *strat.EnterReq
exit *strat.ExitReq
}
func MACDEMAV2(p *config.RunPolicyConfig) *strat.TradeStrat {
// 初始HMA 25 4
maType := com.DefMaType(p, "ma", com.MaHMA) // hma 包揽优化列表头部
maLen := p.DefInt("maLen", 25, core.PNormF(7, 100, 25, 0)) // 超过40表现不佳,20多表现最好
maRate := p.Def("maRate", 4.1, core.PNorm(1.5, 7))
emaLen := p.DefInt("emaLen", 100, core.PNormF(30, 240, 100, 0))
macdRt := p.Def("macdRt", 1, core.PNormF(0.7, 3, 1.2, 0))
maBigLen := int(math.Round(float64(maLen) * maRate))
mc1, mc2, mc3 := int(math.Round(12*macdRt)), int(math.Round(26*macdRt)), int(math.Round(9*macdRt))
p.KeepHyperOnly("ma", "maLen", "maRate")
return &strat.TradeStrat{
WarmupNum: 500,
EachMaxLong: 1,
EachMaxShort: 1,
//OnStartUp: func(s *strat.StratJob) {
// s.More = &entExitMore{}
//},
//OnPairInfos: func(s *strat.StratJob) []*strat.PairSub {
// return []*strat.PairSub{
// {"_cur_", "4h", 500},
// }
//},
//OnBar: func(s *strat.StratJob) {
// m, _ := s.More.(*entExitMore)
// if m.enter != nil {
// s.OpenOrder(m.enter)
// m.enter = nil
// }
// if m.exit != nil {
// s.CloseOrders(m.exit)
// m.exit = nil
// }
//},
//OnInfoBar: func(s *strat.StratJob, e *ta.BarEnv, pair, tf string) {
// m, _ := s.More.(*entExitMore)
OnBar: func(s *strat.StratJob) {
e := s.Env
c := e.Close.Get(0)
atr := ta.ATR(e.High, e.Low, e.Close, 30)
openRate := 0.015 * c / atr.Get(0)
if openRate < 1 {
openRate = math.Sqrt(openRate)
}
er := ta.ER(e.Close, 50)
erDelta := min(1.3, er.Get(0)/er.Get(12))
if erDelta > 1 {
openRate *= erDelta
} else {
openRate = 0
}
ema := ta.EMA(e.Close, emaLen).Get(0)
macd, signal := ta.MACD(e.Close, mc1, mc2, mc3)
macdSub := macd.Get(0) - signal.Get(0)
if c > ema && macdSub > 0 && len(s.LongOrders) == 0 {
s.OpenOrder(&strat.EnterReq{
Tag: "long",
CostRate: openRate,
})
} else if len(s.LongOrders) > 0 && (macdSub < 0 && c < ema) {
s.CloseOrders(&strat.ExitReq{
Tag: "closeL",
Dirt: core.OdDirtLong,
})
}
ma1 := com.GetMA(e.Close, e.Volume, maType, maLen)
ma2 := com.GetMA(e.Close, e.Volume, maType, maBigLen)
cross := ma1.Cross(ma2)
if cross == 1 && len(s.ShortOrders) > 0 {
s.CloseOrders(&strat.ExitReq{
Tag: "closeS",
Dirt: core.OdDirtShort,
})
} else if cross == -1 && len(s.ShortOrders) == 0 {
s.OpenOrder(&strat.EnterReq{
Tag: "short",
Short: true,
CostRate: openRate,
})
}
},
}
}

View File

@@ -0,0 +1,107 @@
package trend
import (
"github.com/banbox/banbot/config"
"github.com/banbox/banbot/core"
"github.com/banbox/banbot/strat"
"github.com/banbox/banstagy/com"
ta "github.com/banbox/banta"
"math"
)
/*
自研均线趋势策略,多空都开。适合1h
在15m上,需把均线周期乘以4
尝试加止损、止盈、限价入场,都会导致整体收益减少
48个品种,21年到24年11月1日,累计收益1560%,最大回撤25%,夏普5.8
总收益93%来自多头
但加入空头可使夏普从4.8提高到5.8
多空的参数独立,比同一套参数,收益更稳定些
基于连续信号的商品期货交易策略
https://mp.weixin.qq.com/s/6MS94OhGYhtjBQ1Ufa5Rag
下面基于170固定品种:
使用1.5%*Close/atr作为开仓倍数,夏普从8.12提升到9.63
增加ErDelta过滤,夏普从9.63提升到11.03;最大回撤从21.8%降低到20.8%
有效信号权重测试没有明显改善:
OnStartUp: func(s *strat.StratJob) {
s.More = []float64{0.5, 0.5} // long, short
},
probs := s.More.([]float64)
if x1 == 1{
newShort := probs[1] * 0.5
s.More = []float64{1 - newShort, newShort}
sigDelta := math.Abs(1 - newShort*2)
if sigDelta > 0.2 {
openRate *= (1 - newShort) * 1.7
s.OpenOrder(&strat.EnterReq{Tag: "long", CostRate: openRate})
}
}
if x2 == -1{
newLong := probs[0] * 0.5
s.More = []float64{newLong, 1 - newLong}
sigDelta := math.Abs(1 - newLong*2)
if sigDelta > 0.2 {
openRate *= (1 - newLong) * 1.7
s.OpenOrder(&strat.EnterReq{Tag: "short", Short: true, CostRate: openRate})
}
}
*/
func SMACrossV1(p *config.RunPolicyConfig) *strat.TradeStrat {
// 初始:两个SMA 做空:SMA 15 8
maLType := com.DefMaType(p, "maL", com.MaSMA)
maSType := com.DefMaType(p, "maS", com.MaSMA)
maLLen := p.DefInt("maLLen", 20, core.PNormF(7, 70, 20, 0))
maSLen := p.DefInt("maSLen", 15, core.PNormF(5, 50, 15, 0))
maLRate := p.Def("maLRate", 9, core.PNorm(2, 15))
maSRate := p.Def("maSRate", 8, core.PNorm(2, 14))
maLBig := int(math.Round(float64(maLLen) * maLRate))
maSBig := int(math.Round(float64(maSLen) * maSRate))
p.KeepHyperOnly("maSLen", "maSRate")
return &strat.TradeStrat{
WarmupNum: 200,
EachMaxShort: 1,
EachMaxLong: 1,
OnBar: func(s *strat.StratJob) {
e := s.Env
c := e.Close.Get(0)
atr := ta.ATR(e.High, e.Low, e.Close, 30)
openRate := 0.015 * c / atr.Get(0)
if openRate < 1 {
openRate = math.Sqrt(openRate)
}
er := ta.ER(e.Close, 50)
erDelta := min(1.3, er.Get(0)/er.Get(12))
if erDelta > 1 {
openRate *= erDelta
} else {
// 过滤掉ER下降的
openRate = 0
}
// 方案一:多空SMA:略微平滑,收益略小一点
// 方案二:多空VWMA:略微波动大点,收益略好一点
price := ta.AvgPrice(e)
ma1 := com.GetMA(price, e.Volume, maLType, maLLen)
ma2 := com.GetMA(price, e.Volume, maLType, maLBig)
x1 := ma1.Cross(ma2)
if x1 == 1 && openRate > 0 {
s.OpenOrder(&strat.EnterReq{Tag: "long", CostRate: openRate})
} else if x1 == -1 {
s.CloseOrders(&strat.ExitReq{Tag: "xlong", Dirt: core.OdDirtLong})
}
ma3 := com.GetMA(price, e.Volume, maSType, maSLen)
ma4 := com.GetMA(price, e.Volume, maSType, maSBig)
x2 := ma3.Cross(ma4)
if x2 == -1 && openRate > 0 {
s.OpenOrder(&strat.EnterReq{Tag: "short", Short: true, CostRate: openRate})
} else if x2 == 1 {
s.CloseOrders(&strat.ExitReq{Tag: "xshort", Dirt: core.OdDirtShort})
}
},
}
}

View File

@@ -0,0 +1,98 @@
package trend
import (
"github.com/banbox/banbot/config"
"github.com/banbox/banbot/core"
"github.com/banbox/banbot/strat"
ta "github.com/banbox/banta"
"strconv"
)
/*
版本1:备份:2025/02/23
原始灵感:https://www.fmz.com/strategy/442547
已改动了大部分
测试在4h上表现最佳;210101-241101,48个品种
收益1919%,回撤23%,夏普5.5
添加adx过滤,收益增加
测试使用ATRBase显著收益下降
*/
type supFlwMore struct {
enter *strat.EnterReq
exitIds []int64
}
func SuperFollow(p *config.RunPolicyConfig) *strat.TradeStrat {
atrLen := 14 // 默认12
mult := p.Def("mult", 1.4, core.PNorm(0.7, 2.5))
sRate := p.Def("sRate", 1.1, core.PNorm(0.7, 1.8))
slAtr := p.Def("slAtr", 1.6, core.PNorm(1, 2.5))
tpRate := p.Def("tpRate", 3, core.PNorm(1.7, 5.5))
shortMult := -mult * sRate
return &strat.TradeStrat{
WarmupNum: 200,
EachMaxShort: 1,
EachMaxLong: 1,
//OnStartUp: func(s *strat.StratJob) {
// s.More = &supFlwMore{}
//},
//OnPairInfos: func(s *strat.StratJob) []*strat.PairSub {
// return []*strat.PairSub{
// {"_cur_", "4h", 200},
// }
//},
//OnBar: func(s *strat.StratJob) {
// m, _ := s.More.(*supFlwMore)
// if m.enter != nil {
// s.OpenOrder(m.enter)
// m.enter = nil
// } else if len(m.exitIds) > 0 {
// for _, id := range m.exitIds {
// s.CloseOrders(&strat.ExitReq{Tag: "tooLong", OrderID: id})
// }
// m.exitIds = nil
// }
//},
//OnInfoBar: func(s *strat.StratJob, e *ta.BarEnv, pair, tf string) {
// m, _ := s.More.(*supFlwMore)
OnBar: func(s *strat.StratJob) {
e := s.Env
o, c := e.Open.Get(0), e.Close.Get(0)
atr := ta.ATR(e.High, e.Low, e.Close, atrLen).Get(0)
dist := (c - o) / atr
adx := ta.ADX(e.High, e.Low, e.Close, 30).Get(0)
openDirt := 0
overRate := 0.0
if len(s.ShortOrders) == 0 && dist < shortMult && adx < 20 {
openDirt = -1
overRate = dist / shortMult
} else if len(s.LongOrders) == 0 && dist > mult {
openDirt = 1
overRate = dist / mult
}
if openDirt != 0 {
costRate := overRate * 1.1
s.OpenOrder(&strat.EnterReq{
Tag: strconv.Itoa(openDirt),
Short: openDirt < 0,
TakeProfitVal: atr * slAtr * tpRate * overRate,
StopLossVal: atr * slAtr * overRate,
CostRate: costRate,
})
} else {
// 长期未关闭的,超过200个平仓
odList := s.GetOrders(core.OdDirtBoth)
for _, od := range odList {
holdNum := e.BarCount(od.EnterAt)
if holdNum > 200 {
s.CloseOrders(&strat.ExitReq{Tag: "tooLong", OrderID: od.ID})
}
}
}
},
}
}

11
trend/20250208/main.go Normal file
View File

@@ -0,0 +1,11 @@
package trend
import "github.com/banbox/banbot/strat"
func init() {
strat.AddStratGroup("20250208", map[string]strat.FuncMakeStrat{
"m01": SuperFollow,
"m02": MACDEMAV2,
"m03": SMACrossV1,
})
}