[R] Merge xts and Return.portfolio

Simone Medori simone.medori at me.com
Fri Nov 15 16:01:17 CET 2013


Thanks

Simone

> Il giorno 15/nov/2013, alle ore 15:30, Joshua Ulrich <josh.m.ulrich at gmail.com> ha scritto:
> 
>> On Fri, Nov 15, 2013 at 6:32 AM, Simone Medori <simone.medori at me.com> wrote:
>> Hi,
>> I'm triyng to merge two xts time series objects, one of them is from Return.portfolio (PerformanceAnalytics).
>> 
>> Despite the merging xts objects have the same indexes, the merged object shows extra lines at the day before of every entry.
>> 
>> I noticed that indexes of merging objects have different classes ("POSIXct" and "Date"): might be this the reason? Why do I get different extra dates anyway?
> Yes, this is the reason.  Specifically, the cause is the difference in
> timezone between the POSIXct index and the Date index.
> 
> For some reason, Return.portfolio returns a xts object with a POSIXct
> index.  Convert it to Date and your merge will work.
> rp <- Return.portfolio(returns)
> index(rp) <- as.Date(index(rp))
> merge(returns,rp)
> 
>> Kind regards,
>> 
>> Simone
>> 
>> 
>>> require(PerformanceAnalytics)
>>> require(quantmod)
>>> 
>>> benchmark<-c("^STOXX50E","^NDX")
>>> downloaded<-getSymbols(benchmark,from=as.Date(Sys.Date()-15))
>>> prices <- merge.xts(na.locf(do.call(merge,lapply(downloaded, function(x) Cl(get(x))))))
>>> returns <- Return.calculate(prices)[-1,] #get rid of first NA
>>> 
>>> returns
>>> #STOXX50E.Close     NDX.Close
>>> #2013-11-01   -0.005153278  0.0006009953
>>> #2013-11-04    0.000000000  0.0014764362
>>> #2013-11-05   -0.005314304  0.0012024522
>>> #2013-11-06    0.006745896 -0.0010151026
>>> #2013-11-07   -0.004390787 -0.0188959585
>>> #2013-11-08    0.000000000  0.0136779259
>>> #2013-11-11    0.003236959 -0.0011464756
>>> #2013-11-12   -0.005945303  0.0006690495
>>> #2013-11-13   -0.004451870  0.0119843220
>>> #2013-11-14    0.010764042  0.0028130469
>>> 
>>> Return.portfolio(returns)
>>> #             portfolio.returns
>>> #2013-11-01     -0.0022761415
>>> #2013-11-04      0.0007403469
>>> #2013-11-05     -0.0020441262
>>> #2013-11-06      0.0028386740
>>> #2013-11-07     -0.0116652539
>>> #2013-11-08      0.0068094113
>>> #2013-11-11      0.0010398246
>>> #2013-11-12     -0.0026371940
>>> #2013-11-13      0.0037957949
>>> #2013-11-14      0.0067416934
>>> #Warning message:
>>> #  In Return.portfolio(returns) :
>>> #  weighting vector is null, calulating an equal weighted portfolio
>>> 
>>> merge(returns,Return.portfolio(returns))
>>> 
>>> #STOXX50E.Close     NDX.Close portfolio.returns
>>> #2013-10-31             NA            NA     -0.0022761415 # Return.portfolio merges into extra lines!
>>> #2013-11-01   -0.005153278  0.0006009953                NA
>>> #2013-11-03             NA            NA      0.0007403469
>>> #2013-11-04    0.000000000  0.0014764362                NA
>>> #2013-11-04             NA            NA     -0.0020441262
>>> #2013-11-05   -0.005314304  0.0012024522                NA
>>> #2013-11-05             NA            NA      0.0028386740
>>> #2013-11-06    0.006745896 -0.0010151026                NA
>>> #2013-11-06             NA            NA     -0.0116652539
>>> #2013-11-07   -0.004390787 -0.0188959585                NA
>>> #2013-11-07             NA            NA      0.0068094113
>>> #2013-11-08    0.000000000  0.0136779259                NA
>>> #2013-11-10             NA            NA      0.0010398246
>>> #2013-11-11    0.003236959 -0.0011464756                NA
>>> #2013-11-11             NA            NA     -0.0026371940
>>> #2013-11-12   -0.005945303  0.0006690495                NA
>>> #2013-11-12             NA            NA      0.0037957949
>>> #2013-11-13   -0.004451870  0.0119843220                NA
>>> #2013-11-13             NA            NA      0.0067416934
>>> #2013-11-14    0.010764042  0.0028130469                NA
> 
> Best,
> --
> Joshua Ulrich  |  about.me/joshuaulrich
> FOSS Trading  |  www.fosstrading.com
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list