tsmovavg

Moving average

Syntax

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

Description

example

output = tsmovavg(tsobj,'s',lag,dim) returns the simple moving average 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,dim) 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%. (2/(timeperiod + 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,dim) 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,dim) 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,dim) 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

expand all

Compute Five Forms of Moving Averages Using a Financial Time Series Object

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 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')

Input Arguments

expand all

tsobj — Financial time series objectobject

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

lag — Number of previous data pointsnonnegative integer

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

vector — Set of observationsvector or matrix

Set of observations specified as a vector or matrix.

dim — dimension to operate alongpositive integer with value 1 or 2

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.

timeperiod — Length of time periodnonnegative integer

Length of time period specified as a nonnegative integer.

numperiod — Number of periods considerednonnegative integer

Number of periods considered specified as a nonnegative integer.

weights — Weights for each element in the moving windowvector of weights

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

Output Arguments

expand all

output — Moving average calculationvector or matrix

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 printing, McGraw-Hill, 1995, pp. 184-192.

See Also

|

Was this topic helpful?