[R-SIG-Finance] issue with xts affectin Return.calculate in PerformanceAnalytics

Joshua Ulrich josh.m.ulrich at gmail.com
Wed Jun 20 21:52:28 CEST 2012


On Tue, Jun 19, 2012 at 1:43 PM,  <susan22 at mail.com> wrote:
> I came across the issue below trying to calculate simple returns of an xts
> object using Return.calculate() in PerformanceAnalytics. This returns a
> series of all zeros. The root of the problem, however, seems to be with xts
> for which division seems to be a challenge. What am I missing? Coercion to
> numeric is not really a reasonable solution.
>
Return.calculate was fixed on R-Forge on April 4, 2012.  Please see
this SO question about installing from R-Forge:
http://stackoverflow.com/q/11105131/271616

Division is only a challenge when you try to divide two objects that
do not have any index values in common.  Use lag() to align the index
instead.

>> p <- to.monthly(as.xts(s.ts), OHLC = FALSE)
>> head(aap)
>                    s1           s2
> Dec 1999      1000.00      1000.00
> Jan 2000      1021.27       959.85
> Feb 2000      1017.30       962.06
> Mar 2000      1022.99      1008.44
> Apr 2000      1015.95       980.95
> May 2000      1044.30       963.79
>
>> r <- Return.calculate(aap, method = "simple")
>> head(r)
>                    s1           s2
> Dec 1999            0            0
> Jan 2000            0            0
> Feb 2000            0            0
> Mar 2000            0            0
> Apr 2000            0            0
> May 2000            0            0
>
>> lr <- Return.calculate(aap, method = "compound")
>> head(lr)
>                         s1                s2
> Dec 1999                NA                NA
> Jan 2000  0.02104695084364 -0.04097825672856
> Feb 2000 -0.00389489202608  0.00229979652957
> Mar 2000  0.00557765293308  0.04708304254280
> Apr 2000 -0.00690557640376 -0.02763837145911
> May 2000  0.02752266915648 -0.01764806131103
>
>> p[2]/p[1]-1
> Data:
> numeric(0)
>
> Index:
> NULL
>
>> as.numeric(p[2])/as.numeric(p[1])-1
> [1]  0.02127 -0.04015
>

Best,
--
Joshua Ulrich  |  FOSS Trading: www.fosstrading.com



More information about the R-SIG-Finance mailing list