Skip to content

MACD

Moving Average Convergence Divergence indicator.

Usage

typescript
const data = await sdk.getKlineWithIndicators('sz000858', {
  indicators: { macd: true }
});

data.forEach(k => {
  console.log(`DIF: ${k.macd?.dif}, DEA: ${k.macd?.dea}, MACD: ${k.macd?.macd}`);
});

Manual Calculation

typescript
import { calcMACD } from 'stock-sdk';

const closes = [/* close prices */];
const result = calcMACD(closes, 12, 26, 9);

// result.dif  - DIF line (fast EMA - slow EMA)
// result.dea  - DEA line (signal line)
// result.macd - MACD histogram (2 * (DIF - DEA))

Parameters

ParameterDefaultDescription
short12Short EMA period
long26Long EMA period
signal9Signal line period

Formula

$$DIF = EMA_{12} - EMA_{26}$$ $$DEA = EMA_9(DIF)$$ $$MACD = 2 \times (DIF - DEA)$$

Released under the ISC License.