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

95 lines
2.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 J2874(pol *config.RunPolicyConfig) *strat.TradeStrat {
// 定义可调参数
rsiPeriod := pol.DefInt("rsiPeriod", 70, core.PNorm(20, 100))
longExitThreshold := pol.Def("longExitThreshold", 0.65, core.PNorm(0.55, 0.8))
shortExitThreshold := pol.Def("shortExitThreshold", 0.35, core.PNorm(0.2, 0.45))
allowLong := pol.Def("allowLong", 1.0, core.PUniform(0, 1)) > 0.5
allowShort := pol.Def("allowShort", 1.0, core.PUniform(0, 1)) > 0.5
return &strat.TradeStrat{
WarmupNum: rsiPeriod, // 预热期至少为RSI周期
HedgeOff: true, // 单向持仓模式,与原PineScript策略一致
RunTimeFrames: []string{"4h"},
OnBar: func(s *strat.StratJob) {
e := s.Env
// 计算各种价格组合
ohlc4 := ta.HLC3(e.High, e.Low, e.Close).Add(e.Open).Div(4)
hlc3 := ta.HLC3(e.High, e.Low, e.Close)
hl2 := ta.HL2(e.High, e.Low)
// 计算各种RSI值并归一化(除以100)
rsiOhlc4 := ta.RSI(ohlc4, rsiPeriod).Div(100)
rsiClose := ta.RSI(e.Close, rsiPeriod).Div(100)
rsiOpen := ta.RSI(e.Open, rsiPeriod).Div(100)
rsiHigh := ta.RSI(e.High, rsiPeriod).Div(100)
rsiHlc3 := ta.RSI(hlc3, rsiPeriod).Div(100)
rsiHl2 := ta.RSI(hl2, rsiPeriod).Div(100)
// 计算RSI均值
rsiAvg := rsiOhlc4.Add(rsiClose).Add(rsiOpen).Add(rsiHigh).Add(rsiHlc3).Add(rsiHl2).Div(6)
rsiAvgVal := rsiAvg.Get(0)
// 检查数据有效性
if math.IsNaN(rsiAvgVal) {
return
}
// 检测RSI均值与0.5的交叉
entryThreshold := 0.5
crossSignal := rsiAvg.Cross(entryThreshold)
// 生成交易信号
longSignal := crossSignal == 1 // RSI均值上穿0.5
shortSignal := crossSignal == -1 // RSI均值下穿0.5
// 生成平仓信号
longExit := rsiAvgVal >= longExitThreshold || shortSignal
shortExit := rsiAvgVal <= shortExitThreshold || longSignal
// 执行交易逻辑
if allowLong && longSignal && len(s.LongOrders) == 0 {
// 开多头仓位
s.OpenOrder(&strat.EnterReq{
Tag: "long",
})
}
if allowShort && shortSignal && len(s.ShortOrders) == 0 {
// 开空头仓位
s.OpenOrder(&strat.EnterReq{
Tag: "short",
Short: true,
})
}
if allowLong && len(s.LongOrders) > 0 && longExit {
// 平多头仓位
s.CloseOrders(&strat.ExitReq{
Tag: "long_exit",
Dirt: core.OdDirtLong,
})
}
if allowShort && len(s.ShortOrders) > 0 && shortExit {
// 平空头仓位
s.CloseOrders(&strat.ExitReq{
Tag: "short_exit",
Dirt: core.OdDirtShort,
})
}
},
}
}