Skip to content
Wildflower
Join free
Backtesting in Python (optional)

Free Trading School · Level 7 · Course 7.5

Calculating indicators

Lesson 6 of 15 in Backtesting in Python (optional)Module 2: Building a test

Advanced · Backtesting in Python

Calculating indicators

Wildflower Trading School · Lesson 7.5.2.1

Wildflower

Your rules may lean on an indicator or two. By the end of this lesson, you'll work out a simple moving average, an exponential one and average true range, in a few lines each. You'll know why the first values come out blank, and why your numbers might differ a little from your chart's.

1 of 8

What you'll learn

  • Add a moving average. Use rolling(20).mean() to average the last 20 closes.
  • Skip the warm-up. The first rows are blank until there are enough bars.
  • Build an EMA. Use ewm(span=20, adjust=False).mean(). The start of your data changes it.
  • Work out the average true range. Average the biggest of three ranges over 14 bars.
  • Compare with your chart. Charts may average differently. Check a few values by hand.

Exercise

  1. On your own file, add a 20-bar simple moving average and a 14-bar ATR.
  2. Set your chart's ATR smoothing to a simple average if it allows, and compare three values with the chart.

Next: Writing entry and exit rules.

Quick check

  1. 1. Why are the first 19 values of a 20-bar average blank?

  2. 2. What does df["close"].shift(1) give each row?

  3. 3. Your ATR differs a little from your chart's. What's one likely reason?

0 of 3 answered.

Finished this lesson?

Tick it off and keep your place

Make a free account and we'll remember where you are, so “Writing entry and exit rules” is waiting next time.

Read the full notes

In , a is rolling(20).mean(), an EMA is ewm(span=20, adjust=False).mean(), and is the biggest of three ranges averaged with rolling(14). The first rows are blank, and a chart may average differently.

  • : df["close"].rolling(20).mean(). Rolling makes a window of the last 20 rows, including this one. The first 19 rows are NaN (not a number): the warm-up, which a test skips. Not re-taught here: Simple moving average.
  • : df["close"].ewm(span=20, adjust=False).mean(). span=20 sets the usual weight for a 20-bar EMA; adjust=False builds it step by step. It depends on where the data starts. More: EMA vs SMA.
  • : prev_close = df["close"].shift(1); the three distances (high − low, |high − prev_close|, |low − prev_close|); .max(axis=1) gives the ; .rolling(14).mean() averages it (Average true range).
  • Numbers can differ from a chart: TradingView's ATR uses an average it calls RMA by default (switchable to simple); EMAs depend on the start; a different file gives different bars. Check a few values by hand.

Worked example

Made-up numbers

True range by hand

Made-up numbers · high 5,012 · low 4,998 · close before 5,020

  1. High − low: 5,012 − 4,998 = 14
  2. High − close before: |5,012 − 5,020| = 8
  3. Low − close before: |4,998 − 5,020| = 22
  4. The biggest of the three is 22 .

The true range counts the gap from the close before, which the bar's own range misses.

Your turn

Drill

Made-up numbers · high 5,030 · low 5,021 · close before 5,015

Trading is risky. You can lose money. Test results are hypothetical, and a test can't tell you what will happen next. This lesson explains how testing works. It isn't advice.