TradingView indicator calculations

Dynamic Momentum Index: audit the changing RSI window

The Dynamic Momentum Index changes its RSI calculation window as relative price volatility changes. Higher relative volatility shortens the window; lower relative volatility lengthens it. This page implements an unsmoothed window calculation with explicit bounds, rounding, and missing-data rules. The abbreviation DMI here means Dynamic Momentum Index, not the Directional Movement Index associated with +DI, -DI, and ADX.

See the decision guide

Define the volatility mapping and the RSI variant separately

Chande and Kroll describe sigma = standard deviation of five closes, A = the ten-period simple average of sigma, V = sigma/A, and N = INT(14/V), bounded from 5 to 30. Their discussion uses unsmoothed RSI. The HPotter TradingView description instead gives a minimum of 3. These are different parameter choices; a script name alone does not identify the calculation.

  • For each selected change delta, add max(delta, 0) to U and max(-delta, 0) to D. N changes require N + 1 closes. Recompute the complete selected window on every bar.
  • At V = 1, N = 14. This output equals a 14-change unsmoothed RSI ratio, but it need not equal the built-in Wilder-smoothed RSI.
  • All lengths count chart bars. A five-minute chart does not turn the five-bar volatility window into five days.
StageDefinition used hereCheck before comparing
VolatilityPopulation standard deviation of the latest five closesClose levels, not returns or true range
Reference volatilitySMA of ten sigma values, including the current barRequires 14 consecutive valid closes
Effective lengthN = max(lower, min(upper, floor(14/V)))Round down before using the integer window
Oscillator100 × U/(U + D), using the latest N one-bar changesNo Wilder recursion or added smoothing

Check rounding and both limits before looking at signals

The following are hypothetical positive volatility ratios supplied directly to the mapping, using base length 14. They isolate the length calculation from the price calculation. The lower limit affects sharp volatility increases, while the upper limit restricts expansion in quiet windows.

  • Rounding to nearest would turn 14/4 = 3.5 into 4, whereas truncation gives 3. Small differences near integer boundaries can change which older gain or loss enters the window.
  • The 5-to-30 mapping reaches its upper cap when floor(14/V) is at least 30. With a zero numerator volatility, division needs a separate policy; do not silently cast infinity to an integer.
  • The example rejects lower > upper. Input bounds permit 1 through 100 bars, with defaults 5 and 30. These defaults define the demonstration, not a recommended trading configuration.
V14/VTruncatedBounds 5 to 30Bounds 3 to 30
0.2070703030
0.5028282828
1.0014141414
1.509.3333999
2.805555
4.003.5353

Follow effective length and output across eleven completed bars

This synthetic fixture starts with 30 closes alternating 100, 101, beginning at 100 and ending at 101. Bars 31 through 40 close at 104, 109, 106, 112, 110, 111, 110, 111, 110, 111. Use the default 5-to-30 bounds. The table retains full precision internally and rounds displayed ratios to four decimals and oscillator values to two.

  • At bar 31, sigma = sqrt(2.16) = 1.469694 and A = 0.587878, so V = 2.5 and floor(14/V) = 5. The selected changes are +1, -1, +1, -1, +3. The index is 100 × 5/7 = 71.4286.
  • At bar 38, 14/V is about 38.45, so N is capped at 30. The window expands from 13 to 30 changes. Its output can move because older observations re-enter, even without a large current price move.
  • The fixture crosses above 70 on bars 31 and 34 and below 70 on bars 33 and 36. These arithmetic events are not backtest results or evidence of profitable entries.
BarCloseVNUDDynamic index
301011.0000147750.00
311042.500055271.43
321093.7882510190.91
331062.864759469.23
341122.5839515383.33
351101.6632816672.73
361111.09861218869.23
371101.00511318966.67
381110.364130271761.36
391100.238430271761.36
401110.238430271761.36

Use a dynamic window without passing it to ta.rsi()

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.

  • On a standard candle chart, compare the Data Window values on completed bars in this order: sigma, average sigma, ratio, length, gains, losses, oscillator. An error in an earlier stage propagates to the later stages.
  • Use identical symbol, exchange feed, session, timeframe, and price source when comparing scripts. The synthetic table is an arithmetic fixture, not a claim that market candles reproduce it.
  • Set an alert to Once Per Bar Close after verifying the values. Recreate running alerts after input changes because TradingView stores a snapshot of the script and settings.
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.")

Make flat data and warm-up behavior explicit

A zero volatility ratio does not define an infinite usable window. This implementation chooses the upper length when sigma or its average is zero, then evaluates the price-change window normally. That fallback is an implementation choice. A window with no movement has an undefined gain share and returns na.

  • A missing close is rejected within the latest 14-bar volatility span and the selected N-change span. This prevents skip-na averaging from silently stitching together a different window.
  • An increasing sequence can remain at 100, and a decreasing sequence can remain at 0. Neither extreme sets a reversal deadline. A volatility shock can also shorten the window and increase threshold crossings.
  • The live close affects both volatility and momentum. Confirmation hides the unfinished bar in this example; it does not establish an edge or resolve discrepancies with other DMI definitions.
ConditionBehavior hereInterpretation
Fewer than 14 valid consecutive closesNo effective length or outputVolatility reference is not ready
N chosen, fewer than N + 1 valid closesLength visible; output naWindow is incomplete
sigma = 0 or A = 0Choose upper lengthExplicit fallback, not a measured ratio
U = D = 0Output na; suppress crossingA flat window does not imply a bearish score of zero
U > 0 and D = 0Output 100All selected movement is upward
U = 0 and D > 0Output 0All selected movement is downward
Missing close within either required windowOutput na until valid windows recoverDo not replace missing prices with zero

Give the coding agent a testable specification

Use the Pine Script AI Coding Agent to draft or revise this public calculation. Specify the volatility source and windows, length bounds, truncation, raw-window sums, zero policies, and confirmed crossing rule. Keep the intermediate Data Window plots until the numbers agree with your chosen reference.

  • Ask for syntax checking, then compare numerical output in TradingView before creating alerts. A successful syntax check does not prove the chosen formula matches a reference script.
  • Protected source code and private filters cannot be inferred from an indicator name. This specification implements the disclosed calculation rather than promising a proprietary clone.
Where Pineify fits

Pine Script AI Coding Agent

Draft an auditable Dynamic Momentum Index with explicit window bounds, intermediate values, and bar-close conditions.

Draft a dynamic-length indicator

Frequently asked questions

Educational information, not investment advice. The prices and ratios are synthetic calculation fixtures, not trading results. Adaptive length, oscillator extremes, and valid syntax do not establish profitability.

Sources and verification

Published by Pineify. Pineify is an independent product and is not affiliated with TradingView. Sources support the technical explanations; examples do not establish trading performance.