Indicators · condition block
MACD
The MACD block reads the Moving Average Convergence Divergence indicator: the gap between a fast and a slow EMA, and a signal line that smooths that gap. It can fire on signal-line crosses or on the MACD line being above or below zero.
How the engine evaluates it
The MACD line is EMA(fast) − EMA(slow) of closes. The signal line is an EMA of the MACD line with the signal length, seeded with the first MACD value. Every EMA uses k = 2 / (N + 1).
crosses above signal fires on the candle where the MACD line moves from at or below the signal line to above it; crosses below signal is the reverse. These are the moments the MACD histogram changes sign.
above zero and below zero are states: the fast EMA is above (or below) the slow EMA.
The block is false until the slow length has passed. Before your start date the engine loads extra history (at least 50 candles, up to 5,000, scaled to the longest indicator in the bot) so the indicator has settled by the first candle of the run.
MACD[t] = EMA_fast[t] − EMA_slow[t]
signal[t] = EMA(MACD, signal length)[t]
crosses above signal: MACD[t−1] ≤ signal[t−1] and MACD[t] > signal[t]
Botscript syntax
In the Lab you pick this block from the Indicators group and fill in its fields. In the code view the same condition is written like this:
macd(12,26,9) crosses above signal
macd(12,26,9) crosses below signal
macd(12,26,9) above zero
macd(12,26,9) below zero
Settings
| Setting | Values | Default | Notes |
|---|---|---|---|
| Condition | crosses above signal, crosses below signal, above zero, below zero | crosses above signal | Events (crosses) or states (zero line). |
| Fast length | 1 – 5,000 | 12 | Must be shorter than the slow length. |
| Slow length | 2 – 5,000 | 26 | — |
| Signal length | 1 – 5,000 | 9 | Smoothing of the MACD line. |
| Timeframe | the run's candles, or 15m, 30m, 1h, 2h, 4h, 6h, 12h, 1d, 1w | the run's candles | Optional. At least the run's candle size and a whole multiple of it. |
On a higher timeframe
Add a timeframe at the end to read MACD from bigger candles than the run uses, for example macd(12,26,9) above zero on 1d. The engine builds those candles from the run's own candles (UTC, weeks start on Monday) and evaluates the block only on candles that have closed, so there is no look-ahead. Lengths and lookbacks count the bigger candles.
On a higher timeframe crosses above signal and crosses below signal are events: they fire once, on the run candle that closes the bigger candle where they happened. above zero and below zero are states that hold until the next bigger candle closes.
The timeframe must be at least the run's candle size and a whole multiple of it; otherwise the run stops with an error and is not charged. Multi-timeframe conditions covers the details, including warm-up.
How traders use it
- Momentum entries:
crosses above signalwhileabove zerofor trades with the trend. - Exits:
crosses below signalwithholding. - Trend filter:
macd(12,26,9) above zeroin front of any entry.
Example bot
bot "MACD Momentum"
when macd(12,26,9) crosses above signal and macd(12,26,9) above zero
then buy 100%
when macd(12,26,9) crosses below signal
then sell 100%
- Buys a bullish signal-line cross only while the MACD line is above zero.
- Sells on the next bearish cross.
Pitfalls
- MACD is built from moving averages and lags price; crosses often come after the turn.
- Signal-line crosses happen often in flat markets and produce many small losses.
MACD: frequently asked questions
Is there a histogram condition?
The histogram is MACD minus signal. Signal-line crosses are exactly the moments the histogram changes sign, so crosses above signal covers it.
What do 12, 26 and 9 mean?
Fast EMA length, slow EMA length and the length of the signal EMA. They are Gerald Appel's original settings.
Why might my MACD differ slightly from another chart?
EMAs depend a little on where their history starts and how the first value is seeded. The engine loads warm-up candles before your start date so the values have converged by the first candle of the run.
Can I use other lengths?
Yes, any lengths where the fast one is shorter than the slow one.
Related blocks
Simulated results are hypothetical. They include only the fee and slippage you set on the bot, not every real-world cost or market condition, and past performance does not guarantee future results. Nothing here is financial advice.