user8491385
user8491385

Reputation: 443

Price to compounded returns using the Return.Calculate Function

I have the following sample data and trying to calculate compounded returns from the prices below.

sample_data <- data.frame(Date = c ("2017-01-31", "2017-02-28", "2017-03-31", 
                                "2017-04-30", "2017-05-31", "2017-06-30"), 
                      stock = c("a", "a", "a","a", "a", "a"), 
                      Price = c(10, 11, 17, 12, 13, 14))

I using the Return.calculate package as such :

Return.calculate(sample_data$Price, method = "compound")

But getting the following error :

no applicable method for 'xtsAttributes<-' applied to an object of class "zoo"

Upvotes: 0

Views: 456

Answers (1)

GoGonzo
GoGonzo

Reputation: 2877

You need to pass data as xts object. Be careful while converting to xts to include only numeric variables - reason here - That's why it's possible to convert only sample_data$Price

library(xts)
library(PerformanceAnalytics)
sample_data$Date <- as.Date(sample_data$Date)
sample_data_xts <- xts(sample_data$Price, order.by = sample_data$Date) 

Return.calculate(sample_data_xts, method = "compound")

#                   [,1]
# 2017-01-31          NA
# 2017-02-28  0.09531018
# 2017-03-31  0.43531807
# 2017-04-30 -0.34830669
# 2017-05-31  0.08004271
# 2017-06-30  0.07410797

Upvotes: 2

Related Questions