能否修改RSI指标的绘图范围至-50至100?
如何将RSI显示范围从0-100拉伸至-50-100(不改变指标逻辑)
已知RSI指标默认绘图范围为0至100,希望将其显示范围拉伸至-50至100,不改变指标本身的波动逻辑,仅调整显示尺度以便更便捷地在RSI线上绘制趋势线。当前使用的Pine Script代码如下:
//@version=5 indicator(title="Relative Strength Index", shorttitle="RSI", format=format.price, precision=2, timeframe="", timeframe_gaps=true) 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) rsiLengthInput = input.int(14, minval=1, title="RSI Length", group="RSI Settings") rsiSourceInput = input.source(close, "Source", group="RSI Settings") maTypeInput = input.string("SMA", title="MA Type", options=["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA Settings") maLengthInput = input.int(50, title="MA Length", group="MA Settings") 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) rsicolor = rsi > 70 ? color.new(#ff0057, 0) : rsi < 30 ? color.new(#ff0057, 0) : #1056ee plot(rsi, "RSI", color=rsicolor, linewidth=2, style=plot.style_stepline) plot(rsiMA, "RSI-based MA", color=color.rgb(120, 206, 255)) OverBought1 = input.int(80, minval=1) OverBought2 = input.int(90, minval=1) OverSold1 = input.int(20, minval=1) OverSold2 = input.int(10, minval=1) OB1 = hline(OverBought1 , color=color.red, linestyle=hline.style_dashed) OB2 = hline(OverBought2 , color=color.red, linestyle=hline.style_dashed) OS1 = hline(OverSold1 , color=color.green, linestyle=hline.style_dashed) OS2 = hline(OverSold2 , color=color.green, linestyle=hline.style_dashed) fill(OB1 , OB2, color=color.rgb(255, 82, 82, 80), title="Red Zoon Fill") fill(OS1 , OS2 , color=color.rgb(76, 175, 79, 80), title="Green Zoon Fill")
可以通过线性区间映射实现需求,核心是保持RSI的波动比例不变,仅将原0-100的数值范围转换为-50-100。具体修改如下:
关键修改点
- RSI值转换:用公式
stretched_rsi = rsi * 1.5 - 50完成区间映射:- 原RSI=0 → 转换后=-50
- 原RSI=100 → 转换后=100
- 中间数值按比例缩放,完全保留原波动逻辑
- 同步调整配套元素:将RSI均线、超买超卖水平线、填充区域全部按相同比例转换,确保视觉对齐。
修改后的完整代码
//@version=5 indicator(title="Stretched RSI (-50 to 100)", shorttitle="Stretched RSI", format=format.price, precision=2, timeframe="", timeframe_gaps=true) 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) rsiLengthInput = input.int(14, minval=1, title="RSI Length", group="RSI Settings") rsiSourceInput = input.source(close, "Source", group="RSI Settings") maTypeInput = input.string("SMA", title="MA Type", options=["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA Settings") maLengthInput = input.int(50, title="MA Length", group="MA 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)) // 核心:将RSI从0-100映射到-50-100 stretched_rsi = rsi * 1.5 - 50 // 同步转换RSI均线 stretched_rsiMA = ma(stretched_rsi, maLengthInput, maTypeInput) // 保留原颜色逻辑(基于原始RSI值判断) rsicolor = rsi > 70 ? color.new(#ff0057, 0) : rsi < 30 ? color.new(#ff0057, 0) : #1056ee // 绘制转换后的RSI和均线 plot(stretched_rsi, "Stretched RSI", color=rsicolor, linewidth=2, style=plot.style_stepline) plot(stretched_rsiMA, "Stretched RSI-based MA", color=color.rgb(120, 206, 255)) // 转换超买超卖水平线数值 OverBought1 = input.int(80, minval=1) * 1.5 - 50 OverBought2 = input.int(90, minval=1) * 1.5 - 50 OverSold1 = input.int(20, minval=1) * 1.5 - 50 OverSold2 = input.int(10, minval=1) * 1.5 - 50 // 绘制转换后的水平线 OB1 = hline(OverBought1 , color=color.red, linestyle=hline.style_dashed) OB2 = hline(OverBought2 , color=color.red, linestyle=hline.style_dashed) OS1 = hline(OverSold1 , color=color.green, linestyle=hline.style_dashed) OS2 = hline(OverSold2 , color=color.green, linestyle=hline.style_dashed) // 填充转换后的区域 fill(OB1 , OB2, color=color.rgb(255, 82, 82, 80), title="Red Zone Fill") fill(OS1 , OS2 , color=color.rgb(76, 175, 79, 80), title="Green Zone Fill")
说明
- 原始RSI的波动比例完全保留,只是显示尺度被拉伸,不会影响指标的判断逻辑
- 颜色判断依然基于原始RSI值(70/30阈值),确保交易信号逻辑不变
- 所有配套元素(均线、水平线、填充)都同步转换,视觉上完全适配新的显示范围
内容的提问来源于stack exchange,提问作者Colin0686
相关产品推荐
相关产品推荐

