PineScript问题:RSI绘图过大,如何移至底部并缩小?
解决PineScript中RSI与价格叠加显示异常的问题
修改后的完整代码
//@version=5 strategy("Simple MA Strategy", overlay=true, initial_capital=10000) // Date range startDate = timestamp(2000, 01, 01, 0, 0, 0) // Define inputs ema1Value = input(title="EMA 1", defval=50) ema1Source = input(title="EMA 1 Source", defval=close) ema2Value = input(title="EMA 2",defval=200) ema2Source = input(title="EMA 2 Source", defval=close) rsiSource = input(title="RSI Source", defval=close) rsiLength = input(title="RSI Length",defval=14) rsiOverboughtLevel = input(title="RSI Overbought Level", defval=70) rsiOversoldLevel = input(title="RSI Oversold Level", defval=30) // Indicators ema1 = ta.ema(ema1Source, ema1Value) ema2= ta.ema(ema2Source, ema2Value) // 移除重复RSI计算,保留参数化的rsiValue rsiValue = ta.rsi(rsiSource, rsiLength) // Rules ema_crossover = ta.crossover(ema1, ema2) ema_crossunder = ta.crossunder(ema1, ema2) isRsiOverbought = rsiValue >= rsiOverboughtLevel isRsiOversold = rsiValue <= rsiOversoldLevel // Plot indicators & levels plot(ema1, color=color.green,linewidth = 1) plot(ema2, color=color.fuchsia,linewidth = 1) // 关键改动:添加subplot=true将RSI移至独立子图 plot(rsiValue, "RSI", color=color.rgb(0, 18, 212), subplot=true) // 修正超买超卖线的数值与标签对应错误 rsiOverboughtLine = hline(rsiOverboughtLevel, "OverBought", color=#C0C0C0) rsiOversoldLine = hline(rsiOversoldLevel, "Oversold", color=#C0C0C0) fill(rsiOverboughtLine, rsiOversoldLine, color=color.rgb(147, 163, 208, 86), title="Background")
核心改动说明
- 将RSI移至底部子图:在
plot(rsiValue)中添加subplot=true参数。由于你设置了strategy(..., overlay=true),默认所有绘图会叠加在价格主图上,但RSI数值范围为0-100,和价格(通常远大于100)不在同一量级,导致显示异常。subplot=true会将RSI单独放在主图下方的独立子图中,自动适配其数值范围。 - 移除冗余计算:原代码重复计算了两次RSI,保留参数化的
rsiValue即可,避免不必要的性能消耗。 - 修正超买超卖线错误:原代码中超买线和超卖线的数值与标签写反,现在调整为数值与标签一一对应,逻辑更准确。
内容的提问来源于stack exchange,提问作者checkmate711
相关产品推荐
相关产品推荐

