TradingView documents that ta.rsi() requires a simple integer length. A length calculated from current volatility is a series integer; converting it with int() does not remove that qualifier. The script below sums a bounded window directly. Replacing Wilder alpha with 1/N on each bar would create another adaptive recurrence, not this window-based definition.
Pine Script v6 audit indicator with raw window sums, diagnostic values, and confirmed chart-bar alerts. It does not place trades.pine
//@version=6
indicator("Dynamic Momentum Index window audit", overlay = false, precision = 4, max_bars_back = 101)
lower = input.int(5, "Minimum length", minval = 1, maxval = 100)
upper = input.int(30, "Maximum length", minval = 1, maxval = 100)
if lower > upper
runtime.error("Minimum length must not exceed maximum length.")
sigma = ta.stdev(close, 5, true)
averageSigma = ta.sma(sigma, 10)
missingCloses = math.sum(na(close) ? 1.0 : 0.0, 14)
volatilityReady = bar_index >= 13 and missingCloses == 0 and not na(averageSigma)
ratio = volatilityReady and averageSigma > 0 ? sigma / averageSigma : na
int effectiveLength = na
if volatilityReady
effectiveLength := averageSigma == 0 or sigma == 0 ? upper : int(math.max(lower, math.min(upper, math.floor(14.0 / ratio))))
float gains = 0.0
float losses = 0.0
bool windowReady = not na(effectiveLength)
if windowReady
for offset = 0 to effectiveLength - 1
change = close[offset] - close[offset + 1]
if na(change)
windowReady := false
else
gains += math.max(change, 0.0)
losses += math.max(-change, 0.0)
total = gains + losses
value = windowReady and total > 0 ? 100.0 * gains / total : na
confirmed = barstate.isconfirmed
crossUp = ta.crossover(value, 70.0)
crossDown = ta.crossunder(value, 70.0)
validPair = not na(value) and not na(value[1])
plot(confirmed ? value : na, "Dynamic Momentum Index", color = color.blue, linewidth = 2, style = plot.style_linebr)
hline(70, "Upper reference")
hline(30, "Lower reference")
plot(confirmed ? effectiveLength : na, "Effective length", display = display.data_window)
plot(confirmed ? ratio : na, "Volatility ratio", display = display.data_window)
plot(confirmed ? sigma : na, "Five-bar standard deviation", display = display.data_window)
plot(confirmed ? averageSigma : na, "Average standard deviation", display = display.data_window)
plot(confirmed and windowReady ? gains : na, "Window gains", display = display.data_window)
plot(confirmed and windowReady ? losses : na, "Window losses", display = display.data_window)
alertcondition(confirmed and validPair and crossUp, "Cross above 70", "Confirmed Dynamic Momentum Index crossed above 70.")
alertcondition(confirmed and validPair and crossDown, "Cross below 70", "Confirmed Dynamic Momentum Index crossed below 70.")