SMA
SMA
At a glance — Simple Moving Average: the arithmetic mean of the last
period values of the chosen source. Smooth and lag-prone; great as a trend
filter. Computed in O(1) per bar.
Signature
const sma: Series = api.use(SMA, { source: 'close', period: 50 });| Field | Type | Description |
|---|---|---|
period | number (≥ 1) | Look-back window. |
source | Source | Price source: open, high, low, close, hl2, hlc3, ohlc4. |
Warm-up: period bars.
Example
import { defineStrategy, SMA } from '@nexpips/sdk-trading';
/** * Filtre de tendance : n'acheter que lorsque la clôture est au-dessus de * la moyenne mobile simple SMA(50). */export default defineStrategy({ symbol: 'EURUSD', timeframe: 'H1', risk: { maxRiskPercentPerTrade: 1, maxOpenPositions: 1, maxDailyLossPercent: 5 }, setup: (api) => { const sma = api.use(SMA, { source: 'close', period: 50 });
return { onBar(ctx) { if (!ctx.position.isFlat || ctx.position.hasPendingOrder) return;
if (ctx.series.close.at(0) > sma.at(0)) { ctx.order.marketBuy({ riskPercent: 1, stopLoss: { type: 'atr', period: 14, multiple: 2 }, takeProfit: { type: 'rr', value: 2 }, }); } }, }; },});Only buy when price is above the SMA(50).