如何在Pine Script中设置指标分时段可见?VWAP与MA指定周期显示
实现Pine Script中指标按时间周期显示的需求
完全可以实现,不用拆分脚本,利用Pine Script内置的时间周期判断变量,就能让不同指标在指定周期下才显示。
核心思路
通过timeframe.isMinutes、timeframe.multiplier等内置变量判断当前图表的时间周期,给每个指标设置显示条件,只有满足条件时才绘制指标。
代码示例
//@version=5 indicator("多周期控制指标显示", overlay=true) // 计算各指标数值 vwapValue = ta.vwap(close, volume) emaValue = ta.ema(close, 50) smaValue = ta.sma(close, 200) // 设置VWAP显示条件:仅1-5分钟周期 showVWAP = timeframe.isMinutes and timeframe.multiplier >= 1 and timeframe.multiplier <= 5 // 设置MA指标显示条件:30分钟及以上周期(含30分钟、1小时、日线等) showMA = (timeframe.isMinutes and timeframe.multiplier >= 30) or timeframe.isHours or timeframe.isDays or timeframe.isWeeks or timeframe.isMonths // 按条件绘制指标 if showVWAP plot(vwapValue, color=color.new(color.blue, 0), title="VWAP", linewidth=2) if showMA plot(emaValue, color=color.new(color.red, 0), title="EMA 50", linewidth=1) plot(smaValue, color=color.new(color.green, 0), title="SMA 200", linewidth=1)
代码说明
timeframe.isMinutes:判断当前图表是否为分钟周期timeframe.multiplier:获取周期的倍数(比如5分钟周期返回5,30分钟返回30)showVWAP:只有当当前是1-5分钟的分钟周期时,该值为true,VWAP才会被绘制showMA:当当前是30分钟及以上的分钟周期,或者是小时、日线、周线等更大周期时,MA指标才会被绘制
你可以根据自己的实际需求,调整指标参数、显示条件里的周期数值,或者添加更多指标的显示控制逻辑。
内容的提问来源于stack exchange,提问作者user19858705
相关产品推荐
相关产品推荐

