R:计算投资组合的累计回报

问题描述 投票:1回答:1

我已经使用quantmod-package从雅虎下载了调整后的收盘价,并用它来创建一个由50%AAPL-和50%FB-stocks组成的投资组合。

当我绘制我的投资组合的累积表现时,我得到的表现是(可疑)高,因为​​它高于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()

enter image description here

累积性能高于100%对我来说似乎非常不切实际。这让我想到在使用之前可能需要调整/缩放quantmod下载的数据?

r finance quantmod yahoo-api quantitative-finance
1个回答
1
投票

我一直在努力解决这个问题,我觉得它下面有一个数据问题。为了证明这一点,我使用两种方法计算累积回报。结果显示了一些我无法解释的差异 - 因此,您可能先看一下这些。

首先,我运行你的代码:

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
}

然后我通过将当前值除以起始值(即行#1中的价格)并减去1来手动计算累积回报。此外,我分别对两个股票的回报进行cumsum

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)

现在,我正在用手动计算的回报(红色)绘制cumsum回报(蓝色)。

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")

enter image description here enter image description here

特别是对于Facebook,我们发现这两种方法之间存在很大差异。对不起,我无法解决您的问题,但我希望这会引导您找到解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.