116 lines
4.1 KiB
Go
116 lines
4.1 KiB
Go
// 修复后的banbot完整策略代码
|
||
package llmjob
|
||
|
||
import (
|
||
"github.com/banbox/banbot/config"
|
||
"github.com/banbox/banbot/core"
|
||
"github.com/banbox/banbot/orm/ormo"
|
||
"github.com/banbox/banbot/strat"
|
||
ta "github.com/banbox/banta"
|
||
"math"
|
||
)
|
||
|
||
func J4316(pol *config.RunPolicyConfig) *strat.TradeStrat {
|
||
// 定义可调超参数
|
||
ema1Length := pol.DefInt("ema1_length", 19, core.PNorm(5, 50))
|
||
ema2Length := pol.DefInt("ema2_length", 50, core.PNorm(20, 100))
|
||
rsiOverbought := pol.DefInt("rsi_overbought", 70, core.PNorm(60, 80))
|
||
rsiOversold := pol.DefInt("rsi_oversold", 30, core.PNorm(20, 40))
|
||
macdFast := pol.DefInt("macd_fast", 12, core.PNorm(8, 16))
|
||
macdSlow := pol.DefInt("macd_slow", 26, core.PNorm(20, 32))
|
||
macdSignal := pol.DefInt("macd_signal", 9, core.PNorm(6, 12))
|
||
atrLength := pol.DefInt("atr_length", 14, core.PNorm(7, 21))
|
||
atrMult := pol.Def("atr_mult", 1.85, core.PNorm(1.5, 5))
|
||
useVolume := pol.Def("use_volume", 1.0, core.PNorm(0.0, 1.0)) > 0.5 // 修复:使用Def代替DefBool
|
||
volumeMult := pol.Def("volume_mult", 2.0, core.PNorm(1.0, 4.0))
|
||
capitalRisk := pol.Def("capital_risk", 2.0, core.PNorm(0.5, 5.0)) / 100
|
||
|
||
return &strat.TradeStrat{
|
||
WarmupNum: ema2Length + 20, // 确保所有指标都有足够数据
|
||
HedgeOff: true, // 单向持仓模式
|
||
EachMaxLong: 1, // 最多一个多头订单
|
||
EachMaxShort: 1, // 最多一个空头订单
|
||
DrawDownExit: true, // 启用回撤止盈
|
||
RunTimeFrames: []string{"15m"},
|
||
OnBar: func(s *strat.StratJob) {
|
||
e := s.Env
|
||
|
||
// 计算技术指标
|
||
ema1 := ta.EMA(e.Close, ema1Length)
|
||
ema2 := ta.EMA(e.Close, ema2Length)
|
||
rsi := ta.RSI(e.Close, 14)
|
||
|
||
// 计算MACD
|
||
macdLine := ta.EMA(e.Close, macdFast).Sub(ta.EMA(e.Close, macdSlow))
|
||
macdSignalLine := ta.EMA(macdLine, macdSignal)
|
||
macdHist := macdLine.Sub(macdSignalLine)
|
||
|
||
// 计算ATR
|
||
atr := ta.ATR(e.High, e.Low, e.Close, atrLength)
|
||
|
||
// 计算成交量过滤条件
|
||
volumeMA := ta.SMA(e.Volume, 20)
|
||
volumeFilter := e.Volume.Get(0) > volumeMA.Get(0)*volumeMult
|
||
|
||
// 检查数据有效性
|
||
if math.IsNaN(ema1.Get(0)) || math.IsNaN(ema2.Get(0)) ||
|
||
math.IsNaN(rsi.Get(0)) || math.IsNaN(macdHist.Get(0)) ||
|
||
math.IsNaN(atr.Get(0)) {
|
||
return
|
||
}
|
||
|
||
// 信号过滤 - 优化重复计算
|
||
crossResult := ema1.Cross(ema2)
|
||
emaCrossUp := crossResult == 1 // 快速EMA上穿慢速EMA
|
||
emaCrossDown := crossResult == -1 // 快速EMA下穿慢速EMA
|
||
rsiFilterLong := rsi.Get(0) > float64(rsiOversold) // RSI高于超卖水平
|
||
rsiFilterShort := rsi.Get(0) < float64(rsiOverbought) // RSI低于超买水平
|
||
macdFilterLong := macdHist.Get(0) > 0 // MACD柱状图为正
|
||
macdFilterShort := macdHist.Get(0) < 0 // MACD柱状图为负
|
||
volumeOk := !useVolume || volumeFilter // 成交量条件满足
|
||
|
||
// 生成交易信号
|
||
longEntry := emaCrossUp && rsiFilterLong && macdFilterLong && volumeOk
|
||
shortEntry := emaCrossDown && rsiFilterShort && macdFilterShort && volumeOk
|
||
|
||
// 风险管理计算
|
||
atrValue := atr.Get(0) // 修复:删除未使用的closePrice变量
|
||
|
||
// 计算交易规模 - 修复基于ATR的交易规模计算
|
||
riskAmount := s.Strat.GetStakeAmount(s) * capitalRisk
|
||
tradeSize := riskAmount / math.Max(atrValue*atrMult, 1.0)
|
||
|
||
// 计算追踪止损价格
|
||
|
||
// 执行交易
|
||
if longEntry && len(s.LongOrders) == 0 {
|
||
s.OpenOrder(&strat.EnterReq{
|
||
Tag: "Long",
|
||
Amount: tradeSize,
|
||
// 修复:移除不存在的TrailOffset字段
|
||
})
|
||
}
|
||
|
||
if shortEntry && len(s.ShortOrders) == 0 {
|
||
s.OpenOrder(&strat.EnterReq{
|
||
Tag: "Short",
|
||
Short: true,
|
||
Amount: tradeSize,
|
||
// 修复:移除不存在的TrailOffset字段
|
||
})
|
||
}
|
||
},
|
||
|
||
// 实现回撤止盈逻辑
|
||
GetDrawDownExitRate: func(s *strat.StratJob, od *ormo.InOutOrder, maxChg float64) float64 {
|
||
// 根据最大盈利回撤比例决定止盈
|
||
if maxChg > 0.1 { // 盈利超过10%
|
||
return 0.5 // 回撤50%止盈
|
||
} else if maxChg > 0.05 { // 盈利超过5%
|
||
return 0.7 // 回撤70%止盈
|
||
}
|
||
return 0 // 不设置回撤止盈
|
||
},
|
||
}
|
||
}
|