MACD

Incremental, causal technical analysis documentation

Summary

MACD is RTTA's multi-output Moving Average Convergence/Divergence oscillator: fast EMA minus slow EMA, a signal EMA of that difference, and a histogram of MACD minus signal.

Update API

result = rtta.MACD(a=12, b=26, c=9, fillna=False).update(value)
Parameter Default Meaning
a 12 Fast EMA period
b 26 Slow EMA period
c 9 Signal EMA period
fillna False If False, NaN until warm-up

update(value) returns a result with:

advance(value) updates state; last() returns the cached result.

Related APIs: MACDFix (fixed 12/26), MACDExt (selectable SMA/EMA types).

Theory Of Operation

MACD measures the gap between a short-horizon and long-horizon exponential average of price. When the fast EMA is above the slow EMA, intermediate momentum is positive; the opposite signals negative momentum. The signal line is a further EMA that smooths MACD so that:

All three nested EMAs run with internal fillna=True so coefficients and seeds advance every bar; the outer fillna flag only gates whether incomplete warm-up samples are returned as NaN.

Recurrence

Let \(x_t\) be the input series and \(a,b,c\) the three periods.

\[F_t = \operatorname{EMA}_a(x_t),\qquad S_t = \operatorname{EMA}_b(x_t)\]
\[MACD_t = F_t - S_t\]
\[signal_t = \operatorname{EMA}_c(MACD_t)\]
\[hist_t = MACD_t - signal_t\]

When fillna=False, all three fields are NaN while the sample counter is less than \(\max(a,b)+c\); otherwise the current MACD triple is returned.

EMA seeding and \(\alpha=2/(n+1)\) follow RTTA's EMA.

Implementation Notes

Reference