Files
banstagy/llmjob/3614.go
2026-08-24 20:53:21 +08:00

67 lines
1.8 KiB
Go

// 修复后的banbot完整策略代码
package llmjob
import (
"github.com/banbox/banbot/config"
"github.com/banbox/banbot/core"
"github.com/banbox/banbot/strat"
ta "github.com/banbox/banta"
"math"
)
func J3614(pol *config.RunPolicyConfig) *strat.TradeStrat {
// 定义可调超参数
length := pol.DefInt("length", 66, core.PNorm(20, 100))
hmaRate := pol.Def("hmaRate", 8.72, core.PNorm(3, 15))
channelFactor := pol.Def("channelFactor", 0.98, core.PNorm(0.95, 0.995))
// 计算HMA实际周期
hmaPeriod := int(float64(length) * hmaRate)
return &strat.TradeStrat{
WarmupNum: hmaPeriod, // 预热数量取最大的指标周期
HedgeOff: true, // 单向持仓模式
RunTimeFrames: []string{"1h"},
OnBar: func(s *strat.StratJob) {
e := s.Env
// 计算线性回归线
linreg := ta.LinReg(e.Close, length)
linregValue := linreg.Get(0)
// 计算通道上限线
band2 := linregValue * channelFactor
// 计算Hull移动平均线
hma := ta.HMA(e.Close, hmaPeriod)
hmaValue := hma.Get(0)
// 获取当前收盘价
closePrice := e.Close.Get(0)
// 检查数据有效性
if math.IsNaN(closePrice) || math.IsNaN(linregValue) || math.IsNaN(band2) || math.IsNaN(hmaValue) {
return
}
// 生成交易信号
longCondition := closePrice < band2 && hmaValue < closePrice
shortCondition := closePrice > linregValue
// 执行交易操作
if longCondition && len(s.LongOrders) == 0 {
// 满足做多条件且没有多头持仓时开仓
s.OpenOrder(&strat.EnterReq{
Tag: "BUY",
})
} else if shortCondition && len(s.LongOrders) > 0 {
// 满足平仓条件且持有多头仓位时平仓
s.CloseOrders(&strat.ExitReq{
Tag: "SELL",
Dirt: core.OdDirtLong,
})
}
},
}
}