RSI指标水平线随趋势自动调整报错求助(附代码)
问题原因
报错核心:hline()函数要求传入固定的输入型浮点数(input float),但你在barstate.islast代码块里给h、m、l重新赋值后,这些变量变成了随K线变化的序列值(series float),参数类型不匹配导致报错。
解决方法
用plot()函数替代hline()绘制动态水平线——plot()支持接收序列值,只需设置style=plot.style_hline就能模拟出水平线效果,完全适配你的动态参数需求。
修改后的完整代码
//@version=5 indicator(title="Relative Strength Index", shorttitle="RSI", format=format.price, precision=2, timeframe="", timeframe_gaps=true) len = input.int(14, minval=1, title="Length") src = input(close, "Source") var arrTrend = array.new_float() var float trend = 0.0 // 定义为var float,用于存储动态更新的水平线数值 var float h = 0.0 var float m = 0.0 var float l = 0.0 up = ta.rma(math.max(ta.change(src), 0), len) down = ta.rma(-math.min(ta.change(src), 0), len) rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down)) plot(rsi, "RSI", color=#7E57C2) // [supertrend, direction] = ta.supertrend(1, 2) array.push(arrTrend, direction) // if barstate.islast trend := array.get(arrTrend, array.size(arrTrend) - 1) trendLabel = label.new(bar_index, high, trend > 0 ? 'down' : 'up' , yloc = yloc.belowbar, textcolor = color.white) if trend < 0 h := 80 m := 60 l := 40 if trend > 0 h := 60 m := 40 l := 20 // 用plot模拟动态水平线,设置style为hline样式 band1 = plot(h, "Upper Band", color=#787B86, style=plot.style_hline) bandm = plot(m, "Middle Band", color=color.new(#787B86, 50), style=plot.style_hline) band0 = plot(l, "Lower Band", color=#787B86, style=plot.style_hline) fill(band1, band0, color=color.rgb(126, 87, 194, 90), title="Background")
关键修改说明
- 将h、m、l改为
var float类型,确保能存储动态更新的数值 - 用
plot(..., style=plot.style_hline)替换hline(),兼容动态序列值输入 - 保留原有的趋势判断逻辑,功能不受影响
内容的提问来源于stack exchange,提问作者Eugene1111
相关产品推荐
相关产品推荐

