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:
| Code | Name | Description |
|---|---|---|
sma | Simple Moving Average | Arithmetic average of the values over the period, with equal weight given to each observation. |
ema | Exponential Moving Average | Exponentially smoothed average in which the weight of each value decreases progressively with its age. |
wma | Weighted Moving Average | Weighted average in which each value receives a weight based on its position within the period. |
hma | Hull Moving Average | Hull moving average built from successive WMAs to reduce lag while keeping the curve smoothed. |
dema | Double EMA | Average built from two levels of exponential smoothing applied to the same series. |
tema | Triple EMA | Average built from three successive levels of exponential smoothing applied to the same series. |
trima | Triangular MA | Triangular moving average whose weighting is concentrated around the middle of the calculation period. |
smma | Smoothed MA | Recursive smoothed average calculated by extending the previous value with the new observation. |
zlema | Zero-Lag EMA | Exponential average applied to a series adjusted for the lag associated with the calculation period. |
rma | Running MA | Wilder’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 = 20Specific source
This block computes a 34-period WMA on the hl2 source.
[[ma]]
id = "ma"
source = "hl2"
type = "wma"
length = 34Hull Moving Average
This block computes a 55-period HMA on the close series.
[[ma]]
id = "hma_close"
source = "close"
type = "hma"
length = 55Source 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 = 10Search for the optimal source
This block compares multiple price sources.
[[ma]]
id = "ma"
source = ["close", "hl2", "ohlc4"]
type = "ema"
length = 21Search 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 = 5Search for the optimal type
This block compares multiple moving-average types.
[[ma]]
id = "ma"
type = ["sma", "ema", "wma", "hma"]
length = 20Applying 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 = 3Search 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 = 5Dedicated 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
| Parameter | Description |
|---|---|
idString Required | Unique name for the moving‑average series. |
sourceString 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" |
typeString or Array Required | Moving‑average type (see code table). |
lengthInteger 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) |
offsetInteger 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 |
symbolString 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. |
timeframeString 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 = 20Then:
| Variable | Description |
|---|---|
ma or ma[0]Decimal | Current moving‑average value. |
ma[n]Decimal | Moving‑average value from n candles ago. |
ma.typeString | Moving‑average type ("ema", "sma", etc.). |
ma.lengthDecimal | Length in use. |
ma.offsetDecimal | Offset applied. |
ma.sourceString | Input series name (e.g., close, hlc3, or another indicator id). |
ma.symbolString | Symbol used. |
ma.timeframeString | Timeframe used. |
Notes
- Numeric variables support arithmetic, comparisons, and logical operators.
- Text variables are strings intended for equality/inequality checks only.