Skip to content

n-period return

5 messages · Jeff Ryan, Brian G. Peterson, ehxpieterse +1 more

#
Apologies if this has been asked before.

I am looking for a function to calculate the n-period return for a time
series. For example, the user would specify 11, 90 or 180 and then generate
an accompanying n-period delta.

Thanks in advance.
Eduard
#
Try:

?diff

library(quantmod)
getSymbols("AAPL")
AAPL.Cl <- Cl(AAPL)

diff(log(AAPL.Cl), lag=11)
diff(log(AAPL.Cl), lag=90)
diff(log(AAPL.Cl), lag=180)

Or using quantmod's Delt:

Delt(Cl(AAPL), k=c(11,90,180), type="log")

HTH
Jeff

On Mon, Aug 3, 2009 at 5:55 AM,
ehxpieterse<eduard.pieterse at macquarie.com> wrote:

  
    
#
ehxpieterse wrote:
See to.period() in xts to decompose.  Also see period.apply()

Regards,

  - Brian
#
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?
#
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:
+  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: