init
This commit is contained in:
116
llmjob/199.go
Normal file
116
llmjob/199.go
Normal file
@@ -0,0 +1,116 @@
|
||||
// 修复后的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 J199(pol *config.RunPolicyConfig) *strat.TradeStrat {
|
||||
// 定义可调参数
|
||||
emaShortPeriod := pol.DefInt("emaShortPeriod", 12, core.PNorm(5, 20))
|
||||
emaPeriodRatio := pol.Def("emaPeriodRatio", 2.5, core.PNorm(1.5, 4))
|
||||
rsiOverbought := pol.DefInt("rsiOverbought", 70, core.PNorm(60, 85))
|
||||
rsiOversold := pol.DefInt("rsiOversold", 30, core.PNorm(15, 40))
|
||||
|
||||
// 计算长期EMA周期
|
||||
emaLongPeriod := int(float64(emaShortPeriod) * emaPeriodRatio)
|
||||
|
||||
// MACD参数
|
||||
macdFastLength := 12
|
||||
macdSlowLength := 26
|
||||
macdSignalSmoothing := 9
|
||||
|
||||
// RSI参数
|
||||
rsiLength := 14
|
||||
|
||||
// 成交量移动平均周期
|
||||
volumeMAPeriod := 20
|
||||
|
||||
// 计算预热数量,取所有指标中最大的周期
|
||||
warmupNum := emaLongPeriod
|
||||
if macdSlowLength > warmupNum {
|
||||
warmupNum = macdSlowLength
|
||||
}
|
||||
if rsiLength > warmupNum {
|
||||
warmupNum = rsiLength
|
||||
}
|
||||
if volumeMAPeriod > warmupNum {
|
||||
warmupNum = volumeMAPeriod
|
||||
}
|
||||
|
||||
return &strat.TradeStrat{
|
||||
WarmupNum: warmupNum,
|
||||
HedgeOff: true, // 单向持仓模式
|
||||
RunTimeFrames: []string{"4h"},
|
||||
OnBar: func(s *strat.StratJob) {
|
||||
e := s.Env
|
||||
|
||||
// 计算技术指标
|
||||
emaShort := ta.EMA(e.Close, emaShortPeriod)
|
||||
emaLong := ta.EMA(e.Close, emaLongPeriod)
|
||||
|
||||
macdLine, signalLine := ta.MACD(e.Close, macdFastLength, macdSlowLength, macdSignalSmoothing)
|
||||
|
||||
rsi := ta.RSI(e.Close, rsiLength)
|
||||
|
||||
volumeMA := ta.SMA(e.Volume, volumeMAPeriod)
|
||||
|
||||
// 获取当前价格
|
||||
closePrice := e.Close.Get(0)
|
||||
|
||||
// 检查数据有效性
|
||||
if math.IsNaN(closePrice) {
|
||||
return
|
||||
}
|
||||
|
||||
// 生成交易信号 - 修复重复计算EMA交叉的问题
|
||||
emaCross := emaShort.Cross(emaLong) // 只计算一次EMA交叉
|
||||
emaCrossUp := emaCross == 1 // 短期EMA上穿长期EMA
|
||||
emaCrossDown := emaCross == -1 // 短期EMA下穿长期EMA
|
||||
macdBullish := macdLine.Get(0) > signalLine.Get(0) // MACD多头信号
|
||||
macdBearish := macdLine.Get(0) < signalLine.Get(0) // MACD空头信号
|
||||
rsiNotOverbought := rsi.Get(0) < float64(rsiOverbought) // RSI未超买
|
||||
rsiNotOversold := rsi.Get(0) > float64(rsiOversold) // RSI未超卖
|
||||
volumeHigh := e.Volume.Get(0) > volumeMA.Get(0) // 成交量高于平均
|
||||
|
||||
// 交易条件判断
|
||||
buyCondition := emaCrossUp && macdBullish && rsiNotOverbought && volumeHigh
|
||||
sellCondition := emaCrossDown && macdBearish && rsiNotOversold && volumeHigh
|
||||
|
||||
// 计算止损止盈价格 - 修复为使用固定比例,与原始策略一致
|
||||
//longStopLoss := closePrice * 0.95
|
||||
//longTakeProfit := closePrice * 1.05
|
||||
shortStopLoss := closePrice * 1.05
|
||||
shortTakeProfit := closePrice * 0.95
|
||||
|
||||
// 执行交易
|
||||
_ = buyCondition
|
||||
//if buyCondition && len(s.LongOrders) == 0 {
|
||||
// s.OpenOrder(&strat.EnterReq{
|
||||
// Tag: "Buy",
|
||||
// OrderType: core.OrderTypeMarket,
|
||||
// StopLoss: longStopLoss,
|
||||
// TakeProfit: longTakeProfit,
|
||||
// StopLossTag: "Stop Loss",
|
||||
// TakeProfitTag: "Take Profit",
|
||||
// })
|
||||
//}
|
||||
|
||||
if sellCondition && len(s.ShortOrders) == 0 {
|
||||
s.OpenOrder(&strat.EnterReq{
|
||||
Tag: "Sell",
|
||||
Short: true,
|
||||
OrderType: core.OrderTypeMarket,
|
||||
StopLoss: shortStopLoss,
|
||||
TakeProfit: shortTakeProfit,
|
||||
StopLossTag: "Stop Loss",
|
||||
TakeProfitTag: "Take Profit",
|
||||
})
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user