n-period return
This is because xts aligns the series before operations. (Delta[i]>Delta[i-1]) returns an empty xts object because Delta[i] and Delta[i-1] don't share any common index values. Something like this will work:
lDelta <- lag(Delta)
for (i in 3:length(Delta)) {
+ if (Delta[i]>lDelta[i]) sum <- sum + Delta + } Though you're going to have the same problem when you try to add "sum" and "Delta" if they're both xts objects. HTH, Josh -- http://www.fosstrading.com On Mon, Aug 3, 2009 at 10:55 AM,
ehxpieterse<eduard.pieterse at macquarie.com> wrote:
Thanks Jeff,
I'm not too clear on how to use the GetSeries data type, after using diff().
getSymbols("^GSPC", src="yahoo")
CloseData <- Cl(GSPC)
Delta <- diff(CloseData, lag=1)
for (i in 3:length(Delta)) {
?if (Delta[i]>Delta[i-1]) sum <- sum + Delta
}
It seems if the variables have no length? Is this something specific to the
way getSymbols create objects?
--
View this message in context: http://www.nabble.com/n-period-return-tp24788735p24793238.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.