## Plot buy and sell points in an Excel Chart based on two moving averages

This article demonstrates how to display buy and sell signals on an Excel chart based on two moving averages, the workbook lets you change how these moving averages are calculated. There is a get link below.

Above is an animated gif demonstrating a stock chart of S&P 500 with monthly prices. Two moving averages #1, #2, buy and sell points are plotted in this chart.

When the moving averages intersects a buy or sell point is created. If you use the value 7 in cell C1, moving average #1 uses the average of 7 months.

As you can see using a 15 month average (#1) returns better and fewer buy and sell points.

### Extracting buy and sell points

Not only is the chart refreshed as you type new values in cell C1 and C2, the data below the chart is also instantly refreshed.

Array formula in cell B25:

Array formula in cell C25:

Array formula in cell D25:

**How to create an array formula**

- Copy array formula above
- Select cell B25
- Paste formula to cell
- Make sure you see the prompt in the cell, double press with left mouse button on the cell if you don't.
- Press and hold Ctrl+ Shift simultaneously
- Press Enter once
- Release all keys

**How to copy array formula**

- Select cell B25
- Copy cell (not formula in formula bar)
- Select cell range B26:B50
- Paste

### Explaining formula in cell B25

#### Step 1 - Remove errors

The IFERROR function removes errors and returns an array containing values or nothing, errors are filtered out.

IFERROR(Calculation!$M$2:$M$247, "")

#### Step 2 - Check if cell is empty

The less than and greater than sign combined means "not equal to", in this case the logical operators make sure that the value is not equal to nothing. It returns boolean values TRUE or FALSE.

IFERROR(Calculation!$M$2:$M$247, "")<>""

#### Step 3 - Check column N as well

Actions described in step 1 and 2 are also applied to column N.

(IFERROR(Calculation!$N$2:$N$247, "")<>"")

#### Step 4 - If cells are not empty return the corresponding relative row number

The + sign between the logical expressions performs an OR calculation meaning if the cell in column M is not equal to nothing OR if the cell in column N is not equal to nothing then return TRUE, both logical expressions must return FALSE in order to return a FALSE.

(IFERROR(Calculation!$M$2:$M$247, "")<>"")+(IFERROR(Calculation!$N$2:$N$247, "")<>"")

The IF function then returns the second argument if the logical expression returns TRUE.

IF((IFERROR(Calculation!$M$2:$M$247, "")<>"")+(IFERROR(Calculation!$N$2:$N$247, "")<>""), MATCH(ROW(Calculation!$M$2:$M$247), ROW(Calculation!$M$2:$M$247)), "")

The second argument is the corresponding row number and the third argument is nothing.

MATCH(ROW(Calculation!$M$2:$M$247), ROW(Calculation!$M$2:$M$247)

#### Step 5 - Extract the k-th largest row number

The LARGE function extracts the k-th largest value in the array. LARGE( array, k)

LARGE(IF((IFERROR(Calculation!$M$2:$M$247, "")<>"")+(IFERROR(Calculation!$N$2:$N$247, "")<>""), MATCH(ROW(Calculation!$M$2:$M$247), ROW(Calculation!$M$2:$M$247)), ""), ROW(A1))

Argument k is a relative cell reference that changes when the cell is copied, this makes it possible to extract different values.

#### Step 6 - Return value from column D based on row number

The INDEX function returns a value from a cell range based on a row and column number.

INDEX(Calculation!$D$2:$D$247, LARGE(IF((IFERROR(Calculation!$M$2:$M$247, "")<>"")+(IFERROR(Calculation!$N$2:$N$247, "")<>""), MATCH(ROW(Calculation!$M$2:$M$247), ROW(Calculation!$M$2:$M$247)), ""), ROW(A1)))

### Calculating buy and sell points

Sheet: Calculation

**Array formula in cell K2:**

**Array formula in cell L2:**

**Formula in cell M2:**

**Formula in cell N2:**

Copy cells and paste down as far as needed.

The calculation sheet imports historical prices using this user defined function:

Excel udf: Import historical stock prices from yahoo â€“ added features

### Further reading

If you want to know how to **plot buy and sell points in an excel stock chart**, read this post:

Add buy and sell points to a stock chart

### Stock market trend category

The image above shows an Excel chart of the S&P 500 with buy and sell signals based on a 50 […]

In my previous post, I described how to build a dynamic stock chart that lets you easily adjust the date […]

If you study a stock chart you will discover that sometimes significant trend reversals happen when a stock chart […]

This time I want to demonstrate an alternative way to identify a majorÂ trend in the stock market. The previous post […]

## Functions in this article

More than 1300 Excel formulas

## Excel formula categories

## Excel categories

### 4 Responses to “Plot buy and sell points in an Excel Chart based on two moving averages”

### 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

Hi Oscar,

This post is awesome! But when I tired to play with the attached Excel sheet, I got a blank plot on "Overview tab" with #DIV/0! displayed under the columns of ave, buy and sells on "Calculation tab".

The Excel version I'm using is 2007 and I've tried quite long time to get it work in vain. Really appreciate if you can point out what could be the issues. (I thought it could be caused by the fact that the AVERAGE function is trying to work on the retrieved stock price in "text/date" format while "number" format is actually required. However, your post shows the screen capture of the Excel in action. Unless the version I got following the above link is different all I must have missed something important. Keep wondering ...)

Thanks!

Smith

Thanks for letting me know. I have uploaded a new file:

excel-stock-chart-two-moving-averagesv2.xlsm

See above.

Your site is wonderful and educational. I wanted to thank you for your generosity. I'm new at Excel and Investing so it's going to take some time mastering VBA. All the programming I've done is VB6 and there is a lot of overlap.

Thanx

Warren

Hi There, is there a chance you could update using STOCKHISTORY as Yahoo unavailble