Table of contents

  1. Simple Moving Average
    1. Definition
  2. Code examples
    1. Plot time series and simple moving average in R
      1. Trading time series
      2. Trading time series with simple moving average applied

Simple Moving Average

Definition

The simple moving average of order \(2q + 1\) centred on \(t\) is given by the transformation

\[\begin{equation} MA(t) = \frac{1}{2q + 1}(X_{t-q} + \ldots + X_t + \ldots + X_{t+q}) \end{equation}\]

Code examples

Plot time series and simple moving average in R

#install.packages("TTR")
library("TTR")

data("ttrc")
tradingTs = ts(ttrc['Close'])
plot.ts(tradingTs)

tradingTsWithSma <- SMA(tradingTs, n = 100)
plot.ts(tradingTsWithSma)

Trading time series

Trading time series with simple moving average applied