0

I am creating a stock portfolio and I want to run an efficiency frontier. I am running into an error with my portfolio optimum code. I am also noticing that all the stocks have the same monthly return. I am working through this error at the time. Any help with either of my problems would be greatly apprecieated

tickers <- c('DPZ','SPY','AMD','AAPL','TSLA','MSFT','V', 'WMT', 'SQ','EA','ATVI','AMZN','ROKU', 'PYPL','KO','AXP','CCL','DFS') Portfolio1 <- getSymbols.yahoo(tickers[1], from="2016-01-01", to= "2018-12-31", auto.assign=FALSE) Portfolio2 <- Portfolio1[,6] my_portfolio <- monthlyReturn(Portfolio2) for(i in 2:length(tickers)){ ticker1 <- c('DPZ','SPY','AMD','AAPL','TSLA','MSFT','V', 'WMT', 'SQ','EA','ATVI','AMZN','ROKU', 'PYPL','KO','AXP','CCL','DFS') getSymbols.yahoo(tickers[i], from="2016-01-01", to= "2018-12-31", auto.assign=FALSE) Portfolio2 <- Portfolio1[,6] holder <- monthlyReturn(Portfolio2) my_portfolio <- cbind( my_portfolio, holder ) } #Applies ticker name to column names (my_portfolio) <- tickers # Target 7% eff_port <- portfolio.optim(my_portfolio, pm = 0.07, shorts = TRUE) eff_port$pw #Efficiency Frontier #Mean Returns mu <- colMeans(my_portfolio) grid <- seq(0.005, 0.033, length.out = 60) vector_pm <- rep(NA, length(grid)) vector_psd <- rep(NA, length(grid)) eff_weights <- matrix(NA, 60, 18) #FOR LOOP for (i in 1 : length(grid)) { eff.port <- portfolio.optim(my_portfolio, pm = grid[i], shorts =TRUE) vector_pm[i] <- eff.port$pm vector_psd[i] <- eff.port$ps eff_weights[i, ] <- eff.port$pw } 

1 Answer 1

1

You mention, that you always get the same return. I think it is due to your first loop. You calculate the monthly return N-time for your portfolio2. Which is equal to Portfolio1[,6].

EDIT 1

So another thing would be again at the specification of the Portfolio2. Before you start your loop you save the Portfolio2 <- Portfolio1[,6] which takes always the same column of stock 'DPZ'. I think you wanted to update this specification with every iteration, as you also take always another ticker[i] but do not update the Portfolio2. Since you do not save the getSymbol.yahoo() nowhere. please try following loop for the first one:

for(i in 2:length(tickers)){ ticker1 <- c('DPZ','SPY','AMD','AAPL','TSLA','MSFT','V', 'WMT', 'SQ','EA','ATVI','AMZN','ROKU', 'PYPL','KO','AXP','CCL','DFS') # here is my change############## Portfolio1 <- getSymbols.yahoo(tickers[i], from="2016-01-01", to= "2018-12-31", auto.assign=FALSE) ################# Portfolio2 <- Portfolio1[,6] holder <- monthlyReturn(Portfolio2) my_portfolio <- cbind( my_portfolio, holder ) } 
Sign up to request clarification or add additional context in comments.

2 Comments

The [,6] is in reference to pull the 6th column of the stock. I specifically am looking at the Adjusted values. I attempted your code and was met with an error
please try my edit1 mentioned in my answer. If not, could you provide the error message?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.