How do I calculate a simple moving average for stock prices?

To give a little context, this is for a programming assignment at school so the simplest form of proper moving average is all that's needed. I've searched around and seen the summation formulas and all that good stuff but I'm still a bit unclear on the algorithm.

Suppose I am current displaying 30 days worth of information for a stock (FDX), from 4/25/11 - 6/6/11. Naturally the x-axis represents some time interval and y-axis is my closing price. Now, I need to display a simple moving average. There is where I get a bit confused. To get the moving average for 4/25/11 do I just go back 30 days from that, sum those 30 days, divide by 30 and that will be my "close price" or "y point"? If so that means the moving average for 6/6 will include all data points up to 4/25 and also 4/21 (4/25 must have been a Monday)?

• This might be better suited at quant.stackexchange.com Jun 22 '11 at 13:16
• @Dheer You sure? The description for this site reads "Q&A for people who want to be financially literate", that seems appropriate. The description for Quant reads "Q&A for finance professionals and academics".
– Pete
Jun 22 '11 at 13:20

The 30 day moving average for a given day would be the sum of the close prices from the previous 30 days divided by 30.

So, for 4/25, you would sum the prices from 3/24 to 4/25 and divide by 30.

For 4/26, you would sum the prices from 3/25 to 4/26 and divide by 30.

For 4/27, you would sum the prices from 3/26 to 4/27 and divide by 30.

etc...

I see you are trying to write a program to do this. I'd post code here, but that probably wouldn't be appropriate. You might be better off asking over on stackoverflow.com if you need help with the implementation details.

The moving part of the moving average means that the period used to calculate it moves. For instance, if your data (D) is as follows, and you are doing a moving average (A) with a period of 3, this is what you will get:

D A

1 0

2 0

3 2

2 2.3

3 2.6

4 3

2 3

So, the average calculation ignores the previous average calculations - it is only based on the data.

When setting up a spreadsheet, just list the prices in a vertical column. In the next column choose the cell next to the Nth entry (where N is say 30, for your 30day average) then just calculate the average of the cells adjacent and prior N days. When you "fill down" the equation will shift the cells it uses to calculate always using the adjacent cell and prior N cells.

See Simple moving average on Wikipedia, it provides an equation that can be easily translated into code.

MyLib.getSimpleMovingAverage = function (closes, mvg, idx) {

for (n = 0; n < closes.length; n++) {
if (n > 0) {
if (closes[n] == null && closes[n - 1] != null) {
closes[n] = closes[n - 1]
}
}
}

if (idx > mvg) {

start = idx - mvg;
end = (idx + 1)
narr = closes.slice(start, end)
var total = narr.reduce(function (sum, value) { return sum + value }, 0);
return parseFloat(total / narr.length)

} else {
start = 0;
end = (idx + 1)
narr = closes.slice(start, end)
var total = narr.reduce(function (sum, value) { return sum + value }, 0);
return parseFloat(total / narr.length)
}
}
• Some context and explanation would help make sense of this answer - the OP is really asking for an explanation, not an code/pseudo-code. Oct 12 '17 at 11:43