I have a portfolio of five shares, I calculate monthly returns and need all possible combination of three stocks at the time, hence 10 combinations.
How do I calculate monthly return for all possible portfolios? I think I need to use 'combn'. But the result I get is just a veeery long list of numbers. Maybe it's in there somewhere but I cant make anything out of all the numbers.. code so far:
library('quantmod')
tickers <- c('MSFT','YHOO','ORCL','EBAY','AMZN')
getSymbols(tickers, src='yahoo', from='2015-03-01', to='2016-02-29')
amzn_ret = monthlyReturn(AMZN, type = 'log')
ebay_ret = monthlyReturn(EBAY, type = 'log')
msft_ret = monthlyReturn(MSFT, type = 'log')
orcl_ret = monthlyReturn(ORCL, type = 'log')
yhoo_ret = monthlyReturn(YHOO, type = 'log')
stock_ret = c(amzn_ret, ebay_ret,yhoo_ret,orcl_ret,msft_ret)
combin = combn(stock_ret, 3, sum, simplify = FALSE)
You are binding your returns as a vector c
, what you need is either cbind
or some form of table
:
stock_ret <- data.table(amzn_ret, ebay_ret,yhoo_ret,orcl_ret,msft_ret)
combin <- combn(stock_ret, 3, rowSums, simplify = TRUE)
and you probably meant to say rowSums
?