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

106 lines
3.8 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"
)
func J1569(pol *config.RunPolicyConfig) *strat.TradeStrat {
// 定义可调超参数
fastPeriod := pol.DefInt("fastPeriod", 10, core.PNorm(5, 20))
smaRatio := pol.Def("smaRatio", 2.0, core.PNorm(1.5, 4.0))
supportResistancePeriod := pol.DefInt("supportResistancePeriod", 12, core.PNorm(5, 20))
confirmPeriod := pol.DefInt("confirmPeriod", 3, core.PNorm(2, 5))
riskRewardRatio := pol.Def("riskRewardRatio", 2.2, core.PNorm(1.5, 3.0))
// 计算慢速SMA周期
slowPeriod := int(float64(fastPeriod) * smaRatio)
// 确保预热K线数量足够计算所有指标
warmupNum := slowPeriod
if supportResistancePeriod > warmupNum {
warmupNum = supportResistancePeriod
}
return &strat.TradeStrat{
WarmupNum: warmupNum,
HedgeOff: true, // 单向持仓模式
EachMaxLong: 1, // 最多1个多头订单
EachMaxShort: 1, // 最多1个空头订单
RunTimeFrames: []string{"1h"},
OnBar: func(s *strat.StratJob) {
e := s.Env
// 计算移动平均线
smaFast := ta.SMA(e.Close, fastPeriod)
smaSlow := ta.SMA(e.Close, slowPeriod)
// 计算支撑阻力位(过去10根K线的最高价和最低价)
recentLow := ta.Lowest(e.Low, supportResistancePeriod)
recentHigh := ta.Highest(e.High, supportResistancePeriod)
// 计算确认条件所需的指标
highestSmaSlow := ta.Highest(smaSlow, confirmPeriod)
lowestSmaSlow := ta.Lowest(smaSlow, confirmPeriod)
lowestLow3 := ta.Lowest(e.Low, confirmPeriod)
highestHigh3 := ta.Highest(e.High, confirmPeriod)
// 获取当前值
closePrice := e.Close.Get(0)
smaSlowVal := smaSlow.Get(0)
recentLowVal := recentLow.Get(0)
recentHighVal := recentHigh.Get(0)
highestSmaSlowVal := highestSmaSlow.Get(0)
lowestSmaSlowVal := lowestSmaSlow.Get(0)
lowestLow3Val := lowestLow3.Get(0)
highestHigh3Val := highestHigh3.Get(0)
// 生成交易信号 - 只计算一次交叉信号
crossSignal := smaFast.Cross(smaSlow)
bullishCross := crossSignal == 1 // 10周期SMA上穿20周期SMA
bearishCross := crossSignal == -1 // 10周期SMA下穿20周期SMA
breakoutUp := closePrice > highestSmaSlowVal // 收盘价突破过去3根K线的20周期SMA最高点
breakoutDown := closePrice < lowestSmaSlowVal // 收盘价跌破过去3根K线的20周期SMA最低点
retestUp := lowestLow3Val > smaSlowVal // 过去3根K线的最低价保持在20周期SMA之上
retestDown := highestHigh3Val < smaSlowVal // 过去3根K线的最高价保持在20周期SMA之下
// 三重确认条件
buyCondition := bullishCross && breakoutUp && retestUp
sellCondition := bearishCross && breakoutDown && retestDown
// 计算止损止盈
longSL := recentLowVal // 买入止损设为过去10根K线的最低价
shortSL := recentHighVal // 卖出止损设为过去10根K线的最高价
riskSizeLong := closePrice - longSL // 买入交易的风险大小
riskSizeShort := shortSL - closePrice // 卖出交易的风险大小
longTP := closePrice + (riskSizeLong * riskRewardRatio) // 买入交易的止盈价格
shortTP := closePrice - (riskSizeShort * riskRewardRatio) // 卖出交易的止盈价格
// 执行交易
if buyCondition && len(s.LongOrders) == 0 {
s.OpenOrder(&strat.EnterReq{
Tag: "Long",
OrderType: core.OrderTypeMarket,
StopLoss: longSL,
TakeProfit: longTP,
})
}
if sellCondition && len(s.ShortOrders) == 0 {
s.OpenOrder(&strat.EnterReq{
Tag: "Short",
Short: true,
OrderType: core.OrderTypeMarket,
StopLoss: shortSL,
TakeProfit: shortTP,
})
}
},
}
}