What Is MACD? Build It from Two EMAs with Python

AAPL candlesticks with EMA 12 and EMA 26 above Momentum ROC RSI and MACD line panels calculated in Python

We already know what an exponential moving average does.

prices
   ↓
give more weight to recent prices
   ↓
EMA

In this lesson, we will not invent a completely new kind of calculation.

We will build a new indicator by combining two EMAs we already know how to calculate.

fast EMA
-
slow EMA
=
MACD line

That is the central idea of MACD.

If you want to understand why Gerald Appel created MACD and how the indicator evolved, see the story behind Gerald Appel and MACD.

1. Start with Two EMAs

The common MACD settings use:

Fast EMA = 12 periods
Slow EMA = 26 periods

Both lines start from the same Close prices. The only difference is how quickly they respond.

same Close prices
      ↓
  ┌───┴────┐
  ↓        ↓
EMA 12   EMA 26
faster   slower

EMA 12 gives more weight to recent movement because its span is shorter. EMA 26 changes more slowly because its span is longer.

2. MACD Is the Distance Between Those Two Smoothed Levels

The MACD line is:

MACD
=
Fast EMA
-
Slow EMA

With the common settings:

MACD
=
EMA 12
-
EMA 26

So MACD is not a mysterious number added on top of price.

It is a difference between two smoothed versions of the same price series.

3. Read Positive, Negative, and Zero Directly from the Formula

Suppose:

EMA 12 = 105
EMA 26 = 102

Then:

MACD = 105 - 102 = +3

A positive MACD line simply means:

EMA 12
is above
EMA 26

Now suppose:

EMA 12 = 98
EMA 26 = 101

Then:

MACD = 98 - 101 = -3

A negative MACD line means:

EMA 12
is below
EMA 26

And if the two EMA values are equal:

MACD = 0

4. Why the Zero Line Has a Natural Meaning

We do not need to memorize the zero line. It falls directly out of subtraction.

EMA 12 > EMA 26
→ MACD > 0

EMA 12 < EMA 26
→ MACD < 0

EMA 12 = EMA 26
→ MACD = 0

This also means that a zero crossing happens when the two EMA lines cross each other.

two EMA lines cross
        ⇅
MACD crosses zero

They are two views of the same mathematical event.

5. Work Through a Tiny Example

The standard 12 and 26 spans are too long for a small hand calculation. So we will use toy spans:

Fast EMA = 3
Slow EMA = 5

Use these prices:

100, 102, 101, 104, 103, 106

Our previously taught EMA function uses:

alpha
=
2 / (span + 1)

So:

EMA 3 alpha = 2 / 4 = 0.5

EMA 5 alpha = 2 / 6 ≈ 0.333333

At the final price, our frozen EMA block gives approximately:

Fast EMA 3 ≈ 104.375000

Slow EMA 5 ≈ 103.489712

Subtract:

MACD
=
104.375000
-
103.489712

≈ 0.885288

The MACD value is positive because the faster EMA is above the slower EMA.

6. Build the MACD Line from Scratch

We already know how to calculate an EMA. So the new Learning Block can stay very small.

def macd_line(fast_ema_values, slow_ema_values):
    macd_values = []

    for fast_value, slow_value in zip(
        fast_ema_values,
        slow_ema_values,
    ):
        macd = float(fast_value) - float(slow_value)
        macd_values.append(macd)

    return macd_values

Read the function literally.

take one fast EMA value
        ↓
take the matching slow EMA value
        ↓
subtract
        ↓
save the difference
        ↓
repeat

The calculation itself is simple because the important work was already done when we learned EMA.

7. Reuse the EMA Block Twice

First build the fast EMA:

fast_ema_values, fast_alpha = exponential_moving_average(
    values=close_prices,
    span=12,
)

Then build the slow EMA with the same function:

slow_ema_values, slow_alpha = exponential_moving_average(
    values=close_prices,
    span=26,
)

Nothing inside exponential_moving_average() changes.

same function
+
different span
=
different smoothing speed

Then the new block combines the two outputs:

macd_values = macd_line(
    fast_ema_values=fast_ema_values,
    slow_ema_values=slow_ema_values,
)

8. This Is a Family Tree, Not a Bag of Unrelated Indicators

MACD becomes much easier when we see where it came from.

Price
  ↓
EMA
  ↓
two different spans
  ↓
EMA 12 and EMA 26
  ↓
subtract
  ↓
MACD line

This is exactly why earlier lessons built indicators from scratch.

A more advanced indicator can reuse a simpler calculation instead of becoming a new formula to memorize.

9. Reuse the Other Blocks Without Relearning Them

The complete Python file keeps familiar blocks from earlier lessons.

exponential_moving_average()
→ REUSE

momentum()
→ REUSE

rate_of_change()
→ REUSE

relative_strength_index()
→ REUSE

draw_candlesticks()
→ REUSE

create_stacked_panels()
→ REUSE

macd_line()
→ NEW

The new algorithmic idea in this lesson is still only one thing:

fast EMA - slow EMA

10. The Plot Block Grows to Five Panels

We keep the same reusable layout function:

fig, axes = create_stacked_panels(
    panel_count=5,
    height_ratios=[3, 1, 1, 1, 1],
)

The panels become:

Panel 1
Candles + EMA 12 + EMA 26

Panel 2
Momentum 10

Panel 3
ROC 10

Panel 4
RSI 14

Panel 5
MACD line

The important visual comparison is between Panel 1 and Panel 5.

distance between EMA 12 and EMA 26
              ↓
same information expressed as
              ↓
          MACD line

11. What Does a Rising MACD Line Mean?

Start from the formula again.

MACD
=
EMA 12 - EMA 26

If the MACD value rises, the difference between the two EMA values is becoming more positive or less negative.

That can happen because the faster EMA is pulling farther above the slower EMA, or because it is catching up from below.

So:

MACD rising
does not automatically mean
price rose on that exact bar

It tells us how the relationship between the two smoothed price levels changed.

12. What Does "Convergence" Mean?

If the two EMA values move closer together:

| EMA 12 - EMA 26 |
becomes smaller

Their difference is converging toward zero.

EMA 12
   ↘
    closer
   ↗
EMA 26

→ MACD moves toward zero

13. What Does "Divergence" Mean Here?

If the two EMA values move farther apart:

| EMA 12 - EMA 26 |
becomes larger

Their smoothed levels are separating.

This use of the word divergence comes directly from the distance between the moving averages.

We are not introducing price-versus-indicator divergence trading in this lesson.

14. MACD Is Not Bounded Like RSI

RSI was transformed onto a fixed scale:

0
to
100

MACD is different.

MACD
=
difference between two EMA price levels

Its numerical size therefore depends partly on the price scale and on how far the two averages are separated.

This is another reason not to treat every technical indicator as if the numbers mean the same thing.

15. One Important Initialization Detail

Our EMA function has already been taught and frozen. It begins the EMA series with the first available price.

first EMA
=
first available price

Some software libraries initialize EMA calculations differently.

Therefore, the earliest MACD values from two implementations can differ slightly even when both use 12 and 26 periods.

We will not silently change the EMA function just to force the first rows to match another library.

known EMA block
→ keep it familiar

new MACD idea
→ learn the subtraction

16. Where Are the Signal Line and Histogram?

Most MACD charts show more than one thing.

MACD line
Signal line
Histogram

We are deliberately stopping after the first item.

Today:

MACD line
=
EMA 12 - EMA 26

In the next lesson, we will ask:

What happens if we smooth
the MACD line itself?

That will lead naturally to the signal line, and then to the histogram.

17. MACD Is Still Not a Trading Rule

If MACD is positive, we can say:

fast EMA
is above
slow EMA

We cannot automatically say:

buy now

If MACD is negative, we can say:

fast EMA
is below
slow EMA

We cannot automatically say:

sell now

A trading rule is a later hypothesis that must be tested.

18. Change One Thing Yourself

Start with:

macd_fast_span = 12
macd_slow_span = 26

Then change only the fast span:

macd_fast_span = 8
macd_slow_span = 26

Ask:

Does the fast EMA react sooner?

Does the MACD line move farther
from zero during short moves?

Then restore the fast span and change only the slow span:

macd_fast_span = 12
macd_slow_span = 35

Ask what changed.

The goal is not to find the best parameters. The goal is to understand what the two time scales do.

Check Your Understanding

  • The MACD line is the fast EMA minus the slow EMA.
  • The common fast and slow periods are 12 and 26.
  • A positive MACD means the fast EMA is above the slow EMA.
  • A negative MACD means the fast EMA is below the slow EMA.
  • MACD equals zero when the two EMA values are equal.
  • A zero crossing and an EMA crossover describe the same mathematical event.
  • MACD is not bounded between 0 and 100 like RSI.
  • Different EMA initialization conventions can make early MACD values differ slightly.
  • The signal line and histogram are intentionally left for the next lesson.
  • A MACD value describes an EMA relationship; it is not automatically a buy or sell command.

What You Just Learned

Close prices
     ↓
EMA 12      EMA 26
   \          /
    \        /
     subtract
        ↓
    MACD line
        ↓
positive / zero / negative
        ↓
relationship between
two smoothed price levels

If one idea stays in your head after this lesson, let it be this:

MACD begins with something you already know. Build two EMAs from the same prices, then subtract the slower one from the faster one.

Sources