PineScript V5 RSI背离策略无交易信号问题排查求助
基于RSI看涨背离的PineScript V5策略无交易触发问题排查与资源推荐
问题描述
开发一款基于RSI看涨背离做多的PineScript V5简易策略,但代码始终未触发任何交易,请求排查原因。初始RSI背离指标代码来自Libertus,策略逻辑从//strategy code注释下方开始,完整代码如下:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © rypto_dynasty //@version=5 strategy("RSI div strategy", overlay=true) len = input.int(14, minval=1, title='RSI Length') ob = input.int(defval=70, title='Overbought', minval=0, maxval=100) os = input.int(defval=30, title='Oversold', minval=0, maxval=100) i_stop_prcnt = input.float(title='Stop percentage?', defval=0.5) i_exposure = input.float(title='Exposure?', defval=0.5) i_period_start = input.time(timestamp("1 Apr 2022 00:00 +0300"), "Period start") i_period_end = input.time(timestamp("14 Apr 2022 00:00 +0300"), 'Period end') // RSI code rsi = ta.rsi(close, len) band1 = hline(ob) band0 = hline(os) plot(rsi, color=rsi > ob or rsi < os ? color.new(color.red, 0) : color.new(color.black, 0)) fill(band1, band0, color=color.new(color.purple, 97)) // DIVS code piv = input(false, 'Hide pivots?') shrt = input(false, 'Shorter labels?') hidel = input(false, 'Hide labels and color background') xbars = input.int(defval=90, title='Div lookback period (bars)?', minval=1) hb = math.abs(ta.highestbars(rsi, xbars)) // Finds bar with highest value in last X bars lb = math.abs(ta.lowestbars(rsi, xbars)) // Finds bar with lowest value in last X bars // Defining variable values, mandatory in Pine 3 max = float(na) max_rsi = float(na) min = float(na) min_rsi = float(na) pivoth = bool(na) pivotl = bool(na) divbear = bool(na) divbull = bool(na) // If bar with lowest / highest is current bar, use it's value max := hb == 0 ? close : na(max[1]) ? close : max[1] max_rsi := hb == 0 ? rsi : na(max_rsi[1]) ? rsi : max_rsi[1] min := lb == 0 ? close : na(min[1]) ? close : min[1] min_rsi := lb == 0 ? rsi : na(min_rsi[1]) ? rsi : min_rsi[1] // Compare high of current bar being examined with previous bar's high // If curr bar high is higher than the max bar high in the lookback window range if close > max // we have a new high max := close // change variable "max" to use current bar's high value max if rsi > max_rsi // we have a new high max_rsi := rsi // change variable "max_rsi" to use current bar's RSI value max_rsi if close < min // we have a new low min := close // change variable "min" to use current bar's low value min if rsi < min_rsi // we have a new low min_rsi := rsi // change variable "min_rsi" to use current bar's RSI value min_rsi // Finds pivot point with at least 2 right candles with lower value pivoth := max_rsi == max_rsi[2] and max_rsi[2] != max_rsi[3] ? true : na pivotl := min_rsi == min_rsi[2] and min_rsi[2] != min_rsi[3] ? true : na // Detects divergences between price and indicator with 1 candle delay so it filters out repeating divergences if max[1] > max[2] and rsi[1] < max_rsi and rsi <= rsi[1] divbear := true divbear if min[1] < min[2] and rsi[1] > min_rsi and rsi >= rsi[1] divbull := true divbull // Plots divergences and pivots with offest l = divbear ? label.new(bar_index - 1, rsi[1] + 1, 'BEAR', color=color.red, textcolor=color.white, style=label.style_label_down, yloc=yloc.price, size=size.small) : divbull ? label.new(bar_index - 1, rsi[1] - 1, 'BULL', color=color.green, textcolor=color.white, style=label.style_label_up, yloc=yloc.price, size=size.small) : pivoth ? label.new(bar_index - 2, max_rsi + 1, 'PIVOT', color=color.blue, textcolor=color.white, style=label.style_label_down, yloc=yloc.price, size=size.small) : pivotl ? label.new(bar_index - 2, min_rsi - 1, 'PIVOT', color=color.blue, textcolor=color.white, style=label.style_label_up, yloc=yloc.price, size=size.small) : na // Shorter labels if shrt label.set_text(l, na) // Hides pivots or labels if piv and (pivoth or pivotl) or hidel label.delete(l) //strategy code //checking if current time lies in the defined time period timeCond = i_period_start < time and time < i_period_end //Defining condition for bullish divergence long and bearish divergence short buyCondition = (divbull and strategy.position_size <= 0) ? true : false sellCondition = (divbear and strategy.position_size <= 0) ? true : false if buyCondition and timeCond strategy.entry('long', strategy.long, qty=1000) if strategy.position_size > 0 effective_sl = strategy.position_avg_price * i_stop_prcnt / 100 effective_exp = strategy.position_avg_price * i_exposure / 100 strategy.exit(id='long exit', from_entry='long', comment='long closed', stop=effective_sl , profit=effective_exp)
问题排查与修复建议
1. 背离信号变量生命周期问题
divbull仅在满足条件的单根K线被设为true,后续K线会自动回到na,导致策略判断时信号已失效。需用var声明变量并维护信号状态:
// 改用var声明并维护信号状态 var divbull = false var divbear = false // 每根K线开始时重置信号(可根据需求调整重置逻辑) if ta.barstate.isnew divbull := false divbear := false // 检测看涨背离时设置信号为true if min[1] < min[2] and rsi[1] > min_rsi and rsi >= rsi[1] divbull := true
2. 止损止盈计算逻辑错误
原代码中止损价计算为仓位均价的百分比(如均价100,止损价为0.5),不符合实际逻辑,需修改为:
effective_sl = strategy.position_avg_price * (1 - i_stop_prcnt / 100) // 止损价=均价*(1-止损百分比) effective_exp = strategy.position_avg_price * (1 + i_exposure / 100) // 止盈价=均价*(1+盈利百分比)
3. 时间条件时区匹配问题
i_period_start和i_period_end设置为GMT+3时区,需确保图表时区与该设置一致,或改用time_close避免时区偏差:
timeCond = i_period_start < time_close and time_close < i_period_end
4. 无效做空逻辑干扰
策略仅做多,sellCondition中strategy.position_size <= 0时触发做空的逻辑无意义,建议删除相关代码,避免信号判断混乱。
PineScript V5学习资源推荐
- 官方文档:Pine Script V5官方手册包含完整语法说明、策略开发指南,内置示例库有大量可直接参考的策略模板。
- 社区策略库:TradingView社区公开的用户分享策略,筛选Pine Script V5标签,重点学习带有详细注释的策略源码。
- 官方分步教程:官方推出的从指标编写到策略回测的系列教程,适合系统掌握开发流程。
- 回测优化指南:官方文档中的回测参数优化部分,学习设置滑点、佣金、仓位管理等参数,提升策略真实有效性。
内容的提问来源于stack exchange,提问作者Kashish Kumar
相关产品推荐
相关产品推荐

