← All lessons

Moving-average systems

Build and test a crossover rule · 8 min

ProGoal: Build and test a crossover ruleBefore this: BreakoutsFurther reading: Clenow, Following the Trend
A range market with a 10-day and a 40-day average
7986940199
  • Close
  • 10-day average
  • 40-day average

Every crossing of the two lines would be a trade for a crossover rule.

Predict first

A rule is long when the 10-day average is above the 40-day average and short when it is below. On a market that moves sideways like the one above, what happens most often?

A moving average is the mean of the last n closes. A crossover rule compares a fast average with a slow one: long while the fast is above, short (or flat) while it is below. It is a smoothed way of asking whether recent prices are higher than older ones.

SMA(n) = (C₁ + C₂ + … + Cₙ) ÷ n
position = +1 if SMA(fast) > SMA(slow), −1 if below

The price of smoothness is lag. On a steady trend an n-day average sits (n − 1) ÷ 2 days behind price, so a 200-day average reacts to a turn about 100 days late. Faster averages react sooner but switch more often on noise. There is no setting that is both quick and quiet: you choose where on that trade-off to sit, and in a range every setting pays for whipsaws.

Try it: fast and slow averages, lag and whipsaws
1518210329
  • Close
  • 20-day average
  • 100-day average
0.18×1.09×2.00×01411
  • Crossover system
  • Buy and hold
Sharpe ratio0.59
Return a year8.4%
Volatility15.9%
Max drawdown-34.2%
Switches a year2.9
Winners29%

Generated markets, not real data. They show how a rule depends on the market it meets; they do not predict how real markets behave.

Worked calculation

Signal and lag from two averages

Last five closes
50, 51, 53, 52, 54
5-day average
(50 + 51 + 53 + 52 + 54) ÷ 5 = 52.0
3-day average
(53 + 52 + 54) ÷ 3 = 53.0
Fast (3) above slow (5)?
53.0 > 52.0: long
Lag of a 20-day average on a +0.5 a day trend
(20 − 1) ÷ 2 × 0.5 = 4.75 below price
Check yourself
3 questions

Price has risen a steady 1 point a day for months. About how far below price does its 41-day average sit?

In a range a crossover switches 12 times a year. Each switch costs 0.1% in trading costs and loses another 0.5% on average from buying high and selling low. What is the yearly drag?

The 20-day average is 101.4 and the 100-day average is 103.0. What does a long-short crossover rule hold?

Missed questions come back for review after one day, then three, then a week.

Practice

The highs of the last 5 days were 43.23, 45.59, 46.82, 45.68, 47.08. A 5-day breakout rule buys when the close is above the highest of them. At what price level does it buy?

Hint: The channel top is the highest high of the previous days (today is not included).

More in Drills →

Closing round

Three years of a fresh generated market are on screen; the next two years do not exist yet. Tune the rule on what you can see, commit to two predictions, then generate the future.

0.91×1.43×1.95×0625
  • Your rule so far
Sharpe before split2.22
Return before split95.2%
Max drawdown before-11.9%
Dickey–Fuller t (log price)-1.19

Over the next two years, after costs, this rule will…

What kind of market is this?

Generated markets, not real data. They show how a rule depends on the market it meets; they do not predict how real markets behave.

Want to feel it candle by candle? Feel the whipsaw in the choppy-range market →