Moving Average Convergence Divergence (macd)
[[macd]] measures the gap between two moving averages calculated from the same price series. The MACD line is the difference between a fast average and a slow average. The Signal line is a moving average applied to the MACD line. The Histogram represents the gap between the MACD line and the Signal line.
The MACD is used to track how momentum evolves, that is, how a move gains or loses intensity. A MACD line above zero indicates that the fast average remains above the slow average. A MACD line below zero indicates the opposite. When the MACD line moves away from the Signal line, the gap between the two readings becomes more pronounced. When it moves closer, that gap is slowing down.
The MACD measures neither volatility nor the price level itself. It also does not define fixed overbought or oversold zones. It is mainly used to identify crossovers, follow the strengthening or weakening of a move, or compare several settings in a grid search.
Block declaration
A strategy can contain multiple [[macd]] blocks.
Each block outputs one to three numeric series: the MACD line, the Signal line, and, when requested, the Histogram.
Examples
Minimal setup
This block does not specify a source and exposes only the MACD line. The close series is used by default, with the classic lengths 12 for fast and 26 for slow.
[[macd]]
macd_id = "macd_line"Custom fixed lengths
This block uses custom fast, slow, and signal values.
[[macd]]
macd_id = "macd_line"
signal_id = "macd_signal"
hist_id = "macd_hist"
fast = 8
slow = 21
signal = 7Search for the optimal lengths
This block explores ranges of values for fast, slow, and signal to identify the best-performing combination.
[[macd]]
macd_id = "macd_line"
signal_id = "macd_signal"
hist_id = "macd_hist"
fast.start = 6
fast.stop = 30
slow.start = 6
slow.stop = 30
signal.start = 6
signal.stop = 12Custom source and moving average type
This block uses a source other than close and explicitly chooses the moving average type (sma) for both the MACD line and the Signal line.
[[macd]]
macd_id = "macd_line"
signal_id = "macd_signal"
hist_id = "macd_hist"
source = "hlc3"
macd_type = "sma"
signal_type = "sma"Bullish momentum filter
This example uses the MACD as a confirmation filter. The condition becomes true only if the MACD line remains above zero and the Histogram is positive. This lets an entry signal pass only when momentum remains oriented to the upside.
[[macd]]
macd_id = "macd_line"
signal_id = "macd_signal"
hist_id = "macd_hist"
[[condition]]
id = "bullish_momentum_filter"
condition = "macd_line > 0 and macd_hist > 0"
next_block_id = "..."Parameters
| Parameter | Description |
|---|---|
macd_idString Required | Identifier for the MACD series. |
signal_idString Optional | Identifier for the Signal series. When this key is set, the signal length becomes optional and defaults to 9 when omitted. |
hist_idString Optional | Identifier for the Histogram series (valid only when signal_id is defined). |
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" |
fastInteger Optional | Length of the fast moving average; must be ≥ 2. Default value: 12.Usage: • Fixed: fast = value• Grid search: – fast.start = min_value– fast.stop = max_value– fast.step = value (optional, default 1) |
slowInteger Optional | Length of the slow moving average (must be > fast).Default value: 26.Usage: • Fixed: slow = value• Grid search: – slow.start = min_value– slow.stop = max_value– slow.step = value (optional, default 1) |
signalInteger Conditional | Length of the moving average applied to the MACD line; must be ≥ 1. This key is only allowed when signal_id is defined. If it is omitted in that case, the default value 9 is used.Usage: • Fixed: signal = value• Grid search: – signal.start = min_value– signal.stop = max_value– signal.step = value (optional, default 1) |
macd_typeString or Array Optional | Moving average type(s) used for the MACD line (fast and slow). Allowed values: "sma", "ema", "wma", "dema", "tema", "trima", "smma", "zlema", "rma". Accepts either a single string or an array for grid search. Default: "ema". |
signal_typeString or Array Conditional | Moving average type(s) used for the Signal line. Same value set as macd_type. Accepts a string or an array for grid search. Default: "ema". This key is only allowed when signal_id is defined. |
symbolString or Array Optional | Market symbol(s) used when source only consists of standard prices (open, close, high, low, hl2, hlc3, ohlc4, hlcc4, volume).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.When 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 MACD block exposes up to three numeric series (MACD line, Signal line, Histogram), their moving-average lengths and types, plus the input source, symbol, and timeframe.
Assume the block is configured as:
[[macd]]
macd_id = "macd_line"
signal_id = "macd_signal"
hist_id = "macd_hist"Then:
| Variable | Description |
|---|---|
macd_line ormacd_line[0]Decimal | Current MACD line value. |
macd_line[n]Decimal | MACD value from n candles ago. |
macd_signal ormacd_signal[0]Decimal | Current Signal line value. |
macd_signal[n]Decimal | Signal value from n candles ago. |
macd_hist ormacd_hist[0]Decimal | Current Histogram value. |
macd_hist[n]Decimal | Histogram value from n candles ago. |
macd_line.fast_lenDecimal | Fast moving average length. |
macd_line.slow_lenDecimal | Slow moving average length. |
macd_line.signal_lenDecimal | Length of the moving average used for the signal (available when a Signal id is provided). |
macd_line.typeString | Moving average type used for the MACD line ("ema", "sma", etc.). |
macd_signal.typeString | Moving average type used for the Signal line (when signal_id is defined). |
macd_line.sourcemacd_signal.sourcemacd_hist.sourceString | Input series name. |
macd_line.symbolmacd_signal.symbolmacd_hist.symbolString | Symbol used. |
macd_line.timeframemacd_signal.timeframemacd_hist.timeframeString | Timeframe used. |
Notes
- Numeric variables support arithmetic, comparisons, and logical operators.
- Text variables are strings intended for equality/inequality checks only.