June 2017
Beginner to intermediate
576 pages
15h 22m
English
The following code will compute an EMA with a ratio of .8, using a lookback period of 5. The ratio of .8 will give the most weight to the most recent period, whilel still allowing the past to influence the prediction.
Then we will use cbind() to display the data point, as well as the simple and exponential moving averages:
ExpMA <- EMA(x, n = 5, ratio = 0.8)cbind(head(x, 15), head(MA, 15), head(ExpMA, 15)) > [,1] [,2] [,3]> [1,] 0.1541247 NA NA> [2,] 0.1574131 NA NA> [3,] 0.1629424 NA NA> [4,] 0.1609860 NA NA> [5,] 0.1485338 0.1568000 0.1568000> [6,] 0.1474000 0.1554551 0.1492800> [7,] 0.1523271 0.1544379 0.1517177> [8,] 0.1464598 0.1511414 0.1475114> [9,] 0.1433967 0.1476235 0.1442196> [10,] 0.1455136 ...