RMA in Pine Script: Smoothing, Seeds and Slope
Pine Script’s ta.rma applies Wilder-style recursive smoothing with a coefficient of 1/length. The example below plots it on close with editable length and source. The recurrence explains its memory and its relationship to EMA; it does not establish a universal ranking of trading signals.
Inspect the Pine ScriptInitialization and the recursive update
For length N, RMA first uses an N-observation simple average to initialize its state, then updates with coefficient alpha = 1/N. With complete input data, length 9 therefore needs nine observations for its initial value. A zero-seeded recursion would be a different calculation.
alpha = 1 / N
R(t) = alpha × source(t) + (1 − alpha) × R(t−1)
R(t) − R(t−1) = [source(t) − R(t−1)] / N
The Pine reference for ta.rma documents the function. TradingView’s RSI explanation also uses RMA on gains and losses. That is different from taking an RMA of price: the input series and the later ratio determine the RSI output.
Length controls the coefficient and initialization window; it is not a hard cutoff for all later history. Once seeded, the state retains geometrically decaying influence from earlier observations. Missing input data should be checked explicitly when comparing implementations, rather than silently replacing absent prices with zero.
Match the EMA coefficient without confusing the seed
EMA uses alpha = 2/(M + 1). Setting M = 2N − 1 matches RMA’s coefficient. Thus RMA9 matches EMA17’s recurrence coefficient, RMA14 matches EMA27, and RMA21 matches EMA41—not EMA21.
Identical coefficients give identical subsequent values only when the starting state and inputs also match. Pine’s ordinary EMA starts from a first-value seed, while RMA waits for its simple-average seed. With complete common inputs after initialization, a difference between those states shrinks by (1 − 1/N) on each update; it does not vanish merely because the coefficients agree.
At length 9, the current-source weights are 1/9 for RMA and 1/5 for EMA9. Their ratio is 1.8, but that is a coefficient comparison, not a measured “1.8 times smoother” score, fixed entry delay or false-signal reduction. A trading result also depends on the exact rule and data.
A five-observation initialization check
Take synthetic sources 100, 103, 106, 109, 112 and set N = 3. RMA is unavailable for the first two observations; its third value is their three-observation mean, 103. Its next values are 105 and 107.333333.
| Source | RMA3 | EMA5, first-value seed | Difference |
|---|---|---|---|
| 100 | na | 100 | na |
| 103 | na | 101 | na |
| 106 | 103 | 102.666667 | 0.333333 |
| 109 | 105 | 104.777778 | 0.222222 |
| 112 | 107.333333 | 107.185185 | 0.148148 |
Both recursions use 1/3 after the RMA seed, and their difference contracts by 2/3. These are arithmetic fixtures, not historical prices or performance measurements. Matching the loaded history matters when diagnosing differences near startup.
Source position and one-bar slope are linked
For an initialized RMA, complete inputs and N greater than 1, the recurrence gives a second identity:
source(t) − R(t) = (1 − alpha) × [source(t) − R(t−1)]
R(t) − R(t−1) = alpha × [source(t) − R(t−1)]
Both coefficients are positive. Therefore source above the current RMA and a positive one-bar RMA slope are equivalent sign conditions. With source = close, close cannot be above that same RMA while it is falling on that update. Adding this slope test to a same-bar close-above condition does not provide an independent filter.
The statement changes if the selected source is HL2 but the comparison uses close, if you compare a multi-bar slope, shift the plot, or use a different timeframe. At N = 1, RMA equals the valid source, so a strict source-above test never holds even though the source and line can rise.
A crossover additionally needs the previous pair relationship. “Above” is a state, not a new cross on every bar. The displayed slope angle also depends on chart scaling; use value differences and specified units rather than treating a screen angle as a stable strength measurement.
Memory, source choice and practical boundaries
Assume RMA9 is settled at 100. A single source observation of 109 moves it to 101. If subsequent sources return to 100, the excess becomes 8/9, then (8/9)², and continues decaying. After nine further valid updates, about 34.64% of that initial excess remains. A nine-period RMA on a four-hour chart therefore does not contain only the last 36 hours.
With close selected, an intrabar wick that leaves the final close unchanged does not directly enter the closed-bar calculation. Changing to HL2 introduces high and low into the source; it does not universally reduce the effect of wicks. During an open bar, the chosen source and RMA can still change.
The copy exposes length and source, draws one blue line, and has no alert conditions, trade orders, higher-timeframe requests or bar-close gate. The default of 9 is an example setting, not a verified best length. For a trading test, specify event timing, exits, costs and evaluation data separately. No strategy win rate is established here.
Pine Script v6: implementation and limits
The original minimal Pine Script v6 example below is retained. Its editable inputs default to length 9 and close, and ta.rma supplies the calculation.
//@version=6
indicator(title="Rolling Moving Average", overlay=true, max_labels_count=500)
// Inputs
length = input.int(9, "Length", minval=1)
src = input.source(close, "Source")
// RMA Calculation — Wilder's Smoothing
rma_val = ta.rma(src, length)
// Plot
plot(rma_val, "RMA", color.rgb(41, 98, 255, 0), 2)
Specify a checkable adaptation in Pineify
Ask the Pine Script AI Coding Agent for a specified length, source and confirmation policy. Include the initialization table and same-source slope identity as checks. If requesting alerts, distinguish a continuing above-line state from a first crossover; validate generated code before defining strategy execution.
Open Pine Script AI Coding AgentEducational indicator analysis. No measured trading performance is established here. Syntax validation does not prove formula equivalence, execution assumptions or profitability.