Follow stock market trends – Moving Average
In my previous post, I described how to build a dynamic stock chart that lets you easily adjust the date range and change index/company. Price data is quickly and automatically fetched from yahoo finance.
This post shows you how to add tools to the charts so you can quickly identify the trend for a stock or index. The stock market often trends for many months up or down and a moving average smooths out price data.
The examples shown in this post are based on S&P 500 index and larger trends. Charts show price data on a monthly scale and the date range is from 1995 to the present time.
This Excel stock chart has a 10-month moving average. It can be calculated using the AVERAGE function on an excel worksheet.
What's on this page
1. Calculate moving average
Formula in cell K23:
This function calculates the average from 10 latest closing prices. Copy cell K23 and paste to cells below as far as needed.
2. How to add a moving average to an Excel stock chart
- Press with right mouse button on on chart
- Press with mouse on "Select Data..."
- Press with left mouse button on "Add" button
- Select cell range $K$23:$K$272
- Press with left mouse button on OK button
- Go to tab "Layout" on the ribbon.
- Select "Series 4"
- Press with left mouse button on "Format Selection" button, see picture above.
- Select "Secondary Axis"
- Go to "Line Color"
- Select "Solid Line"
- Pick a color
The chart now looks like this:
The data on the secondary axis has to be reversed.
- Select line on chart
- Go to tab "Layout" on the ribbon
- Press with left mouse button on "Axis" button and then "Secondary horizontal axis" and finally press with left mouse button on "Show right to left axis"
- Select and delete the horizontal axis above the chart
- Go to tab "Layout" on the ribbon
- Press with left mouse button on "Axis" button and "Secondary Vertical Axis" and finally "None"
The chart looks like this:
A moving average indicates if a market is about to go up or down in the long term.
3. Plot moving average turning points
Let's start with calculating when the moving average changes from moving down to up.
Buy formula in cell L23:
If the value in cell K23 is larger than cell K24 AND cell K24 is smaller than K25 THEN return the closing price. If not return nothing.
Read more about IF function.
The next formula calculates when the moving average changes from going up to going down.
Sell formula in cell M23:
Copy cell range L23:M23 and paste to cells below as far as needed. It is now time to plot these moving average turning points.
- Press with right mouse button on on chart
- Press with left mouse button on "Select Data..."
- Press with left mouse button on "Add" button
- Select cell range $L$23:$L$273
- Press with left mouse button on OK button
- Press with left mouse button on "Add" button again
- Select cell range $M$23:$M$273
- Press with left mouse button on OK button
- Press with left mouse button on OK button
Here is how to remove lines shown above and use markers instead.
- Go to tab "Layout"
- Select Series 5
- Press with left mouse button on "Format Selection"
- Select "Marker Options"
- Select "Built-in"
- Pick a type
- Go to "Line Color"
- Select "No Line"
- Select "Series 6" on tab "Layout" on the ribbon
- Select "Marker Options"
- Select "Built-in"
- Pick a type
- Go to "Line Color"
- Select "No Line"
4. How to extract the moving average turning dates using Excel array formulas
Array formula (Buy) in cell P23:
Formula (Buy Date) in cell O23:
Array formula (Sell) in cell S23:
Formula (Sell Date) in cell R23:
4.1 How to enter an array formula
- Select cell P23
- Copy array formula
- Paste array formula in the formula bar
- Press and hold CTRL + SHIFT simultaneously
- Press Enter once
- Release all keys
If you did the above instructions correctly, the formula begins and ends with a curly bracket, like this {=formula}. Don´t enter these characters yourself.
Make sure you enter array formulas in cell P23 and S23. Then copy cell P23 and paste to cells below as far as needed. Repeat with cell S23, R23 and O23.
4.2 Explaining formula in cell P23
Step 1 - Check if each value in $K$23:$K$262 is larger than the next cell value below
The less than sign checks if the numerical value in a cell is less than the number in the next cell below.
$K$23:$K$262<$K$24:$K$263
The less than sign is a logical operator and the result is a boolean value, TRUE or FALSE.
Step 2 - Check if each value in $K$24:$K$263 is larger than the next cell value below
The larger than sign checks if the numerical value in a cell is larger than the number in the next cell below.
$K$24:$K$263>$K$25:$K$264
The larger than sign is also a logical operator and the result is a boolean value, TRUE or FALSE.
Step 3 - Multiply arrays
When we multiply arrays using the asterisk sign we apply AND-logic meaning TRUE is returned only if TRUE is present in the same position in both arrays.
TRUE * TRUE = TRUE
TRUE * FALSE = FALSE
FALSE * TRUE = FALSE
FALSE * FALSE = FALSE
($K$23:$K$262>$K$24:$K$263)*($K$24:$K$263<$K$25:$K$264)
The numerical equivalent is returned when two boolean values are multiplied.
TRUE = 1
FALSE = 0 (zero)
Step 4 - Create sequence from 1 to n
The ROW function returns a number representing the row based on a cell reference, for example, ROW(A1) returns 1.
This works also if we use a cell range as a cell reference, however, the formula is now an array formula. It calculates an array of values.
MATCH(ROW($G$23:$G$262), ROW($G$23:$G$262))
The MATCH function returns the relative position of a given value.
MATCH({23; 24; 25; ... ; 262}, {23; 24; 25; ... ; 262})
and returns {1; 2; 3; ... }.
Step 5 - Returns row number if logical expression is TRUE
The IF function returns one value if the logical test is TRUE and another value if the logical test is FALSE.
IF(logical_test, [value_if_true], [value_if_false])
IF(($K$23:$K$262>$K$24:$K$263)*($K$24:$K$263<$K$25:$K$264), MATCH(ROW($G$23:$G$262), ROW($G$23:$G$262)), "")
Step 6 - Extract the k-th smallest row number
The SMALL function returns the k-th smallest value from a group of numbers.
SMALL(array, k)
The ROWS function returns the number of rows based on a cell reference.
ROWS(array)
SMALL(IF(($K$23:$K$262>$K$24:$K$263)*($K$24:$K$263<$K$25:$K$264), MATCH(ROW($G$23:$G$262), ROW($G$23:$G$262)), ""), ROWS($A$1:A1)))
Step 7 - Get corresponding value from column G
The INDEX function returns a value from a cell range, you specify which value based on a row and column number.
INDEX(array, [row_num], [column_num])
INDEX($G$23:$G$262, SMALL(IF(($K$23:$K$262<$K$24:$K$263)*($K$24:$K$263>$K$25:$K$264), MATCH(ROW($G$23:$G$262), ROW($G$23:$G$262)), ""), ROWS($A$1:A1)))
Step 8 - Handle errors
The IFERROR function lets you catch most errors in Excel formulas.
IFERROR(value, value_if_error)
IFERROR(INDEX($G$23:$G$262, SMALL(IF(($K$23:$K$262>$K$24:$K$263)*($K$24:$K$263<$K$25:$K$264), MATCH(ROW($G$23:$G$262), ROW($G$23:$G$262)), ""), ROWS($A$1:A1))), "")
This template has dynamic named ranges for all chart series. This lets you change the date range and all chart data is adjusted automatically. I have not described how they work in this post, if you are curious
The template also contains a small custom function to fetch stock data from yahoo finance.
Tip! - Add data labels
Stock market trend category
This article demonstrates how to display buy and sell signals on an Excel chart based on two moving averages, the […]
The image above shows an Excel chart of the S&P 500 with buy and sell signals based on a 50 […]
If you study a stock chart you will discover that sometimes significant trend reversals happen when a stock chart […]
Excel categories
One Response to “Follow stock market trends – Moving Average”
Leave a Reply
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
Contact Oscar
You can contact me through this contact form
[…] ← Previous post - […]