From 81e6472cd0b6fec19952f7b21cf68301be3c37d9 Mon Sep 17 00:00:00 2001 From: yanweidong Date: Thu, 29 Jan 2026 02:57:10 +0800 Subject: [PATCH] deving --- internal/logic/strategy/rule/rsi.go | 51 ++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/internal/logic/strategy/rule/rsi.go b/internal/logic/strategy/rule/rsi.go index ad00ffd..a8f2710 100644 --- a/internal/logic/strategy/rule/rsi.go +++ b/internal/logic/strategy/rule/rsi.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" - "git.apinb.com/bsm-sdk/core/utils" "git.apinb.com/quant/gostock/internal/impl" "git.apinb.com/quant/gostock/internal/logic/types" "git.apinb.com/quant/gostock/internal/models" @@ -65,11 +64,53 @@ func (r *Rsi) Run(code string) *types.RuleResult { } rsiResult := talib.Rsi(close, r.Args.RsiPeriod) + prveRsi := rsiResult[len(rsiResult)-2] lastRsi := rsiResult[len(rsiResult)-1] - lastRsi = utils.FloatRound(lastRsi, 2) - if lastRsi > float64(r.Args.RsiOversold) { - return &types.RuleResult{Key: r.Key, Name: r.Name, Score: -1, Desc: fmt.Sprintf("RSI=%.2f 高于%d", lastRsi, r.Args.RsiOversold)} + prveRsiInt := int(prveRsi) + lastRsiInt := int(lastRsi) + + // 跌破RSI下轨 + if lastRsiInt > r.Args.RsiOversold { + if CheckLowest(close, lastRsiInt, 5) { + return &types.RuleResult{Key: r.Key, Name: r.Name, Score: 1, Desc: fmt.Sprintf("RSI=%d 跌破下轨,5日最低", lastRsiInt)} + } + if CheckLowest(close, lastRsiInt, 14) { + return &types.RuleResult{Key: r.Key, Name: r.Name, Score: 1, Desc: fmt.Sprintf("RSI=%d 跌破下轨,14日最低", lastRsiInt)} + } + if CheckLowest(close, lastRsiInt, 20) { + return &types.RuleResult{Key: r.Key, Name: r.Name, Score: 1, Desc: fmt.Sprintf("RSI=%d 跌破下轨,20日最低", lastRsiInt)} + } + return &types.RuleResult{Key: r.Key, Name: r.Name, Score: -1, Desc: fmt.Sprintf("RSI=%d 高于%d", lastRsiInt, r.Args.RsiOversold)} } - return &types.RuleResult{Key: r.Key, Name: r.Name, Score: 1, Rsi: lastRsi, Desc: fmt.Sprintf("RSI=%.2f 低于%d", lastRsi, r.Args.RsiOversold)} + // RSI跌破下轨后呈上涨趋势 + if lastRsiInt <= prveRsiInt { + return &types.RuleResult{Key: r.Key, Name: r.Name, Score: -1, Desc: fmt.Sprintf("Rsi=%d prveRsi=%d,跌破下轨,持续下跌", lastRsiInt, prveRsiInt)} + } + return &types.RuleResult{Key: r.Key, Name: r.Name, Score: 1, Rsi: lastRsi, Desc: fmt.Sprintf("RSI=%d prveRsi=%d,跌破下轨后呈上涨趋势", lastRsiInt, prveRsiInt)} +} + +// 检查值是否为数组最后N个元素中的最小值 +func CheckLowest(prices []float64, target int, n int) bool { + if len(prices) == 0 || n <= 0 { + return false + } + + // 如果n大于数组长度,使用整个数组 + if n > len(prices) { + n = len(prices) + } + + // 获取最后n个元素 + slice := prices[len(prices)-n:] + + // 查找最小值 + minVal := slice[0] + for _, val := range slice[1:] { + if val < minVal { + minVal = val + } + } + + return target == int(minVal) }