tsmovavg

Moving average

tsmovavg is not recommended. Use timetable instead. For more information, see Convert Financial Time Series Objects fints to Timetables.

Description

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.

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.

weekly = toweekly(dis);
Warning: FINTS is not recommended. Use convert2weekly instead.
dates = (weekly.dates);
price = fts2mat(weekly.CLOSE);
Warning: FINTS is not recommended. Use TIMETABLE instead. For more information, see <a href="matlab:web(fullfile(docroot, 'finance/convert-from-fints-to-timetables.html'))">Convert Financial Time Series Objects (fints) to Timetables</a>.

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);
Warning: TSMOVAVG will be removed in a future release. Use MOVAVG instead.

Calculate the exponential weighted moving average moving average.

exp = tsmovavg(price,'e',window_size,1);
Warning: TSMOVAVG will be removed in a future release. Use MOVAVG instead.

Calculate the triangular moving average moving average.

tri = tsmovavg(price,'t',window_size,1);
Warning: TSMOVAVG will be removed in a future release. Use MOVAVG instead.

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);
Warning: TSMOVAVG will be removed in a future release. Use MOVAVG instead.

Calculate the modified moving average moving average.

modif = tsmovavg(price,'m',window_size,1);
Warning: TSMOVAVG will be removed in a future release. Use MOVAVG instead.

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

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 is 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.

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