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

如何将两个不同周期的RSI指标合并为单个Pine Script脚本

双周期RSI指标修复方案

你的代码出现两条RSI线重复的核心原因是第二个RSI的计算完全复用了第一个RSI的变量,同时存在大量冗余代码。以下是修复后的完整脚本,以及关键修改说明:

修复后的完整代码

indicator(title="Dual Period RSI", shorttitle="Dual RSI", format=format.price, precision=2, timeframe="", timeframe_gaps=true)

// 复用MA计算函数,无需重复定义
ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "Bollinger Bands" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)

// 第一个RSI(周期25)的设置
rsiLengthInput = input.int(25, minval=1, title="RSI Length (1st)", group="1st RSI Settings")
rsiSourceInput = input.source(close, "Source (1st)", group="1st RSI Settings")
maTypeInput = input.string("SMA", title="MA Type (1st)", options=["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="1st RSI Settings")
maLengthInput = input.int(14, title="MA Length (1st)", group="1st RSI Settings")
bbMultInput = input.float(2.0, minval=0.001, maxval=50, title="BB StdDev (1st)", group="1st RSI Settings")

// 计算第一个RSI
up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsiMA = ma(rsi, maLengthInput, maTypeInput)
isBB = maTypeInput == "Bollinger Bands"

// 绘制第一个RSI及相关指标
plot(rsi, "RSI 25", color=#00d812)
bbUpperBand = plot(isBB ? rsiMA + ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = "Upper BB (1st)", color=color.green)
bbLowerBand = plot(isBB ? rsiMA - ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = "Lower BB (1st)", color=color.green)
fill(bbUpperBand, bbLowerBand, color= isBB ? color.new(color.green, 90) : na, title="BB Fill (1st)")

// 第二个RSI(周期100)的设置
rsiLengthInput1 = input.int(100, minval=1, title="RSI Length (2nd)", group="2nd RSI Settings")
rsiSourceInput1 = input.source(close, "Source (2nd)", group="2nd RSI Settings")
maTypeInput1 = input.string("SMA", title="MA Type (2nd)", options=["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="2nd RSI Settings")
maLengthInput1 = input.int(14, title="MA Length (2nd)", group="2nd RSI Settings")
bbMultInput1 = input.float(2.0, minval=0.001, maxval=50, title="BB StdDev (2nd)", group="2nd RSI Settings")

// 计算第二个RSI(关键:全部使用自身变量)
up1 = ta.rma(math.max(ta.change(rsiSourceInput1), 0), rsiLengthInput1)
down1 = ta.rma(-math.min(ta.change(rsiSourceInput1), 0), rsiLengthInput1)
rsi1 = down1 == 0 ? 100 : up1 == 0 ? 0 : 100 - (100 / (1 + up1 / down1))
rsiMA1 = ma(rsi1, maLengthInput1, maTypeInput1)
isBB1 = maTypeInput1 == "Bollinger Bands"

// 绘制第二个RSI及相关指标
plot(rsi1, "RSI 100", color=#010a02)
bbUpperBand1 = plot(isBB1 ? rsiMA1 + ta.stdev(rsi1, maLengthInput1) * bbMultInput1 : na, title = "Upper BB (2nd)", color=color.red)
bbLowerBand1 = plot(isBB1 ? rsiMA1 - ta.stdev(rsi1, maLengthInput1) * bbMultInput1 : na, title = "Lower BB (2nd)", color=color.red)
fill(bbUpperBand1, bbLowerBand1, color= isBB1 ? color.new(color.red, 90) : na, title="BB Fill (2nd)")

// 只绘制一套RSI通用水平线(避免重复)
rsiUpperBand = hline(70, "RSI Upper Band", color=#787B86)
hline(50, "RSI Middle Band", color=color.new(#787B86, 50))
rsiLowerBand = hline(30, "RSI Lower Band", color=#787B86)
fill(rsiUpperBand, rsiLowerBand, color=color.rgb(126, 87, 194, 90), title="RSI Background Fill")

关键修改点

  • 修正RSI计算逻辑:第二个RSI的up1/down1/rsi1全部使用自身的输入变量(rsiLengthInput1、rsiSourceInput1),不再复用第一个RSI的变量
  • 移除冗余函数:删除重复定义的ma1函数,直接复用原ma函数,减少代码冗余
  • 区分输入分组:将两个RSI的设置分为独立的分组(1st RSI Settings/2nd RSI Settings),避免界面参数混乱
  • 移除重复绘制:只保留一套RSI水平线(70/50/30)的绘制,避免重复元素占用资源
  • 独立布林带设置:给第二个RSI的布林带设置独立参数和颜色,方便区分两条指标线

内容的提问来源于stack exchange,提问作者rony sen

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 13:57:56