创建RSI背离指标时遇lowest函数参数错误求助
问题分析与修复
错误根源
报错提示lowest函数的length参数为0,核心问题有两个:
ta.lowest调用参数顺序错误:你写的ta.lowest(bars_lowest)把周期长度当成了数据源,正确调用逻辑是ta.lowest(数据源, 周期长度),这里需要计算的是bars_lowest周期内的最低价,所以要传入low作为数据源。bars_lowest可能为0:当当前K线的RSI正好等于rsi_lowest时,ta.barssince(rsi_lowest == rsi)会返回0,直接作为lowest的长度参数会触发报错,必须确保周期长度始终大于0。
修复后的完整代码
//@version=5 indicator("My script") noColor = color.new(color.white, 100) Bullcondition = false // 创建RSI指标 rsi = ta.rsi(close, 14) plot(rsi, color=#7E57C2) hline(50, linestyle=hline.style_dashed, color=color.new(#787B86, 50)) upperband = hline(30, linestyle=hline.style_dashed, color=#787B86, linewidth=1) lowerband = hline(70, linestyle=hline.style_dashed, color=#787B86, linewidth=1) fill(upperband, lowerband, color=color.rgb(126, 87, 194, 90), title="RSI Background Fill") //--------------------------------------------------- pivot_right = input.int(1, minval=1, title="pivot right") pivot_left = input.int(10, minval=1, title="pivot left") min_range = input.int(5, minval=1, title="Min range") max_range = input.int(60, minval=1, title="Max range") // 验证区间有效性的函数 confirm_range(x) => bars = ta.barssince(x == true) min_range <= bars and bars <= max_range //--------------看涨背离逻辑-------------- // 强背离:价格创低但RSI走高 rsi_lowest = ta.lowest(rsi, 50) // 强制bars_lowest至少为1,避免周期长度为0 bars_lowest = math.max(nz(ta.barssince(rsi_lowest == rsi)), 1) price_atlowest = low[bars_lowest] // 修正ta.lowest参数:数据源为low,周期为bars_lowest lowest_since = ta.lowest(low, bars_lowest) // 简化pivot_low判断逻辑 pivot_low = not na(ta.pivotlow(rsi, bars_lowest, pivot_right)) Bullcondition := rsi_lowest < rsi and price_atlowest < lowest_since and pivot_low plot(pivot_low ? rsi[pivot_right] : na, offset=-pivot_right, linewidth=1, color=(Bullcondition ? color.yellow : noColor))
关键修复细节
- 用
math.max(nz(ta.barssince(...)), 1)确保bars_lowest始终≥1,彻底避免0值传入周期参数。 - 修正
ta.lowest的调用格式,明确指定数据源和周期长度,符合Pine Script函数规范。 - 简化
pivot_low的判断逻辑,用not na()替代冗余的三元表达式,代码更简洁易读。
内容的提问来源于stack exchange,提问作者Gaming World
相关产品推荐
相关产品推荐

