Lesson 4 of 15

MACD

MACD — Moving Average Convergence Divergence

MACD is a trend-following momentum indicator. It consists of three components:

Components

MACD Line: Difference between a fast and slow EMA:

macd_line[t] = EMA(fast)[t] - EMA(slow)[t]

Signal Line: EMA of the MACD line with a signal period:

signal[t] = EMA(macd_line, signal_period)[t]

Histogram: Difference between the MACD line and signal line:

histogram[t] = macd_line[t] - signal[t]

EMA Formula

Use alpha = 2 / (period + 1) and the standard EMA recurrence starting from prices[0].

Typical Parameters

  • Fast: 12 periods
  • Slow: 26 periods
  • Signal: 9 periods

Task

Implement:

  • macd_line(prices, fast=12, slow=26)
  • macd_signal(prices, fast=12, slow=26, signal=9)
  • macd_histogram(prices, fast=12, slow=26, signal=9)
Python runtime loading...
Loading...
Click "Run" to execute your code.