MacdOutput
MacdOutput
At a glance — The per-bar value produced by the MACD
indicator. Read the latest value with macd.at(0).
Signature
export interface MacdOutput { macd: number; signal: number; hist: number;}| Field | Description |
|---|---|
macd | The MACD line: EMA(fast) − EMA(slow). |
signal | The signal line: EMA(macd, signal). |
hist | Histogram: macd − signal. Positive means bullish momentum. |
Example
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 }, }); } }, }; },});Read macd / signal / hist from the series.