You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求将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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 19:15:35