Moving average (MAV#)

Moving average (MAV#)

This calculates a series of moving averages for a data series.

Format

MAV#(Expression,Period,Parameter)

where:

Period is the time period for your moving average calculations and is in the standard time period format, for example 3M for a rolling 3 month period.

Parameter is an optional parameter (C, I or R), which determines how null values in a time series are handled. There are three ways of dealing with null values:

*NB – the I and R parameters calculate values using the default padding setting of the underlying series/expression being averaged. Some series are padded by default, for instance Price (P), whereas others are unpadded by default, for instance Volume (VO).  

When using the I and R parameters therefore, if you wish to unpad the default values for the purposes of excluding them from the moving average calculation, attach #S as a suffix to the datatype, for instance P#S will unpad the price datatype.

Examples

MAV#(MKS(VO),30D)

This calculates the average daily volume traded for Marks and Spencer shares over a rolling time period of 30 weekdays. The Volume (VO) datatype is unpadded by default (in order to avoid giving the impression that a stock traded on a particular day when in fact it did not) and no parameter is specified, so any null values within each 30 day period in the underlying series generate a null value in the moving average series. This is why long periods of null values may be seen in the output.

MAV#(MKS(VO),30D,I)

In this example, the I parameter ignores any null values within each 30 day period, so less than 30 observations may be used in the calculation. The moving average appears as a continuous series, with values calculated for every day within the requested period.

MAV#(MKS(VO),30D,R)

This calculates the rolling 30 day moving average, using a total of 30 real values. Null values are excluded, but unlike the I parameter, the calculation uses the latest 30 real values rather than reducing the number of observations used.

MAV#(MKS(P),30D)

This expression calculates the average daily closing share price for Marks and Spencer over a rolling 30 day time period. The Price (P) datatype is padded by default (in order that the price is continuous when charted for example) so the average calculation is based on 30 days of values, including those which are padded.

MAV#(MKS(P#S),30D,I)

If you wish to use unpadded prices in the average calculation, this example uses the #S suffix and I parameter to ensure that this is the case. Each calculated value uses a 30 day time period, but excludes days where the stock did not trade – so e.g. if the stock did not trade on two weekdays within the period, only 28 values would be used for the calculation of the average.

Note

You must use a moving average period that is consistent with the frequency of the stored data. For example:

MAV#(UKRP....F,12W)

is invalid because the series for UK inflation is held monthly. (Most price and index series are daily, while company accounts items are yearly.)

See also

Average functions