本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=5
//By Alifer
//Upgrade of the Average True Range default indicator by TradingView. Adds and plots a trailing mean to show moments of increased volatility more clearly.
//The sensitivity of the ATR Trailing Mean can be adjusted with the Multiplier value. (1.0 = 100% sensitivity)
indicator(title="Average True Range Trailing Mean [Alifer]", shorttitle="ATR+TM [Alifer]", overlay=false, timeframe="", timeframe_gaps=true)
// Inputs
length = input.int(title="Length", defval=20, minval=1)
atr_mult = input.float(defval=1.0, step = 0.1, title = "Multiplier", tooltip = "Adjust the sensitivity of the ATR Trailing Mean line.")
smoothing = input.string(title="Smoothing", defval="RMA", options=["RMA", "SMA", "EMA", "WMA"])
ma_function(source, length) =>
switch smoothing
"RMA" => ta.rma(source, length)
"SMA" => ta.sma(source, length)
"EMA" => ta.ema(source, length)
=> ta.wma(source, length)
// Calculate ATR and ATR Trailing Mean
atr= ma_function(ta.tr(true), length)
atr_mean = ta.cum(atr) / (bar_index+1) * atr_mult
// Color ATR above/below ATR Trailing Mean
color atr_color = atr > atr_mean ? color.new(#3e6bff, 0) : color.new(#ff9b3e, 0)
color atr_bg_color = atr > atr_mean ? color.new(#3e6bff, 80) : color.new(#ff9b3e, 80)
// Plot ATR and ATR Trailing Mean
plot_atr = plot(atr, title = "ATR", color=atr_color)
plot_atr_mean = plot(atr_mean, "ATR Trailing Mean", color=color.new(#ffffff, 30))
// Background fill between ATR and ATR Trailing Mean
fill(plot1 = plot_atr, plot2 = plot_atr_mean, color = atr_bg_color)