Moving Average

In statistics, a Moving Average is used to analyze a set of data points by creating a series of averages of different subsets of the full data set.

Given a data series and fixed subset size(N), the first element of Moving Average is obtained by averaging the initial fixed subset (first N elements) of number series. Then the subset is move forward by 1 element, removing the first element and adding the N+1th element of the subset, and average is calculated for the 2nd element of the Moving average. And the process is repeated over the entire data series.

Following graph depicts the Moving Average of Google Weekly closing price for 8 years for window size 10 & 20.

MovingAverage

Moving Average is used commonly for analyzing the short term fluctuations from a time series data and highlighting the long term trends. It can be easily seen in the above graph that the line MA(20) is smoothing out a lot of fluctuation as compared to the MA(10).

Moving averages help smooth price action and filter out the noise. They also form the building blocks for many other technical indicators and overlays, such as the McClellan Oscillator and Bollinger Bands.

Types of Moving Average :

Simple moving average (SMA) and Exponential moving average (EMA) are the most popular type of Moving Average.

Moving Average Excel

Cumulative Moving Average

A cumulative moving average, computes the running averages of an ordered datum stream.

For example, an investor may want the average price of all of the stock transactions for a particular stock up until the current time. As each new transaction occurs, the average price at the time of the transaction can be calculated for all of the transactions up to that point using the cumulative average, typically an equally weighted average of the sequence of i values x1, …, xi up to the current time:

CA_i = {{x_1 + \cdots + x_i} \over i}\,.

The new cumulative average when a new value xi+1 arrives can be calculated using the formula:

CA_{i+1} = {(x_{i+1} + iCA_i) \over {i+1}} = {CA_i} + {{x_{i+1} - CA_i} \over {i+1}}\,.

Following is an example of cumulative moving average of  Google prices.

CMA

Cumulative average in q

KDB provides a built in function avgs (running averages) to calculate the cumulative averages.

q)avgs 17 23 44 12 2 36 37 44 28 20
17 20 28 24 19.6 22.33333 24.42857 26.875 27 26.3

If you are Sachin Tendulkar fan then you would be interested to see how he managed to keep CMA of his ODI runs near to 40.
SachinODI_CMA