What Does a Moving Average Crossover Really Mean? See It with Python

Recent AAPL candlesticks with five-day and twenty-day moving averages and crossover markers in Python

Two moving averages sometimes cross.

Many trading books give that event a lot of importance. You may even see phrases such as “buy when the short average crosses above.”

But before we talk about a trading rule, we should ask a simpler question:

What actually changed
when the two lines crossed?

The answer is much simpler than it first looks.

A crossover means that the order of two averages changed.

1. Start with the Two Lines We Already Know

In the previous lesson, we plotted:

candlesticks
+ short moving average
+ long moving average

We used a 5-day average and a 20-day average.

SMA 5
→ average of the most recent 5 Close prices

SMA 20
→ average of the most recent 20 Close prices

Because the 5-day average uses fewer prices, it usually reacts faster to recent movement.

Because the 20-day average uses more prices, it usually moves more slowly.

2. A Crossover Is Really a Change in Order

Imagine that yesterday:

SMA 5  = 100
SMA 20 = 102

The short average is below the long average.

Then today:

SMA 5  = 103
SMA 20 = 102

Now the short average is above the long average.

Somewhere between those two observations, the two lines changed order.

before

short SMA
   ↓
below long SMA


after

short SMA
   ↓
above long SMA

That change is the crossover.

3. One Number Makes the Idea Easier

Instead of staring at two lines, subtract one from the other:

MA_gap
=
short SMA - long SMA

Now the relationship becomes very easy to read.

MA_gap > 0
→ short SMA is above long SMA

MA_gap < 0
→ short SMA is below long SMA

MA_gap ≈ 0
→ the two averages are close

A crossover happens when this number changes sign.

negative
→ positive
= short SMA crossed above

positive
→ negative
= short SMA crossed below

This is the key idea of the entire lesson.

4. Why Does the Short Average Cross?

Remember what each line contains.

The short average gives much more importance to recent prices simply because older prices leave its window sooner.

If price starts rising strongly, the recent 5-day average can move upward before the 20-day average catches up.

recent prices rise
      ↓
SMA 5 reacts first
      ↓
SMA 20 follows more slowly
      ↓
their order may change

The same idea works in the opposite direction when recent prices fall.

5. The Crossover Does Not Cause the Price Move

This distinction is important.

The moving averages are calculated from price. They do not push price upward or downward.

price changes
     ↓
moving averages change
     ↓
the two averages may cross

So the crossover is a description of what has already happened in the price data.

It is not a force acting on the market.

6. Build the Crossover Detector with Python

The Python logic follows the same idea.

First calculate:

gap_before
=
short SMA before - long SMA before

gap_now
=
short SMA now - long SMA now

Then check whether the sign changed.

gap_before <= 0
and
gap_now > 0

→ cross above


gap_before >= 0
and
gap_now < 0

→ cross below

We do not need a special crossover library. We only need subtraction and a comparison.

7. Full Runnable Python File

Save this file as:

phase3_03_moving_average_crossover.py

Run:

python phase3_03_moving_average_crossover.py

The complete runnable code is included in the downloadable Python file in this lesson package. Its main building blocks are:

simple_moving_average()
→ builds both averages

draw_candlesticks()
→ keeps OHLC price visible

find_crossovers()
→ watches for a sign change in MA_gap

The final chart contains:

candlesticks
+ SMA 5
+ SMA 20
+ crossover markers

8. Read the Chart in the Right Order

When the chart opens, do not look at the crossover marker first.

Read it in this order:

1. Look at the candles.
2. See what price was doing.
3. Look at the short moving average.
4. Look at the long moving average.
5. Find where their order changed.

This order matters because the moving averages came from the candles.

The indicator should help you summarize price, not make you forget the price underneath it.

9. Why Can the Crossover Arrive After the Move Has Started?

Both lines are averages of past prices.

That means neither line can know tomorrow's price.

The short average may react quickly, but it still needs several recent prices to move enough to cross the long average.

price starts moving
      ↓
short average reacts
      ↓
long average reacts more slowly
      ↓
crossover may happen later

This is our first glimpse of a very important idea: lag.

We will study lag more carefully in a later lesson. First, the next lesson will show three simple ways to read a moving average: price position, slope, and distance.

10. Is “Cross Above” Automatically a Buy Signal?

No.

At this stage, the crossover tells us only this:

the short average
was below the long average

and now

the short average
is above the long average

That is a fact about the two calculations.

Whether that event is useful for trading is a different question.

indicator event
≠
proven trading edge

To answer the trading question later, we would need rules, many historical examples, costs, risk assumptions, and a backtest.

We are deliberately not doing that yet.

11. Try One Small Experiment

Start with:

short_ma_days = 5
long_ma_days = 20

Then try:

short_ma_days = 10
long_ma_days = 30

Run the file again and ask:

Did the crossover dates change?

Are there more crossovers
or fewer crossovers?

Do the slower averages
cross later?

The goal is not to find the “best” numbers.

The goal is to see that the parameter changes the behavior of the indicator.

Check Your Understanding

You are ready to move on if you can explain these ideas in your own words:

  • A crossover is a change in the order of two moving averages.
  • The short moving average usually reacts faster because it uses fewer prices.
  • short SMA - long SMA summarizes which line is above.
  • A sign change in that difference marks a crossover.
  • The crossover is calculated from past price; it does not cause price to move.
  • A crossover is an indicator event, not automatically a profitable trading rule.

What You Just Learned

Close prices
     ↓
short SMA + long SMA
     ↓
short SMA - long SMA
     ↓
MA_gap
     ↓
sign changes
     ↓
crossover

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

A moving-average crossover is not magic. It is simply the moment when two averages of past prices change order.

Before we study lag directly, the next lesson asks another useful question: how can we read a moving average using price position, slope, and distance?