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
- On your own file, add a 20-bar simple moving average and a 14-bar ATR.
- Set your chart's ATR smoothing to a simple average if it allows, and compare three values with the chart.
Quick check
1. Why are the first 19 values of a 20-bar average blank?
2. What does df["close"].shift(1) give each row?
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
Read the full notes
In A free add-on for Python that works with tables of data, such as a file of price bars, a bit like a spreadsheet you control with code.More in the glossary, a The average of the last few closing prices, worked out again on every candle and drawn as a line.More in the glossary is rolling(20).mean(), an EMA is ewm(span=20, adjust=False).mean(), and An indicator that averages recent candles’ true ranges, so it shows how far price has been moving in each candle, in the same units as price.More in the glossary is the biggest of three ranges averaged with rolling(14). The first rows are blank, and a chart may average differently.
- A moving average where every close in the window counts the same: add them up and divide by how many there are.More in the glossary: 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.
- A moving average that gives the newest closes more weight, so it reacts sooner than a simple one of the same length.More in the glossary: 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.
- An indicator that averages recent candles’ true ranges, so it shows how far price has been moving in each candle, in the same units as price.More in the glossary: prev_close = df["close"].shift(1); the three distances (high − low, |high − prev_close|, |low − prev_close|); .max(axis=1) gives the The largest of a candle’s high minus its low, its high minus the close before it, and the close before it minus its low. It counts gaps between candles.More in the glossary; .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 numbersTrue range by hand
Made-up numbers · high 5,012 · low 4,998 · close before 5,020
- High − low: 5,012 − 4,998 = 14
- High − close before: |5,012 − 5,020| = 8
- Low − close before: |4,998 − 5,020| = 22
- The biggest of the three is 22 One whole unit of a price. On the S&P 500, a move from 5,000 to 5,001 is one point, and each futures contract sets what a point is worth in dollars.More in the glossary.
The true range counts the gap from the close before, which the bar's own range misses.
Your turn
DrillMade-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.