101 lines
2.8 KiB
Go
101 lines
2.8 KiB
Go
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 J5743(pol *config.RunPolicyConfig) *strat.TradeStrat {
|
|
// 定义可调超参数
|
|
fastPeriod := pol.DefInt("fastPeriod", 12, core.PNorm(5, 20))
|
|
macdRate := pol.Def("macdRate", 2.17, core.PNorm(1.5, 4))
|
|
signalPeriod := pol.DefInt("signalPeriod", 9, core.PNorm(5, 15))
|
|
stopLossBars := pol.DefInt("stopLossBars", 4, core.PNorm(1, 10))
|
|
riskRewardRatio := pol.Def("riskRewardRatio", 2, core.PNorm(1, 3))
|
|
direction := pol.DefInt("direction", 2, core.PUniform(0, 2)) // 0: long, 1: short, 2: both
|
|
|
|
// 计算慢速周期
|
|
slowPeriod := int(float64(fastPeriod) * macdRate)
|
|
|
|
// 确定预热数量,取所有参数中的最大值
|
|
warmupNum := fastPeriod
|
|
if slowPeriod > warmupNum {
|
|
warmupNum = slowPeriod
|
|
}
|
|
if signalPeriod > warmupNum {
|
|
warmupNum = signalPeriod
|
|
}
|
|
if stopLossBars > warmupNum {
|
|
warmupNum = stopLossBars
|
|
}
|
|
|
|
return &strat.TradeStrat{
|
|
WarmupNum: warmupNum,
|
|
EachMaxLong: 1, // 最多一个做多订单
|
|
EachMaxShort: 1, // 最多一个做空订单
|
|
HedgeOff: true, // 单向持仓模式
|
|
RunTimeFrames: []string{"8h"},
|
|
OnBar: func(s *strat.StratJob) {
|
|
e := s.Env
|
|
|
|
// 计算MACD指标
|
|
macdLine, signalLine := ta.MACD(e.Close, fastPeriod, slowPeriod, signalPeriod)
|
|
|
|
// 获取当前值
|
|
macdVal := macdLine.Get(0)
|
|
signalVal := signalLine.Get(0)
|
|
closePrice := e.Close.Get(0)
|
|
|
|
// 计算直方图值
|
|
histVal := macdVal - signalVal
|
|
|
|
// 检测交叉信号
|
|
macdCross := macdLine.Cross(signalLine)
|
|
longSignal := macdCross == 1 && histVal > 0 // MACD金叉且直方图为正
|
|
shortSignal := macdCross == -1 && histVal < 0 // MACD死叉且直方图为负
|
|
|
|
// 计算止损价格
|
|
longStopLoss := ta.Lowest(e.Low, stopLossBars).Get(0)
|
|
shortStopLoss := ta.Highest(e.High, stopLossBars).Get(0)
|
|
|
|
// 检查止损价格有效性
|
|
if math.IsNaN(longStopLoss) || math.IsNaN(shortStopLoss) {
|
|
return
|
|
}
|
|
|
|
// 执行做多交易
|
|
if direction != 1 && longSignal && len(s.LongOrders) == 0 {
|
|
// 计算止盈价格
|
|
distance := closePrice - longStopLoss
|
|
takeProfit := closePrice + distance*riskRewardRatio
|
|
|
|
s.OpenOrder(&strat.EnterReq{
|
|
Tag: "Larga",
|
|
Short: false,
|
|
OrderType: core.OrderTypeMarket,
|
|
StopLoss: longStopLoss,
|
|
TakeProfit: takeProfit,
|
|
})
|
|
}
|
|
|
|
// 执行做空交易
|
|
if direction != 0 && shortSignal && len(s.ShortOrders) == 0 {
|
|
// 计算止盈价格
|
|
distance := shortStopLoss - closePrice
|
|
takeProfit := closePrice - distance*riskRewardRatio
|
|
|
|
s.OpenOrder(&strat.EnterReq{
|
|
Tag: "Corta",
|
|
Short: true,
|
|
OrderType: core.OrderTypeMarket,
|
|
StopLoss: shortStopLoss,
|
|
TakeProfit: takeProfit,
|
|
})
|
|
}
|
|
},
|
|
}
|
|
}
|