# movavg

Moving average of a financial time series

## Syntax

## Description

computes the moving average (MA) of a financial time series.`ma`

= movavg(`Data`

,`type`

,`windowSize`

)

adds an optional argument for `ma`

= movavg(___,`initialPoints`

)`initialPoints`

.

## Examples

### Calculate the Moving Average for a Data Series

Load the file `SimulatedStock.mat`

and compute the moving average using simulated closing price data.

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

`ma = `*1000×1*
100.2500
100.3433
100.8700
100.4916
99.9937
99.3603
98.8769
98.6364
98.4348
97.8491
⋮

### Calculate the Leading and Lagging Moving Averages for a Data Series

Load the file `SimulatedStock.mat`

and compute the moving average using simulated closing price data.

load SimulatedStock.mat type = 'linear'; mashort_term=movavg(TMW_CLOSE,type,20) % Short-term moving average

`mashort_term = `*1000×1*
100.2500
100.3423
100.8574
100.4943
100.0198
99.4230
98.9728
98.7509
98.5688
98.0554
⋮

`malong_term=movavg(TMW_CLOSE,type,3) % Long-term moving average`

`malong_term = `*1000×1*
100.2500
100.3580
101.0900
100.4300
99.3183
97.8217
97.0833
97.1950
97.4133
96.1133
⋮

Plot the long-term and short-term moving averages.

plot(TMW_CLOSE(1:100)) hold on plot(malong_term(1:100)) plot(mashort_term(1:100)) hold off legend('Actual','Long-term','Short-term')

## Input Arguments

`Data`

— Data for a financial series

matrix | table | timetable

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`

— Type of moving average to compute

character vector with value of `'simple'`

,
`'square-root'`

, `'linear'`

,
`'square'`

, `'exponential'`

,
`'triangular'`

, `'modified'`

, or
`'custom'`

| string with value of `"simple"`

,
`"square-root"`

, `"linear"`

,
`"square"`

, `"exponential"`

,
`"triangular"`

, `"modified"`

, or
`"custom"`

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

`"simple"`

— The simple moving average (SMA) is calculated by summing up a specified number of data points and dividing the sum by the number of data points. The SMA gives equal importance to each data point within the specified period. The resulting value represents the average price over the specified period.`"square-root"`

— In the square root method, the weights assigned to each data point decrease in a square root fashion as the data points move further back in time. The most recent data point has the highest weight, and the weights decrease progressively for older data points.`"linear"`

— The linear method assigns weights to each data point that decrease in a linear fashion as the data points move further back in time. This method can be useful for capturing trends and price movements while minimizing the impact of outliers or extreme values.`"square"`

— The square method assigns weights to each data point that decrease in a square fashion as the data points move further back in time. The most recent data point has the highest weight, and the weights decrease progressively for older data points.`"exponential"`

— An exponential moving average (EMA) places a greater weight and significance on the most recent data points. The exponential moving average is also referred to as the exponentially weighted moving average. An exponentially weighted moving average reacts more significantly to recent price changes than a simple moving average simple moving average (SMA), which applies an equal weight to all observations in the period.`"triangular"`

— The triangular moving average (TMA) is a variation of the SMA that assigns weights to the data points in a triangular pattern. This method aims to give more weight to the data points in the middle of the moving average period and less weight to the data points at the beginning and end of the period.`"modified"`

— A modified moving average (MMA) is an indicator that shows the average value of a security's price over a period of time. It is also known as the running moving average (RMA) or smoothed moving average (SMMA). Modified moving averages are similar to simple moving averages, but all subsequent points are calculated by first adding the new price and then subtracting the last average from the resulting sum.`"custom"`

— A moving average using a custom method is a type of moving average that uses a custom measure of`weights`

to calculate the average, instead of just taking the close price of each bar as most other moving averages do. The custom measure calculates the difference between the lowest lows and highest highs over different data points.

**Data Types: **`char`

| `string`

`windowSize`

— Number of observations of the input series to include in moving average

positive integer

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.

**Note**

The `windowSize`

argument applies only to moving
averages whose `type`

is
`'simple'`

, `'square-root'`

,
`'linear'`

, `'square'`

,
`'exponential'`

,
`'triangular'`

, or
`'modified'`

.

**Data Types: **`double`

`weights`

— Custom weights used to compute moving average

vector

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

**Note**

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`

`initialPoints`

— Indicates how moving average is calculated at initial points

`'shrink'`

(default) | character vector with values of `'shrink'`

,
`'fill'`

, or `'zero'`

| string with values of `"shrink"`

,
`"fill"`

, or `"zero"`

(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`NaN`

s

**Note**

The `initialPoints`

argument applies to all
`type`

specifications except for the
`'exponential'`

and `'modified'`

options.

**Data Types: **`char`

| `string`

## Output Arguments

`ma`

— Moving average series

matrix | table | timetable

Moving average series, returned with the same number of rows
(`M`

) and the same type (matrix, table, or timetable)
as the input `Data`

.

## References

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

## Version History

**Introduced before R2006a**

### R2023a: `fints`

support removed for `Data`

input argument

`fints`

object support for the `Data`

input
argument is removed.

### R2018a: `Lead`

and `Lag`

input arguments not supported

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`

.

## MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)