You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否为SMA添加百分比垂直偏移并绑定至1周期ATR?

为SMA添加基于1周期ATR的百分比垂直偏移指标(Pine Script)

完全可以实现这个需求,以下是针对TradingView平台的Pine Script代码,直接复制粘贴就能用,参数可灵活调整:

//@version=5
indicator("SMA with ATR Percentage Offset", overlay=true)

// 可自定义的输入参数
smaLength = input.int(20, title="SMA周期")
atrPercent = input.float(50.0, title="ATR百分比偏移", suffix="%")

// 计算核心指标值
smaValue = ta.sma(close, smaLength)
onePeriodATR = ta.atr(1)

// 计算基于ATR百分比的偏移量
offsetAmount = onePeriodATR * (atrPercent / 100)

// 生成上下偏移后的SMA线
smaUpperBand = smaValue + offsetAmount
smaLowerBand = smaValue - offsetAmount

// 绘制到图表上
plot(smaValue, color=color.new(color.blue, 0), title="原始SMA", linewidth=2)
plot(smaUpperBand, color=color.new(color.green, 0), title="上偏移SMA")
plot(smaLowerBand, color=color.new(color.red, 0), title="下偏移SMA")

代码说明:

  • //@version=5:指定使用Pine Script第5版,兼容最新平台功能
  • indicator(..., overlay=true):设置指标叠加在K线图上,无需单独开面板
  • 输入参数部分:smaLength可修改SMA的周期,atrPercent直接输入你想要的偏移百分比(比如输入50就是偏移50%的1周期ATR)
  • ta.atr(1):精准计算1周期的平均真实波幅
  • 偏移量计算:把百分比转为小数后乘以ATR,得到实际的价格偏移值
  • 最后通过plot函数画出三条线,颜色和粗细可根据喜好调整

使用方法:打开TradingView -> 新建指标 -> 清空默认代码 -> 粘贴上述代码 -> 点击保存即可,在指标设置面板能随时调整参数。

内容的提问来源于stack exchange,提问作者Davidator

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 02:38:11