本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=5
indicator('Moving Average Percent Difference (0-100)', 'MAPD', timeframe='')
pd = input(50, 'Lookback') // Moving average period for raw calculation
norm_length = 100 // Fixed normalization period
ma_period = input(5, 'Smoothing line') // Period for the smoothed moving average
threshold_trending = input(60, 'Threshold Trending') // Threshold for trending market
threshold_ranging = input(20, 'Threshold Ranging') // Threshold for ranging market
// Drop-down menu for Source Type
source_type = input.string("SMA", "Source Type", options=["SMA", "EMA", "WMA", "VWMA", "HullMA"])
// Calculate the selected moving average
avg =
source_type == "SMA" ? ta.sma(close, pd) :
source_type == "EMA" ? ta.ema(close, pd) :
source_type == "WMA" ? ta.wma(close, pd) :
source_type == "VWMA" ? ta.vwma(close, pd) :
ta.wma(2 * ta.wma(close, pd / 2) - ta.wma(close, pd), math.round(math.sqrt(pd))) // HullMA
// Raw percentage difference
yuzde = (close - avg) * 100 / avg
// Transform values: Make positive values negative, then take absolute value
yuzde_transformed = yuzde > 0 ? -yuzde : yuzde
yuzde_abs = math.abs(yuzde_transformed)
// Min-Max Normalization to 0-100 range
yuzde_min = ta.lowest(yuzde_abs, norm_length)
yuzde_max = ta.highest(yuzde_abs, norm_length)
yuzde_norm = ((yuzde_abs - yuzde_min) / (yuzde_max - yuzde_min)) * 100
// Apply EMA smoothing to the original oscillator line
yuzde_smooth = ta.ema(yuzde_norm, pd)
// Calculate the EMA of the oscillator
ma_of_oscillator = ta.ema(yuzde_norm, ma_period)
// Plot the original oscillator line (no smoothing applied)
plot(yuzde_norm, color=color.blue, linewidth=2, title="Original Oscillator Line")
// Plot the EMA of the oscillator line (smoothing applied)
plot(ma_of_oscillator, color=color.orange, linewidth=2, title="EMA of Oscillator")
// Visualize the Threshold Trending value as a thin, dotted, white horizontal line
hline(threshold_trending, "Threshold Trending", color=color.rgb(76, 73, 73, 51), linestyle=hline.style_dotted, linewidth=1)
// Visualize the Threshold Ranging value as a thin, dotted, gray horizontal line
hline(threshold_ranging, "Threshold Ranging", color=color.rgb(45, 45, 48, 58), linestyle=hline.style_dotted, linewidth=1)
// Set background color to purple when the EMA of the oscillator is above the Threshold Trending
bgcolor(ma_of_oscillator > threshold_trending ? color.new(color.purple, 90) : na)
// Set background color to light blue when the EMA of the oscillator is below the Threshold Ranging
bgcolor(ma_of_oscillator < threshold_ranging ? color.new(color.blue, 90) : na)