请求将SuperTrend ATR + RSI指标从V1/V2转换为V5版本
SuperTrend ATR + RSI 脚本从 Pine Script v1 转换到 v5 的版本
转换后的 v5 代码
//@version=5 indicator(title="SuperTrend ATR + RSI", shorttitle="SuperTrend ATR + RSI", overlay=true) // 输入参数 factor = input.int(title="Super Trend 系数", defval=3, minval=1, maxval=100) atrLength = input.int(title="ATR 周期", defval=7, minval=1, maxval=100) rsiLength = input.int(title="RSI 周期", defval=7, minval=1, maxval=100) // Super Trend ATR 计算 up = hl2 - (factor * ta.atr(atrLength)) dn = hl2 + (factor * ta.atr(atrLength)) // 初始化并更新上轨和下轨 var float tUp = na tUp := close[1] > ta.nz(tUp[1], up) ? math.max(up, ta.nz(tUp[1], up)) : up var float tDown = na tDown := close[1] < ta.nz(tDown[1], dn) ? math.min(dn, ta.nz(tDown[1], dn)) : dn // 确定趋势方向 var int trend = 1 trend := close > ta.nz(tDown[1], dn) ? 1 : close < ta.nz(tUp[1], up) ? -1 : trend[1] // 获取止损线 tsl = trend == 1 ? tUp : tDown lineColor = trend == 1 ? color.green : color.red // RSI 通道计算 src = close ep = 2 * rsiLength - 1 auc = ta.ema(math.max(src - src[1], 0), ep) adc = ta.ema(math.max(src[1] - src, 0), ep) x1 = (rsiLength - 1) * (adc * 70 / (100 - 70) - auc) ub = x1 >= 0 ? src + x1 : src + x1 * (100 - 70) / 70 x2 = (rsiLength - 1) * (adc * 30 / (100 - 30) - auc) lb = x2 >= 0 ? src + x2 : src + x2 * (100 - 30) / 30 // 绘制指标 plot(ta.avg(ub, lb), color=color.purple, style=plot.style_line, linewidth=1, title="RSI 中线") plot(tsl, color=lineColor, style=plot.style_line, linewidth=1, title="SuperTrend ATR")
核心转换细节
- 脚本声明: 替换 v1 的
study为 v5 的indicator,保留overlay=true实现主图叠加。 - 参数定义: 用
input.int替代旧版input,明确参数类型,调整名称避免歧义(如ATR→atrLength)。 - 状态变量: 通过
var关键字声明tUp、tDown、trend,确保变量跨 K 线保留状态,这是 v5 处理持续变量的标准方式。 - 函数调用: v5 要求技术分析函数添加命名空间前缀,如
ta.atr、ta.ema;数学函数使用math.max、math.min。 - 语法修正: 移除旧代码中多余的逗号,将
iff替换为更简洁的三元运算符? :。 - 颜色适配: 使用 v5 原生颜色常量
color.green、color.red,保持原脚本视觉效果。
内容的提问来源于stack exchange,提问作者RayPush
相关产品推荐
相关产品推荐

