Standard Deviations using Sliding window?
Yes, try this:
library(TTR) set.seed(1) x <- xts(rnorm(10), Sys.Date() - 1:10) runSD(x, 3)
[,1] 2010-02-24 NA 2010-02-25 NA 2010-02-26 0.5615778 2010-02-27 0.1272629 2010-02-28 0.8369966 2010-03-01 0.7139071 2010-03-02 1.2083371 2010-03-03 1.2158018 2010-03-04 1.2207208 2010-03-05 0.5383504
rollapply(x, 3, sd, align = "right", na.pad = TRUE)
2010-02-24 NA 2010-02-25 NA 2010-02-26 0.5615778 2010-02-27 0.1272629 2010-02-28 0.8369966 2010-03-01 0.7139071 2010-03-02 1.2083371 2010-03-03 1.2158018 2010-03-04 1.2207208 2010-03-05 0.5383504 On Sat, Mar 6, 2010 at 9:15 PM, Robert Nicholson
<robert.nicholson at gmail.com> wrote:
currently using rollapply with sd so I assume that will do the same as runSD On Mar 5, 2010, at 9:23 AM, Joshua Ulrich wrote:
On Thu, Mar 4, 2010 at 8:15 PM, Robert Nicholson <robert.nicholson at gmail.com> wrote:
Here was a naive attempt to do standard deviation with sliding window
require(quantmod)
getSymbols("AAPL")
AAPL$STDDEV = sd(Cl(AAPL), 20)
AAPL$SMA = SMA(Cl(AAPL), 10)
AAPL
apparently sd doesn't work with a sliding window where was SMA does so it seems
No, sd doesn't work with a sliding window, but there's nothing in ?sd to indicate that it would.
I need to look at SMA to see how the sliding window code is handled and write my own sd that uses a sliding window?
Yes, or you can just use the runSD function in TTR. ;-)
the SD columns up the same for all rows which isn't what I want.
but that's exactly what it's documented to do... Best, Josh -- Joshua Ulrich FOSS Trading: www.fosstrading.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. -- Also note that this is not the r-help list where general R questions should go.
_______________________________________________ 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. -- Also note that this is not the r-help list where general R questions should go.