Pine Script波动率调整均线指标无法在图表显示的问题求助
问题解决:波动率调整均线无法显示
问题原因
Pine Script v5的ta.sma()函数仅支持固定常量周期参数,你代码中adjusted_SMA_period是随K线实时变化的动态值,传入后会导致ta.sma()无法生成有效序列,最终指标无输出。
解决方案
改用ta.sum()实现动态周期的简单移动平均计算(SMA本质是指定周期内收盘价的平均值),替代内置的ta.sma()函数。
修改后的完整代码
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © isaiahhoussou //@version=5 indicator("Volatility Adjusted Simple Moving Average", overlay=true) base_SMA_period = input(26, "Base Period") current_ATR_period = input(10, "Current ATR Period") average_ATR_period = input(50, "Average ATR Period") volatility_ratio = ta.atr(current_ATR_period) / ta.atr(average_ATR_period) adjusted_SMA_period = math.max(1, math.round(base_SMA_period / volatility_ratio)) // 自定义动态周期SMA计算 VASMA = ta.sum(close, adjusted_SMA_period) / adjusted_SMA_period plot(VASMA)
补充说明
ta.sum(close, n)/n完全等价于SMA的计算逻辑,且支持动态变化的n值;- 如果需要优化周期突变时的平滑效果,可以额外添加过滤逻辑(比如限制周期的单次变化幅度)。
内容的提问来源于stack exchange,提问作者coder123
相关产品推荐
相关产品推荐

