MA | R Documentation |
The function computes a moving average of a vector.
MA(y, order, pad = NULL)
y |
a numeric vector |
order |
An integer. The order of the moving average. The function is defined
such that order one returns |
pad |
Defaults to |
Returns a vector of length length(y)
.
Enrico Schumann
Gilli, M., Maringer, D. and Schumann, E. (2019) Numerical Methods and Optimization in Finance. 2nd edition. Elsevier. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1016/C2017-0-01621-X")}
Schumann, E. (2023) Financial Optimisation with R (NMOF Manual). https://enricoschumann.net/NMOF.htm#NMOFmanual
MA(1:10, 3)
MA(1:10, 3, pad = NA)
y <- seq(1, 4, by = 0.3)
z <- MA(y, 1)
all(y == z) ### (typically) FALSE
all.equal(y, z) ### should be TRUE
## 'Relative strength index'
rsi <- function(y, t) {
y <- diff(y)
ups <- y + abs(y)
downs <- y - abs(y)
RS <- -MA(ups, t) / MA(downs, t)
RS/(1 + RS)
}
x <- cumprod(c(100, 1 + rnorm(100, sd = 0.01)))
par(mfrow = c(2,1))
plot(x, type = "l")
plot(rsi(x, 14), ylim = c(0,1), type = "l")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.