Processing math: 100%

The Moving Average Prices

The moving average prices can be calculated for a streaming time series of prices pi.

The Simple Moving Average (SMA) price pSMAi is the average of the past prices pij over a look-back window of length L:

pSMAi=1LL1j=0pij The parameter L is equal to the length of the look-back window.

The drawback of the SMA is that it applies the same weight to the recent and past prices. Its calculation also requires maintaining a buffer (queue) of past prices, with the length equal to the parameter L.


The Exponential Moving Average (EMA) price pEMAi is calculated using a decay factor λ and the recursive formula:

pEMAi=λpEMAi1+(1λ)pi

The above recursive formula can be expressed as a series:

pEMAi=(1λ)j=0λjpij=(1λ)(pi+λpi1+λ2pi2+) The formula is called exponential because the decay factor λ is raised to the power of j, so the weights λj decrease exponentially as j increases.

The advantage of the EMA is that it applies a greater weight to more recent prices than to past ones.

And it doesn’t forget the past prices completely - it only applies a smaller weight to them.
The EMA calculation is also faster because there’s less computation, and it doesn’t require maintaining a buffer (queue) of past prices.

The SMA and EMA averages are not the same, but they are related to each other, and they move in sympathy with each other.

The decay factor λ determines the persistence of data memory, i.e. how quickly the contribution of the past data to the average value fades over time.

If the decay factor λ is closer to 1 (one) then the effect of past data fades slowly over time. If λ is closer to 0 (zero) then the effect of past data fades quickly over time.

The length of the look-back window also determines the persistence of data memory.

In the Simple Moving Average (SMA), the data which is more than L time periods in the past is abruptly forgotten. If the window is small, then only very recent data is used in the calculation of the average. If the window is large, then older data is used in the calculation of the average.

The decay factor λ and the look-back window length L are related to each other.

If the decay factor λ is closer to 1 (one) then the EMA adjusts slowly to new prices, corresponding to a larger look-back window length L. If λ is closer to 0 (zero) then the EMA adjusts quickly to new prices, corresponding to a smaller look-back window length L.


Choosing The Decay Factor λ And The Look-back window length L

The moving average prices are smoothed versions of the streaming prices.

The decay factor λ and the look-back window length L determine the strength of the data smoothing.

If λ is closer to 1 (one) then the moving average prices are smoother, but if it’s closer to 0 (zero) then the average prices are more variable and they follow the streaming prices more closely.

If the look-back window length L is large, then more prices are used in the calculation of the average and it’s therefore smoother. If the look-back window length L is small, then fewer prices are used and the average follows the streaming prices more closely.

The effect of the decay factor λ on the moving average prices is illustrated in the animated plot below of VTI stock prices. When λ=0.99 the average prices are very smooth, and they lag behind the streaming prices. But as λ decreases, the average prices become more variable and they follow the streaming prices more closely.


The moving average prices are less volatile (smoother), but they also lag behind the streaming prices - they are biased.
Notice for example that the average prices (red line above) reach their minimum much later than the streaming prices (blue line).
And the smoother the average prices are, the more biased they are.
A lower variance is desirable, but it comes at the cost of a larger bias (time lag).
This is an example of the bias-variance tradeoff.
Achieving the best bias-variance tradeoff is one of the major objectives of machine learning.

The decay factor λ and the look-back window length L are usually chosen to achieve the best bias-variance tradeoff.

The best tradeoff depends on the application. For example, if the streaming prices are very volatile, then a larger decay factor λ is desirable (or a larger look-back window length L), to reduce the volatility.

The optimal value of the decay factor λ can be determined using a simulation called backtesting (cross-validation).

Backtesting is performed on historical data.
The data is split into in-sample and out-of-sample sets.
Forecasting models often require the smoothing of the data.
The smoothing is applied to the in-sample data, and the forecasts are tested on the out-of-sample data.
The value of λ which minimizes the out-of-sample forecast errors is chosen as the optimal value.

The backtesting procedure risks overfitting to the historical data.
The optimal value of λ may not be the best choice in live data. So users should be cautious when selecting parameters using the backtesting procedure.


The Moving Average Variance of Returns

The moving average variance of returns σ2i measures the dispersion of the streaming returns around the moving average return. It can be calculated for a streaming time series of prices pi.

The dollar return ri is the difference between the current price pi minus the previous price pi1: ri=pipi1

The Simple Moving Average (SMA) return rSMAi is equal to the average of the past returns:

rSMAi=1LL1j=0rij

The Simple Moving Average (SMA) variance σ2SMAi is then given by the sum:

σ2SMAi=1L1L1j=0(rijrSMAi1)2

Note that the average return rSMAi1 is from the previous time step, to better capture the unexpected return in the current time step.


The Exponential Moving Average (EMA) variance σ2EMAi is calculated using the two recursive formulas:

σ2EMAi=λσ2EMAi1+(1λ)(rirEMAi1)2 rEMAi=λrEMAi1+(1λ)ri

The SMA and EMA variances are not the same, but they are related to each other, and they move in sympathy with each other.

If the decay factor λ is closer to 1 (one) then the EMA variance adjusts slowly to new prices, corresponding to a larger look-back window length L. If λ is closer to 0 (zero) then the EMA variance adjusts quickly to new prices, corresponding to a smaller look-back window length L.


The Moving Average Variance of Prices

The moving average variance of prices σ2i measures the dispersion of the streaming prices around the moving average price. It can be calculated for a streaming time series of prices pi.

The Simple Moving Average (SMA) price pSMAi is equal to the average of the past prices:

pSMAi=1LL1j=0pij

The Simple Moving Average (SMA) variance σ2SMAi is then given by the sum:

σ2SMAi=1L1L1j=0(pijpSMAi1)2

Note that the average price pSMAi1 is from the previous time step, to better capture the unexpected price in the current time step.


The Exponential Moving Average (EMA) variance of prices σ2EMAi is calculated using the two recursive formulas:

σ2EMAi=λσ2EMAi1+(1λ)(pipEMAi1)2 pEMAi=λpEMAi1+(1λ)pi

The variance of the returns and of the prices measure different forms of dispersion.

Take for example the extreme case when the returns are constant ri=r.
Then the prices pi=ri increase in a straight line. The variance of returns is zero, but the variance of prices is not zero. So if the prices exhibit a significant trend, then the variance of prices is large, even if the variance of returns is small.

Another example is Brownian motion. The returns are normally distributed, with a constant volatility. But the variance of prices is not constant, and it increases with the factor λ, since then prices have more time to drift away from their past average value.