Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

tsmovavg

Moving average

tsmovavg calculates the simple, exponential, triangular, weighted, and modified moving average of a vector or fints object of data. For information on working with financial time series (fints objects) data, see Working with Financial Time Series Objects.

Syntax

  • output = tsmovavg(tsobj,'s',lag)
    example
  • output = tsmovavg(vector,'s',lag,dim)
  • output = tsmovavg(tsobj,'e',timeperiod)
    example
  • output = tsmovavg(vector,'e',timeperiod,dim)
  • output = tsmovavg(tsobj,'t',numperiod)
    example
  • output = tsmovavg(vector,'t',numperiod,dim)
  • output = tsmovavg(tsobj,'w',weights)
    example
  • output = tsmovavg(vector,'w',weights,dim)
  • output = tsmovavg(tsobj,'m',numperiod)
    example
  • output = tsmovavg(vector,'m',numperiod,dim)

Description

example

output = tsmovavg(tsobj,'s',lag) returns the simple moving average by for financial time series object, tsobj. lag indicates the number of previous data points used with the current data point when calculating the moving average.

output = tsmovavg(vector,'s',lag,dim) returns the simple moving average for a vector. lag indicates the number of previous data points used with the current data point when calculating the moving average.

example

output = tsmovavg(tsobj,'e',timeperiod) returns the exponential weighted moving average for financial time series object, tsobj. The exponential moving average is a weighted moving average, where timeperiod specifies the time period. Exponential moving averages reduce the lag by applying more weight to recent prices. For example, a 10-period exponential moving average weights the most recent price by 18.18%. Exponential Percentage = 2/(TIMEPER + 1) or 2/(WINDOW_SIZE + 1).

output = tsmovavg(vector,'e',timeperiod,dim) returns the exponential weighted moving average for a vector. The exponential moving average is a weighted moving average, where timeperiod specifies the time period. Exponential moving averages reduce the lag by applying more weight to recent prices. For example, a 10-period exponential moving average weights the most recent price by 18.18%. (2/(timeperiod + 1)).

example

output = tsmovavg(tsobj,'t',numperiod) returns the triangular moving average for financial time series object, tsobj. The triangular moving average double-smooths the data. tsmovavg calculates the first simple moving average with window width of ceil(numperiod + 1)/2. Then it calculates a second simple moving average on the first moving average with the same window size.

output = tsmovavg(vector,'t',numperiod,dim) returns the triangular moving average for a vector. The triangular moving average double-smooths the data. tsmovavg calculates the first simple moving average with window width of ceil(numperiod + 1)/2. Then it calculates a second simple moving average on the first moving average with the same window size.

example

output = tsmovavg(tsobj,'w',weights) returns the weighted moving average for the financial time series object, tsobj, by supplying weights for each element in the moving window. The length of the weight vector determines the size of the window. If larger weight factors are used for more recent prices and smaller factors for previous prices, the trend is more responsive to recent changes.

output = tsmovavg(vector,'w',weights,dim) returns the weighted moving average for the vector by supplying weights for each element in the moving window. The length of the weight vector determines the size of the window. If larger weight factors are used for more recent prices and smaller factors for previous prices, the trend is more responsive to recent changes.

example

output = tsmovavg(tsobj,'m',numperiod) returns the modified moving average for the financial time series object, tsobj. The modified moving average is similar to the simple moving average. Consider the argument numperiod to be the lag of the simple moving average. The first modified moving average is calculated like a simple moving average. Subsequent values are calculated by adding the new price and subtracting the last average from the resulting sum.

output = tsmovavg(vector,'m',numperiod,dim) returns the modified moving average for the vector. The modified moving average is similar to the simple moving average. Consider the argument numperiod to be the lag of the simple moving average. The first modified moving average is calculated like a simple moving average. Subsequent values are calculated by adding the new price and subtracting the last average from the resulting sum.

Examples

collapse all

Load the financial time series object, dis for Disney stock and look at the weekly data for this time series.

load disney.mat
weekly = toweekly(dis);
dates = (weekly.dates);
price = fts2mat(weekly.CLOSE);

Set the|lag| input argument for the window size for the moving average.

window_size = 12;

Calculate the simple moving average.

simple = tsmovavg(price,'s',window_size,1);

Calculate the exponential weighted moving average moving average.

exp = tsmovavg(price,'e',window_size,1);

Calculate the triangular moving average moving average.

tri = tsmovavg(price,'t',window_size,1);

Calculate the weighted moving average moving average.

semi_gaussian = [0.026 0.045 0.071 0.1 0.12 0.138];
semi_gaussian = [semi_gaussian fliplr(semi_gaussian)];
weighted = tsmovavg(price,'w',semi_gaussian,1);

Calculate the modified moving average moving average.

modif = tsmovavg(price,'m',window_size,1);

Plot the results for the five moving average calculations for Disney stock.

plot(dates,price,...
    dates,simple,...
    dates,exp,...
    dates,tri,...
    dates,weighted,...
    dates,modif)
datetick
legend('Stock Price','Simple','Exponential','Triangular','Weighted',...
    'Modified','Location','NorthWest')
title('Disney Weekly Price & Moving Averages')

Related Examples

Input Arguments

collapse all

Financial time series object specified using a time series object created using fints.

lag is the parameter indicating the number of previous data points to be used in conjunction with the current data point when calculating the simple moving average.

Number of previous data points specified as a nonnegative integer. Lag indicates the window size or number of periods of the moving average.

Set of observations specified as a vector or matrix.

Dimension to operate along, specified as a positive integer with a value of 1 or 2. dim is an optional input argument, and if it is not included as an input, the default value 2 is assumed. The default of dim = 2 indicates a row-oriented matrix, where each row is a variable and each column is an observation.

If dim = 1, the input is assumed to be a column vector or column-oriented matrix, where each column is a variable and each row an observation.

Exponential moving average is a weighted moving average, where timeperiod is the time period of the exponential moving average. Exponential moving averages reduce the lag by applying more weight to recent prices. For example, a 10 period exponential moving average weights the most recent price by 18.18%.

Exponential Percentage = 2/(TIMEPER + 1) or 2/(WINDOW_SIZE + 1)

Length of time period specified as a nonnegative integer.

Triangular moving average is a double-smoothing of the data. The first simple moving average is calculated with a window width of ceil(numperiod + 1)/2. Then a second simple moving average is calculated on the first moving average with the same window size.

The modified moving average is similar to the simple moving average. Consider the argument numperiod to be the lag of the simple moving average. The first modified moving average is calculated like a simple moving average. Subsequent values are calculated by adding the new price and subtracting the last average from the resulting sum.

Number of periods considered specified as a nonnegative integer.

A weighted moving average is calculated with a weight vector, weights. The length of the weight vector determines the size of the window. If larger weight factors are used for more recent prices and smaller factors for previous prices, the trend will be more responsive to recent changes.

Weights for each element in the window specified as a vector of weights.

Output Arguments

collapse all

Moving average calculation returned as a vector or matrix. The output returned from tsmovavg is identical in format to the input.

References

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

Introduced before R2006a

Was this topic helpful?