Moving Averages

[[moving_average]] smooths a series in order to reduce its shortest-term variations. It is used to visualize the recent trend, compare the current price level with its average, or build signals from crossovers and deviations.

Not all moving averages react in the same way. Depending on the selected type, the curve follows recent price changes more or less quickly. Length also has a direct effect on the result: a shorter moving average stays closer to the original series, while a longer one produces a more stable curve.

The [[moving_average]] block outputs a numeric series that can be used directly in a condition or serve as the basis for other calculations. It also lets you compare multiple types, lengths, symbols, or timeframes in a grid search.

This block supports multiple variants:

CodeNameDescription
smaSimple Moving AverageSimple average where each value in the period has the same weight.
emaExponential Moving AverageExponential average that gives more weight to recent values and reacts faster than an SMA.
wmaWeighted Moving AverageWeighted average that emphasizes recent values in order to follow series changes more quickly.
demaDouble EMAAverage based on a double EMA, used to reduce the lag of a standard EMA.
temaTriple EMAAverage based on a triple EMA, even more reactive than a DEMA.
trimaTriangular MATriangular average that smooths the series more strongly by giving more importance to the center of the period.
smmaSmoothed MASmoothed average that evolves more slowly and strongly attenuates short-term variations.
zlemaZero‑Lag EMAExponential average adjusted to limit smoothing lag and stay closer to the current move.
rmaRunning MAWilder average, more progressive than a standard EMA and often used in other indicators.

Block declaration

A strategy can contain multiple [[moving_average]] blocks. Each block outputs one numeric series representing the chosen moving average.

Examples

Minimal setup

This block computes a 20-period EMA on the close series.

[[moving_average]]
id     = "ma"
type   = "ema"
length = 20

Specific source

This block computes a 34-period WMA on the hl2 source.

[[moving_average]]
id     = "ma"
source = "hl2"
type   = "wma"
length = 34

Source from another indicator

This block computes a 10-period SMA on the series produced by the rsi block.

[[rsi]]
id     = "rsi"
length = 14

[[moving_average]]
id     = "rsi_signal"
source = "rsi"
type   = "sma"
length = 10

Search for the optimal source

This block compares multiple price sources.

[[moving_average]]
id     = "ma"
source = ["close", "hl2", "ohlc4"]
type   = "ema"
length = 21

Search for the optimal length

This block explores a range of length values.

[[moving_average]]
id           = "ma"
type         = "ema"
length.start = 10
length.stop  = 50
length.step  = 5

Search for the optimal type

This block compares multiple moving-average types.

[[moving_average]]
id     = "ma"
type   = ["sma", "ema", "wma"]
length = 20

Applying an offset

This block computes an EMA and projects the series 3 candles forward.

[[moving_average]]
id     = "ma"
type   = "ema"
length = 50
offset = 3

Search for the optimal offset

This block explores multiple offset values in order to identify the most relevant shift.

[[moving_average]]
id           = "ma"
type         = "ema"
length       = 50
offset.start = 0
offset.stop  = 5

Dedicated timeframe

This block computes an EMA on the daily timeframe (timeframe = "D"), not on the main timeframe defined in [backtest]. See the Exchanges, Symbols and Timeframes page for alignment rules between this timeframe and the main timeframe.

[[moving_average]]
id        = "ma"
type      = "ema"
length    = 50
timeframe = "D"

Specific symbol

This block reads its series from the specific symbol BINANCE:ETHUSDT. See the Exchanges, Symbols and Timeframes page for alignment with the main symbol.

[[moving_average]]
id     = "ma"
type   = "ema"
length = 50
symbol = "BINANCE:ETHUSDT"

Parameters

ParameterDescription
id
 String
 Required
Unique name for the moving‑average series.
source
 String
 or Array
 Optional
Input series used for the calculation.
Accepted forms: source = "hl2" or source = ["close", "hl2"].
Each value can be either a standard price source (open, close, high, low, hl2, hlc3, ohlc4, hlcc4, volume) or the id of another indicator.
Default value: "close"
type
 String
 or Array
 Required
Moving‑average type (see code table).
length
 Integer
 Required
Look‑back window; must be ≥ 1.

Usage:
• Fixed: length = value
• Grid search:
 – length.start = min_value
 – length.stop = max_value
 – length.step = value (optional, default 1)
offset
 Integer
 Optional
Horizontal shift of the result; must be ≥ 0. A positive value delays the moving average by that many candles.
Default value: 0
symbol
 String
 or Array
 Optional
Market symbol(s) used when source only consists of standard prices (open, close, high, low, hl2, hlc3, ohlc4, hlcc4, volume).
Symbols must include the exchange prefix in the EXCHANGE:SYMBOL format (for example "KUCOIN:BTCUSDT").
If source mixes standard prices and indicator ids, symbol is applied only to combinations based on standard prices.
If source contains only indicator ids, symbol is ignored.
If omitted, the block inherits the [backtest] symbol.
For symbol format, arrays, and alignment rules, see Exchanges, Symbols and Timeframes.
timeframe
 String
 or Array
 Optional
Timeframe on which this indicator is computed.
If timeframe is omitted, the computation uses the grid’s main timeframe defined in [backtest].
For accepted formats and timeframe alignment rules, see Exchanges, Symbols and Timeframes.

Available variables

Use the identifiers below directly in your expressions. The moving‑average block exposes one numeric series and its parameters (type, length, offset), along with the input source, symbol, and timeframe.

Assume the block is configured as:

[[moving_average]]
id     = "ma"
type   = "ema"
length = 20

Then:

VariableDescription
ma or ma[0]
Decimal
Current moving‑average value.
ma[n]
Decimal
Moving‑average value from n candles ago.
ma.type
String
Moving‑average type ("ema", "sma", etc.).
ma.length
Decimal
Length in use.
ma.offset
Decimal
Offset applied.
ma.source
String
Input series name (e.g., close, hlc3, or another indicator id).
ma.symbol
String
Symbol used.
ma.timeframe
String
Timeframe used.

Notes

  • Numeric variables support arithmetic, comparisons, and logical operators.
  • Text variables are strings intended for equality/inequality checks only.