SYSTEM DESIGN


Simplifying Your Life

Moving Average Computations, Made Easier

by John F. Ehlers


Presenting programming tricks to simplify coding indicators and strategies.

A simple moving average of length N is computed by adding N values and dividing the sum by N. The process is repeated on a bar-by-bar basis. What could be easier? While conceptually simple, coding for long moving averages can quickly become wearisome because there are so many terms.

This tedium can be reduced by putting the summation in a loop, but looping is difficult to achieve in some applications, such as Excel. Another simplifying approach is to drop off the oldest value and add a new value to the moving average. However, this requires computing the initial value of the long moving average at least once. Here are two ways to compute the simple moving average (SMA) with ease.

In Z-transform notation, a unit delay is represented by Z-1. The transfer response is the output of the filter divided by its input. Thus, the transfer response of an eight-bar simple moving average would be written as:

(Equation 1)
H(z) = (1 + Z-1 + Z-2 + Z-3 + Z-4 + Z-5 + Z-6 + Z-7)/8

  ...Continued in the December 2003 issue of Technical Analysis of STOCKS & COMMODITIES


Excerpted from an article originally published in the December 2003 issue of Technical Analysis of STOCKS & COMMODITIES magazine. All rights reserved. © Copyright 2003, Technical Analysis, Inc.



Return to December 2003 Contents