Reputation: 15
I want to have overall returns for data series over the whole of zoo series time perid, which I have in both a prices or daily returns;
e.g
GOLD PA PL SLV
2001-05-22 0.000000000 -0.009132420 -0.004838710 0.0
or as prices where simple return would be last prices in series minus first / first
GOLD PA PL SLV
2020-10-09 1920 2454 888 25
I've tried some performance analytic packages but I know the returns are wrong.
Upvotes: 0
Views: 181
Reputation: 269754
Assuming the input data shown reproducibly in the Note at the end and using returns:
apply(rets + 1, 2, prod) - 1
## GOLD PA PL SLV
## 0.00000000 -0.02714782 -0.01444600 0.00000000
or
library(PerformanceAnalytics)
Return.cumulative(rets)
## GOLD PA PL SLV
## Cumulative Return 0 -0.02714782 -0.014446 0
or approximating using sums:
colSums(rets)
## GOLD PA PL SLV
## 0.00000000 -0.02739726 -0.01451613 0.00000000
or using prices:
n <- nrow(prices)
diff(prices[c(1, n)], arith = FALSE) - 1
## GOLD PA PL SLV
## 2020-10-11 0 0 0.002252252 0.08
or using n
from above:
exp(diff(log(prices[c(1, n)]))) - 1
## GOLD PA PL SLV
## 2020-10-11 0 0 0.002252252 0.08
Lines <- "
Date GOLD PA PL SLV
2001-05-22 0.000000000 -0.009132420 -0.004838710 0.0
2001-05-23 0.000000000 -0.009132420 -0.004838710 0.0
2001-05-24 0.000000000 -0.009132420 -0.004838710 0.0"
Lines2 <- "
Date GOLD PA PL SLV
2020-10-09 1920 2454 888 25
2020-10-10 1900 2454 899 26
2020-10-11 1920 2454 890 27"
library(zoo)
rets <- read.zoo(text = Lines, header = TRUE)
prices <- read.zoo(text = Lines2, header = TRUE)
Upvotes: 1