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

65 lines
1.7 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 修复后的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 J559(pol *config.RunPolicyConfig) *strat.TradeStrat {
fastPeriod := pol.DefInt("fastPeriod", 14, core.PNorm(5, 20))
macdRate := pol.Def("macdRate", 2.35, core.PNorm(1.5, 4))
signalPeriod := 9 // 固定值,不在可调参数中
// 计算慢线周期
slowPeriod := int(float64(fastPeriod) * macdRate)
// 预热数量应该大于慢线周期
warmupNum := slowPeriod + 10
return &strat.TradeStrat{
WarmupNum: warmupNum,
HedgeOff: true, // 单向持仓模式
EachMaxLong: 1, // 最多一个多头订单
EachMaxShort: 1, // 最多一个空头订单
RunTimeFrames: []string{"8h"},
OnBar: func(s *strat.StratJob) {
e := s.Env
// 计算MACD指标
fastLine, slowLine := ta.MACD(e.Close, fastPeriod, slowPeriod, signalPeriod)
// 获取当前和前一个周期的值
fastCurr := fastLine.Get(0)
fastPrev := fastLine.Get(1)
slowCurr := slowLine.Get(0)
slowPrev := slowLine.Get(1)
// 检查数据有效性
if math.IsNaN(fastCurr) || math.IsNaN(fastPrev) || math.IsNaN(slowCurr) || math.IsNaN(slowPrev) {
return
}
// 检测MACD快线上穿慢线(金叉)
if fastCurr > slowCurr && fastPrev < slowPrev && len(s.LongOrders) == 0 {
// 执行买入操作
s.OpenOrder(&strat.EnterReq{
Tag: "Enter Long",
})
}
// 检测MACD快线下穿慢线(死叉)
if fastCurr < slowCurr && fastPrev > slowPrev && len(s.ShortOrders) == 0 {
// 执行卖出操作
s.OpenOrder(&strat.EnterReq{
Tag: "Enter Short",
Short: true,
})
}
},
}
}