如何修复TradingView Pine Script多时间框架指标切换无效问题
解决Pine Script v5多时间框架RSIx2指标不更新问题
问题核心是你仅定义了多时间框架参数,但未基于该参数获取对应周期的价格数据,所有计算仍使用当前图表周期的数据。以下是修改后的完整代码:
//@version=5 indicator(title="Relative Strength Index x2", shorttitle="MTF RSIx2") // 输入参数 len_fast = input(14, title="Fast RSI Length") len_slow = input(28, title="Slow RSI Length") timeframe = input.timeframe(title="Time Frame", defval="1") // 获取指定时间框架的收盘价数据 src = request.security(syminfo.tickerid, timeframe, close) // 计算快速RSI up_fast = ta.rma(math.max(ta.change(src), 0), len_fast) down_fast = ta.rma(-math.min(ta.change(src), 0), len_fast) rsi_fast = down_fast == 0 ? 100 : up_fast == 0 ? 0 : 100 - (100 / (1 + up_fast / down_fast)) // 计算慢速RSI up_slow = ta.rma(math.max(ta.change(src), 0), len_slow) down_slow = ta.rma(-math.min(ta.change(src), 0), len_slow) rsi_slow = down_slow == 0 ? 100 : up_slow == 0 ? 0 : 100 - (100 / (1 + up_slow / down_slow)) // 计算差值并设置颜色 diff = rsi_fast - rsi_slow + 50 diff_color = diff > diff[1] ? color.new(color.green, 50) : color.new(color.red, 50) // 绘制指标 diff_plot = plot(diff, title="Difference", color=diff_color, style=plot.style_columns, histbase=50) rsi_slow_plot = plot(rsi_slow, title="RSI Slow", color=color.white) rsi_fast_plot = plot(rsi_fast, title="RSI Fast", color=color.blue) // 绘制水平参考线 level1 = 70 level2 = 60 level3 = 40 level4 = 30 hline(level1, "Level 1", color.new(color.red, 50), linewidth=1) hline(level2, "Level 2", color.new(color.white, 50), linewidth=1) hline(level3, "Level 3", color.new(color.white, 50), linewidth=1) hline(level4, "Level 4", color.new(color.lime, 50), linewidth=1)
关键修改点:
- 使用
request.security()函数从指定时间框架获取收盘价,替代原有的直接使用当前周期close request.security(syminfo.tickerid, timeframe, close)的三个参数分别代表:当前交易品种ID、目标时间框架、需要获取的数据(这里是收盘价)
修改后,当你在1分钟图表设置时间框架为5分钟时,指标会基于5分钟周期的收盘价计算RSI并显示对应数据。
内容的提问来源于stack exchange,提问作者7X Investors
相关产品推荐
相关产品推荐

