能否用单段Pine Script代码在TradingView主图画MA副图画RSI?
在TradingView用单段Pine Script实现主图MA+副图RSI的可行方案
完全可行,你之前的代码无法运行是因为ChatGPT给出的new_overlay_window、new()、TradingView.widget都不是Pine Script的原生语法,不符合TradingView的指标编写规范。
以下是可直接运行的完整代码,实现主图绘制移动平均线、副图绘制RSI的需求:
//@version=5 indicator("MA + RSI 组合指标", overlay=true, shorttitle="MA+RSI") // 可自定义参数 ma_period = input.int(20, title="移动平均线周期", minval=1) ma_type = input.string("SMA", title="移动平均线类型", options=["SMA", "EMA", "WMA"]) rsi_period = input.int(14, title="RSI周期", minval=1) // 计算移动平均线 ma_value = switch ma_type "SMA" => ta.sma(close, ma_period) "EMA" => ta.ema(close, ma_period) "WMA" => ta.wma(close, ma_period) // 计算RSI rsi_value = ta.rsi(close, rsi_period) // 主图绘制移动平均线 plot(ma_value, title="移动平均线", color=color.blue, linewidth=2) // 副图绘制RSI及超买超卖线 plot(rsi_value, title="RSI", color=color.red, linewidth=2, display=display.data_window) hline(70, title="超买线", color=color.red, linestyle=hline.style_dashed) hline(30, title="超卖线", color=color.green, linestyle=hline.style_dashed)
代码说明:
indicator("MA + RSI 组合指标", overlay=true):声明指标名称,并设置overlay=true让移动平均线直接绘制在主图- 自定义参数:通过
input.int和input.string提供可调整的周期和指标类型,方便按需修改 - 移动平均线计算:用
switch语句支持SMA、EMA、WMA三种常用类型 - RSI绘制:通过
display=display.data_window强制将RSI放在主图下方的独立副图,同时添加70/30的超买超卖参考线 - 所有函数均为Pine Script v5原生函数,完全兼容TradingView平台
内容的提问来源于stack exchange,提问作者anonymous_hacker
相关产品推荐
相关产品推荐

