gs_quant.timeseries.technicals.macd

macd(x, m=12, n=26, s=1)[source]

Moving average convergence divergence (MACD).

Moving average convergence divergence (MACD) is a trend-following momentum indicator that shows the relationship between two moving averages of a timeseries. It is the result of subtracting the exponential moving average of x with a period of \(m\) from the exponential moving average of \(x\) with a period of \(n\).

Optionally, specify \(s\) to apply an exponential moving average to the resulting series with a period of \(s\) (default 1, equivalent to no exponential moving average).

Parameters:
  • x (Series) – time series

  • m (int) – period of first, short exponential moving average (default 12)

  • n (int) – period of second, long exponential moving average (default 26)

  • s (int) – optional smoothing parameter (default 1)

Return type:

Series

Returns:

date-based time series of return

Usage

The exponential(ly weighted) moving average (EMA) of a series [\(X_0\), \(X_1\), \(X_2\), …], is defined as:

\(Y_0 = X_0\)

\(Y_t = \beta \cdot Y_{t-1} + (1 - \beta) \cdot X_t\)

where \(\beta = \frac{2}{\text{period} + 1}\) is the weight we place on the previous average.

The MACD of a series is defined as \(\text{EMA}(\text{EMA}(X, M) - \text{EMA}(X, N), S)\)

Examples

Generate price series with 100 observations starting from today’s date:

>>> prices = generate_series(100)
>>> macd(prices, 12, 26)

See also

exponential_moving_average() moving_average() smoothed_moving_average()