r - Calculating portfolio level returns -


edit updated i've found great post authors of performanceanalytics. post sums ins-outs of cumulative portfolio returns, , author shows it's pretty tricky (he got wrong too)! here reference: https://tradeblotter.wordpress.com/2014/09/25/aggregate-portfolio-contributions-through-time/

so i've run little bit of stump 2 numbers should adding not. here's example dataframe stock choices , weightings of portfolio context:

      stock.choices stock_weights 1          goog         0.150 2          amzn         0.200 3            ba         0.250 4            fb         0.225 5          aapl         0.175 

then i'm going use return.portfolio function wealth.index = true show return of portfolio.

portfolio <- merge.xts(goog,amzn,ba,fb,aapl) dollar_growth <- return.portfolio(portfolio, weights = stock_weights, wealth.index = true) 

i use dygraph visualise dollar growth.

dygraph(dollar_growth, main = "portfolio growth base.$1") %>% dyaxis("y", label = "$")%>%   dyannotation("2017-05-01", text = may1, tooltip = "initial investment", width = 70, height = 18, tickheight = -75)%>%   dyannotation(lastday, text = today, tooltip = "percentage increase",width = 70,  attachatbottom = true) %>%   dyaxis("y", label = "dollars usd") 

for example i'm going use may 1st initial point of investment. on portfolio i'm getting 11.5% return form may 1st - calculated taking current value ($1.37) , dividing may 1st ($1.23057) yielding 11.33% increase.

however when use different method different answer strange because have thought second method accurate way of calculating return of portfolio.

firstly create dataframe has stock values @ may 1st , current values. multiply both respective weighting in portfolio. here's output:

      may1 current stock.weights    may1c  currentc goog 912.57  926.50         0.150 136.8855 138.97500 amzn 948.23  965.90         0.200 189.6460 193.18000 ba   182.39  238.78         0.250  45.5975  59.69500 fb   152.46  170.95         0.225  34.3035  38.46375 aapl 146.58  158.63         0.175  25.6515  27.76025  may1c = may1 * stock.weights | currentc = current * stock.weights 

now when sum both may1c , currentc get:

> sum(df$may1c) [1] 432.084 > sum(df$currentc) [1] 458.074 

which think current value of portfolio stock choices * respective weights. yields increase of 6.015%.

my question is: how return.portfolio function returning 11.3% increase, second method returning 6.015%?

edit in reply comments have found when using return.portfolio verbose = true function returns stock weights changing on time. output shows weights changing overtime eop , bop.

for reference, here's complete code run dygraph output:

library(performanceanalytics) library(quantmod) library(dygraphs) library(scales)  daily_stock_returns = function(ticker) {      symbol <- getsymbols(ticker, src = 'google', auto.assign = false, warnings = false)     symbol <- xts::last(symbol, "1 year")     data <- periodreturn(symbol, period = 'daily', type = 'log')     colnames(data) <- as.character(ticker)     assign(ticker, data, .globalenv) }       daily_stock_returns("goog")     daily_stock_returns("amzn")     daily_stock_returns("ba")     daily_stock_returns("fb")     daily_stock_returns("aapl")     portfolio <- merge.xts(goog,amzn,ba,fb,aapl)     test <- periodreturn(portfolio, period = 'monthly', type = 'log')     stock_weights <- c(.15, .20, .25, .225, .175)      dollar_growth <- return.portfolio(portfolio, weights = stock_weights, wealth.index = true)     may1 <- as.numeric(dollar_growth["2017-05-01"])      format(round(may1, 3), nsmall = 2)     today <- as.numeric(xts::last(dollar_growth, "1 day"))     today <- ((today/may1)-1) %>% percent()     format(round(may1, 3), nsmall = 2)     lastday <- xts::last(dollar_growth, "1 day")     dygraph(dollar_growth, main = "portfolio growth base.$1")  

if want see dollar value of portfolio components , total portfolio can following. assuming want investing in portfolio on “daystart (2017-01-01)” allocation “alloc (.15, .20, .25, .225, .175)" , without rebalancing let run it’s course until “dayend (2017-05-01)”:

initial alloc(e.g.1000 usd) goog, amzn, ba, fb, aapl: 150, 200, 250, 225, 175

taking portfolio returns “portfolio” ( took ‘discrete' returns not ‘log’):

startcapital <- c(150, 200, 250, 225, 175) portdollar <- cumprod(1+portfolio["::2017-05-01”]) * startcapital portdollar <- cbind(portdollar,portf=rowsums(portdollar)) 

you can plot portfolio value in dollars or convert returns.

both(portdollar)                goog     amzn       ba       fb     aapl    portf 2017-01-03 151.4052 248.5942 175.7486 201.4256 225.6790 1002.853 2017-01-04 202.0686 224.7743 152.2168 255.6943 175.3316 1010.086 2017-01-05 254.8609 180.1164 203.0709 233.9321 151.0465 1023.027                goog     amzn       ba       fb     aapl    portf 2017-04-27 195.9950 241.4572 262.7753 190.4188 309.3954 1200.042 2017-04-28 173.9812 303.9860 206.1689 258.2377 278.1846 1220.558 2017-05-01 233.6613 280.3763 174.3678 327.5105 220.7346 1236.650 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -