// 修复后的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 J128(pol *config.RunPolicyConfig) *strat.TradeStrat { // 定义可调参数 longEntryThreshold := pol.Def("long_entry_threshold", 60.0, core.PNorm(50.0, 70.0)) thresholdRange := pol.Def("threshold_range", 14.0, core.PNorm(10.0, 25.0)) smaPeriod := pol.DefInt("sma_period", 3, core.PNorm(2, 8)) atrPeriod := pol.DefInt("atr_period", 14, core.PNorm(7, 21)) tp1ATR := pol.Def("tp1_atr", 3.0, core.PNorm(1.5, 5.0)) tp2ATR := pol.Def("tp2_atr", 8.0, core.PNorm(5.0, 12.0)) tp3ATR := pol.Def("tp3_atr", 14.0, core.PNorm(10.0, 20.0)) tp1Percent := pol.Def("tp1_percent", 12.0, core.PNorm(5.0, 25.0)) tp2Percent := pol.Def("tp2_percent", 12.0, core.PNorm(5.0, 25.0)) tp3Percent := pol.Def("tp3_percent", 12.0, core.PNorm(5.0, 25.0)) useTakeProfit := pol.Def("use_take_profit", 0.0, core.PNorm(0.0, 1.0)) // 0=false, 1=true tradingDirection := pol.Def("trading_direction", 2.0, core.PNorm(0.0, 2.0)) // 0=Long, 1=Short, 2=Both // 计算衍生参数 shortEntryThreshold := longEntryThreshold - thresholdRange longExitThreshold := shortEntryThreshold shortExitThreshold := longEntryThreshold // 确定最长预热周期 warmupNum := 55 + smaPeriod + 10 return &strat.TradeStrat{ Name: "FATR", WarmupNum: warmupNum, HedgeOff: true, // 单向持仓模式 EachMaxLong: 1, EachMaxShort: 1, RunTimeFrames: []string{"4h"}, OnBar: func(s *strat.StratJob) { e := s.Env // 计算真实高低点和真实波动范围 trueLow := e.Low.Min(e.Close.Back(1)) trueHigh := e.High.Max(e.Close.Back(1)) trueRange := trueHigh.Sub(trueLow) // 计算买入压力(BP) bp := e.Close.Sub(trueLow) // 计算不同周期的压力比率 calcRatio := func(len int) *ta.Series { sumBP := ta.Sum(bp, len) sumTR := ta.Sum(trueRange, len) return sumBP.Div(sumTR).Mul(100) } // 计算Fibonacci数列周期的压力比率 ratio8 := calcRatio(8) ratio13 := calcRatio(13) ratio21 := calcRatio(21) ratio34 := calcRatio(34) ratio55 := calcRatio(55) // 计算加权平均值 weightedAvg := ratio8.Mul(5).Add( ratio13.Mul(4)).Add( ratio21.Mul(3)).Add( ratio34.Mul(2)).Add( ratio55).Div(15) // 应用SMA平滑 weightedAvgSMA := ta.SMA(weightedAvg, smaPeriod) // 计算ATR值 atrValue := ta.ATR(e.High, e.Low, e.Close, atrPeriod) // 获取当前值 atrVal := atrValue.Get(0) closePrice := e.Close.Get(0) // 生成交易信号 longCondition := weightedAvgSMA.Cross(longEntryThreshold) == 1 shortCondition := weightedAvgSMA.Cross(shortEntryThreshold) == -1 longExit := weightedAvgSMA.Cross(longExitThreshold) == -1 shortExit := weightedAvgSMA.Cross(shortExitThreshold) == 1 // 执行交易逻辑 isLongEnabled := tradingDirection == 0 || tradingDirection == 2 isShortEnabled := tradingDirection == 1 || tradingDirection == 2 // 多头交易逻辑 if isLongEnabled { if longCondition && len(s.LongOrders) == 0 { // 开多单 s.OpenOrder(&strat.EnterReq{ Tag: "Long", }) // 设置止盈订单 if useTakeProfit > 0.5 { tpPrice1 := closePrice + tp1ATR*atrVal tpPrice2 := closePrice + tp2ATR*atrVal tpPrice3 := closePrice + tp3ATR*atrVal // 设置多个止盈订单 s.CloseOrders(&strat.ExitReq{ Tag: "TP1_Long", Dirt: core.OdDirtLong, ExitRate: tp1Percent / 100, Limit: tpPrice1, }) s.CloseOrders(&strat.ExitReq{ Tag: "TP2_Long", Dirt: core.OdDirtLong, ExitRate: tp2Percent / 100, Limit: tpPrice2, }) s.CloseOrders(&strat.ExitReq{ Tag: "TP3_Long", Dirt: core.OdDirtLong, ExitRate: tp3Percent / 100, Limit: tpPrice3, }) } } if longExit && len(s.LongOrders) > 0 { // 平多单 s.CloseOrders(&strat.ExitReq{ Tag: "Long_Exit", Dirt: core.OdDirtLong, }) } } // 空头交易逻辑 if isShortEnabled { if shortCondition && len(s.ShortOrders) == 0 { // 开空单 s.OpenOrder(&strat.EnterReq{ Tag: "Short", Short: true, }) // 设置止盈订单 if useTakeProfit > 0.5 { tpPrice1 := closePrice - tp1ATR*atrVal tpPrice2 := closePrice - tp2ATR*atrVal tpPrice3 := closePrice - tp3ATR*atrVal // 设置多个止盈订单 s.CloseOrders(&strat.ExitReq{ Tag: "TP1_Short", Dirt: core.OdDirtShort, ExitRate: tp1Percent / 100, Limit: tpPrice1, }) s.CloseOrders(&strat.ExitReq{ Tag: "TP2_Short", Dirt: core.OdDirtShort, ExitRate: tp2Percent / 100, Limit: tpPrice2, }) s.CloseOrders(&strat.ExitReq{ Tag: "TP3_Short", Dirt: core.OdDirtShort, ExitRate: tp3Percent / 100, Limit: tpPrice3, }) } } if shortExit && len(s.ShortOrders) > 0 { // 平空单 s.CloseOrders(&strat.ExitReq{ Tag: "Short_Exit", Dirt: core.OdDirtShort, }) } } }, } }