R tutorial for Moving Average

rizky normelia
4 min readOct 24, 2020

--

There are so many variations of moving averages such as Single/simple moving average (SMA), Double moving average, Triple moving average (TRIX), Exponential moving average (EMA), Weighted moving average (WMA), Sinus weighted moving average (SWMA), Spencer 15 point moving average (SpMA), etc. In this post, I will show how to use SMA, WMA, and EMA in R.

From the article I read, Moving average is a calculation used to analyze data points by creating a series of averages of different subsets of the full data set.

Simple moving average (SMA) is calculated by taking the arithmetic mean of a given set of values. The Exponential moving average (EMA)is a type of moving average that gives more weight to recent prices in an attempt to make it more responsive to new information. The weighted moving average (WMA) is a technical indicator that assigns a greater weighting to the most recent data points, and less weighting to data points in the distant past.

First step is to load the libraries into the environment. I use (TTR) to create technical trading rules and (quantmod) to manage the quantitative financial modelling workflow. note that these packages need to be installed first

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

and then I’m gonna scrap data stocks from https://finance.yahoo.com/

use function ‘getSymbols’, (Symbols=) filled by the financial code company you’d like to choose. I wrote “KAEF.JK” since I’m using data from PT Kimia Farma. and time period for the last one year from 9 Oct 2019 to 9 Oct 2020

getSymbols(Symbols = "KAEF.JK", scr="yahoo", from="2019-10-09", to = "2020-10-09")

view the data :

View(KAEF.JK)

here’s the result

there are “Open”, “High”, “Low”, “Close”, “Volume”, and “Adjusted” columns. In this case, I’m gonna use the Adjusted column only and defined it into a variable called ‘y’

y=Ad(KAEF.JK)
y

next is analyzing the moving average from data. first, I use the simple moving average method and named it as a ‘sma10’ variable.

sma10 = SMA(y,n=10)

let’s see the difference from the actual data and prediction data by making a data frame

datasma10= data.frame(y, sma10)
View(datasma10)

the data look like

and here is the comparison plot of actual data and prediction data using SMA method.

##plot actual data in blue
plot(y, col="blue", main="KAEF.JK", type="b", pch=21, cex=0.5)
##plot prediction data sma in red
lines(sma10, col="red", lwd=2, type = "b", pch = 12, cex=0.5)

here is the visual look of the actual and prediction data using SMA

move to the next method which is weighted moving average (WMA)

wma10= WMA(y,n=10, wts=1:10)
datawma10= data.frame(y, wma10)
View(datawma10)
##plot prediction data wma in green
lines(wma10, col="green", lwd=2, type = "b", pch = 3, cex=0.8)

you can see the comparison data between actual and prediction using wma in data frame and then add lines prediction into the previous plot. so here in visual look we can see the difference clearly

next to the third method that is exponential moving average (EMA)

ema10= EMA(y,n=10)
dataema10= data.frame(y, ema10)
View(dataema10)
##plot prediction data ema in orange
lines(ema10, col="orange", lwd=2, type = "b", pch = 3, cex=0.8)

and the result will look like this

after seeing the difference between these methods, we can assume that WMA’s method is more sensistive to the movement. In term of that, we can’t decide which method is really suits by just an insting. now, let’s find the error each method to decide which method is better

#MSE sma
mse.sma10=mean((y-sma10)^2, na.rm=TRUE)
#MSE wma
mse.wma10=mean((y-wma10)^2, na.rm=TRUE)
#MSE ema
mse.ema10=mean((y-ema10)^2, na.rm=TRUE)
## vector
mse=cbind(mse.sma10,mse.wma10,mse.ema10)
colnames(mse)=c("SMA","WMA","EMA")
mse

and the result is shown below

the smallest mean square error is WMA’s at 26509.08. The more smaller MSE value, the better for forecasting model. Based on the output above, it is found that the best method is by using the weighted moving average method.

So that’s how I do moving average in R. Please correct me if I’m wrong:)

Sources :

https://corporatefinanceinstitute.com/

https://www.investopedia.com/

--

--