Pine Script v5中stochRsi报错原因及正确用法,含交叉信号绘图
Pine Script v5中stochRsi报错原因与解决方法
一、报错核心原因
- 参数顺序不匹配:
ta.stochRsi()的标准参数顺序为source, lengthRsi, lengthStoch, smoothK, smoothD,你把数据源close放在了参数列表最后,导致参数类型与函数要求不符。 - 返回值使用错误:
ta.stochRsi()返回的是两个系列值(%K线和%D线),而ta.crossover()仅接受单一系列值作为输入,直接传入会触发类型不兼容报错。
二、正确使用方式
1. 正确获取StochRSI的K/D线
先通过变量接收ta.stochRsi()返回的两个值,参数顺序严格按照函数要求传入:
//@version=5 indicator("My script", overlay = true) // 参数对应:数据源、RSI周期、随机指标周期、%K平滑周期、%D平滑周期 stochRsiK, stochRsiD = ta.stochRsi(close, 14, 14, 5, 5)
2. 绘制交叉信号
根据需求选择交叉逻辑,以下是两种常见场景的实现:
场景1:%K与%D交叉(金叉/死叉)
// 金叉:%K上穿%D bullishCross = ta.crossover(stochRsiK, stochRsiD) // 死叉:%K下穿%D bearishCross = ta.crossunder(stochRsiK, stochRsiD) // 绘制金叉信号(K线下方绿色向上箭头) plotshape(bullishCross, title="金叉", location=location.belowbar, color=color.green, style=shape.labelup, text="金叉", textcolor=color.white) // 绘制死叉信号(K线上方红色向下箭头) plotshape(bearishCross, title="死叉", location=location.abovebar, color=color.red, style=shape.labeldown, text="死叉", textcolor=color.white)
场景2:%K穿越阈值(超买/超卖)
// %K上穿70(进入超买区间) overboughtCross = ta.crossover(stochRsiK, 70) // %K下穿30(进入超卖区间) oversoldCross = ta.crossunder(stochRsiK, 30) // 绘制超买信号 plotshape(overboughtCross, title="超买", location=location.abovebar, color=color.orange, style=shape.triangledown) // 绘制超卖信号 plotshape(oversoldCross, title="超卖", location=location.belowbar, color=color.blue, style=shape.triangleup)
三、完整可运行代码示例
//@version=5 indicator("StochRSI 交叉信号", overlay = true) // 计算StochRSI的K、D线 stochRsiK, stochRsiD = ta.stochRsi(close, 14, 14, 5, 5) // 定义各类交叉信号 bullishCross = ta.crossover(stochRsiK, stochRsiD) bearishCross = ta.crossunder(stochRsiK, stochRsiD) overboughtCross = ta.crossover(stochRsiK, 70) oversoldCross = ta.crossunder(stochRsiK, 30) // 绘制主图信号 plotshape(bullishCross, title="金叉", location=location.belowbar, color=color.new(color.green,0), style=shape.labelup, text="金叉", textcolor=color.white, size=size.small) plotshape(bearishCross, title="死叉", location=location.abovebar, color=color.new(color.red,0), style=shape.labeldown, text="死叉", textcolor=color.white, size=size.small) plotshape(overboughtCross, title="超买", location=location.abovebar, color=color.new(color.orange,0), style=shape.triangledown, size=size.small) plotshape(oversoldCross, title="超卖", location=location.belowbar, color=color.new(color.blue,0), style=shape.triangleup, size=size.small) // 副图绘制StochRSI曲线与阈值(可选) plot(stochRsiK, "%K", color=color.blue) plot(stochRsiD, "%D", color=color.red) hline(70, "超买线", color=color.orange) hline(30, "超卖线", color=color.blue)
内容的提问来源于stack exchange,提问作者Unique631
相关产品推荐
相关产品推荐

