Indicators · condition block
EMA cross (exponential moving average)
The EMA cross block fires when the close crosses its exponential moving average, an average that gives more weight to recent candles and so reacts faster than a simple one.
How the engine evaluates it
The EMA is computed with the smoothing factor k = 2 / (N + 1) and seeded with the first close of the loaded history. Each new value moves a fraction k of the way toward the latest close.
Crossings use the same rule as the SMA block: the previous close on or below the previous EMA, and the current close above the current EMA (or the reverse).
Because the seed is the first close, early EMA values depend on where the history starts. 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. For an EMA, about four times its length.
k = 2 / (N + 1)
EMA[0] = close[0]
EMA[t] = close[t] × k + EMA[t−1] × (1 − k)
crosses above: close[t−1] ≤ EMA[t−1] and close[t] > EMA[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:
price crosses above ema(21)
price crosses below ema(9)
Settings
| Setting | Values | Default | Notes |
|---|---|---|---|
| Direction | crosses above, crosses below | crosses above | Bullish or bearish cross. |
| Length | 1 – 5,000 candles | 21 | Sets the smoothing factor k. |
| 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 EMA cross from bigger candles than the run uses, for example price crosses below ema(21) on 4h. 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.
EMA cross is an event on a higher timeframe: it fires once, on the run candle that closes the bigger candle where it happened, and is false on the other run candles.
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
- Short-term trend entries:
price crosses above ema(21). - Fast exits:
price crosses below ema(9)to protect gains quickly. - Pullback buying: an RSI dip while price is above a longer EMA.
Example bot
bot "EMA 21 Pullback"
when price crosses above ema(21) and rsi(14) < 60
then buy 100%
when price crosses below ema(21)
then sell 100%
- Buys when price reclaims the 21 EMA while RSI is not yet stretched.
- Sells when price loses the EMA again.
Pitfalls
- Faster averages mean more whipsaws in ranging markets.
- Values can differ slightly from charting sites that seed the first EMA value differently; after the warm-up the difference is negligible.
EMA cross: frequently asked questions
How is the EMA seeded?
With the first close of the loaded history, then smoothed with k = 2 / (N + 1). Warm-up history before your start date lets it settle.
Will my EMA match other charting tools?
After the warm-up, yes to within rounding. Tools differ only in how they seed the very first value (the first close or a simple average), and that difference fades away after a few multiples of N candles.
Why does an EMA react faster than an SMA?
Every candle in an SMA has equal weight. In an EMA the latest close has weight k and older closes fade away exponentially.
Does the cross fire on wicks?
No. Crossings use closes only.
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.