Moving Averages (ma)

[[ma]] turns a series into a smoother line that dampens its shortest-term changes and brings out its broader movement. Each value summarizes recent observations from source over the period set by length. A short length follows the source more closely, while a longer length produces a slower, steadier line.

The moving average type determines how those observations are weighted or combined. Two averages with the same length can therefore react differently to the same data. The block produces one numeric series. A positive offset shifts this series to the right on the time axis: on any given candle, the resulting value is the one calculated offset candles earlier. This shift does not predict future values.

Moving average types

The block supports the following variants:

CodeNameDescription
smaSimple Moving AverageArithmetic average of the values over the period, with equal weight given to each observation.
emaExponential Moving AverageExponentially smoothed average in which the weight of each value decreases progressively with its age.
wmaWeighted Moving AverageWeighted average in which each value receives a weight based on its position within the period.
hmaHull Moving AverageHull moving average built from successive WMAs to reduce lag while keeping the curve smoothed.
demaDouble EMAAverage built from two levels of exponential smoothing applied to the same series.
temaTriple EMAAverage built from three successive levels of exponential smoothing applied to the same series.
trimaTriangular MATriangular moving average whose weighting is concentrated around the middle of the calculation period.
smmaSmoothed MARecursive smoothed average calculated by extending the previous value with the new observation.
zlemaZero-Lag EMAExponential average applied to a series adjusted for the lag associated with the calculation period.
rmaRunning MAWilder’s recursive moving average, based on a smoothing factor derived from the period length.

Block declaration

A strategy can contain multiple [[ma]] 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.

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

Specific source

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

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

Hull Moving Average

This block computes a 55-period HMA on the close series.

[[ma]]
id     = "hma_close"
source = "close"
type   = "hma"
length = 55

Source from another indicator

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

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

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

Search for the optimal source

This block compares multiple price sources.

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

Search for the optimal length

This block explores a range of length values.

[[ma]]
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.

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

Applying an offset

This block computes an EMA and delays its values by 3 candles. On each candle, the series therefore contains the EMA value calculated 3 candles earlier.

[[ma]]
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.

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

Dedicated symbol and timeframe

This block computes an EMA from the daily series of BINANCE:ETHUSDT, independently of the primary symbol and timeframe defined in [backtest]. See Exchanges, Symbols and Timeframes for alignment rules.

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

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: with offset = n, the value available on the current candle is the one calculated n candles earlier. This shift does not use future data.
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:

[[ma]]
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.