TradingView indicator calculations

Random Walk Index: compare upward and downward distance

The Random Walk Index (RWI) has two components: RWI High measures the distance from earlier lows to the current high; RWI Low measures the distance from earlier highs to the current low. Each distance is divided by an ATR-based scale multiplied by the square root of its lookback. A reading above 1 exceeds that reference scale, but does not establish a probability, a profitable trade, or a lasting trend. The two lines can both rise on a wide-range bar.

See the decision guide

Define the distance and volatility denominator

Let H[t] and L[t] be the current high and low, and k a positive lag in chart bars. One documented convention, used by Linn Software, evaluates Up(k) = (H[t] - L[t-k]) / (A(k) × sqrt(k)) and Down(k) = (H[t-k] - L[t]) / (A(k) × sqrt(k)), with A(k) taken one bar back. RWI High and RWI Low are the separate maxima over the selected candidate lags. The two maxima need not come from the same lag.

  • For the examples below, A(k) is explicitly the arithmetic mean of the k true ranges immediately before the current bar. This fixes an example convention; it does not claim every TradingView script uses the same averaging rule.
  • True range is max(high - low, abs(high - previous close), abs(low - previous close)). A gap can therefore increase the volatility scale even when the candle itself is narrow.
  • Use matching price units in numerator and denominator. Their ratio has no price unit. For a fixed distance, doubling ATR halves the candidate ratio; quadrupling k doubles sqrt(k), before accounting for changes in the endpoints or ATR.
  • The square-root reference is an indicator normalization. Its threshold is not a calibrated significance test or a confidence interval.

Recalculate three candidate lags

This synthetic fixture uses current high 106, low 104, and close 105. The previous four bars, newest first, have high/low/close values 105.5/103.5/104.5, 105/103/104, 104.5/102.5/103.5, and 104/102/103. Assume the close before those bars was 102.5. Each preceding true range is 2, so A(2), A(3), and A(4) all equal 2. Candidate lags are exactly 2, 3, and 4; round only the displayed results.

  • Taking separate maxima gives RWI High = 1.0607 and RWI Low = 0.3536. Both winning candidates happen to use lag 2 here.
  • Using only lag 4 instead gives 1.0000 and 0.0000. A strict greater-than-1 rule passes for the multi-lag high component but fails for the single-lag calculation.
  • This is a calculation check, not a historical performance sample. It demonstrates why matching the displayed period alone may not reproduce another script.
Lag kPast low / high2 × sqrt(k)Up(k)Down(k)
2103 / 1052.82843 / 2.8284 = 1.06071 / 2.8284 = 0.3536
3102.5 / 104.53.46413.5 / 3.4641 = 1.01040.5 / 3.4641 = 0.1443
4102 / 1044.00004 / 4 = 1.00000 / 4 = 0.0000

Separate a short window from a long window

A window may mean one lag, all lags up to a maximum, or a restricted lag band. These definitions answer different questions. For a controlled comparison, define short candidates as k = 2 through 7 and long candidates as k = 8 through 64, with the same per-lag ATR convention. These are illustrative bands, not recommended settings. On a five-minute chart they count five-minute bars, not days.

  • These readings are hypothetical interpretation fixtures, not outputs from the preceding OHLC example.
  • If the long calculation instead takes the maximum over k = 2 through 64, it includes every short candidate. With unchanged per-lag inputs, each long maximum must be at least its short counterpart. It is not an independent confirmation.
  • Changing one fixed ATR length for every lag also changes the comparison. Do not apply that nested-window invariant when the denominator changes with the maximum-window setting.
Closed-bar readingsInterpretation under those bandsNext check
Short High 1.4 / Low 0.6; long High 1.2 / Low 0.7Both bands have upward dominance.Check a predefined price condition and the next completed observation.
Short High 0.7 / Low 1.3; long High 1.4 / Low 0.6A recent decline conflicts with longer upward displacement.A pullback and a reversal remain competing explanations.
Both bands: High 0.8 / Low 0.9Neither component exceeds the selected reference.Do not interpret a small line crossover as strong directional evidence.
One band: High 1.6 / Low 1.4Both directional distances exceed their references.Inspect a wide range, gap, or reversal before assigning direction.

A range expansion can lift both lines without a higher close

Keep the four prior bars from the worked example, but replace the current bar with high 108, low 102, and close 104.5. The close equals the previous close. Under the same prior-bar ATR convention and lags 2 through 4, the maximum upward ratio is 5 / (2 × sqrt(2)) = 1.7678, and the maximum downward ratio is 3 / (2 × sqrt(2)) = 1.0607. Both exceed 1 even though close-to-close movement is zero.

  • The indicator uses highs and lows, so a candle can describe substantial travel in both directions. A high component above 1 alone does not distinguish a directional close from a rejected intrabar move.
  • Here the current true range is 6. An alternative two-bar simple ATR including the current bar would be (6 + 2) / 2 = 4, giving lag-2 ratios 0.8839 and 0.5303. Those are candidate values, not the full multi-lag maxima. ATR timing materially changes the result.
  • A sharp reversal can leave one component above 1 because of older endpoints while the other rises from recent movement. Repeated threshold crossings in a range can also create repeated false starts.

Turn readings into explicit confirmation rules

One testable upward-state policy is: on a completed bar, require RWI High > 1 and RWI Low < 1, then require the same state on two consecutive completed bars. Equality fails this illustrative policy; any other state resets the count. A downward policy swaps the components. This persistence rule is a research choice and can delay recognition of a move.

  • To record only the start of a confirmed state, trigger when the count first reaches two. Recording every bar with count at least two is a different alert policy.
  • In TradingView, inspect the Data Window on completed standard-price bars. For chart-timeframe scripts, bar confirmation can gate decisions; unconfirmed highs and lows can still move the plotted lines.
  • For a trade experiment, add a separate entry price rule, invalidation, position sizing, costs, and exit. Freeze the choices before checking a later sample. An RWI state alone is not a complete strategy.
ObservationHigh / LowUpward countConfirmed state
Completed A1.20 / 0.801No
Completed B1.15 / 0.752Yes
Completed C1.40 / 1.100No: components conflict
Completed D1.00 / 0.700No: equality fails

Compare TradingView implementations before creating alerts

TradingView publishes an open-source ta library whose rwi(length) function returns RWI High and RWI Low. Its documented length covers lookback and ATR smoothing. Community scripts also use the Random Walk Index name. Record the author and version, inspect the accessible calculation, and compare intermediate values before assuming it matches the lag-band example above.

  • Match symbol feed, session, chart type, timeframe, lag bounds, ATR smoothing, ATR offset, and startup history. Save the bar timestamp together with both components and their winning lags when available.
  • Require all selected lags and their true-range inputs before producing a comparison value. The example convention needs the current bar, the maximum lag of preceding bars, and one earlier close for true range. Do not replace missing inputs with zero.
  • If any required ATR denominator is zero, mark the example calculation unavailable and suppress its decision. A tiny positive ATR can produce a large ratio; inspect price precision and flat or illiquid bars.
  • Give Pineify the formulas, bounds, initialization rule, and closed-bar confirmation policy. Ask the Pine Script AI Coding Agent for intermediate plots and a separate syntax check. Compare its numbers with the intended TradingView implementation before adding alerts; source access and licensing still govern any reuse.
Where Pineify fits

Pine Script AI Coding Agent

Draft an RWI indicator from an explicit calculation convention, inspect both components, and request syntax checking before comparing completed-bar values in TradingView.

Draft an RWI calculation

Frequently asked questions

Educational information, not investment advice. Numerical examples are synthetic calculation and rule fixtures, not trading results. RWI readings, confirmed bars, and accepted code syntax do not guarantee future performance.

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.