EMA
EMA
At a glance — Exponential Moving Average: seeded by an SMA over the first
period bars, then smoothed exponentially (ema = α·value + (1 − α)·prevEma,
α = 2/(period + 1)). Reacts faster than SMA to recent prices.
Signature
const ema: Series = api.use(EMA, { source: 'close', period: 20 });| Field | Type | Description |
|---|---|---|
period | number (≥ 1) | Look-back window. |
source | Source | Price source. |
Warm-up: period bars.
Example
import { crossedOver, crossedUnder, defineStrategy, EMA } from '@nexpips/sdk-trading';
/** * Croisement de moyennes exponentielles. Golden cross (EMA rapide passe * au-dessus de la lente) → entrée ; death cross → sortie. Montre `EMA`, * `crossedOver` et `crossedUnder`. */export default defineStrategy({ symbol: 'EURUSD', timeframe: 'H1', risk: { maxRiskPercentPerTrade: 1, maxOpenPositions: 1, maxDailyLossPercent: 5 }, setup: (api) => { const fast = api.use(EMA, { source: 'close', period: 12 }); const slow = api.use(EMA, { source: 'close', period: 26 });
return { onBar(ctx) { if (ctx.position.isFlat && !ctx.position.hasPendingOrder && crossedOver(fast, slow)) { ctx.order.marketBuy({ riskPercent: 1, stopLoss: { type: 'atr', period: 14, multiple: 2 }, takeProfit: { type: 'rr', value: 2 }, }); return; }
if (ctx.position.isLong && crossedUnder(fast, slow)) { ctx.order.closePosition('ema death cross'); } }, }; },});Golden/death cross of EMA(12) and EMA(26).
See also
SMA— smoother, more lagcrossedOver·crossedUnder