tidyquant
tidyquant copied to clipboard
Maybe something wrong about getting Portfolio Return by tq_portfolio()
When I calculate a monthly portfolio return I found an issue:
The results are not equal when you add and do not add the ** rebalance_on = c("months")** argument. And By calculating returns on myself, I think the argument should not be omitted.
I don not know how the tq_portfolio() works exactly, and I suggest you guys to make a check. If I were right, then original examples about portfolio returns should be corrected.
For your convenience , the following is my codes :
` library(tidyquant) stock_returns_monthly <- c("AAPL", "GOOG", "NFLX") %>% tq_get(get = "stock.prices", from = "2010-01-01", to = "2015-12-31") %>% group_by(symbol) %>% tq_transmute(select = adjusted, mutate_fun = periodReturn, period = "monthly", col_rename = "Ra")
weights <- c( 0.50, 0.25, 0.25 ) stocks <- c("AAPL", "GOOG", "NFLX")
weight_tibble <- tibble(symbol = stocks) %>% bind_cols(tibble(weights))
portfolio_returns_monthly_rebalance <- stock_returns_monthly %>%
tq_portfolio(assets_col = symbol,
returns_col = Ra,
weights = weight_tibble,
rebalance_on = c("months"),
col_rename = "port_ret_rebalance")
portfolio_returns_monthly <- stock_returns_monthly %>% tq_portfolio(assets_col = symbol, returns_col = Ra, weights = weight_tibble, # rebalance_on = c("months"), col_rename = "port_ret")
portfolio_returns_monthly_byhand <- stock_returns_monthly %>% left_join( weight_tibble, by= "symbol" ) %>% group_by(date) %>% mutate( port_ret_byhand = sum(Ra*weights) ) %>% select( date, port_ret_byhand ) %>% distinct_all()
all <- portfolio_returns_monthly_rebalance %>% bind_cols(portfolio_returns_monthly) %>% bind_cols(portfolio_returns_monthly_byhand)
note: the 2nd, 4th and 6th column are calculated with rebalance_on option, without that option and by hand.
`