EMA (Exponential Moving Average)
The EMA applies exponentially decreasing weights to older prices, making it more responsive to recent price changes than the SMA.
Declaration
function init(ctx) { ctx.indicator('fastEma', 'EMA', { period: ctx.p.fastLength, source: 'close' }); ctx.indicator('slowEma', 'EMA', { period: ctx.p.slowLength, source: 'close' }); }
Options
| Option | Type | Default | Description |
|---|---|---|---|
period | number | 20 | Lookback period |
source | string | 'close' | Price source (open, high, low, close, hl2, hlc3, ohlc4) |
Output
Returns a single number[] series.
Use Cases
EMA Crossover Strategy
function init(ctx) { ctx.indicator('fast', 'EMA', { period: 9, source: 'close' }); ctx.indicator('slow', 'EMA', { period: 21, source: 'close' }); } function onBar(ctx, i) { if (q.crossOver(ctx.ind.fast, ctx.ind.slow, i)) { ctx.order.market('ASSET', 1, { signal: 'buy', reason: 'ema_cross_up' }); } if (q.crossUnder(ctx.ind.fast, ctx.ind.slow, i)) { ctx.order.close('ASSET', { signal: 'sell', reason: 'ema_cross_down' }); } }
Dynamic Support/Resistance
function onBar(ctx, i) { const close = ctx.series.close[i]; const ema = ctx.ind.ema21[i]; // Price bouncing off EMA as support if (close > ema && ctx.series.low[i] <= ema * 1.001) { ctx.order.market('ASSET', 1, { signal: 'buy', reason: 'ema_bounce' }); } }
Calculation
$$EMA_t = Price_t \times k + EMA_{t-1} \times (1 - k)$$
Where $k = \frac{2}{period + 1}$
Related
Related
indicatoremamoving-averagetrendqsl