Skip to content

MACD

MACD

At a glance — Moving Average Convergence Divergence. macd = EMA(fast) − EMA(slow), signal = EMA(macd, signal), hist = macd − signal. The series yields a MacdOutput object per bar.

Signature

const macd: Series<MacdOutput> = api.use(MACD, { fast: 12, slow: 26, signal: 9 });
FieldTypeDescription
fastnumberFast EMA period (e.g. 12).
slownumberSlow EMA period (e.g. 26).
signalnumberSignal EMA period (e.g. 9).

Warm-up: slow + signal bars (conservative).

Example

indicators/macd-momentum
import { defineStrategy, MACD } from '@nexpips/sdk-trading';
/**
* Momentum MACD. La série renvoie un `MacdOutput` ({ macd, signal, hist }).
* Entrée quand la ligne MACD repasse au-dessus de sa ligne de signal avec
* un histogramme positif.
*/
export default defineStrategy({
symbol: 'EURUSD',
timeframe: 'H1',
risk: { maxRiskPercentPerTrade: 1, maxOpenPositions: 1, maxDailyLossPercent: 5 },
setup: (api) => {
const macd = api.use(MACD, { fast: 12, slow: 26, signal: 9 });
return {
onBar(ctx) {
if (!ctx.position.isFlat || ctx.position.hasPendingOrder) return;
if (macd.length < 2) return;
const now = macd.at(0);
const prev = macd.at(1);
const crossedUp = prev.macd <= prev.signal && now.macd > now.signal;
if (crossedUp && now.hist > 0) {
ctx.order.marketBuy({
riskPercent: 1,
stopLoss: { type: 'atr', period: 14, multiple: 2 },
takeProfit: { type: 'rr', value: 2 },
});
}
},
};
},
});

Enter on MACD crossing above its signal line.

See also