Pine Script functions

Pine Script rma2 vs ta.rma

rma2() is available in TradingView's published ta library, introduced in library version 7. It is separate from the built-in ta.rma() function. Import the library with a distinct alias, then call that alias's rma2(source, length). The library accepts a series float length; built-in ta.rma requires a simple int length. Use ta.rma when the length stays fixed during a script run.

Review a Pine Script error

Import the library before calling rma2

The complete Pine v6 example below pins TradingView/ta/9 and names it tvta. Library version 9 migrated to Pine v6; the number in the import path identifies the library release, not the Pine language version. If existing code calls ta.rma2, inspect its import declaration before changing the function name. A script can use ta as a library alias, which makes an imported call look like a built-in.

  • If no import exists, ta.rma2 is not a built-in call. Add the intended library import or use ta.rma with a compatible length.
  • If an import exists, match the call prefix to its alias and use a release that exports rma2.
  • The 10/28 switch demonstrates a series argument. It is not a trading rule or a claim of better signals.
Pine v6: a library RMA with a changing lengthpinescript
//@version=6
import TradingView/ta/9 as tvta
indicator("RMA with a series length", overlay = true)

int fixedLength = input.int(14, "Fixed length", minval = 1)
float changingLength = close >= open ? 10.0 : 28.0

float fixedRma = ta.rma(close, fixedLength)
float changingRma = tvta.rma2(close, changingLength)

plot(fixedRma, "Built-in RMA", color.blue)
plot(changingRma, "Library RMA with changing length", color.orange)

Why ta.rma rejects a series length

A simple value is established by the first bar and does not change across bars. It does not have to be known at compile time. Pine accepts const, input, and simple integers where ta.rma requires simple int, but rejects series int. Converting a changing float with int() changes the numeric type; it does not remove the series qualifier.

PropertyBuilt-in ta.rmaImported tvta.rma2
Lengthsimple int or a weaker qualifierseries float; integers are also accepted
ImportNoneAn explicit version of TradingView/ta
Suitable inputinput.int(14) with a positive minimumA positive length calculated per bar
Changing lengthNot acceptedChanges the smoothing parameter as the script runs
Replacement checkBaseline for fixed-length comparisonsCompare output and initialization for the pinned version

Compare the same source and fixed length first

Run this diagnostic with one symbol, timeframe, and dataset. It plots the signed difference only when both outputs exist, and marks bars where only one output exists. Check the earliest bars before comparing later values. This page supplies the diagnostic procedure; it does not report chart measurements or claim numerical equality for every source and library version.

  • Start with missing-source injection disabled. Record the first bar where each output becomes available and the difference after both initialize.
  • Enable missing-source injection to inspect gaps explicitly. An unbroken close series does not test missing-data handling.
  • Only after the fixed-length comparison, introduce a series length. There is no matching dynamic-length ta.rma call to use as a baseline.
Pine v6: inspect warm-up and numerical differencespinescript
//@version=6
import TradingView/ta/9 as tvta
indicator("RMA comparison diagnostic", overlay = false)

int length = input.int(14, "Length", minval = 1)
bool injectGaps = input.bool(false, "Insert a missing source every 17 bars")
float source = injectGaps and bar_index % 17 == 0 ? na : close

float builtIn = ta.rma(source, length)
float imported = tvta.rma2(source, length)
bool bothAvailable = not na(builtIn) and not na(imported)
bool availabilityDiffers = na(builtIn) != na(imported)

plot(bothAvailable ? imported - builtIn : na, "Library minus built-in", color.orange)
plotchar(availabilityDiffers, "Different availability", "!", location.top)
plot(builtIn, "Built-in value", display = display.data_window)
plot(imported, "Library value", display = display.data_window)
hline(0, "Zero difference")

The recurrence does not specify the seed

RMA uses y[t] = x[t] / N + (1 - 1 / N) × y[t-1]. TradingView's reference example seeds ta.rma with an SMA. A custom filter seeded from its first valid source can differ even with the same smoothing factor. For a fixed N and matching later inputs, the seed difference is multiplied by (1 - 1 / N) on each subsequent update. There is no universal two- or three-period point at which the values become identical.

  • An EMA of length 2N - 1 has the same smoothing coefficient as an RMA of length N. Different seeds can still produce different values.
  • A changing N changes the coefficient applied to the current input and previous state; it does not recalculate a moving window from scratch.
  • The local example below deliberately seeds from the first valid source, holds its state on missing sources, substitutes 14 for a missing length, and clamps length to at least 1. These are explicit example policies, not a claim to reproduce the library.
Pine v6: an optional custom filter with explicit seed and gap rulespinescript
//@version=6
indicator("First-source-seeded smoothing", overlay = true)

firstSeedRma(float source, float length) =>
    float safeLength = math.max(1.0, nz(length, 14.0))
    float alpha = 1.0 / safeLength
    var float value = na
    if not na(source)
        value := na(value) ? source : alpha * source + (1.0 - alpha) * value
    value

float length = close >= open ? 10.0 : 28.0
float smoothed = firstSeedRma(close, length)
plot(smoothed, "Custom smoothing", color.teal)

Diagnose the error before replacing the calculation

Keep recursive calls on every bar so their history follows the chart. For dynamic lengths, define what zero, negative, or missing values mean before calculating a reciprocal. Replacing missing prices with zero changes the input data and can introduce an artificial drop in the average.

SymptomCheckAction
Function not foundImport owner, library version, and aliasUse a release exporting rma2 and call it through the declared alias.
series int is not acceptedWhether length depends on bar dataKeep a fixed input for ta.rma or use the library for a changing length.
Different early valuesSeed, first available bar, and chart historyUse the fixed-length diagnostic before assuming either result is wrong.
Missing or discontinuous outputSource gaps and invalid lengthsDefine a missing-data policy and inspect it with deliberate gaps.
Where Pineify fits

Pine Script AI Coding Agent

Bring your script and compiler message to Pineify's coding agent to review the import, function call, and length qualifier. Validate the revised calculation in TradingView against the behavior you intended.

Review a Pine Script error

Frequently asked questions

Programming examples are for education. Syntax validation does not establish numerical equivalence, strategy performance, or suitability for live trading.

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.