简体   繁体   中英

R: Calculating cumulative return of a portfolio

I've downloaded adjusted closing prices from Yahoo using the quantmod -package, and used that to create a portfolio consisting of 50% AAPL - and 50% FB -stocks.

When I plot the cumulative performance of my portfolio, I get a performance that is (suspiciously) high as it is above 100%:

library(ggplot2)
library(quantmod)

cmp <- "AAPL"
getSymbols(Symbols = cmp)
tail(AAPL$AAPL.Adjusted)

cmp <- "FB"
getSymbols(Symbols = cmp)
tail(FB$FB.Adjusted)


df <- data.frame("AAPL" = tail(AAPL$AAPL.Adjusted, 1000),
                 "FB"   = tail(FB$FB.Adjusted, 1000))

for(i in 2:nrow(df)){
  df$AAPL.Adjusted_prc[i] <- df$AAPL.Adjusted[i]/df$AAPL.Adjusted[i-1]-1
  df$FB.Adjusted_prc[i] <- df$FB.Adjusted[i]/df$FB.Adjusted[i-1]-1
}

df <- df[-1,]
df$portfolio   <- (df$AAPL.Adjusted_prc + df$FB.Adjusted_prc)*0.5
df$performance <- cumprod(df$portfolio+1)-1
df$idu <- as.Date(row.names(df))

ggplot(data = df, aes(x = idu, y = performance)) + geom_line()

在此处输入图片说明

A cumulative performance above 100% seems very unrealistic to me. This lead me to think that maybe it is necessary to adjust/scale the downloaded data from quantmod before using it?

I've been really struggeling with this issue and I have the feeling that there is a data issue beneath it. To demonstrate this I'm computing cumulative returns using two approaches. The results show some differences, that I can't really explain - therfore, you might take a look at these first.

First, I running your code:

library(quantmod)
library(tidyverse

cmp <- "AAPL"
getSymbols(Symbols = cmp)
tail(AAPL$AAPL.Adjusted)

cmp <- "FB"
getSymbols(Symbols = cmp)
tail(FB$FB.Adjusted)


df <- data.frame("AAPL" = tail(AAPL$AAPL.Adjusted, 1000),
                 "FB"   = tail(FB$FB.Adjusted, 1000))

for(i in 2:nrow(df)){
  df$AAPL.Adjusted_prc[i] <- df$AAPL.Adjusted[i]/df$AAPL.Adjusted[i-1]-1
  df$FB.Adjusted_prc[i] <- df$FB.Adjusted[i]/df$FB.Adjusted[i-1]-1
}

Then I am computing the cumulative returns manually, by dividing the current value by the starting value (ie the price in row #1) and subtracting 1. In addition, I'm cumsum 'ing the two stocks' returns separately.

df$aapl_man <- df$AAPL.Adjusted / df$AAPL.Adjusted[1] - 1
df$fb_man <- df$FB.Adjusted / df$FB.Adjusted[1] - 1

df <- df[-1,]
df$portfolio   <- (df$AAPL.Adjusted_prc + df$FB.Adjusted_prc)*0.5
df$performance <- cumprod(df$portfolio+1)-1
df$idu <- as.Date(row.names(df))

df <- mutate_at(df, vars(contains("_prc")), cumsum)

Now, I am plotting the cumsum returns (in blue) with the manually computed returns (in red).

df %>%
  ggplot(aes(x = idu)) +
  geom_line(aes(y = AAPL.Adjusted_prc), colour = "blue") +
  geom_line(aes(y = aapl_man), colour = "red") +
  ggtitle("Apple")

df %>%
  ggplot(aes(x = idu)) +
  geom_line(aes(y = FB.Adjusted_prc), colour = "blue") +
  geom_line(aes(y = fb_man), colour = "red") +
  ggtitle("Facebook")

在此处输入图片说明 在此处输入图片说明

In particular for Facebook, we see quite some difference between the two approaches. I'm sorry that I couldn't solve your problem, but I hope that this will lead you towards the solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM