This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.


Moving average of a financial time series

movavg is updated to accept data input as a matrix, table, or timetable.

The syntax for movavg has changed. There is no longer support for the input arguments Lead and Lag, only a single windowSize is supported, and there is only one output argument (ma). If you want to compute the leading and lagging moving averages, you need to run movavg twice and adjust the windowSize.


ma = movavg(Data,type,windowSize)
ma = movavg(___,Initialpoints)
ma = movavg(Data,type,weights)
ma = movavg(___,Initialpoints)



ma = movavg(Data,type,windowSize) computes the moving average (MA) of a financial time series.


ma = movavg(___,Initialpoints) adds an optional argument for Initialpoints.


ma = movavg(Data,type,weights) computes the moving average (MA) of a financial time series using a 'custom' type and weights.


ma = movavg(___,Initialpoints) adds an optional argument for Initialpoints.


collapse all

Load the file SimulatedStock.mat, which provides a timetable (TMW) for financial data.

load SimulatedStock.mat
type = 'linear';
windowSize = 14;
ma = movavg(TMW_CLOSE,type,windowSize)
ma = 1000×1


Input Arguments

collapse all

Data for a financial series, specified as a column-oriented matrix, table, or timetable. Timetables and tables must contain variables with only a numeric type.

Data Types: double | table | timetable

Type of moving average to compute, specified as a character vector or string with an associated value.

Data Types: char | string

Number of observations of the input series to include in moving average, specified as a scalar positive integer. The observations include (windowSize - 1) previous data points and the current data point.


The windowSize argument applies only to moving averages whose type is 'simple', 'square-root', 'linear', 'square', 'exponential', 'triangular', or 'modified'.

Data Types: double

Custom weights used to compute the moving average, specified as a vector.


The length of weights (N) determines the size of the moving average window (windowSize). The weights argument applies only to a 'custom' type of moving average.

To compute moving average with custom weights, the weights (w) are first normalized such that they sum to one:

W(i) = w(i)/sum(w), for i = 1,2,...,N

The normalized weights (W) are then used to form the N-point weighted moving average (y) of the input Data (x):

y(t) = W(1)*x(t) + W(2)*x(t-1) + ... + W(N)*x(t-N)

The initial moving average values within the window size are then adjusted according to the method specified in the name-value pair argument Initialpoints.

Data Types: double

(Optional) Indicates how the moving average is calculated at initial points (before there is enough data to fill the window), specified as a character vector or string using one of the following values:

  • 'shrink' - Initializes the moving average such that the initial points include only observed data

  • 'zero' - Initializes the initial points with 0

  • 'fill' - Fills initial points with NaNs


The Initialpoints argument applies to all type specifications except for the 'exponential' and 'modified' options.

Data Types: char | string

Output Arguments

collapse all

Moving average series, returned with the same number of rows (M) and the same type (matrix, table, or timetable) as the input Data.


[1] Achelis, S. B. Technical Analysis from A to Z. Second Edition. McGraw-Hill, 1995, pp. 184–192.

Introduced before R2006a