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:
| Code | Name | Description |
|---|---|---|
sma | Simple Moving Average | Simple average where each value in the period has the same weight. |
ema | Exponential Moving Average | Exponential average that gives more weight to recent values and reacts faster than an SMA. |
wma | Weighted Moving Average | Weighted average that emphasizes recent values in order to follow series changes more quickly. |
dema | Double EMA | Average based on a double EMA, used to reduce the lag of a standard EMA. |
tema | Triple EMA | Average based on a triple EMA, even more reactive than a DEMA. |
trima | Triangular MA | Triangular average that smooths the series more strongly by giving more importance to the center of the period. |
smma | Smoothed MA | Smoothed average that evolves more slowly and strongly attenuates short-term variations. |
zlema | Zero‑Lag EMA | Exponential average adjusted to limit smoothing lag and stay closer to the current move. |
rma | Running MA | Wilder 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 = 20Specific source
This block computes a 34-period WMA on the hl2 source.
[[moving_average]]
id = "ma"
source = "hl2"
type = "wma"
length = 34Source 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 = 10Search for the optimal source
This block compares multiple price sources.
[[moving_average]]
id = "ma"
source = ["close", "hl2", "ohlc4"]
type = "ema"
length = 21Search 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 = 5Search for the optimal type
This block compares multiple moving-average types.
[[moving_average]]
id = "ma"
type = ["sma", "ema", "wma"]
length = 20Applying an offset
This block computes an EMA and projects the series 3 candles forward.
[[moving_average]]
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.
[[moving_average]]
id = "ma"
type = "ema"
length = 50
offset.start = 0
offset.stop = 5Dedicated 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
| 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. 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:
[[moving_average]]
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.