[R-SIG-Finance] [R-sig-finance] Moving volatility
Gabor Grothendieck
ggrothendieck at gmail.com
Tue Aug 4 15:13:18 CEST 2009
There is also rollapply in the zoo package:
set.seed(123)
library(zoo)
x <- zoo(rnorm(25))
z1 <- rollapply(x, 5, sd)
or using rollmean (which is faster)
z2 <- sqrt(5/4 * (rollmean(x*x,5) - rollmean(x,5)^2))
all.equal(z1, z2) # TRUE
On Tue, Aug 4, 2009 at 8:41 AM,
ehxpieterse<eduard.pieterse at macquarie.com> wrote:
>
> Hi,
>
> I have found a function online to calculate moving volatility. I am aware of
> addVolatility in the quantmod package, but that only adds the vol to a
> graph. Does any one know if there exists a better function to use than the
> one shown below? I find the current one quite slow when working with large
> data sets.
>
> movsd <- function(series,lag)
> {
> movingsd <- vector(mode="numeric")
> for (i in lag:length(series))
>
> {
> movingsd[i] <- sd(series[(i-lag+1):i])
> }
>
> assign("movingsd",movingsd,.GlobalEnv)
> }
>
> to.dat <- as.Date(Sys.Date(), format="%m/%d/%y")
> getSymbols("^GSPC", src="yahoo", from = "2000-01-01", to = to.dat)
> CloseData <- Cl(GSPC)
>
> x <- movsd(Delt(CloseData),40)
> xx <- x*100
> plot(xx, type="l")
>
> --
> View this message in context: http://www.nabble.com/Moving-volatility-tp24807675p24807675.html
> Sent from the Rmetrics mailing list archive at Nabble.com.
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.
>
More information about the R-SIG-Finance
mailing list