← All lessons

Lesson 7 of 17Build a signal1 min read

Calculate a moving average

Update an average when a new price arrives.

The idea

A moving average is an average that updates as new observations arrive.

For this example, keep the latest three prices. When a fourth arrives, drop the oldest one.

A small example

Latest three prices Average
10, 11, 12 11
11, 12, 13 12

The new price is 13. Drop 10, keep 11 and 12, and add 13.

Our signal is now 13 − 12 = 1.

Try it

The next price is 9. Which three prices belong in the window now? What is their average?

Check your answer

Keep 12, 13, and 9. Their average is 34 ÷ 3, or about 11.33.

The latest price is below that average, so the signal is negative.

Before three prices have arrived, record not enough data. Choosing what to do with missing information is part of defining the rule.